def resolve(self) -> Results: """ Finish the remote job and return the results. This function will block until the remote job is complete. :returns: Results """ # Poll the job status until it finishes. while not self.__poll_job_status(): time.sleep(5) # Request the results of the finished job. results_response = self.server.get(Endpoint.RESULT, f"/{self.result_id}/get") if not results_response.ok: raise Exception(ErrorResponse.parse_raw(results_response.text).msg) print(f"Results size: {sys.getsizeof(results_response.content)}") results_json = bz2.decompress(results_response.content).decode() print(f"Expanded to: {sys.getsizeof(results_json)}") # Parse and return the response body into a valid Results object. results = Results.from_json(results_json) return results
def make_plot(memory_id: str): # Make sure we can grab a memory with this ID. if not delegate.job_complete(memory_id): status = delegate.job_status(memory_id) return status.status_text, 404 # Grab the memory. memory: Results = Results.from_json(delegate.job_results(memory_id)) # Swap the pyplot backend so the Results#plot call wont try to write to a GUI. pyplot.switch_backend("template") # Write the plot as a .png to the tempfile. _, temp = tempfile.mkstemp(suffix=".png") memory.plot(save_png=temp) # Read the contents of the temp file and cleanup. with open(temp, "rb") as infile: plot_bytes = infile.read() os.remove(temp) compressed_plot = bz2.compress(plot_bytes) response = make_response(compressed_plot) response.headers["Content-Encoding"] = "bzip2" return response, 200
def make_plotplotly(results_json: str, *args, **kwargs) -> str: import plotly.io as plotlyio results = Results.from_json(results_json) plot = Results.plotplotly(results, return_plotly_figure=True, **kwargs) plot_json = plotlyio.to_json(plot) return plot_json
def test_equality_of_named_results(self): """ Test that Model simulation results can be converted to JSON and back. """ for model in self.runnable_models: target = model() results = target.run() results_from = Results.from_json(results.to_json()) self.assertEquals(results.to_json(), results_from.to_json())
def test_equality_of_named_results(self): """ Test that anonymous Model simulation results can be converted to JSON and back. """ for model in self.runnable_models: target = model() results = target.run() results._translation_table = target.get_translation_table() results = results.to_anon().to_json() self.assertEqual(results, Results.from_json(results).to_json())
def results_to_json(results_json): results = Results.from_json(results_json) return results.average_ensemble().to_json()