From 1f1a77ecba894722f5e44518497e503785901d35 Mon Sep 17 00:00:00 2001 From: idealbuq <iryna.dealbuquerquesilva@cea.fr> Date: Wed, 2 Oct 2024 09:40:03 +0000 Subject: [PATCH] Normalized use of pathlib in functions --- aidge_core/show_graphview.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/aidge_core/show_graphview.py b/aidge_core/show_graphview.py index b8521d2c0..ddf0fc4b4 100644 --- a/aidge_core/show_graphview.py +++ b/aidge_core/show_graphview.py @@ -11,9 +11,11 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo :param graph: A Node in the list of ordered nodes. :type graph: aidge_core.Node + :return: A dictionary with the Node's attributes. :rtype: dict[str, int, float, bool, None] - """ + """ + if node.get_operator().attr is not None: node_attr_dict = node.get_operator().attr.dict() for key,value in node_attr_dict.items(): @@ -25,7 +27,7 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo return node_attr_dict -def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_embed : bool, write_trainable_params_ext : bool, path_trainable_params : str, params_file_format : str) -> dict[str, int, float, bool, None]: +def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_embed : bool, write_trainable_params_ext : bool, path_trainable_params : Path, params_file_format : str) -> dict[str, int, float, bool, None]: """ Creates a dictionary to store the information of a given ordered GraphView. @@ -36,8 +38,8 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e :param write_trainable_params_ext: Whether or not to write the eventual trainable parameters of the Nodes in an external file. :type write_trainable_params_ext: bool :param path_trainable_params: Path of the external file used to store the Nodes' trainable parameters. - :type path_trainable_params: str - :param params_file_format: Format of the external file used to store the Nodes' trainable parameters. Options: ``npz`` or ``json``. + :type path_trainable_params: Path + :param params_file_format: Format of the external file used to store the Nodes' trainable parameters. Options: ``npz`` or ``json``. Default : ``json``. Requires ``write_trainable_params_ext``. :type params_file_format: str :return: A dictionary with the GraphView description. @@ -126,8 +128,8 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e params_file_format.casefold() if params_file_format=='npz': - np.savez_compressed(os.path.join(path_trainable_params, node.name()), **{node.name() : node.get_operator().get_output(0)}) - node_dict['tensor_data'] = os.path.join(path_trainable_params, node.name() + '.npz') + np.savez_compressed(Path(path_trainable_params, node.name()), **{node.name() : node.get_operator().get_output(0)}) + node_dict['tensor_data'] = Path(path_trainable_params, node.name() + '.npz') elif params_file_format=='json': tensor = np.array(node.get_operator().get_output(0)) @@ -140,10 +142,10 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e } } - with open(os.path.join(path_trainable_params, node.name() + '.json'), 'w') as fp: + with open(Path(path_trainable_params, node.name() + '.json'), 'w') as fp: json.dump(tensor_dict, fp, indent=4) - node_dict['tensor_data'] = os.path.join(path_trainable_params, node.name() + '.json') + node_dict['tensor_data'] = Path(path_trainable_params, node.name() + '.json') else: raise Exception("File format to write trainable parameters not recognized.") @@ -205,7 +207,7 @@ def gview_to_json(gview : aidge_core.GraphView, json_path : Path, write_trainabl if write_trainable_params_ext: path_trainable_params = (json_path.parent).joinpath(json_path.stem + '_trainable_params/') else: - path_trainable_params = '' + path_trainable_params = Path() if isinstance(gview, aidge_core.GraphView): # Sort GraphView in topological order @@ -218,6 +220,6 @@ def gview_to_json(gview : aidge_core.GraphView, json_path : Path, write_trainabl _write_dict_json(graphview_dict, json_path) else: - raise Exception("Graph must be an aidge_core.GraphView instance.") + raise Exception("Graph must be an instance of aidge_core.GraphView.") return None \ No newline at end of file -- GitLab