diff --git a/devtools/inspector/_inspector.py b/devtools/inspector/_inspector.py index d951a1ada90..001ea505501 100644 --- a/devtools/inspector/_inspector.py +++ b/devtools/inspector/_inspector.py @@ -1143,23 +1143,18 @@ def to_dataframe( ] return pd.concat(df_list, ignore_index=True) - def print_data_tabular( + def _prepare_dataframe( self, - file: IO[str] = sys.stdout, include_units: bool = True, include_delegate_debug_data: bool = False, - ) -> None: + ) -> pd.DataFrame: """ - Displays the underlying EventBlocks in a structured tabular format, with each row representing an Event. - Args: - file: Which IO stream to print to. Defaults to stdout. - Not used if this is in an IPython environment such as a Jupyter notebook. include_units: Whether headers should include units (default true) include_delegate_debug_data: Whether to include delegate debug metadata (default false) Returns: - None + Returns a pandas DataFrame of the Events in each EventBlock in the inspector, with additional filtering. """ combined_df = self.to_dataframe(include_units, include_delegate_debug_data) @@ -1171,7 +1166,44 @@ def print_data_tabular( ] filtered_column_df.reset_index(drop=True, inplace=True) - display_or_print_df(filtered_column_df, file) + return filtered_column_df + + def print_data_tabular( + self, + file: IO[str] = sys.stdout, + include_units: bool = True, + include_delegate_debug_data: bool = False, + ) -> None: + """ + Displays the underlying EventBlocks in a structured tabular format, with each row representing an Event. + + Args: + file: Which IO stream to print to. Defaults to stdout. + Not used if this is in an IPython environment such as a Jupyter notebook. + include_units: Whether headers should include units (default true) + include_delegate_debug_data: Whether to include delegate debug metadata (default false) + + Returns: + None + """ + df = self._prepare_dataframe(include_units, include_delegate_debug_data) + display_or_print_df(df, file) + + def save_data_to_tsv( + self, + file: IO[str], + ) -> None: + """ + Stores the underlying EventBlocks in tsv format to facilitate copy-paste into spreadsheets. + + Args: + file: Which IO stream to print to. Do not use stdout, as tab separator is not preserved. + + Returns: + None + """ + df = self._prepare_dataframe() + df.to_csv(file, sep="\t") # TODO: write unit test def find_total_for_module(self, module_name: str) -> float: diff --git a/devtools/inspector/inspector_cli.py b/devtools/inspector/inspector_cli.py index db3536a84bf..00e74cc25f8 100644 --- a/devtools/inspector/inspector_cli.py +++ b/devtools/inspector/inspector_cli.py @@ -43,6 +43,11 @@ def main() -> None: required=False, help="Provide an optional buffer file path.", ) + parser.add_argument( + "--tsv_path", + required=False, + help="Provide an optional tsv file path.", + ) parser.add_argument("--compare_results", action="store_true") args = parser.parse_args() @@ -55,6 +60,8 @@ def main() -> None: target_time_scale=TimeScale(args.target_time_scale), ) inspector.print_data_tabular() + if args.tsv_path: + inspector.save_data_to_tsv(args.tsv_path) if args.compare_results: for event_block in inspector.event_blocks: if event_block.name == "Execute":