def call_online_actions(step, msg, msg_idx): def print_message(result): try: print(json.dumps(result, indent=4, sort_keys=True)) except TypeError: print("Unable to serialize the object returned!") if self.print_response: print("\nMessage {} :\n".format(msg_idx)) print_message(msg) if profiling_enabled: with profiling(output_path=".profiling", uid=clazz) as prof: result = step.execute(input_message=msg, params=params) prof.disable print("\nProfile images created in {}\n".format(prof.image_path)) else: result = step.execute(input_message=msg, params=params) if self.print_response: print("\nResult for Message {} :\n".format(msg_idx)) print_message(result) return result
def test_jupyter_repr_html(self): output_path = tempfile.mkdtemp() uid = str(uuid.uuid4()) with profiling(output_path=output_path, uid=lambda *args, **kwargs: uid, info={'test': 42}) as prof: def foo(): return foo() prof_repr_html = prof._repr_html_() assert '<pre>' in prof_repr_html assert '<img' in prof_repr_html assert os.path.join(output_path, uid + '.png') in prof_repr_html shutil.rmtree(output_path)
def test_context_manager_disabled(self): output_path = tempfile.mkdtemp() uid = str(uuid.uuid4()) with profiling(enable=False, output_path=output_path, uid=uid, info={'test': 42}) as prof: def foo(): return foo() assert not os.path.isfile(os.path.join(output_path, uid + '.pstats')) assert not os.path.isfile(os.path.join(output_path, uid + '.dot')) assert not os.path.isfile(os.path.join(output_path, uid + '.png')) assert not os.path.isfile(os.path.join(output_path, uid + '.json')) shutil.rmtree(output_path)
def test_subprocess_exception_jupyter_repr_html(self, subprocess_mock): subprocess_mock.call.side_effect = Exception() output_path = tempfile.mkdtemp() uid = str(uuid.uuid4()) with profiling(output_path=output_path, uid=uid, info={'test': 42}) as prof: def foo(): return foo() prof_repr_html = prof._repr_html_() assert '<pre>' in prof_repr_html assert '<img' not in prof_repr_html assert os.path.join(output_path, uid + '.png') not in prof_repr_html shutil.rmtree(output_path)
def execute(self, clazz, params, initial_dataset, dataset, model, metrics, profiling_enabled=False): self.print_start_step(clazz) _Step = dynamic_import("{}.{}".format(self.package_name, clazz)) if not self.kwargs: self.kwargs = generate_kwargs(_Step, params, initial_dataset, dataset, model, metrics) step = _Step(**self.kwargs) def call_online_actions(step, msg, msg_idx): def print_message(result): try: print(json.dumps(result, indent=4, sort_keys=True)) except TypeError: print("Unable to serialize the object returned!") if self.print_response: print("\nMessage {} :\n".format(msg_idx)) print_message(msg) if profiling_enabled: with profiling(output_path=".profiling", uid=clazz) as prof: result = step.execute(input_message=msg, params=params) prof.disable print("\nProfile images created in {}\n".format(prof.image_path)) else: result = step.execute(input_message=msg, params=params) if self.print_response: print("\nResult for Message {} :\n".format(msg_idx)) print_message(result) return result if clazz == 'PredictionPreparator': for idx, msg in enumerate(self.predictor_messages): self.pmessages.append(call_online_actions(step, msg, idx)) elif clazz == 'Feedback': for idx, msg in enumerate(self.feedback_messages): self.pmessages.append(call_online_actions(step, msg, idx)) elif clazz == 'Predictor': self.execute("PredictionPreparator", params, initial_dataset, dataset, model, metrics) self.pmessages = self.messages if not self.pmessages else self.pmessages for idx, msg in enumerate(self.pmessages): call_online_actions(step, msg, idx) else: if profiling_enabled: with profiling(output_path=".profiling", uid=clazz) as prof: step.execute(params=params) prof.disable print("\nProfile images created in {}\n".format(prof.image_path)) else: step.execute(params=params) self.print_finish_step()