Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_AnyPyProcessOutput():
out = AnyPyProcessOutputList( [ AnyPyProcessOutput({'AAAA':1}),
AnyPyProcessOutput({'AAAA':2}),
AnyPyProcessOutput({'AAAA':3}),
AnyPyProcessOutput({'AAAA':4}),
AnyPyProcessOutput({'AAAA':5}),
AnyPyProcessOutput({'AAAA':6, 'ERROR':0})
])
assert(len(out)==6)
assert(isinstance(out[0], AnyPyProcessOutput ))
# Test slice get
assert(len(out[1:3])==2)
assert(isinstance(out[0:2][0], AnyPyProcessOutput))
# Test slice set
out[:] = [e for e in out if 'ERROR' not in e]
assert(len(out)==5)
assert( isinstance(out['A'], np.ndarray) )
def test_AnyPyProcessOutput():
out = AnyPyProcessOutputList( [ AnyPyProcessOutput({'AAAA':1}),
AnyPyProcessOutput({'AAAA':2}),
AnyPyProcessOutput({'AAAA':3}),
AnyPyProcessOutput({'AAAA':4}),
AnyPyProcessOutput({'AAAA':5}),
AnyPyProcessOutput({'AAAA':6, 'ERROR':0})
])
assert(len(out)==6)
assert(isinstance(out[0], AnyPyProcessOutput ))
# Test slice get
assert(len(out[1:3])==2)
assert(isinstance(out[0:2][0], AnyPyProcessOutput))
# Test slice set
out[:] = [e for e in out if 'ERROR' not in e]
assert(len(out)==5)
assert( isinstance(out['A'], np.ndarray) )
assert( out['A'].shape == (5,) )
def __init__(self, folder=None, macro=None, taskname=None, number=1, logfile=None):
"""Init the Task class with the class attributes."""
if folder:
folder = Path(folder)
else:
folder = Path(os.getcwd())
self.folder = str(folder.absolute())
if macro is not None:
self.macro = macro
else:
self.macro = []
self.output = AnyPyProcessOutput()
self.number = number
self.logfile = logfile or ""
self.processtime = 0
self.retcode = None
self.name = taskname
if taskname:
self.name = taskname
else:
self.name = f"{folder.parent.name}-{folder.name}-{number}"
def create_repr(maxlength=500):
repr_list = []
for elem in self.list:
if not isinstance(elem, AnyPyProcessOutput):
repr_list.append(" " + pprint.pformat(elem))
continue
for line in elem._repr_gen(prefix=" "):
repr_list.append(line)
if maxlength and len(repr_list) > maxlength:
repr_list.append(" ...")
return repr_list
if repr_list and not repr_list[-1].endswith(","):
repr_list[-1] = repr_list[-1] + ","
if len(repr_list):
repr_list[-1] = repr_list[-1].rstrip(",")
repr_list[0] = "[" + repr_list[0][1:]
repr_list[-1] = repr_list[-1] + "]"
else:
repr_list.append("[]")
def parse_anybodycon_output(
raw, errors_to_ignore=None, warnings_to_include=None, fatal_warnings=False
):
""" Parse the output log file from AnyBodyConsole to
for data, errors and warnings. If fatal_warnins is
True, then warnings are also added to the error list.
"""
warnings_to_include = warnings_to_include or []
errors_to_ignore = errors_to_ignore or []
output = AnyPyProcessOutput()
# Find all data in logfile
prefix_replacement = ("", "")
for dump in DUMP_PATTERN.finditer(raw):
name, val = dump.group(1), dump.group(2)
new_prefix = correct_dump_prefix(raw, dump.start())
if new_prefix:
prefix_replacement = (name, new_prefix)
name = name.replace(*prefix_replacement)
try:
val = _parse_data(dump.group(2))
except (SyntaxError, ValueError):
warnings.warn("\n\nCould not parse console output:\n" + name)
output[name] = val
error_list = []
def _add_non_ignored_errors(error_line):
>>> results = app.load_results('saved_results.db')
Continue processing unfinished batches:
>>> app.load_results('unfinished_results.db')
>>> results = app.start_macro() # rerun unfinished
"""
loadkey = "processed_tasks"
db = shelve.open(filename)
loaded_data = db[loadkey]
db.close()
# Hack to help Enrico convert data to the new structured
if not isinstance(loaded_data[0].output, AnyPyProcessOutput):
for task in loaded_data:
task.output = AnyPyProcessOutput(task.output)
self.cached_tasklist = loaded_data
results = [task.get_output(True) for task in loaded_data]
return AnyPyProcessOutputList(results)
>>> app = AnyPyProcess()
>>> results = app.load_results('saved_results.db')
Continue processing unfinished batches:
>>> app.load_results('unfinished_results.db')
>>> results = app.start_macro() # rerun unfinished
"""
loadkey = "processed_tasks"
db = shelve.open(filename)
loaded_data = db[loadkey]
db.close()
# Hack to help Enrico convert data to the new structured
if not isinstance(loaded_data[0].output, AnyPyProcessOutput):
for task in loaded_data:
task.output = AnyPyProcessOutput(task.output)
self.cached_tasklist = loaded_data
results = [task.get_output(True) for task in loaded_data]
return AnyPyProcessOutputList(results)
def __getitem__(self, i):
if isinstance(i, str):
# Find the entries where i matches the keys
key = _get_first_key_match(i, self.list[0])
try:
data = np.array(
[super(AnyPyProcessOutput, e).__getitem__(key) for e in self.list]
)
except KeyError:
msg = " The key: '{}' is not present in all elements of the output."
raise KeyError(msg.format(key)) from None
if data.dtype == np.dtype("O"):
# Data will be stacked as an array of objects, if the length of the
# time dimension is not consistant across simulations. Warn that some numpy
# featurs will not be avaiable.
warnings.warn(
"\nThe length of the time variable varies across macros. "
"Numpy does not support ragged arrays. Data is returned "
"as an array of array objects"
)
return data
else:
return type(self)(self.list[i]) if isinstance(i, slice) else self.list[i]