Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgarcia committed Jan 15, 2025
1 parent 7e50caa commit 3c1e195
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/spikeinterface/widgets/sorting_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions src/spikeinterface/widgets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
return units_table
22 changes: 13 additions & 9 deletions src/spikeinterface/widgets/utils_sortingview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3c1e195

Please sign in to comment.