-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathapp.py
More file actions
1019 lines (889 loc) · 35.4 KB
/
app.py
File metadata and controls
1019 lines (889 loc) · 35.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module provides a web app for visualizing Mapper graphs.
"""
import logging
import os
from dataclasses import asdict, dataclass
from typing import Any, Callable, Literal, Optional
import networkx as nx
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from nicegui import app, run, ui
from numpy.typing import NDArray
from sklearn.cluster import DBSCAN, AgglomerativeClustering, KMeans
from sklearn.datasets import load_digits, load_iris
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from umap import UMAP
from tdamapper.core import TrivialClustering
from tdamapper.cover import BallCover, CubicalCover, KNNCover
from tdamapper.learn import MapperAlgorithm
from tdamapper.plot import MapperPlot
from tdamapper.protocols import Clustering, Cover
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
GIT_REPO_URL = "https://github.com/lucasimi/tda-mapper-python"
ICON_URL = f"{GIT_REPO_URL}/raw/main/docs/source/logos/tda-mapper-logo-icon.png"
LOGO_URL = f"{GIT_REPO_URL}/raw/main/docs/source/logos/tda-mapper-logo-horizontal.png"
ABOUT_TEXT = """
### About
**tda-mapper** is a Python library built around the Mapper algorithm, a core
technique in Topological Data Analysis (TDA) for extracting topological
structure from complex data. Designed for computational efficiency and
scalability, it leverages optimized spatial search methods to support
high-dimensional datasets. You can find further details in the
[documentation](https://tda-mapper.readthedocs.io/en/main/)
and in the
[paper](https://openreview.net/pdf?id=lTX4bYREAZ).
"""
LOAD_EXAMPLE = "Example"
LOAD_EXAMPLE_DIGITS = "Digits"
LOAD_EXAMPLE_IRIS = "Iris"
LOAD_CSV = "CSV"
LENS_IDENTITY = "Identity"
LENS_PCA = "PCA"
LENS_UMAP = "UMAP"
LENS_PCA_N_COMPONENTS = 2
LENS_UMAP_N_COMPONENTS = 2
COVER_SCALE_DATA = False
COVER_CUBICAL = "Cubical Cover"
COVER_BALL = "Ball Cover"
COVER_KNN = "KNN Cover"
CLUSTERING_TRIVIAL = "Skip"
CLUSTERING_KMEANS = "KMeans"
CLUSTERING_DBSCAN = "DBSCAN"
CLUSTERING_AGGLOMERATIVE = "Agglomerative Clustering"
COVER_CUBICAL_N_INTERVALS = 10
COVER_CUBICAL_OVERLAP_FRAC = 0.25
COVER_KNN_NEIGHBORS = 10
COVER_BALL_RADIUS = 100.0
CLUSTERING_SCALE_DATA = False
CLUSTERING_KMEANS_N_CLUSTERS = 2
CLUSTERING_DBSCAN_EPS = 0.5
CLUSTERING_DBSCAN_MIN_SAMPLES = 5
CLUSTERING_AGGLOMERATIVE_N_CLUSTERS = 2
PLOT_DIMENSIONS: Literal[2, 3] = 2
PLOT_ITERATIONS = 100
PLOT_COLORMAP = "Viridis"
PLOT_NODE_SIZE = 1.0
RANDOM_SEED = 42
@dataclass
class MapperConfig:
"""
Configuration for the Mapper algorithm.
:param lens_type: Type of lens to use for dimensionality reduction.
:param cover_scale_data: Whether to scale the data before covering.
:param cover_type: Type of cover to use for the Mapper algorithm.
:param clustering_scale_data: Whether to scale the data before clustering.
:param clustering_type: Type of clustering algorithm to use.
:param lens_pca_n_components: Number of components for PCA lens.
:param lens_umap_n_components: Number of components for UMAP lens.
:param cover_cubical_n_intervals: Number of intervals for cubical cover.
:param cover_cubical_overlap_frac: Overlap fraction for cubical cover.
:param cover_ball_radius: Radius for ball cover.
:param cover_knn_neighbors: Number of neighbors for KNN cover.
:param clustering_kmeans_n_clusters: Number of clusters for KMeans
clustering.
:param clustering_dbscan_eps: Epsilon parameter for DBSCAN clustering.
:param clustering_dbscan_min_samples: Minimum samples for DBSCAN
clustering.
:param clustering_agglomerative_n_clusters: Number of clusters for
Agglomerative clustering.
:param plot_dimensions: Number of dimensions for the plot (2D or 3D).
:param plot_iterations: Number of iterations for the plot.
:param plot_seed: Random seed for reproducibility.
"""
lens_type: str = LENS_PCA
cover_scale_data: bool = COVER_SCALE_DATA
cover_type: str = COVER_CUBICAL
clustering_scale_data: bool = CLUSTERING_SCALE_DATA
clustering_type: str = CLUSTERING_TRIVIAL
lens_pca_n_components: int = LENS_PCA_N_COMPONENTS
lens_umap_n_components: int = LENS_UMAP_N_COMPONENTS
cover_cubical_n_intervals: int = COVER_CUBICAL_N_INTERVALS
cover_cubical_overlap_frac: float = COVER_CUBICAL_OVERLAP_FRAC
cover_ball_radius: float = COVER_BALL_RADIUS
cover_knn_neighbors: int = COVER_KNN_NEIGHBORS
clustering_kmeans_n_clusters: int = CLUSTERING_KMEANS_N_CLUSTERS
clustering_dbscan_eps: float = CLUSTERING_DBSCAN_EPS
clustering_dbscan_min_samples: int = CLUSTERING_DBSCAN_MIN_SAMPLES
clustering_agglomerative_n_clusters: int = CLUSTERING_AGGLOMERATIVE_N_CLUSTERS
plot_dimensions: Literal[2, 3] = PLOT_DIMENSIONS
plot_iterations: int = PLOT_ITERATIONS
plot_seed: int = RANDOM_SEED
def fix_data(data: pd.DataFrame) -> pd.DataFrame:
"""
Fixes the input data by selecting numeric columns, dropping empty columns,
and filling NaN values with the mean of each column.
:param data: Input DataFrame to be fixed.
:return: Fixed DataFrame with numeric columns, no empty columns, and NaN
values filled with column means.
"""
df = pd.DataFrame(data)
df = df.select_dtypes(include="number")
df.dropna(axis=1, how="all", inplace=True)
df.fillna(df.mean(), inplace=True)
return df
def lens_identity(X: NDArray[np.float_]) -> NDArray[np.float_]:
"""
Identity lens function that returns the input data as is.
:param X: Input data as a NumPy array.
:return: The same input data as a NumPy array.
"""
return X
def lens_pca(n_components: int) -> Callable[[NDArray[np.float_]], NDArray[np.float_]]:
"""
Creates a lens function that reduces the dimensionality of the input data.
This function applies PCA to the input data and returns the transformed
data.
:param n_components: Number of components to keep after PCA.
:return: A function that applies PCA to the input data and returns the
transformed data.
"""
def _pca(X: NDArray[np.float_]) -> NDArray[np.float_]:
pca_model = PCA(n_components=n_components, random_state=RANDOM_SEED)
return pca_model.fit_transform(X)
return _pca
def lens_umap(n_components: int) -> Callable[[NDArray[np.float_]], NDArray[np.float_]]:
"""
Creates a lens function that reduces the dimensionality of the input data.
This function applies UMAP to the input data and returns the transformed
data.
:param n_components: Number of components to keep after UMAP.
:return: A function that applies UMAP to the input data and returns the
transformed data.
"""
def _umap(X: NDArray[np.float_]) -> NDArray[np.float_]:
um = UMAP(n_components=n_components, random_state=RANDOM_SEED)
return um.fit_transform(X)
return _umap
def run_mapper(
df: pd.DataFrame, **kwargs: Any
) -> Optional[tuple[nx.Graph, pd.DataFrame]]:
"""
Runs the Mapper algorithm on the provided DataFrame and returns the Mapper
graph and the transformed DataFrame.
:param df: Input DataFrame containing the data to be processed.
:param kwargs: Additional parameters for the Mapper configuration.
:return: A tuple containing the Mapper graph and the transformed DataFrame,
or None if the computation fails.
"""
logger.info("Mapper computation started...")
if df is None or df.empty:
error = "Mapper computation failed: no data found, please load data first."
logger.error(error)
return None
params: dict[str, Any] = kwargs
mapper_config = MapperConfig(**params)
lens_type = mapper_config.lens_type
cover_scale_data = mapper_config.cover_scale_data
cover_type = mapper_config.cover_type
clustering_scale_data = mapper_config.clustering_scale_data
clustering_type = mapper_config.clustering_type
lens_pca_n_components = mapper_config.lens_pca_n_components
lens_umap_n_components = mapper_config.lens_umap_n_components
cover_cubical_n_intervals = mapper_config.cover_cubical_n_intervals
cover_cubical_overlap_frac = mapper_config.cover_cubical_overlap_frac
cover_ball_radius = mapper_config.cover_ball_radius
cover_knn_neighbors = mapper_config.cover_knn_neighbors
clustering_kmeans_n_clusters = mapper_config.clustering_kmeans_n_clusters
clustering_dbscan_eps = mapper_config.clustering_dbscan_eps
clustering_dbscan_min_samples = mapper_config.clustering_dbscan_min_samples
clustering_agglomerative_n_clusters = (
mapper_config.clustering_agglomerative_n_clusters
)
lens = lens_pca(n_components=LENS_PCA_N_COMPONENTS)
if lens_type == LENS_IDENTITY:
lens = lens_identity
elif lens_type == LENS_PCA:
lens = lens_pca(n_components=lens_pca_n_components)
elif lens_type == LENS_UMAP:
lens = lens_umap(n_components=lens_umap_n_components)
cover: Cover[NDArray[np.float_]]
if cover_type == COVER_CUBICAL:
cover = CubicalCover(
n_intervals=cover_cubical_n_intervals,
overlap_frac=cover_cubical_overlap_frac,
)
elif cover_type == COVER_BALL:
cover = BallCover(radius=cover_ball_radius)
elif cover_type == COVER_KNN:
cover = KNNCover(neighbors=cover_knn_neighbors)
else:
logger.error(f"Unknown cover type: {cover_type}")
return None
clustering: Clustering[NDArray[np.float_]]
if clustering_type == CLUSTERING_TRIVIAL:
clustering = TrivialClustering()
elif clustering_type == CLUSTERING_KMEANS:
clustering = KMeans(
n_clusters=clustering_kmeans_n_clusters,
random_state=RANDOM_SEED,
)
elif clustering_type == CLUSTERING_DBSCAN:
clustering = DBSCAN(
eps=clustering_dbscan_eps,
min_samples=clustering_dbscan_min_samples,
)
elif clustering_type == CLUSTERING_AGGLOMERATIVE:
clustering = AgglomerativeClustering(
n_clusters=clustering_agglomerative_n_clusters,
)
else:
logger.error(f"Unknown clustering type: {clustering_type}")
return None
mapper = MapperAlgorithm(cover=cover, clustering=clustering)
X = df.to_numpy()
y = lens(X)
df_y = pd.DataFrame(y, columns=[f"{lens_type} {i}" for i in range(y.shape[1])])
if cover_scale_data:
y = StandardScaler().fit_transform(y)
if clustering_scale_data:
X = StandardScaler().fit_transform(X)
mapper_graph = mapper.fit_transform(X, y)
logger.info("Mapper computation completed.")
return mapper_graph, df_y
def create_mapper_figure(
df_X: pd.DataFrame,
df_y: pd.DataFrame,
df_target: pd.DataFrame,
mapper_graph: nx.Graph,
**kwargs: Any,
) -> go.Figure:
"""
Renders the Mapper graph as a Plotly figure.
:param df_X: DataFrame containing the input data.
:param df_y: DataFrame containing the lens-transformed data.
:param df_target: DataFrame containing the target labels.
:param mapper_graph: The Mapper graph to be visualized.
:param kwargs: Additional parameters for the Mapper configuration.
:return: A Plotly figure representing the Mapper graph.
"""
logger.info("Mapper rendering started...")
df_colors = pd.concat([df_target, df_y, df_X], axis=1)
params: dict[str, Any] = kwargs
mapper_config = MapperConfig(**params)
plot_dimensions = mapper_config.plot_dimensions
plot_iterations = mapper_config.plot_iterations
plot_seed = mapper_config.plot_seed
mapper_fig = MapperPlot(
mapper_graph,
dim=plot_dimensions,
iterations=plot_iterations,
seed=plot_seed,
).plot_plotly(
colors=df_colors.to_numpy(),
title=df_colors.columns.to_list(),
cmap=[
PLOT_COLORMAP,
"Cividis",
"Jet",
"Plasma",
"RdBu",
],
height=800,
node_size=[i * 0.125 * PLOT_NODE_SIZE for i in range(17)],
)
mapper_fig.update_layout(
width=None,
height=None,
autosize=True,
xaxis=dict(scaleanchor="y"),
uirevision="constant",
)
logger.info("Mapper rendering completed.")
return mapper_fig
class App:
"""
Main application class for the Mapper web application.
This class initializes the user interface, handles data loading, and runs
the Mapper algorithm.
:param storage: Dictionary to store application state and data.
:param draw_area: Optional draw area for rendering Mapper graphs.
:param plot_container: Container for the plot area.
:param left_drawer: Drawer for the left sidebar containing controls and
settings.
:param lens_type: Type of lens to use for dimensionality reduction.
:param cover_type: Type of cover to use for the Mapper algorithm.
:param clustering_type: Type of clustering algorithm to use.
:param lens_pca_n_components: Number of components for PCA lens.
:param lens_umap_n_components: Number of components for UMAP lens.
:param cover_cubical_n_intervals: Number of intervals for cubical cover.
:param cover_cubical_overlap_frac: Overlap fraction for cubical cover.
:param cover_ball_radius: Radius for ball cover.
:param cover_knn_neighbors: Number of neighbors for KNN cover.
:param clustering_kmeans_n_clusters: Number of clusters for KMeans
clustering.
:param clustering_dbscan_eps: Epsilon parameter for DBSCAN clustering.
:param clustering_dbscan_min_samples: Minimum samples for DBSCAN
clustering.
:param clustering_agglomerative_n_clusters: Number of clusters for
Agglomerative clustering.
:param plot_dimensions: Number of dimensions for the plot (2D or 3D).
:param plot_iterations: Number of iterations for the plot.
:param plot_seed: Random seed for reproducibility.
:param load_type: Type of data loading (example or CSV).
:param load_example: Example dataset to load if using example data.
"""
lens_type: Any
cover_type: Any
clustering_type: Any
lens_pca_n_components: Any
lens_umap_n_components: Any
cover_cubical_n_intervals: Any
cover_cubical_overlap_frac: Any
cover_ball_radius: Any
cover_knn_neighbors: Any
clustering_kmeans_n_clusters: Any
clustering_dbscan_eps: Any
clustering_dbscan_min_samples: Any
clustering_agglomerative_n_clusters: Any
plot_dimensions: Any
plot_iterations: Any
plot_seed: Any
load_type: Any
load_example: Any
storage: dict[str, Any]
draw_area: Optional[Any] = None
plot_container: Any
left_drawer: Any
def __init__(self, storage: dict[str, Any]) -> None:
self.storage = storage
ui.colors(
themelight="#ebedf8",
themedark="#132f48",
)
self.left_drawer = ui.left_drawer(elevated=True).classes(
"w-96 h-full overflow-y-auto gap-12"
)
with self.left_drawer:
with ui.link(target=GIT_REPO_URL, new_tab=True).classes("w-full"):
ui.image(LOGO_URL)
with ui.column().classes("w-full gap-2"):
self._init_about()
with ui.column().classes("w-full gap-2"):
self._init_file_upload()
ui.button(
"⬆️ Load Data",
on_click=self.load_data,
color="themelight",
).classes("w-full text-themedark")
with ui.column().classes("w-full gap-2"):
self._init_lens()
with ui.column().classes("w-full gap-2"):
self._init_cover()
with ui.column().classes("w-full gap-2"):
self._init_clustering()
ui.button(
"🚀 Run Mapper",
on_click=self.async_run_mapper,
color="themelight",
).classes("w-full text-themedark")
with ui.column().classes("w-full gap-2"):
self._init_draw()
ui.button(
"🌊 Redraw",
on_click=self.async_draw_mapper,
color="themelight",
).classes("w-full text-themedark")
with ui.column().classes("w-full gap-2"):
self._init_footnotes()
with ui.column().classes("w-full h-screen overflow-hidden"):
self._init_draw_area()
def _init_about(self) -> None:
with ui.dialog() as dialog, ui.card():
ui.markdown(ABOUT_TEXT)
ui.link(
text="If you like this project, please consider giving it a ⭐ on GitHub!",
target=GIT_REPO_URL,
new_tab=True,
).classes("w-full")
ui.button("Close", on_click=dialog.close, color="themelight").classes(
"w-full text-themedark"
)
ui.button("ℹ️ About", on_click=dialog.open, color="themelight").classes(
"w-full text-themedark"
)
def _init_file_upload(self) -> None:
ui.label("📊 Data").classes("text-h6")
self.load_type = ui.select(
options=[LOAD_EXAMPLE, LOAD_CSV],
label="Data Source",
value=LOAD_EXAMPLE,
).classes("w-full")
upload = ui.upload(
on_upload=self.upload_file,
auto_upload=True,
label="Upload CSV File",
).classes("w-full mt-4")
upload.props("accept=.csv")
upload.bind_visibility_from(
target_object=self.load_type,
target_name="value",
value=LOAD_CSV,
)
self.load_example = ui.select(
options=[LOAD_EXAMPLE_DIGITS, LOAD_EXAMPLE_IRIS],
label="Dataset",
value=LOAD_EXAMPLE_DIGITS,
).classes("w-full")
self.load_example.bind_visibility_from(
target_object=self.load_type,
target_name="value",
value=LOAD_EXAMPLE,
)
def _init_lens(self) -> None:
ui.label("🔎 Lens").classes("text-h6")
self._init_lens_settings()
def _init_lens_settings(self) -> None:
self.lens_type = ui.select(
options=[
LENS_IDENTITY,
LENS_PCA,
LENS_UMAP,
],
label="Lens",
value=LENS_PCA,
).classes("w-full")
self.lens_pca_n_components = ui.number(
label="PCA Components",
value=LENS_PCA_N_COMPONENTS,
).classes("w-full")
self.lens_pca_n_components.bind_visibility_from(
target_object=self.lens_type,
target_name="value",
value=LENS_PCA,
)
self.lens_umap_n_components = ui.number(
label="UMAP Components",
value=LENS_UMAP_N_COMPONENTS,
).classes("w-full")
self.lens_umap_n_components.bind_visibility_from(
target_object=self.lens_type,
target_name="value",
value=LENS_UMAP,
)
def _init_cover(self) -> None:
with ui.row().classes("w-full items-center justify-between"):
ui.label("🌐 Cover").classes("text-h6")
self.cover_scale = ui.switch(
text="Scaling",
value=COVER_SCALE_DATA,
)
self._init_cover_settings()
def _init_cover_settings(self) -> None:
self.cover_type = ui.select(
options=[
COVER_CUBICAL,
COVER_BALL,
COVER_KNN,
],
label="Cover",
value=COVER_CUBICAL,
).classes("w-full")
self.cover_cubical_n_intervals = ui.number(
label="Number of Intervals",
value=COVER_CUBICAL_N_INTERVALS,
).classes("w-full")
self.cover_cubical_n_intervals.bind_visibility_from(
target_object=self.cover_type,
target_name="value",
value=COVER_CUBICAL,
)
self.cover_cubical_overlap_frac = ui.number(
label="Overlap Fraction",
value=COVER_CUBICAL_OVERLAP_FRAC,
).classes("w-full")
self.cover_cubical_overlap_frac.bind_visibility_from(
target_object=self.cover_type,
target_name="value",
value=COVER_CUBICAL,
)
self.cover_ball_radius = ui.number(
label="Ball Radius",
value=COVER_BALL_RADIUS,
).classes("w-full")
self.cover_ball_radius.bind_visibility_from(
target_object=self.cover_type,
target_name="value",
value=COVER_BALL,
)
self.cover_knn_neighbors = ui.number(
label="Number of Neighbors",
value=COVER_KNN_NEIGHBORS,
).classes("w-full")
self.cover_knn_neighbors.bind_visibility_from(
target_object=self.cover_type,
target_name="value",
value=COVER_KNN,
)
def _init_clustering(self) -> None:
with ui.row().classes("w-full items-center justify-between"):
ui.label("🧮 Clustering").classes("text-h6")
self.clustering_scale = ui.switch(
text="Scaling",
value=CLUSTERING_SCALE_DATA,
)
self._init_clustering_settings()
def _init_clustering_settings(self) -> None:
self.clustering_type = ui.select(
options=[
CLUSTERING_TRIVIAL,
CLUSTERING_KMEANS,
CLUSTERING_DBSCAN,
CLUSTERING_AGGLOMERATIVE,
],
label="Clustering",
value=CLUSTERING_TRIVIAL,
).classes("w-full")
self.clustering_kmeans_n_clusters = ui.number(
label="Number of Clusters",
value=CLUSTERING_KMEANS_N_CLUSTERS,
).classes("w-full")
self.clustering_kmeans_n_clusters.bind_visibility_from(
target_object=self.clustering_type,
target_name="value",
value=CLUSTERING_KMEANS,
)
self.clustering_dbscan_eps = ui.number(
label="Epsilon",
value=CLUSTERING_DBSCAN_EPS,
).classes("w-full")
self.clustering_dbscan_eps.bind_visibility_from(
target_object=self.clustering_type,
target_name="value",
value=CLUSTERING_DBSCAN,
)
self.clustering_dbscan_min_samples = ui.number(
label="Min Samples",
value=CLUSTERING_DBSCAN_MIN_SAMPLES,
).classes("w-full")
self.clustering_dbscan_min_samples.bind_visibility_from(
target_object=self.clustering_type,
target_name="value",
value=CLUSTERING_DBSCAN,
)
self.clustering_agglomerative_n_clusters = ui.number(
label="Number of Clusters",
value=CLUSTERING_AGGLOMERATIVE_N_CLUSTERS,
).classes("w-full")
self.clustering_agglomerative_n_clusters.bind_visibility_from(
target_object=self.clustering_type,
target_name="value",
value=CLUSTERING_AGGLOMERATIVE,
)
def _init_draw(self) -> None:
ui.label("🎨 Draw").classes("text-h6")
self._init_draw_settings()
def _init_draw_settings(self) -> None:
self.plot_dimensions = ui.select(
options=[2, 3],
label="Dimensions",
value=PLOT_DIMENSIONS,
).classes("w-full")
self.plot_iterations = ui.number(
label="Iterations",
value=PLOT_ITERATIONS,
min=1,
max=10 * PLOT_ITERATIONS,
).classes("w-full")
self.plot_seed = ui.number(
label="Seed",
value=RANDOM_SEED,
).classes("w-full")
def _init_footnotes(self) -> None:
ui.label(text="Made in Rome, with ❤️ and ☕️.").classes(
"text-caption text-gray-500"
).classes("text-caption text-gray-500")
def _init_draw_area(self) -> None:
self.plot_container = ui.element("div").classes("w-full h-full")
self.draw_area = None
def _toggle_drawer() -> None:
self.left_drawer.toggle()
with ui.page_sticky(x_offset=18, y_offset=18, position="top-left"):
toggle_button = ui.button(
icon="menu",
on_click=_toggle_drawer,
).props("fab color=themedark")
toggle_button.bind_visibility_from(
target_object=self.left_drawer,
target_name="value",
value=False,
)
def get_mapper_config(self) -> MapperConfig:
"""
Retrieves the current configuration settings for the Mapper algorithm.
:return: A MapperConfig object containing the current settings.
"""
plot_dim = int(self.plot_dimensions.value)
plot_dimensions: Literal[2, 3]
if plot_dim == 2:
plot_dimensions = 2
elif plot_dim == 3:
plot_dimensions = 3
else:
plot_dimensions = PLOT_DIMENSIONS
return MapperConfig(
lens_type=str(self.lens_type.value) if self.lens_type.value else LENS_PCA,
cover_type=(
str(self.cover_type.value) if self.cover_type.value else COVER_CUBICAL
),
clustering_type=(
str(self.clustering_type.value)
if self.clustering_type.value
else CLUSTERING_TRIVIAL
),
lens_pca_n_components=(
int(self.lens_pca_n_components.value)
if self.lens_pca_n_components.value
else LENS_PCA_N_COMPONENTS
),
lens_umap_n_components=(
int(self.lens_umap_n_components.value)
if self.lens_umap_n_components.value
else LENS_UMAP_N_COMPONENTS
),
cover_cubical_n_intervals=(
int(self.cover_cubical_n_intervals.value)
if self.cover_cubical_n_intervals.value
else COVER_CUBICAL_N_INTERVALS
),
cover_cubical_overlap_frac=(
float(self.cover_cubical_overlap_frac.value)
if self.cover_cubical_overlap_frac.value
else COVER_CUBICAL_OVERLAP_FRAC
),
cover_ball_radius=(
float(self.cover_ball_radius.value)
if self.cover_ball_radius.value
else COVER_BALL_RADIUS
),
cover_knn_neighbors=(
int(self.cover_knn_neighbors.value)
if self.cover_knn_neighbors.value
else COVER_KNN_NEIGHBORS
),
clustering_kmeans_n_clusters=(
int(self.clustering_kmeans_n_clusters.value)
if self.clustering_kmeans_n_clusters.value
else CLUSTERING_KMEANS_N_CLUSTERS
),
clustering_dbscan_eps=(
float(self.clustering_dbscan_eps.value)
if self.clustering_dbscan_eps.value
else CLUSTERING_DBSCAN_EPS
),
clustering_dbscan_min_samples=(
int(self.clustering_dbscan_min_samples.value)
if self.clustering_dbscan_min_samples.value
else CLUSTERING_DBSCAN_MIN_SAMPLES
),
clustering_agglomerative_n_clusters=(
int(self.clustering_agglomerative_n_clusters.value)
if self.clustering_agglomerative_n_clusters.value
else CLUSTERING_AGGLOMERATIVE_N_CLUSTERS
),
plot_dimensions=plot_dimensions,
plot_iterations=(
int(self.plot_iterations.value)
if self.plot_iterations.value
else PLOT_ITERATIONS
),
plot_seed=(
int(self.plot_seed.value) if self.plot_seed.value else RANDOM_SEED
),
)
def upload_file(self, file: Any) -> None:
"""
Handles the file upload event, reads the CSV file,
and stores the data in the application storage.
:param file: The uploaded file object.
:return: None
"""
if file is not None:
df = pd.read_csv(file.content)
self.storage["df"] = fix_data(df)
self.storage["labels"] = pd.DataFrame()
message = "File upload completed."
logger.info(message)
ui.notify(message, type="info")
else:
error = "File upload failed: no file provided."
logger.info(error)
ui.notify(error, type="warning")
def load_data(self) -> None:
"""
Loads example datasets or CSV files based on the selected load type.
If the load type is set to "Example", it loads either the Digits or
Iris dataset. If the load type is set to "CSV", it checks if a
DataFrame is already stored in the application storage and uses it.
:return: None
"""
if self.load_type.value == LOAD_EXAMPLE:
if self.load_example.value == LOAD_EXAMPLE_DIGITS:
df, labels = load_digits(as_frame=True, return_X_y=True)
elif self.load_example.value == LOAD_EXAMPLE_IRIS:
df, labels = load_iris(as_frame=True, return_X_y=True)
else:
error = "Load data failed: unknown example dataset selected."
logger.error(error)
ui.notify(error, type="warning")
return
self.storage["df"] = fix_data(df)
self.storage["labels"] = fix_data(labels)
elif self.load_type.value == LOAD_CSV:
df = self.storage.get("df", pd.DataFrame())
if df is None or df.empty:
error = "Load data failed: no data found, please upload a file first."
logger.warning(error)
ui.notify(error, type="warning")
return
else:
error = "Load data failed: unknown load type selected."
logger.error(error)
ui.notify(error, type="warning")
return
df = self.storage.get("df", pd.DataFrame())
if df is not None and not df.empty:
logger.info("Load data completed.")
ui.notify("Load data completed.", type="positive")
else:
error = "Load data failed: no data found, please upload a file first."
logger.warning(error)
ui.notify(error, type="warning")
def notification_running_start(self, message: str) -> Any:
"""
Starts a notification to indicate that a long-running operation is in
progress.
:param message: The message to display in the notification.
:return: A notification object that can be used to update the message
and status.
"""
notification = ui.notification(timeout=None, type="ongoing")
notification.message = message
notification.spinner = True
return notification
def notification_running_stop(
self, notification: Any, message: str, type: Optional[str] = None
) -> None:
"""
Stops the notification and updates it with the final message and type.
:param notification: The notification object to update.
:param message: The final message to display in the notification.
:param type: The type of notification.
:return: None
"""
if type is not None:
notification.type = type
notification.message = message
notification.timeout = 5.0
notification.spinner = False
async def async_run_mapper(self) -> None:
"""
Runs the Mapper algorithm on the loaded data and updates the storage
with the Mapper graph and transformed DataFrame.
This method retrieves the input DataFrame from storage, applies the
Mapper algorithm, and stores the resulting Mapper graph and transformed
DataFrame back into storage.
:return: None
"""
notification = self.notification_running_start("Running Mapper...")
df_X = self.storage.get("df", pd.DataFrame())
if df_X is None or df_X.empty:
error = "Run Mapper failed: no data found, please load data first."
logger.warning(error)
self.notification_running_stop(notification, error, type="warning")
return
mapper_config = self.get_mapper_config()
result = await run.cpu_bound(run_mapper, df_X, **asdict(mapper_config))
if result is None:
error = "Run Mapper failed: something went wrong."
logger.error(error)
self.notification_running_stop(notification, error, type="error")
return
mapper_graph, df_y = result
if mapper_graph is not None:
self.storage["mapper_graph"] = mapper_graph
if df_y is not None:
self.storage["df_y"] = df_y
self.notification_running_stop(
notification, "Run Mapper completed.", type="positive"
)
await self.async_draw_mapper()
async def async_draw_mapper(self) -> None:
"""
Draws the Mapper graph using the stored graph and input data.
This method retrieves the Mapper graph and input DataFrame from
storage, creates a Plotly figure representing the Mapper graph, and
updates the draw area in the user interface with the new figure.
:return: None
"""
notification = self.notification_running_start("Drawing Mapper...")
mapper_config = self.get_mapper_config()
df_X = self.storage.get("df", pd.DataFrame())
df_y = self.storage.get("df_y", pd.DataFrame())
df_target = self.storage.get("labels", pd.DataFrame())
mapper_graph = self.storage.get("mapper_graph", None)
if df_X.empty or mapper_graph is None:
error = (
"Draw Mapper failed: no Mapper graph found, please run Mapper first."
)
logger.warning(error)
self.notification_running_stop(
notification=notification, message=error, type="warning"
)
return
mapper_fig = await run.cpu_bound(
create_mapper_figure,
df_X,
df_y,
df_target,
mapper_graph,
**asdict(mapper_config),
)
if mapper_fig is None:
error = "Draw Mapper failed: something went wrong."
self.notification_running_stop(notification, error, type="error")
return
logger.info("Displaying Mapper plot.")
if self.draw_area is not None:
self.draw_area.clear()
self.plot_container.clear()
with self.plot_container:
self.draw_area = ui.plotly(mapper_fig).classes("w-full h-full")
self.notification_running_stop(
notification, "Draw Mapper completed.", type="positive"
)
def startup() -> None:
"""
Initializes the NiceGUI app and sets up the main page.
:return: None
"""
@ui.page("/")
def main_page() -> None:
"""
Main page of the application.
:return: None
"""
ui.query(".nicegui-content").classes("p-0")
storage = app.storage.client
App(storage=storage)