File size: 5,284 Bytes
bbcd4a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# The Selector library provides a set of tools for selecting a
# subset of the dataset and computing diversity.
#
# Copyright (C) 2023 The QC-Devs Community
#
# This file is part of Selector.
#
# Selector is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# Selector is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
#
# --

import streamlit as st
import sys
import os

from selector.methods.distance import DISE

# Add the streamlit_app directory to the Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.join(current_dir, "..")
sys.path.append(parent_dir)

from utils import *

# Set page configuration
st.set_page_config(
    page_title = "DISE",
    page_icon = os.path.join(parent_dir, "assets" , "QC-Devs.png"),
)

st.title("Directed Sphere Exclusion (DISE)")


description = """
    In a nutshell, this algorithm iteratively excludes any sample within a given radius from
    any already selected sample. The radius of the exclusion sphere is an adjustable parameter.
    Compared to Sphere Exclusion algorithm, the Directed Sphere Exclusion algorithm achieves a
    more evenly distributed subset selection by abandoning the random selection approach and
    instead imposing a directed selection.

    Reference sample is chosen based on the `ref_index`, which is excluded from the selected
    subset. All samples are sorted (ascending order) based on their Minkowski p-norm distance
    from the reference sample. Looping through sorted samples, the sample is selected if it is
    not already excluded. If selected, all its neighboring samples within a sphere of radius r
    (i.e., exclusion sphere) are excluded from being selected. When the selected number of points
    is greater than specified subset `size`, the selection process terminates. The `r0` is used
    as the initial radius of exclusion sphere, however, it is optimized to select the desired
    number of samples.
    """

references = "Gobbi, A., and Lee, M.-L. (2002). DISE: directed sphere exclusion."\
             "Journal of Chemical Information and Computer Sciences,"\
             "43(1), 317–323. https://doi.org/10.1021/ci025554v"

display_sidebar_info("Directed Sphere Exclusion (DISE)", description, references)

# File uploader for feature matrix or distance matrix (required)
matrix_file = st.file_uploader("Upload a feature matrix or distance matrix (required)",
                               type=["csv", "xlsx", "npz", "npy"], key="matrix_file", on_change=clear_results)

# Clear selected indices if a new matrix file is uploaded
if matrix_file is None:
    clear_results()
# Load data from matrix file
else:
    matrix = load_matrix(matrix_file)
    num_points = st.number_input("Number of points to select", min_value = 1, step = 1,
                                 key = "num_points", on_change=clear_results)
    label_file = st.file_uploader("Upload a cluster label list (optional)", type = ["csv", "xlsx"],
                                  key = "label_file", on_change=clear_results)
    labels = load_labels(label_file) if label_file else None

    # Parameters for Directed Sphere Exclusion
    st.info("The parameters below are optional. If not specified, default values will be used.")

    r0 = st.number_input("Initial guess for radius of exclusion sphere (r0)", value=None, step=0.1,
                         on_change=clear_results)
    ref_index = st.number_input("Reference index (ref_index)", value=0, step=1, on_change=clear_results)
    tol = st.number_input("Percentage tolerance of sample size error (tol)", value=0.05, step=0.05,
                          on_change=clear_results)
    n_iter = st.number_input("Number of iterations for optimizing the radius of exclusion sphere (n_iter)",
                             value=10, step=10, on_change=clear_results)
    p = st.number_input("Minkowski p-norm distance (p)", value=2.0, step=1.0, on_change=clear_results)
    eps = st.number_input("Approximate nearest neighbor search parameter (eps)", value=0.0, step=0.1,
                          on_change=clear_results)

    if st.button("Run DISE Algorithm"):
        selector = DISE(r0=r0, ref_index=ref_index, tol=tol, n_iter=n_iter, p=p, eps=eps)
        selected_ids = run_algorithm(selector, matrix, num_points, labels)
        st.session_state['selector'] = selector
        st.session_state['selected_ids'] = selected_ids

# Check if the selected indices are stored in the session state
if 'selected_ids' in st.session_state and matrix_file is not None:
    selected_ids = st.session_state['selected_ids']
    st.write("Selected indices:", selected_ids)

    if 'selector' in st.session_state:
        st.write("Radius of the exclusion sphere:", st.session_state['selector'].r)

    export_results(selected_ids)