From 3c1e19524c6c04284332a94fad81bc548b20659d Mon Sep 17 00:00:00 2001 From: Samuel Garcia Date: Wed, 15 Jan 2025 09:35:09 +0100 Subject: [PATCH] clean --- src/spikeinterface/widgets/sorting_summary.py | 5 ++-- src/spikeinterface/widgets/utils.py | 26 ++++++++++++++----- .../widgets/utils_sortingview.py | 22 +++++++++------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/spikeinterface/widgets/sorting_summary.py b/src/spikeinterface/widgets/sorting_summary.py index 46796e3be4..0b127abae1 100644 --- a/src/spikeinterface/widgets/sorting_summary.py +++ b/src/spikeinterface/widgets/sorting_summary.py @@ -44,14 +44,13 @@ class SortingSummaryWidget(BaseWidget): label_choices : list or None, default: None List of labels to be added to the curation table (sortingview backend) - unit_table_properties : list or None, default: None + displayed_units_properties : list or None, default: None List of properties to be added to the unit table. These may be drawn from the sorting extractor, and, if available, - the quality_metrics and template_metrics extensions of the SortingAnalyzer. + the quality_metrics/template_metrics/unit_locations extensions of the SortingAnalyzer. See all properties available with sorting.get_property_keys(), and, if available, analyzer.get_extension("quality_metrics").get_data().columns and analyzer.get_extension("template_metrics").get_data().columns. - (sortingview backend) extra_units_properties : None dict, default: None A dict with extra units properties to display. curation_dict : dict or None diff --git a/src/spikeinterface/widgets/utils.py b/src/spikeinterface/widgets/utils.py index d7789f29ed..89025dea31 100644 --- a/src/spikeinterface/widgets/utils.py +++ b/src/spikeinterface/widgets/utils.py @@ -247,6 +247,22 @@ def array_to_image( def make_units_table_from_sorting(sorting, units_table=None): + """ + Make a DataFrame from sorting properties. + Only for properties with ndim=1 + + Parameters + ---------- + sorting : Sorting + The Sorting object + units_table : None | pd.DataFrame + Optionally a existing dataframe. + + Returns + ------- + units_table : pd.DataFrame + Table containing all columns. + """ if units_table is None: import pandas as pd @@ -255,8 +271,6 @@ def make_units_table_from_sorting(sorting, units_table=None): for col in sorting.get_property_keys(): values = sorting.get_property(col) if values.dtype.kind in "iuUSfb" and values.ndim == 1: - print(col, values, sorting.unit_ids) - print(col, len(values), len(sorting.unit_ids)) units_table.loc[:, col] = values return units_table @@ -273,6 +287,8 @@ def make_units_table_from_analyzer( * sorting properties * extra columns + This used in sortingview and spikeinterface-gui to display the units table in a flexible way. + Parameters ---------- sorting_analyzer : SortingAnalyzer @@ -291,12 +307,10 @@ def make_units_table_from_analyzer( if analyzer.get_extension("unit_locations") is not None: locs = analyzer.get_extension("unit_locations").get_data() df = pd.DataFrame(locs[:, :2], columns=["x", "y"], index=analyzer.unit_ids) - print(df.index, df.index.dtype) all_df.append(df) if analyzer.get_extension("quality_metrics") is not None: df = analyzer.get_extension("quality_metrics").get_data() - print(df.index, df.index.dtype) all_df.append(df) if analyzer.get_extension("template_metrics") is not None: @@ -308,12 +322,12 @@ def make_units_table_from_analyzer( else: units_table = pd.DataFrame(index=analyzer.unit_ids) - print(units_table) make_units_table_from_sorting(analyzer.sorting, units_table=units_table) if extra_properties is not None: for col, values in extra_properties.items(): + # the ndim = 1 is important because we need column only for the display in gui. if values.dtype.kind in "iuUSfb" and values.ndim == 1: units_table.loc[:, col] = values - return units_table \ No newline at end of file + return units_table diff --git a/src/spikeinterface/widgets/utils_sortingview.py b/src/spikeinterface/widgets/utils_sortingview.py index 215c1eaf32..f6eb8ea529 100644 --- a/src/spikeinterface/widgets/utils_sortingview.py +++ b/src/spikeinterface/widgets/utils_sortingview.py @@ -76,26 +76,30 @@ def generate_unit_table_view( unit_properties = unit_properties[keep] units_tables = units_tables.loc[:, unit_properties] + dtype_convertor = {"i": "int", "u": "int", "f": "float", "U": "str", "S": "str", "b": "bool"} + ut_columns = [] for col in units_tables.columns: values = units_tables[col].to_numpy() - ut_columns.append(vv.UnitsTableColumn(key=col, label=col, dtype=values.dtype)) + if values.dtype.kind in dtype_convertor: + txt_dtype = dtype_convertor[values.dtype.kind] + ut_columns.append(vv.UnitsTableColumn(key=col, label=col, dtype=txt_dtype)) ut_rows = [] for unit_index, unit_id in enumerate(sorting.unit_ids): row_values = {} for col in units_tables.columns: values = units_tables[col].to_numpy() - value = values[unit_index] - # Check for NaN values and round floats - if values.dtype.kind == "f": - if np.isnan(values[unit_index]): - continue - value = np.format_float_positional(value, precision=4, fractional=False) - row_values[col] = value + if values.dtype.kind in dtype_convertor: + value = values[unit_index] + if values.dtype.kind == "f": + # Check for NaN values and round floats + if np.isnan(values[unit_index]): + continue + value = np.format_float_positional(value, precision=4, fractional=False) + row_values[col] = value ut_rows.append(vv.UnitsTableRow(unit_id=unit_id, values=check_json(row_values))) - v_units_table = vv.UnitsTable(rows=ut_rows, columns=ut_columns, similarity_scores=similarity_scores) return v_units_table