Skip to content

Commit 48581eb

Browse files
authored
Merge pull request #3700 from alejoe91/add-parent-in-repr-html
Add parents in HTML representation and always print class name
2 parents e4707db + 2570e27 commit 48581eb

3 files changed

Lines changed: 48 additions & 34 deletions

File tree

src/spikeinterface/core/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,31 @@ def __init__(self, main_ids: Sequence) -> None:
7979
# preferred context for multiprocessing
8080
self._preferred_mp_context = None
8181

82+
def _repr_html_(self, display_name=True):
83+
pass
84+
85+
def _get_common_repr_html(self, common_style):
86+
html_annotations = f"<details style='{common_style}'> <summary><strong>Annotations</strong></summary><ul>"
87+
for key, value in self._annotations.items():
88+
html_annotations += f"<li> <strong> {key} </strong>: {value}</li>"
89+
html_annotations += f"</details>"
90+
91+
html_properties = f"<details style='{common_style}'><summary><strong>Properties</strong></summary><ul>"
92+
for key, value in self._properties.items():
93+
# Add a further indent for each property
94+
value_formatted = np.asarray(value)
95+
html_properties += f"<details><summary><strong>{key}</strong></summary>{value_formatted}</details>"
96+
html_properties += "</ul></details>"
97+
98+
if self.get_parent():
99+
html_parent = f"<details style='{common_style}'> <summary><strong>Parent</strong></summary><ul>"
100+
display_name = self.name != self.get_parent().name
101+
html_parent += self.get_parent()._repr_html_(display_name=display_name)
102+
html_parent += "</ul></details>"
103+
else:
104+
html_parent = ""
105+
return html_annotations + html_properties + html_parent
106+
82107
@property
83108
def name(self):
84109
name = self._annotations.get("name", None)

src/spikeinterface/core/baserecording.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def list_to_string(lst, max_size=6):
8989

9090
return txt
9191

92-
def _repr_header(self):
92+
def _repr_header(self, display_name=True):
9393
num_segments = self.get_num_segments()
9494
num_channels = self.get_num_channels()
9595
dtype = self.get_dtype()
@@ -105,8 +105,13 @@ def _repr_header(self):
105105
# Khz for high sampling rate and Hz for LFP
106106
sampling_frequency_repr = f"{(sf_hz/1000.0):0.1f}kHz" if sf_hz > 10_000.0 else f"{sf_hz:0.1f}Hz"
107107

108+
if display_name and self.name != self.__class__.__name__:
109+
name = f"{self.name} ({self.__class__.__name__})"
110+
else:
111+
name = self.__class__.__name__
112+
108113
txt = (
109-
f"{self.name}: "
114+
f"{name}: "
110115
f"{num_channels} channels - "
111116
f"{sampling_frequency_repr} - "
112117
f"{num_segments} segments - "
@@ -118,11 +123,11 @@ def _repr_header(self):
118123

119124
return txt
120125

121-
def _repr_html_(self):
126+
def _repr_html_(self, display_name=True):
122127
common_style = "margin-left: 10px;"
123128
border_style = "border:1px solid #ddd; padding:10px;"
124129

125-
html_header = f"<div style='{border_style}'><strong>{self._repr_header()}</strong></div>"
130+
html_header = f"<div style='{border_style}'><strong>{self._repr_header(display_name)}</strong></div>"
126131

127132
html_segments = ""
128133
if self.get_num_segments() > 1:
@@ -143,19 +148,8 @@ def _repr_html_(self):
143148
html_channel_ids = f"<details style='{common_style}'> <summary><strong>Channel IDs</strong></summary><ul>"
144149
html_channel_ids += f"{self.channel_ids} </details>"
145150

146-
html_annotations = f"<details style='{common_style}'> <summary><strong>Annotations</strong></summary><ul>"
147-
for key, value in self._annotations.items():
148-
html_annotations += f"<li> <strong> {key} </strong>: {value}</li>"
149-
html_annotations += "</ul> </details>"
150-
151-
html_properties = f"<details style='{common_style}'><summary><strong>Channel Properties</strong></summary><ul>"
152-
for key, value in self._properties.items():
153-
# Add a further indent for each property
154-
value_formatted = np.asarray(value)
155-
html_properties += f"<details><summary> <strong> {key} </strong> </summary>{value_formatted}</details>"
156-
html_properties += "</ul></details>"
157-
158-
html_repr = html_header + html_segments + html_channel_ids + html_annotations + html_properties
151+
html_extra = self._get_common_repr_html(common_style)
152+
html_repr = html_header + html_segments + html_channel_ids + html_extra
159153
return html_repr
160154

161155
def get_num_segments(self) -> int:

src/spikeinterface/core/basesorting.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,33 @@ def __init__(self, sampling_frequency: float, unit_ids: List):
3030
self._cached_spike_trains = {}
3131

3232
def __repr__(self):
33+
return self._repr_header()
34+
35+
def _repr_header(self, display_name=True):
3336
nseg = self.get_num_segments()
3437
nunits = self.get_num_units()
3538
sf_khz = self.get_sampling_frequency() / 1000.0
36-
txt = f"{self.name}: {nunits} units - {nseg} segments - {sf_khz:0.1f}kHz"
39+
if display_name and self.name != self.__class__.__name__:
40+
name = f"{self.name} ({self.__class__.__name__})"
41+
else:
42+
name = self.__class__.__name__
43+
txt = f"{name}: {nunits} units - {nseg} segments - {sf_khz:0.1f}kHz"
3744
if "file_path" in self._kwargs:
3845
txt += "\n file_path: {}".format(self._kwargs["file_path"])
3946
return txt
4047

41-
def _repr_html_(self):
48+
def _repr_html_(self, display_name=True):
4249
common_style = "margin-left: 10px;"
4350
border_style = "border:1px solid #ddd; padding:10px;"
4451

45-
html_header = f"<div style='{border_style}'><strong>{self.__repr__()}</strong></div>"
52+
html_header = f"<div style='{border_style}'><strong>{self._repr_header(display_name)}</strong></div>"
4653

4754
html_unit_ids = f"<details style='{common_style}'> <summary><strong>Unit IDs</strong></summary><ul>"
4855
html_unit_ids += f"{self.unit_ids} </details>"
4956

50-
html_annotations = f"<details style='{common_style}'> <summary><strong>Annotations</strong></summary><ul>"
51-
for key, value in self._annotations.items():
52-
html_annotations += f"<li> <strong> {key} </strong>: {value}</li>"
53-
html_annotations += f"</details>"
54-
55-
html_unit_properties = (
56-
f"<details style='{common_style}'><summary><strong>Unit Properties</strong></summary><ul>"
57-
)
58-
for key, value in self._properties.items():
59-
# Add a further indent for each property
60-
value_formatted = np.asarray(value)
61-
html_unit_properties += f"<details><summary><strong>{key}</strong></summary>{value_formatted}</details>"
62-
html_unit_properties += "</ul></details>"
57+
html_extra = self._get_common_repr_html(common_style)
6358

64-
html_repr = html_header + html_unit_ids + html_annotations + html_unit_properties
59+
html_repr = html_header + html_unit_ids + html_extra
6560
return html_repr
6661

6762
@property

0 commit comments

Comments
 (0)