def test_from_json_invalid(fig1): dict1 = fig1.to_dict() # Set bad property name dict1['data'][0]['marker']['bogus'] = 123 # Set property with bad value dict1['data'][0]['marker']['size'] = -1 # Serialize to json bad_json = json.dumps(dict1, **opts) with pytest.raises(ValueError): pio.from_json(bad_json)
def test_from_json_invalid(fig1): dict1 = fig1.to_dict() # Set bad property name dict1["data"][0]["marker"]["bogus"] = 123 # Set property with bad value dict1["data"][0]["marker"]["size"] = -1 # Serialize to json bad_json = json.dumps(dict1, **opts) with pytest.raises(ValueError): pio.from_json(bad_json)
def _load(self) -> graph_objects.Figure: load_path = get_filepath_str(self._get_load_path(), self._protocol) with self._fs.open(load_path, **self._fs_open_args_load) as fs_file: # read_json doesn't work correctly with file handler, so we have to read the file, # decode it manually and pass to the low-level from_json instead. return pio.from_json(str(fs_file.read(), "utf-8"), **self._load_args)
def to_plotly_figure(self): try: from plotly.io import from_json except ModuleNotFoundError: raise ModuleNotFoundError( 'Could not find plotly in the installed modules.') return from_json(self.json())
def test_from_json_output_type(fig1, fig_type_spec, fig_type): fig1_json = json.dumps(fig1, **opts) fig1_loaded = pio.from_json(fig1_json, output_type=fig_type_spec) # Check return type assert isinstance(fig1_loaded, fig_type) # Check return json assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
def test_from_json(fig1): fig1_json = json.dumps(fig1, **opts) fig1_loaded = pio.from_json(fig1_json) # Check return type assert isinstance(fig1_loaded, go.Figure) # Check return json assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
def test_plot(self): plot_response = self.server.get(Endpoint.RESULT, f"/{self.model.get_json_hash()}/plot") # plot_request = requests.get(f"{self.server.address}/api/v1/result/{self.model.get_json_hash()}/plot") print(f"Plot size: {sys.getsizeof(plot_response.content)}") plot_json = bz2.decompress(plot_response.content).decode() with open("test", "w") as outfile: outfile.write(plot_json) plot = plotlyio.from_json(plot_json) plot.show()
def test_from_json_skip_invalid(fig1): dict1 = fig1.to_dict() # Set bad property name dict1['data'][0]['marker']['bogus'] = 123 # Set property with bad value dict1['data'][0]['marker']['size'] = -1 # Serialize to json bad_json = json.dumps(dict1, **opts) fig1_loaded = pio.from_json(bad_json, skip_invalid=True) # Check loaded figure assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
def test_from_json_skip_invalid(fig1): dict1 = fig1.to_dict() # Set bad property name dict1["data"][0]["marker"]["bogus"] = 123 # Set property with bad value dict1["data"][0]["marker"]["size"] = -1 # Serialize to json bad_json = json.dumps(dict1, **opts) fig1_loaded = pio.from_json(bad_json, skip_invalid=True) # Check loaded figure assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
def hook_plotplotly(self, *args, **kwargs): plot_request = PlotPlotlyRequest(result_id=self.result_id) plot_response: JobStatusResponse = unwrap_or_err( JobStatusResponse, self.server.post(Endpoint.GILLESPY2_RESULTS, "/plotplotly", request=plot_request)) if plot_response.has_failed: raise Exception("Failed to create plotly figure.") print(f"Plot size: {sys.getsizeof(plot_response.content)}") plot_json = bz2.decompress(plot_response.content).decode() print(f"Expanded to: {sys.getsizeof(plot_json)}") with open("test", "w") as outfile: outfile.write(plot_json) plot = plotlyio.from_json(plot_json) plot.show()
def load_json_figure(json_path): with open(json_path, 'r') as f: json_figure = json.load(f) return pio.from_json(json.dumps(json_figure))
# we want positive delta_x and delta_v close to 0 print("all good") print(f"min_distance:{min_distance}") # with open(os.path.join("/home/edoardo/ray_results/tune_TD3_stopping_car_continuous", "simulation.csv"), 'w', # newline='') as myfile: # wr = csv.writer(myfile, quoting=csv.QUOTE_NONNUMERIC) # for i, item in enumerate(position_list): # wr.writerow((x_list[i], item)) # wr.writerow("") import plotly.graph_objects as go import plotly.io as pio fig = go.Figure() with open( '/home/edoardo/ray_results/tune_TD3_stopping_car_continuous/test/plot.json', 'r') as f: fig = pio.from_json(f.read()) # trace1 = go.Scatter(x=list(range(len(position_list))), y=position_list, mode='markers', ) for i in range(len(y_list)): trace1 = go.Scatter(x=x_list[i], y=y_list[i], mode='markers') fig.add_trace(trace1) fig.update_layout(xaxis_title="delta v", yaxis_title="delta x") fig.show() save_dir = "/home/edoardo/ray_results/tune_TD3_stopping_car_continuous/test" fig.write_html(os.path.join(save_dir, "merged.html"), include_plotlyjs="cdn") ray.shutdown() # suggest 200 timesteps
# + active="" # # # - # ## Linear models (targets: G_O and G_OH) using all features # + jupyter={} path_i = os.path.join(os.environ["PROJ_irox_oer"], "workflow/oer_vs_features", "out_plot/oer_lin_model__G_O_plot.json") with open(path_i, "r") as fle: fig_json_str_i = fle.read() fig = from_json(fig_json_str_i) fig.show() # + jupyter={} path_i = os.path.join(os.environ["PROJ_irox_oer"], "workflow/oer_vs_features", "out_plot/oer_lin_model__G_OH_plot.json") with open(path_i, "r") as fle: fig_json_str_i = fle.read() fig = from_json(fig_json_str_i) fig.show() # + active="" # #
def fig_from_json(filename): with open(filename, "r") as f: fig = pio.from_json(f.read()) return fig
def __init__(self, json_data): self.json = json_data if len(self.json) != 0: self.figure = pio.from_json(self.json) else: self.figure = go.Figure()