hkayabilisim commited on
Commit
958a7a4
·
1 Parent(s): 46554a2

Added first version

Browse files
Files changed (4) hide show
  1. Dockerfile +11 -0
  2. app.py +165 -0
  3. nea_051022.csv +0 -0
  4. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["solara", "run", "app.py", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #%%
2
+ import solara
3
+ import pandas as pd
4
+ import numpy as np
5
+ from sklearn.preprocessing import MinMaxScaler, StandardScaler
6
+ from matplotlib.figure import Figure
7
+ from sklearn.decomposition import PCA
8
+ import plotly.express as px
9
+ from sklearn.manifold import TSNE, Isomap, LocallyLinearEmbedding, SpectralEmbedding
10
+
11
+
12
+ # Global non-reactive variablea
13
+ df_original = pd.read_csv('nea_051022.csv')
14
+ all_features = list(df_original.columns)
15
+ default_features = ['a','e','i','om','w','q','ad','H']
16
+ scalers = ['none','minmax','standard']
17
+ feature_extractors = ['none','pca']
18
+ training_ratio_default = 5
19
+
20
+ # Global reactive variables
21
+ drop_nans = solara.reactive(True)
22
+ features = solara.reactive(default_features)
23
+ feature_maps = {f: solara.reactive('x') for f in all_features}
24
+ scaler = solara.reactive('none')
25
+ training_ratio = solara.reactive(training_ratio_default)
26
+ feature_extractor = solara.reactive('none')
27
+ info_message = solara.reactive("")
28
+ df = solara.reactive(df_original[features.value].copy)
29
+
30
+ @solara.component
31
+ def process_data():
32
+ df_out = df_original[features.value].copy()
33
+ df_out = df_out.dropna() if drop_nans.value else df_out
34
+ df_out = df_out.sample(frac=training_ratio.value/100.0, random_state=42)
35
+ print('processing trn ratio',training_ratio.value)
36
+ info_string = ""
37
+ for f in features.value:
38
+ x = df_out[f].to_numpy()
39
+ y = eval(feature_maps[f].value)
40
+ df_out.loc[:,f] = y
41
+
42
+ if scaler.value != 'none':
43
+ if scaler.value == 'minmax':
44
+ transformer = MinMaxScaler()
45
+ elif scaler.value == 'standard':
46
+ transformer = StandardScaler()
47
+ else:
48
+ transformer = StandardScaler()
49
+ try:
50
+ transformer = transformer.fit(df_out)
51
+ df_out = pd.DataFrame(transformer.transform(df_out), columns=df_out.columns, index=df_out.index)
52
+ except Exception as e:
53
+ df_out = None
54
+ info_string = str(e)
55
+ df.set(df_out)
56
+ solara.DataFrame(df_out, items_per_page=5)
57
+ #info_message.set(info_string)
58
+
59
+
60
+
61
+ def reset():
62
+ features.value = default_features
63
+ drop_nans.set(True)
64
+ training_ratio.set(training_ratio_default)
65
+ for f in all_features:
66
+ feature_maps[f].set('x')
67
+ feature_extractor.set('none')
68
+ scaler.set('none')
69
+
70
+
71
+ @solara.component
72
+ def InfoBox():
73
+ if len(info_message.value) > 0:
74
+ solara.Error(info_message.value)
75
+
76
+ @solara.component
77
+ def BoxPlot():
78
+ if df.value is None:
79
+ return
80
+ fig = Figure()
81
+ ax = fig.subplots()
82
+ df.value.plot.box(figsize=(20,5), ax=ax)
83
+
84
+ ymin = min(df.value.min(numeric_only=True))
85
+ ymax = max(df.value.max(numeric_only=True))
86
+
87
+ yrange = solara.use_reactive((ymin, ymax))
88
+ ax.set_ylim(yrange.value[0], yrange.value[1])
89
+
90
+ solara.SliderRangeFloat("yrange min and max", value=yrange,min=ymin, max=ymax)
91
+ solara.FigureMatplotlib(fig)
92
+ solara.Markdown(f'{yrange}')
93
+
94
+
95
+ @solara.component
96
+ def EmbeddingPlot():
97
+ embedding_method, set_embedding_method = solara.use_state('PCA')
98
+ n_components, set_n_component = solara.use_state(2)
99
+
100
+ with solara.Row():
101
+ solara.ToggleButtonsSingle(value=embedding_method, values=['PCA','TSNE','ISOMAP','LLE','SE'], on_value=set_embedding_method)
102
+ solara.Select(label="Embedding Dimension", value=n_components, values=[1,2,3], on_value=set_n_component)
103
+ #solara.SliderInt(label='n_components', value=n_components, min=1, max=3, on_value=set_n_component)
104
+
105
+ if embedding_method == 'PCA':
106
+ embedder = PCA(n_components=n_components)
107
+ elif embedding_method == 'TSNE':
108
+ embedder = TSNE(n_components=n_components)
109
+ elif embedding_method == 'ISOMAP':
110
+ embedder = Isomap(n_components=n_components)
111
+ elif embedding_method == 'LLE':
112
+ embedder = LocallyLinearEmbedding(n_components=n_components)
113
+ elif embedding_method == 'SE':
114
+ embedder = SpectralEmbedding(n_components=n_components)
115
+ else:
116
+ embedder = PCA(n_components=n_components)
117
+
118
+ data_embedded = embedder.fit_transform(df.value)
119
+ df_embedded = pd.DataFrame(data_embedded)
120
+ df_embedded['pha'] = list(df_original.loc[df.value.index,'pha'])
121
+ df_embedded['moid'] = list(df_original.loc[df.value.index,'moid'])
122
+ print(df_embedded.head())
123
+ if n_components == 1:
124
+ fig = px.scatter(df_embedded, x=0, y='moid', color='pha', width=1024, height=768)
125
+ elif n_components == 2:
126
+ fig = px.scatter(df_embedded, x=0,y=1,color='moid',symbol='pha', width=1024, height=768)
127
+ elif n_components == 3:
128
+ fig = px.scatter_3d(df_embedded, x=0,y=1,z=2,color='moid',symbol='pha', width=1024, height=768)
129
+
130
+ solara.FigurePlotly(fig)
131
+
132
+ @solara.component
133
+ def Page():
134
+ with solara.Card(title="Original Raw Data"):
135
+ solara.DataFrame(df_original, items_per_page=5)
136
+ with solara.Card(title="Transformed Data"):
137
+ process_data() # features, feature_maps, drop_nans, scaler, training_ratio)
138
+
139
+ solara.Button(label='Reset', on_click=reset)
140
+ solara.ToggleButtonsMultiple(features, all_features)
141
+
142
+ with solara.GridFixed(columns=10):
143
+ for f in features.value:
144
+ solara.InputText(label=f, value=feature_maps[f], on_value=lambda text : feature_maps[f].set(text))
145
+
146
+ with solara.Row():
147
+ solara.Checkbox(label='Drop Nans', value=drop_nans, on_value=lambda value : drop_nans.set(value))
148
+ solara.ToggleButtonsSingle(value=scaler, values=scalers)
149
+ solara.SliderInt(label='Training Ratio %', value=training_ratio, min=1, max=99)
150
+
151
+ #solara.Markdown(f"""
152
+ # # Drop_nans: {drop_nans.value}
153
+ # # trn_ratio {training_ratio.value}
154
+ # # features: {features.value}
155
+ # # scaler: {scaler.value}
156
+ # # feature_maps: {feature_maps}
157
+ # # info_message: {info_message.value}
158
+ # """)
159
+ InfoBox()
160
+ EmbeddingPlot()
161
+ BoxPlot()
162
+
163
+
164
+ Page()
165
+
nea_051022.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ solara
2
+ pandas