-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
181 lines (153 loc) · 6.29 KB
/
grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import pandas as pd
import panel as pn
class Table:
def __init__(self, df, width=1000, height=500):
self.df = df
self.tabulator = pn.widgets.Tabulator(
df,
pagination="remote",
width=width,
height=height,
layout="fit_columns",
)
self.column_select = pn.widgets.Select(
name="Select Column", options=df.columns.to_list()
)
self.filter_widget = (
None # Placeholder for the widget responsible for filtering
)
self.stimulus_select = pn.widgets.Select(
name="Select Stimulus",
options=["All"] + df["stimulus_name"].unique().tolist(),
value="All",
)
self.recording_select = pn.widgets.Select(
name="Select Recording",
options=["All"] + df["recording"].unique().tolist(),
value="All",
)
self.filter_placeholder = pn.Column()
# Watch for column selection changes
self.column_select.param.watch(self.update_filter_widget, "value")
# Widget for queries
self.query_widget = pn.widgets.TextInput(
name="Query", placeholder="Enter query"
)
self.query_error = pn.pane.Markdown("", styles={"color": "red"})
# Panel to display the widgets
self.panel = pn.Column(
pn.Row(
pn.Column(
self.column_select, self.filter_placeholder
), # Updated layout
pn.Column(self.stimulus_select),
pn.Column(self.recording_select),
pn.Column(self.query_widget, self.query_error),
),
self.tabulator,
width=width,
height=height + 300,
)
# Initial setup
self.update_filter_widget()
self.stimulus_select.param.watch(
lambda event: self.apply_filters(),
"value",
)
self.recording_select.param.watch(
lambda event: self.apply_filters(),
"value",
)
self.query_widget.param.watch(self.apply_filters, "value")
def show(self):
return self.panel.servable()
def update_filter_widget(self, event=None):
# Remove any previous widget
self.filter_placeholder[:] = []
# Get the selected column
column = self.column_select.value
# Check the dtype of the column
if pd.api.types.is_bool_dtype(self.df[column]):
self.filter_widget = pn.widgets.CheckBoxGroup(
name=column, options=[True, False], value=[True, False]
)
self.filter_widget.param.watch(self.apply_filters, "value")
# Add the new widget
self.filter_placeholder.append(self.filter_widget)
elif pd.api.types.is_numeric_dtype(self.df[column]):
# Create two FloatInput widgets for lower and upper bounds
self.lower_bound_input = pn.widgets.FloatInput(
name=f"Min {column}", value=float(self.df[column].min())
)
self.upper_bound_input = pn.widgets.FloatInput(
name=f"Max {column}", value=float(self.df[column].max())
)
# Watch for changes in input values
self.lower_bound_input.param.watch(self.apply_filters, "value")
self.upper_bound_input.param.watch(self.apply_filters, "value")
# Add to filter placeholder
self.filter_placeholder.extend(
[self.lower_bound_input, self.upper_bound_input]
)
elif pd.api.types.is_string_dtype(self.df[column]):
print("string")
options = ["All"] + list(self.df[column].unique())
self.filter_widget = pn.widgets.MultiSelect(
name=column, options=options, value=options
)
self.filter_widget.param.watch(self.apply_filters, "value")
# Add the new widget
self.filter_placeholder.append(self.filter_widget)
def apply_filters(self, event=None):
filtered_df = self.df.copy()
# Boolean
if pd.api.types.is_bool_dtype(self.df[self.column_select.value]):
column = self.filter_widget.name
selected_values = self.filter_widget.value
filtered_df = filtered_df[filtered_df[column].isin(selected_values)]
# Numeric filter
elif pd.api.types.is_numeric_dtype(self.df[self.column_select.value]):
column = self.column_select.value
lower = self.lower_bound_input.value
upper = self.upper_bound_input.value
filtered_df = filtered_df.loc[
(filtered_df[column] >= lower) & (filtered_df[column] <= upper)
]
# Categorical filter
elif pd.api.types.is_string_dtype(self.df[self.column_select.value]):
column = self.filter_widget.name
selected_values = self.filter_widget.value
if "All" not in selected_values:
filtered_df = filtered_df[filtered_df[column].isin(selected_values)]
# Stimulus filter
if self.stimulus_select.value != "All":
filtered_df = filtered_df[
filtered_df["stimulus_name"] == self.stimulus_select.value
]
# Recording filter
if self.recording_select.value != "All":
filtered_df = filtered_df[
filtered_df["recording"] == self.recording_select.value
]
# Query filter
try:
query = self.query_widget.value
if query != "":
filtered_df = filtered_df.query(query)
self.query_error.object = ""
except KeyError:
self.query_error.object = "Invalid Key"
except pd.errors.UndefinedVariableError:
self.query_error.object = "Invalid Variable, use '' for strings"
self.tabulator.value = filtered_df
def get_filtered_df(self):
return self.tabulator.value
def get_selected_rows(self, event):
selected_indices = self.tabulator.selection
if selected_indices:
selected_rows_df = self.tabulator.value.iloc[selected_indices]
return selected_rows_df
else:
return None
def get_changed_df(self):
return self.tabulator.value