From 2152ff016860f3833984b272b22aa8b557686929 Mon Sep 17 00:00:00 2001 From: Stephanie Labasan Brink Date: Thu, 6 Oct 2022 16:31:48 -0700 Subject: [PATCH] add function to parse caliper metadata (#56) * add function to parse caliper metadata converts caliper metadata values into correct Python objects (e.g., int, string, list) * update docstring * add tests for metadata types --- hatchet/readers/caliper_native_reader.py | 37 +++++++++++++++++++++++- hatchet/tests/caliper.py | 12 ++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/hatchet/readers/caliper_native_reader.py b/hatchet/readers/caliper_native_reader.py index 2380db90..f5069de5 100644 --- a/hatchet/readers/caliper_native_reader.py +++ b/hatchet/readers/caliper_native_reader.py @@ -241,6 +241,40 @@ def _create_parent(child_node, parent_callpath): return list_roots + def _parse_metadata(self, mdata): + """Convert Caliper Metadata values into correct Python objects. + + Args: + mdata (dict[str: str]): metadata to convert + + Return: + (dict[str: str]): modified metadata + """ + parsed_mdata = {} + for k, v in mdata.items(): + # If the value is an int, convert it to an int. + try: + parsed_mdata[k] = int(v) + except ValueError: + # If the value is a float, convert it to a float + try: + parsed_mdata[k] = float(v) + except ValueError: + # If the value is a list or tuple, convert it to a list or + # tuple + if v.startswith("[") and v.endswith("]"): + parsed_mdata[k] = [ + elem.strip() for elem in v.strip("][").split(",") + ] + elif v.startswith("(") and v.endswith(")"): + parsed_mdata[k] = [ + elem.strip() for elem in v.strip(")(").split(",") + ] + # If the value is a string, just save it as-is + else: + parsed_mdata[k] = v + return parsed_mdata + def read(self): """Read the caliper records to extract the calling context tree.""" if isinstance(self.filename_or_caliperreader, str): @@ -370,6 +404,7 @@ def read(self): self.default_metric = exc_metrics[0] metadata = self.filename_or_caliperreader.globals + parsed_metadata = self._parse_metadata(metadata) return hatchet.graphframe.GraphFrame( graph, @@ -377,5 +412,5 @@ def read(self): exc_metrics, inc_metrics, self.default_metric, - metadata=metadata, + metadata=parsed_metadata, ) diff --git a/hatchet/tests/caliper.py b/hatchet/tests/caliper.py index ce23a163..b0612af4 100644 --- a/hatchet/tests/caliper.py +++ b/hatchet/tests/caliper.py @@ -226,6 +226,9 @@ def test_graphframe_native_lulesh_from_file(lulesh_caliper_cali): elif col in ("name", "node"): assert gf.dataframe[col].dtype == object + assert type(gf.metadata["cali.channel"]) == str + assert type(gf.metadata["cali.caliper.version"]) == str + @pytest.mark.skipif( not caliperreader_avail, reason="needs caliper-reader package to be loaded" @@ -239,6 +242,7 @@ def test_graphframe_native_lulesh_from_caliperreader(lulesh_caliper_cali): assert len(gf.dataframe.groupby("name")) == 19 assert "cali.caliper.version" in gf.metadata.keys() + assert type(gf.metadata["cali.caliper.version"]) == str for col in gf.dataframe.columns: if col in ("time (inc)", "time"): @@ -284,6 +288,10 @@ def test_sw4_cuda_from_caliperreader(sw4_caliper_cuda_activity_profile_cali): for col in gf.exc_metrics + gf.inc_metrics: assert col in gf.dataframe.columns + assert type(gf.metadata["mpi.world.size"]) == int + assert type(gf.metadata["cali.caliper.version"]) == str + assert type(gf.metadata["cali.channel"]) == str + def test_sw4_cuda_summary_from_caliperreader( sw4_caliper_cuda_activity_profile_summary_cali, @@ -297,3 +305,7 @@ def test_sw4_cuda_summary_from_caliperreader( for col in gf.exc_metrics + gf.inc_metrics: assert col in gf.dataframe.columns + + assert type(gf.metadata["mpi.world.size"]) == int + assert type(gf.metadata["cali.caliper.version"]) == str + assert type(gf.metadata["cali.channel"]) == str