-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocessing.py
More file actions
354 lines (283 loc) · 10.4 KB
/
preprocessing.py
File metadata and controls
354 lines (283 loc) · 10.4 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from __future__ import annotations
from pathlib import Path
from textual import on
from textual.app import App, ComposeResult
from textual.reactive import reactive
from textual.screen import ModalScreen, Screen
from textual.containers import Center, Horizontal, Container, Vertical
from textual.widgets import Label, Button, Footer, Static, Header, DataTable, RadioSet, RadioButton, Rule, Checkbox
from textual_fspicker import FileOpen, FileSave, Filters
import numpy as np
import pandas as pd
class ExitScreen(ModalScreen):
"""A modal exit screen."""
DEFAULT_CSS = """
ExitScreen {
align: center middle;
}
ExitScreen > Container {
width: auto;
height: auto;
border: thick $background 80%;
background: $surface;
}
ExitScreen > Container > Label {
width: 100%;
content-align-horizontal: center;
margin-top: 1;
}
ExitScreen > Container > Horizontal {
width: auto;
height: auto;
}
ExitScreen > Container > Horizontal > Button {
margin: 2 4;
}
"""
def compose(self) -> ComposeResult:
with Container():
yield Label("Are you sure you want to quit?")
with Horizontal():
yield Button("No", id="no", variant="error")
yield Button("Yes", id="yes", variant="success")
@on(Button.Pressed, "#yes")
def exit_app(self) -> None:
self.app.exit()
@on(Button.Pressed, "#no")
def back_to_app(self) -> None:
self.app.pop_screen()
class ErrorScreen(ModalScreen):
"""A modal error screen."""
DEFAULT_CSS = """
ErrorScreen {
align: center middle;
}
ErrorScreen > Container {
width: auto;
height: auto;
border: thick $background 80%;
background: $surface;
}
ErrorScreen > Container > Label {
width: 100%;
content-align-horizontal: center;
margin-top: 1;
}
ErrorScreen > Container > Horizontal {
width: auto;
height: auto;
}
ErrorScreen > Container > Horizontal > Button {
margin: 2 4;
}
"""
def compose(self) -> ComposeResult:
with Container():
yield Label(self.app.msg)
with Horizontal():
yield Button("OK", id="ok", variant="error")
@on(Button.Pressed, "#ok")
def back_to_app(self) -> None:
self.app.pop_screen()
class StartupScreen(Screen):
CSS = """
StartupScreen {
align: center middle;
}
StartupScreen Horizontal {
align: center middle;
height: auto;
margin-bottom: 1;
}
StartupScreen Horizontal Button {
margin-left: 1;
margin-right: 1;
}
"""
def compose(self) -> ComposeResult:
yield Header()
with Center():
yield Label("Select the file containing your data.")
with Horizontal():
yield Button("Open data file", id="open", variant="primary")
yield Footer()
@on(Button.Pressed, "#open")
def open_file(self) -> None:
"""Show the `FileOpen` dialog when the button is pushed."""
self.app.push_screen(
FileOpen(
".",
filters=Filters(
("CSV", lambda p: p.suffix.lower() == ".csv"),
("XLS", lambda p: p.suffix.lower() == ".xls"),
("XLSX", lambda p: p.suffix.lower() == ".xlsx"),
),
),
callback=self.process_selection,
)
def process_selection(self, to_show: Path | None) -> None:
"""Show the file that was selected by the user.
Args:
to_show: The file to show.
"""
if to_show is None:
self.query_one(Label).update("Cancelled")
else:
self.app.open_path = str(to_show)
ftype = str(to_show).split(".")[-1]
if ftype.lower() == "csv":
self.app.loaded_df = pd.read_csv(self.app.open_path)
else:
self.app.loaded_df = pd.read_excel(self.app.open_path)
self.app.data_rows = self.app.loaded_df.values.tolist()
self.app.data_cols = self.app.loaded_df.columns.tolist()
# self.app.uid_col = data.columns.tolist()[0]
# self.app.selected_data_cols = data.columns.tolist()[1:]
self.app.push_screen(ColSelectorScreen())
class ColSelectorScreen(Screen):
CSS = """
ColSelectorScreen {
align: center middle;
}
DataTable {
height: 25;
min-width: 150;
max-width: 250;
}
RadioSet {
min-width: 15;
max-width: 25;
}
Button{
width: 16;
}
#save {
dock: right;
}
Screen{
}
#instruction{
align: center top;
text-align: center;
height: 8
}
"""
instr_text = """
Please select the column containing the unique ID of the individuals using the buttons on the left. If your data does not contain a unique ID, the program will automatically create a new one.
Afterwards, you may select the attributes to be used for linking by clicking on the name of the column. Note that the order in which you select the columns will be the order in which they are used for linking.
Clicking on a column name that is already part of the selection will remove it.
The ID column cannot be part of the linking data.
"""
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.cursor_type = "cell"
table.zebra_stripes = True
table.add_columns(*self.app.data_cols)
table.add_rows(self.app.data_rows)
def compose(self) -> ComposeResult:
yield Header()
with Center():
yield Static(self.instr_text, id="instruction")
yield Rule(orientation="horizontal", line_style="double")
with Horizontal():
with RadioSet(name="id_selector", id="id_selector"):
yield RadioButton(label="--NO ID--", name="newID", id="newID")
for colname in self.app.data_cols:
yield RadioButton(label=colname, name=colname, id=colname)
yield Rule(orientation="vertical", line_style="double")
yield DataTable()
# yield Rule(orientation="vertical", line_style="double")
# with Vertical():
# yield Checkbox("Create Header", id="make_header")
# yield Checkbox("Create ID", id="make_id")
yield Rule(orientation="horizontal", line_style="double")
with Horizontal():
yield Label("Please select the ID and at least one data column.", id="selectedLabel")
yield Button("Save", id="save", variant="success")
yield Footer()
def on_radio_set_changed(self, event: RadioSet.Changed) -> None:
if str(event.pressed.label) not in self.app.selected_data_cols:
self.app.uid_col = event.pressed.label
else:
self.app.msg = "The ID column cannot be part of the matching data."
self.app.push_screen(ErrorScreen())
event.radio_set.action_toggle_button()
self.app.uid_col = ""
data_str = ", ".join(self.app.selected_data_cols)
info_str = "ID column: %s \nData columns: %s" % (self.app.uid_col, data_str)
self.query_one("#selectedLabel", Label).update(info_str)
def on_data_table_header_selected(self, event) -> None:
selected_col = str(event.label)
if selected_col != str(self.app.uid_col):
if selected_col not in self.app.selected_data_cols:
self.app.selected_data_cols.append(selected_col)
else:
self.app.selected_data_cols = [c for c in self.app.selected_data_cols if c != selected_col]
data_str = ", ".join(self.app.selected_data_cols)
info_str = "ID column: %s \nData columns: %s" % (self.app.uid_col, data_str)
self.query_one("#selectedLabel", Label).update(info_str)
else:
self.app.msg = "The ID column cannot be part of the matching data."
self.app.push_screen(ErrorScreen())
@on(Button.Pressed, "#save")
def save_file(self) -> None:
"""Show the `FileSave` dialog when the button is pushed."""
self.app.push_screen(FileSave(can_overwrite=False,
filters=Filters(
("TSV", lambda p: p.suffix.lower() == ".tsv"),
),
), callback=self.process_save)
def process_save(self, save_path: Path | None) -> None:
"""Show the file that was selected by the user.
Args:
to_show: The file to show.
"""
if save_path is not None:
save_path = str(save_path)
if save_path[-4:] != ".tsv":
save_path += ".tsv"
data = self.app.loaded_df.copy()
data.replace(np.nan, "", inplace=True)
if str(self.app.uid_col) != "--NO ID--":
tmp_id_col = data[str(self.app.uid_col)]
else:
tmp_id_col = list(range(data.shape[0]))
data = data[list(self.app.selected_data_cols)]
data["uid"] = tmp_id_col
data.to_csv(save_path, index=False, sep="\t")
class PreproApp(App[None]):
"""A simple test application."""
TITLE = "Data Preprocessing for GMA against PPRL"
CSS = """
Screen#_default {
align: center middle;
}
Screen#_default Horizontal {
align: center middle;
height: auto;
margin-bottom: 1;
}
Screen#_default Horizontal Button {
margin-left: 1;
margin-right: 1;
}
"""
BINDINGS = [("d", "toggle_dark", "Light/Dark"), ("q", "quit_app", "Quit")]
open_path = reactive("")
data_rows = reactive(list)
data_cols = reactive(list)
msg = ""
loaded_df = None
selected_data_cols = reactive(list)
uid_col = reactive("")
def compose(self) -> ComposeResult:
"""Compose the layout of the test application."""
yield Header()
yield Footer()
def on_mount(self) -> None:
self.push_screen(StartupScreen())
def action_quit_app(self) -> None:
self.push_screen(ExitScreen())
##############################################################################
if __name__ == "__main__":
PreproApp().run()