Skip to content
Snippets Groups Projects
Commit 1f1a77ec authored by Iryna de Albuquerque Silva's avatar Iryna de Albuquerque Silva
Browse files

Normalized use of pathlib in functions

parent 92ee051d
No related branches found
No related tags found
3 merge requests!279v0.4.0,!253v0.4.0,!211Add show_graphview funcionality.
Pipeline #55978 passed
...@@ -11,9 +11,11 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo ...@@ -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. :param graph: A Node in the list of ordered nodes.
:type graph: aidge_core.Node :type graph: aidge_core.Node
:return: A dictionary with the Node's attributes. :return: A dictionary with the Node's attributes.
:rtype: dict[str, int, float, bool, None] :rtype: dict[str, int, float, bool, None]
""" """
if node.get_operator().attr is not None: if node.get_operator().attr is not None:
node_attr_dict = node.get_operator().attr.dict() node_attr_dict = node.get_operator().attr.dict()
for key,value in node_attr_dict.items(): 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 ...@@ -25,7 +27,7 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo
return node_attr_dict 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. 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 ...@@ -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. :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 :type write_trainable_params_ext: bool
:param path_trainable_params: Path of the external file used to store the Nodes' trainable parameters. :param path_trainable_params: Path of the external file used to store the Nodes' trainable parameters.
:type path_trainable_params: str :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``. :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 :type params_file_format: str
:return: A dictionary with the GraphView description. :return: A dictionary with the GraphView description.
...@@ -126,8 +128,8 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e ...@@ -126,8 +128,8 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e
params_file_format.casefold() params_file_format.casefold()
if params_file_format=='npz': if params_file_format=='npz':
np.savez_compressed(os.path.join(path_trainable_params, node.name()), **{node.name() : node.get_operator().get_output(0)}) np.savez_compressed(Path(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') node_dict['tensor_data'] = Path(path_trainable_params, node.name() + '.npz')
elif params_file_format=='json': elif params_file_format=='json':
tensor = np.array(node.get_operator().get_output(0)) 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 ...@@ -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) 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: else:
raise Exception("File format to write trainable parameters not recognized.") 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 ...@@ -205,7 +207,7 @@ def gview_to_json(gview : aidge_core.GraphView, json_path : Path, write_trainabl
if write_trainable_params_ext: if write_trainable_params_ext:
path_trainable_params = (json_path.parent).joinpath(json_path.stem + '_trainable_params/') path_trainable_params = (json_path.parent).joinpath(json_path.stem + '_trainable_params/')
else: else:
path_trainable_params = '' path_trainable_params = Path()
if isinstance(gview, aidge_core.GraphView): if isinstance(gview, aidge_core.GraphView):
# Sort GraphView in topological order # Sort GraphView in topological order
...@@ -218,6 +220,6 @@ def gview_to_json(gview : aidge_core.GraphView, json_path : Path, write_trainabl ...@@ -218,6 +220,6 @@ def gview_to_json(gview : aidge_core.GraphView, json_path : Path, write_trainabl
_write_dict_json(graphview_dict, json_path) _write_dict_json(graphview_dict, json_path)
else: else:
raise Exception("Graph must be an aidge_core.GraphView instance.") raise Exception("Graph must be an instance of aidge_core.GraphView.")
return None return None
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment