Spaces:
Sleeping
Sleeping
Commit
·
98aab12
1
Parent(s):
9bb557d
Added the interface for Medoid
Browse files
streamlit_app/pages/page_Medoid.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# The Selector library provides a set of tools for selecting a
|
2 |
+
# subset of the dataset and computing diversity.
|
3 |
+
#
|
4 |
+
# Copyright (C) 2023 The QC-Devs Community
|
5 |
+
#
|
6 |
+
# This file is part of Selector.
|
7 |
+
#
|
8 |
+
# Selector is free software; you can redistribute it and/or
|
9 |
+
# modify it under the terms of the GNU General Public License
|
10 |
+
# as published by the Free Software Foundation; either version 3
|
11 |
+
# of the License, or (at your option) any later version.
|
12 |
+
#
|
13 |
+
# Selector is distributed in the hope that it will be useful,
|
14 |
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
+
# GNU General Public License for more details.
|
17 |
+
#
|
18 |
+
# You should have received a copy of the GNU General Public License
|
19 |
+
# along with this program; if not, see <http://www.gnu.org/licenses/>
|
20 |
+
#
|
21 |
+
# --
|
22 |
+
|
23 |
+
import streamlit as st
|
24 |
+
import sys
|
25 |
+
import os
|
26 |
+
|
27 |
+
import scipy
|
28 |
+
from selector.methods.partition import Medoid
|
29 |
+
|
30 |
+
# Add the streamlit_app directory to the Python path
|
31 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
32 |
+
parent_dir = os.path.join(current_dir, "..")
|
33 |
+
sys.path.append(parent_dir)
|
34 |
+
|
35 |
+
from utils import *
|
36 |
+
|
37 |
+
# Set page configuration
|
38 |
+
st.set_page_config(
|
39 |
+
page_title = "Medoid",
|
40 |
+
page_icon = os.path.join(parent_dir, "assets" , "QC-Devs.png"),
|
41 |
+
)
|
42 |
+
|
43 |
+
st.title("Medoid Method")
|
44 |
+
|
45 |
+
|
46 |
+
description = """
|
47 |
+
Points are initially used to construct a KDTree. Eucleidean distances are used for this
|
48 |
+
algorithm. The first point selected is based on the starting_idx provided and becomes the first
|
49 |
+
query point. An approximation of the furthest point to the query point is found using
|
50 |
+
find_furthest_neighbor and is selected. find_nearest_neighbor is then done to eliminate close
|
51 |
+
neighbors to the new selected point. Medoid is then calculated from previously selected points
|
52 |
+
and is used as the new query point for find_furthest_neighbor, repeating the process. Terminates
|
53 |
+
upon selecting requested number of points or if all available points exhausted.
|
54 |
+
"""
|
55 |
+
|
56 |
+
references = "Adapted from: https://en.wikipedia.org/wiki/K-d_tree#Construction"
|
57 |
+
|
58 |
+
display_sidebar_info("Medoid Method", description, references)
|
59 |
+
|
60 |
+
# File uploader for feature matrix or distance matrix (required)
|
61 |
+
matrix_file = st.file_uploader("Upload a feature matrix or distance matrix (required)",
|
62 |
+
type=["csv", "xlsx", "npz", "npy"], key="matrix_file", on_change=clear_results)
|
63 |
+
|
64 |
+
# Clear selected indices if a new matrix file is uploaded
|
65 |
+
if matrix_file is None:
|
66 |
+
clear_results()
|
67 |
+
|
68 |
+
# Load data from matrix file
|
69 |
+
else:
|
70 |
+
matrix = load_matrix(matrix_file)
|
71 |
+
num_points = st.number_input("Number of points to select", min_value = 1, step = 1,
|
72 |
+
key = "num_points", on_change=clear_results)
|
73 |
+
label_file = st.file_uploader("Upload a cluster label list (optional)", type = ["csv", "xlsx"],
|
74 |
+
key = "label_file", on_change=clear_results)
|
75 |
+
labels = load_labels(label_file) if label_file else None
|
76 |
+
|
77 |
+
# Parameters for Medoid
|
78 |
+
st.info("The parameters below are optional. If not specified, default values will be used.")
|
79 |
+
|
80 |
+
start_id = st.number_input("Index for the first point to be selected. (start_id)", value = 0, step = 1, on_change=clear_results)
|
81 |
+
|
82 |
+
scaling = st.number_input("Percent of average maximum distance to use when eliminating the closest points. (scaling)",
|
83 |
+
value=10.0, step=1.0, on_change=clear_results)
|
84 |
+
|
85 |
+
if st.button("Run Medoid Algorithm"):
|
86 |
+
selector = Medoid(start_id=start_id, func_distance = lambda x, y: scipy.spatial.minkowski_distance(x, y) ** 2, scaling=scaling)
|
87 |
+
selected_ids = run_algorithm(selector, matrix, num_points, labels)
|
88 |
+
st.session_state['selected_ids'] = selected_ids
|
89 |
+
|
90 |
+
# Check if the selected indices are stored in the session state
|
91 |
+
if 'selected_ids' in st.session_state and matrix_file is not None:
|
92 |
+
selected_ids = st.session_state['selected_ids']
|
93 |
+
st.write("Selected indices:", selected_ids)
|
94 |
+
|
95 |
+
export_results(selected_ids)
|