MilesCranmer commited on
Commit
3c59d19
1 Parent(s): b3d39e3

Make printing compatible with jupyter

Browse files
Files changed (1) hide show
  1. pysr/sr.py +25 -11
pysr/sr.py CHANGED
@@ -117,7 +117,7 @@ def pysr(X, y, weights=None,
117
  extra_jax_mappings=None,
118
  equation_file=None,
119
  verbosity=1e9,
120
- progress=True,
121
  maxsize=20,
122
  fast_cycle=False,
123
  maxdepth=None,
@@ -273,6 +273,16 @@ def pysr(X, y, weights=None,
273
  if constraints is None:
274
  constraints = {}
275
 
 
 
 
 
 
 
 
 
 
 
276
  assert optimizer_algorithm in ['NelderMead', 'BFGS']
277
 
278
  if isinstance(X, pd.DataFrame):
@@ -421,7 +431,7 @@ def _final_pysr_process(julia_optimization, runfile_filename, timeout, **kwargs)
421
  command = [f'timeout', f'{timeout}'] + command
422
  _cmd_runner(command, **kwargs)
423
 
424
- def _cmd_runner(command, **kwargs):
425
  if kwargs['verbosity'] > 0:
426
  print("Running on", ' '.join(command))
427
  process = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=-1)
@@ -429,15 +439,19 @@ def _cmd_runner(command, **kwargs):
429
  while True:
430
  line = process.stdout.readline()
431
  if not line: break
432
- decoded_line = (line.decode('utf-8')
433
- .replace('\\033[K', '\033[K')
434
- .replace('\\033[1A', '\033[1A')
435
- .replace('\\033[1B', '\033[1B')
436
- .replace('\\r', '\r')
437
- .encode(sys.stdout.encoding, errors='replace'))
438
-
439
- sys.stdout.buffer.write(decoded_line)
440
- sys.stdout.flush()
 
 
 
 
441
 
442
  process.stdout.close()
443
  process.wait()
 
117
  extra_jax_mappings=None,
118
  equation_file=None,
119
  verbosity=1e9,
120
+ progress=None,
121
  maxsize=20,
122
  fast_cycle=False,
123
  maxdepth=None,
 
273
  if constraints is None:
274
  constraints = {}
275
 
276
+ if progress is not None:
277
+ if progress and ('buffer' in sys.stdout.__dir__()):
278
+ warnings.warn("Note: it looks like you are running in Jupyter. The progress bar will be turned off.")
279
+ progress = False
280
+ else:
281
+ if 'buffer' in sys.stdout.__dir__():
282
+ progress = True
283
+ else:
284
+ progress = False
285
+
286
  assert optimizer_algorithm in ['NelderMead', 'BFGS']
287
 
288
  if isinstance(X, pd.DataFrame):
 
431
  command = [f'timeout', f'{timeout}'] + command
432
  _cmd_runner(command, **kwargs)
433
 
434
+ def _cmd_runner(command, progress, **kwargs):
435
  if kwargs['verbosity'] > 0:
436
  print("Running on", ' '.join(command))
437
  process = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=-1)
 
439
  while True:
440
  line = process.stdout.readline()
441
  if not line: break
442
+ decoded_line = line.decode('utf-8')
443
+ if progress:
444
+ decoded_line = (decoded_line
445
+ .replace('\\033[K', '\033[K')
446
+ .replace('\\033[1A', '\033[1A')
447
+ .replace('\\033[1B', '\033[1B')
448
+ .replace('\\r', '\r')
449
+ .encode(sys.stdout.encoding, errors='replace')
450
+ )
451
+ sys.stdout.buffer.write(decoded_line)
452
+ sys.stdout.flush()
453
+ else:
454
+ print(decoded_line, end='')
455
 
456
  process.stdout.close()
457
  process.wait()