File size: 7,221 Bytes
324258f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b77b2e
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
#!/usr/bin/env python
# coding: utf-8
import panel as pn
import os
import pandas as pd
import json
pn.extension()

# Global variables to store the input values
input_path = ""
set_path = ""
ls_samples = []
metadata_files = []
selected_metadata_files = []

# Define a callback to store values when the button is clicked
def store_values(event):
    global input_path, set_path, ls_samples, metadata_files
    input_path = file_input.value
    set_path = set_name.value

    # Update the global variables
    global base_dir, input_data_dir, output_data_dir, output_images_dir, metadata_dir, metadata_images_dir
    present_dir = os.path.dirname(os.path.realpath(__file__))
    input_path = os.path.join(present_dir, input_path)
    base_dir = input_path
    project_name = set_path
    step_suffix = 'qc_eda'
    previous_step_suffix_long = ""

    input_data_dir = os.path.join(base_dir, project_name + "_data")
    output_data_dir = os.path.join(base_dir, project_name + "_" + step_suffix)
    output_images_dir = os.path.join(output_data_dir, "images")
    metadata_dir = os.path.join(base_dir, project_name + "_metadata")
    metadata_images_dir = os.path.join(metadata_dir, "images")

    # Create directories if they don't already exist
    for d in [base_dir, input_data_dir, output_data_dir, output_images_dir, metadata_dir, metadata_images_dir]:
        if not os.path.exists(d):
            print(f"Creation of the {d} directory...")
            os.makedirs(d)
        else:
            print(f"The {d} directory already exists!")

    # os.chdir(input_data_dir)  # Avoid changing the working directory

    # Update JSON with directory settings
    with open('stored_variables.json', 'r') as file:
        data = json.load(file)
    data.update({'base_dir': base_dir, 'set_path': set_path})
    with open('stored_variables.json', 'w') as file:
        json.dump(data, file, indent=4)

    # Create a DataFrame with the directory paths
    data = {
        'Directory': ['Base Directory', 'Input Data Directory', 'Output Data Directory', 'Output Images Directory', 'Metadata Directory', 'Metadata Images Directory'],
        'Path': [base_dir, input_data_dir, output_data_dir, output_images_dir, metadata_dir, metadata_images_dir]
    }
    df = pd.DataFrame(data)

    # List CSV files in the input_data_dir
    ls_samples = [sample for sample in os.listdir(input_data_dir) if sample.endswith(".csv")]
    print(f"The following CSV files were detected:\n\n{ls_samples}\n\nin {input_data_dir} directory.")

    # List CSV files in the metadata_dir
    metadata_files = [file for file in os.listdir(metadata_dir) if file.endswith(".tif.csv")]
    print(f"The following metadata CSV files were detected:\n\n{metadata_files}\n\nin {metadata_dir} directory.")

    # Update the CheckBoxGroup options
    checkbox_group.options = ls_samples
    metadata_checkbox_group.options = metadata_files
    # Assuming csv_files should be a DataFrame constructed from ls_samples
    csv_files = pd.DataFrame({'CSV Files': ls_samples})
    md_files = [file for file in os.listdir(metadata_dir) if file.endswith(".tif.csv")]
    print(md_files)
    metadata_files_df = pd.DataFrame({'Selected_Metadata_files' : [file for file in os.listdir(metadata_dir) if file.endswith(".tif.csv")]})
    print(metadata_files_df)
    # Update the output panes
    output_pane_1.object = f"**File Path:** {input_path}\n\n**Set Name:** {set_path}"
    output_pane_2.object = df.to_html(index=False)
    output_pane_3.object = csv_files.to_html(index=False)
    output_pane_4.object = metadata_files_df.to_html(index=False)

# Define a callback to save the selected CSV files when the button is clicked
def save_selected_files(event):
    with open('stored_variables.json', 'r') as file:
        data = json.load(file)
    data['ls_samples'] = ls_samples
    with open('stored_variables.json', 'w') as file:
        json.dump(data, file, indent=4)
    print("ls_samples updated in JSON file.")

# Define a callback to update the ls_samples list based on the selected options
def update_ls_samples(event):
    global ls_samples
    ls_samples = event.new

# Define a callback to update the metadata_files list based on the selected options
def update_metadata_files(event):
    global metadata_files, selected_metadata_files
    metadata_files = event.new
    selected_metadata_files = event.new

# Define a callback to save the selected metadata files when the button is clicked
def save_selected_metadata_files(event):
    with open('stored_variables.json', 'r') as file:
        data = json.load(file)
    data['selected_metadata_files'] = selected_metadata_files
    with open('stored_variables.json', 'w') as file:
        json.dump(data, file, indent=4)
    print("selected_metadata_files updated in JSON file.")

# Define the widgets
file_input = pn.widgets.TextInput(
    name="File Path",
    placeholder="Enter the path to your directory"
)

set_name = pn.widgets.TextInput(
    name="Set Name",
    placeholder="Enter the Set"
)

store_button = pn.widgets.Button(name='Store Values')
store_button.on_click(store_values)

checkbox_group = pn.widgets.CheckBoxGroup(name='Select CSV Files', options=[], value=[])
checkbox_group.param.watch(update_ls_samples, 'value')

metadata_checkbox_group = pn.widgets.CheckBoxGroup(name='Select Metadata Files', options=[], value=[])
metadata_checkbox_group.param.watch(update_metadata_files, 'value')

save_button = pn.widgets.Button(name='Save Files')
save_button.on_click(save_selected_files)

save_metadata_button = pn.widgets.Button(name='Save Metadata Files')
save_metadata_button.on_click(save_selected_metadata_files)

output_pane_1 = pn.pane.Markdown("")
output_pane_2 = pn.pane.HTML("")
output_pane_3 = pn.pane.HTML("")
output_pane_4 = pn.pane.HTML("")

# Create the cards
card_1 = pn.Card(
    pn.Column(
        pn.pane.Markdown("### Input Form"),
        file_input,
        set_name,
        store_button,
        output_pane_1,
        sizing_mode="stretch_width"
    ),
    title="Input Form",
    sizing_mode="stretch_width"
)

card_2 = pn.Card(
    pn.Column(
        pn.pane.Markdown("### Directory Paths"),
        output_pane_2,
        sizing_mode="stretch_width"
    ),
    title="Directory Paths",
    sizing_mode="stretch_width"
)

card_3 = pn.Card(
    pn.Column(
        pn.pane.Markdown("### CSV Files"),
        output_pane_3,
        pn.pane.Markdown("### Selection of CSV files for the further analysis"),
        checkbox_group,
        save_button,
        sizing_mode="stretch_width"
    ),
    title="CSV Files",
    sizing_mode="stretch_width"
)

card_4 = pn.Card(
    pn.Column(
        pn.pane.Markdown("### Metadata Files"),
        output_pane_4,
        pn.pane.Markdown("### Selection of Metadata Files"),
        metadata_checkbox_group,
        save_metadata_button,
        sizing_mode="stretch_width"
    ),
    title="Metadata Files",
    sizing_mode="stretch_width"
)

# Create the main column with the cards
main_column = pn.Column(card_1, card_2, card_3, card_4)

# Create the GoldenTemplate with the main column
app = pn.template.GoldenTemplate(
    site="Cyc-IF",
    title="Setup",
    main=[main_column],
    header_color="black",
)

app.servable()