Skip to content

Commit 2570e27

Browse files
committed
Move extra html to base and use display_name
1 parent a1ceb72 commit 2570e27

3 files changed

Lines changed: 37 additions & 49 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: 6 additions & 24 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, parent_name=None):
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,7 +105,7 @@ def _repr_header(self, parent_name=None):
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 parent_name is None or parent_name != self.name:
108+
if display_name and self.name != self.__class__.__name__:
109109
name = f"{self.name} ({self.__class__.__name__})"
110110
else:
111111
name = self.__class__.__name__
@@ -123,11 +123,11 @@ def _repr_header(self, parent_name=None):
123123

124124
return txt
125125

126-
def _repr_html_(self, parent_name=None):
126+
def _repr_html_(self, display_name=True):
127127
common_style = "margin-left: 10px;"
128128
border_style = "border:1px solid #ddd; padding:10px;"
129129

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

132132
html_segments = ""
133133
if self.get_num_segments() > 1:
@@ -148,26 +148,8 @@ def _repr_html_(self, parent_name=None):
148148
html_channel_ids = f"<details style='{common_style}'> <summary><strong>Channel IDs</strong></summary><ul>"
149149
html_channel_ids += f"{self.channel_ids} </details>"
150150

151-
html_annotations = f"<details style='{common_style}'> <summary><strong>Annotations</strong></summary><ul>"
152-
for key, value in self._annotations.items():
153-
html_annotations += f"<li> <strong> {key} </strong>: {value}</li>"
154-
html_annotations += "</ul> </details>"
155-
156-
html_properties = f"<details style='{common_style}'><summary><strong>Channel Properties</strong></summary><ul>"
157-
for key, value in self._properties.items():
158-
# Add a further indent for each property
159-
value_formatted = np.asarray(value)
160-
html_properties += f"<details><summary> <strong> {key} </strong> </summary>{value_formatted}</details>"
161-
html_properties += "</ul></details>"
162-
163-
if self.get_parent():
164-
html_parent = f"<details style='{common_style}'> <summary><strong>Parent</strong></summary><ul>"
165-
html_parent += self.get_parent()._repr_html_(parent_name=self.name)
166-
html_parent += "</ul></details>"
167-
else:
168-
html_parent = ""
169-
170-
html_repr = html_header + html_segments + html_channel_ids + html_annotations + html_properties + html_parent
151+
html_extra = self._get_common_repr_html(common_style)
152+
html_repr = html_header + html_segments + html_channel_ids + html_extra
171153
return html_repr
172154

173155
def get_num_segments(self) -> int:

src/spikeinterface/core/basesorting.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def __init__(self, sampling_frequency: float, unit_ids: List):
3232
def __repr__(self):
3333
return self._repr_header()
3434

35-
def _repr_header(self, parent_name=None):
35+
def _repr_header(self, display_name=True):
3636
nseg = self.get_num_segments()
3737
nunits = self.get_num_units()
3838
sf_khz = self.get_sampling_frequency() / 1000.0
39-
if parent_name is None or parent_name != self.name:
39+
if display_name and self.name != self.__class__.__name__:
4040
name = f"{self.name} ({self.__class__.__name__})"
4141
else:
4242
name = self.__class__.__name__
@@ -45,37 +45,18 @@ def _repr_header(self, parent_name=None):
4545
txt += "\n file_path: {}".format(self._kwargs["file_path"])
4646
return txt
4747

48-
def _repr_html_(self, parent_name=None):
48+
def _repr_html_(self, display_name=True):
4949
common_style = "margin-left: 10px;"
5050
border_style = "border:1px solid #ddd; padding:10px;"
5151

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

5454
html_unit_ids = f"<details style='{common_style}'> <summary><strong>Unit IDs</strong></summary><ul>"
5555
html_unit_ids += f"{self.unit_ids} </details>"
5656

57-
html_annotations = f"<details style='{common_style}'> <summary><strong>Annotations</strong></summary><ul>"
58-
for key, value in self._annotations.items():
59-
html_annotations += f"<li> <strong> {key} </strong>: {value}</li>"
60-
html_annotations += f"</details>"
57+
html_extra = self._get_common_repr_html(common_style)
6158

62-
html_unit_properties = (
63-
f"<details style='{common_style}'><summary><strong>Unit Properties</strong></summary><ul>"
64-
)
65-
for key, value in self._properties.items():
66-
# Add a further indent for each property
67-
value_formatted = np.asarray(value)
68-
html_unit_properties += f"<details><summary><strong>{key}</strong></summary>{value_formatted}</details>"
69-
html_unit_properties += "</ul></details>"
70-
71-
if self.get_parent():
72-
html_parent = f"<details style='{common_style}'> <summary><strong>Parent</strong></summary><ul>"
73-
html_parent += self.get_parent()._repr_html_(parent_name=self.name)
74-
html_parent += "</ul></details>"
75-
else:
76-
html_parent = ""
77-
78-
html_repr = html_header + html_unit_ids + html_annotations + html_unit_properties + html_parent
59+
html_repr = html_header + html_unit_ids + html_extra
7960
return html_repr
8061

8162
@property

0 commit comments

Comments
 (0)