/
pandas_seaborn.py
192 lines (164 loc) · 6.1 KB
/
pandas_seaborn.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
182
183
184
185
186
187
188
189
190
191
192
from magicclass import magicclass
from magicclass.widgets import Table, Figure
import os
import pandas as pd
import seaborn as sns
from pathlib import Path
class CachedTable(Table):
# Because we have to convert table data into DataFrame many times,
# DataFrame should be cached.
def __init__(self, value, **kwargs):
if isinstance(value, pd.DataFrame):
self._dataframe = value
else:
self._dataframe = None
super().__init__(value, **kwargs)
def to_dataframe(self) -> pd.DataFrame:
if self._dataframe is not None:
return self._dataframe
else:
return super().to_dataframe()
@magicclass(widget_type="tabbed", labels=False)
class TableList:
"""List of tables"""
@magicclass(layout="horizontal", widget_type="split")
class Analyzer:
@magicclass(widget_type="toolbox")
class Tools:
@magicclass(widget_type="scrollable")
class File_Menu:
def Open_file(self, path: Path): ...
def Save_file(self, path: Path): ...
def Save_figure(self, path: Path , transparent: bool): ...
def Delete_tab(self): ...
@magicclass(widget_type="scrollable")
class Plot_Menu:
def Plot(self): ...
def Histogram(self): ...
def Box_Plot(self): ...
def Swarm_Plot(self): ...
def Violin_Plot(self): ...
def Boxen_Plot(self): ...
@magicclass(widget_type="scrollable")
class Plot_Control:
def set_title(self, title: str): ...
def set_xlabel(self, label: str): ...
def set_ylabel(self, label: str): ...
def set_xlim(self, xmin: str, xmax: str): ...
def set_ylim(self, ymin: str, ymax: str): ...
table_list = TableList()
canvas = Figure()
@Tools.File_Menu.wraps
def Open_file(self, path: Path, header: str = ""):
"""
Load data into table.
Parameters
----------
path : Path
csv, txt, dat file
header : str, default is ""
Where header starts.
"""
header = None if header == "" else int(header)
df = pd.read_csv(path, header=header)
table = CachedTable(df, name=os.path.basename(path))
self.table_list.append(table)
self.table_list.current_index = len(self.table_list) - 1
@Tools.File_Menu.wraps
def Save_file(self, path: Path):
"""Save current table data as a csv file."""
df = self._current_df()
df.to_csv(path)
@Tools.File_Menu.wraps
def Save_figure(self, path: Path, transparent=True):
"""
Save current figure as an image file.
Parameters
----------
path : Path
File path.
transparent : bool, default is True
Check if you want to save as a transparent image.
"""
self.canvas.fig.savefig(path, transparent=transparent)
@Tools.File_Menu.wraps
def Delete_tab(self):
"""Delete current tab"""
try:
i = self.table_list.current_index
del self.table_list[i]
except IndexError:
pass
@Tools.Plot_Menu.wraps
def Plot(self):
"""Show plot"""
self.canvas.figure.clf()
df = self._current_df()
df.plot(ax=self.canvas.ax)
self.canvas.draw()
@Tools.Plot_Menu.wraps
def Histogram(self):
"""Show histogram"""
self.canvas.figure.clf()
df = self._current_df()
df.hist(ax=self.canvas.ax)
self.canvas.draw()
def _seaborn_plot(self, plot_function, x: str, y: str, hue: str, dodge: bool = False):
# Seaborn plot functions have the same API.
# Also, this function can be used as a template of signature with "wraps" method.
x = x or None
y = y or None
hue = hue or None
self.canvas.figure.clf()
df = self._current_df()
plot_function(data=df, ax=self.canvas.ax, x=x, y=y, hue=hue, dodge=dodge)
self.canvas.draw()
@Tools.Plot_Menu.wraps(template=_seaborn_plot)
def Box_Plot(self, x, y, hue, dodge):
"""Show box plot"""
self._seaborn_plot(sns.boxplot, x, y, hue, dodge)
@Tools.Plot_Menu.wraps(template=_seaborn_plot)
def Swarm_Plot(self, x, y, hue, dodge):
"""Show (bee)swarm plot"""
self._seaborn_plot(sns.swarmplot, x, y, hue, dodge)
@Tools.Plot_Menu.wraps(template=_seaborn_plot)
def Violin_Plot(self, x, y, hue, dodge):
"""Show violin plot"""
self._seaborn_plot(sns.violinplot, x, y, hue, dodge)
@Tools.Plot_Menu.wraps(template=_seaborn_plot)
def Boxen_Plot(self, x, y, hue, dodge):
"""Show boxen plot"""
self._seaborn_plot(sns.boxenplot, x, y, hue, dodge)
@Tools.Plot_Control.wraps
def set_title(self, title: str):
"""Set title of figure"""
self.canvas.ax.set_title(title)
self.canvas.draw()
@Tools.Plot_Control.wraps
def set_xlabel(self, label: str):
"""Set x-label of figure"""
self.canvas.ax.set_xlabel(label)
self.canvas.draw()
@Tools.Plot_Control.wraps
def set_ylabel(self, label: str):
"""Set y-label of figure"""
self.canvas.ax.set_ylabel(label)
self.canvas.draw()
@Tools.Plot_Control.wraps
def set_xlim(self, xmin: str, xmax: str):
"""Set x-limits of figure"""
self.canvas.ax.set_xlim(float(xmin), float(xmax))
self.canvas.draw()
@Tools.Plot_Control.wraps
def set_ylim(self, ymin: str, ymax: str):
"""Set y-limits of figure"""
self.canvas.ax.set_ylim(float(ymin), float(ymax))
self.canvas.draw()
def _current_df(self) -> pd.DataFrame:
i = self.table_list.current_index
table: Table = self.table_list[i]
df = table.to_dataframe()
return df
if __name__ == "__main__":
ui = Analyzer()
ui.show()