spacr 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spacr/__init__.py +4 -0
- spacr/alpha.py +18 -0
- spacr/cli.py +25 -187
- spacr/core.py +267 -56
- spacr/graph_learning.py +276 -0
- spacr/graph_learning_lap.py +84 -0
- spacr/gui_classify_app.py +197 -0
- spacr/gui_mask_app.py +58 -106
- spacr/gui_measure_app.py +14 -28
- spacr/gui_sim_app.py +0 -0
- spacr/gui_utils.py +311 -115
- spacr/io.py +260 -112
- spacr/measure.py +11 -17
- spacr/old_code.py +187 -1
- spacr/plot.py +92 -85
- spacr/sim.py +8 -1
- spacr/timelapse.py +213 -52
- spacr/train.py +7 -1
- spacr/utils.py +220 -119
- spacr-0.0.2.dist-info/METADATA +130 -0
- spacr-0.0.2.dist-info/RECORD +31 -0
- {spacr-0.0.1.dist-info → spacr-0.0.2.dist-info}/entry_points.txt +2 -0
- spacr-0.0.1.dist-info/METADATA +0 -64
- spacr-0.0.1.dist-info/RECORD +0 -26
- {spacr-0.0.1.dist-info → spacr-0.0.2.dist-info}/LICENSE +0 -0
- {spacr-0.0.1.dist-info → spacr-0.0.2.dist-info}/WHEEL +0 -0
- {spacr-0.0.1.dist-info → spacr-0.0.2.dist-info}/top_level.txt +0 -0
spacr/graph_learning.py
ADDED
@@ -0,0 +1,276 @@
|
|
1
|
+
import os
|
2
|
+
import torch
|
3
|
+
import torch.nn as nn
|
4
|
+
import torch.nn.functional as F
|
5
|
+
from collections import defaultdict
|
6
|
+
from torch.utils.data import Dataset, DataLoader
|
7
|
+
import pandas as pd
|
8
|
+
import numpy as np
|
9
|
+
import torch.optim as optim
|
10
|
+
|
11
|
+
def generate_graphs(sequencing, scores, cell_min, gene_min_read):
|
12
|
+
# Load and preprocess sequencing (gene) data
|
13
|
+
gene_df = pd.read_csv(sequencing)
|
14
|
+
gene_df = gene_df.rename(columns={'prc': 'well_id', 'grna': 'gene_id', 'count': 'read_count'})
|
15
|
+
# Filter out genes with read counts less than gene_min_read
|
16
|
+
gene_df = gene_df[gene_df['read_count'] >= gene_min_read]
|
17
|
+
total_reads_per_well = gene_df.groupby('well_id')['read_count'].sum().reset_index(name='total_reads')
|
18
|
+
gene_df = gene_df.merge(total_reads_per_well, on='well_id')
|
19
|
+
gene_df['well_read_fraction'] = gene_df['read_count'] / gene_df['total_reads']
|
20
|
+
|
21
|
+
# Load and preprocess cell score data
|
22
|
+
cell_df = pd.read_csv(scores)
|
23
|
+
cell_df = cell_df[['prcfo', 'prc', 'pred']].rename(columns={'prcfo': 'cell_id', 'prc': 'well_id', 'pred': 'score'})
|
24
|
+
|
25
|
+
# Create a global mapping of gene IDs to indices
|
26
|
+
unique_genes = gene_df['gene_id'].unique()
|
27
|
+
gene_id_to_index = {gene_id: index for index, gene_id in enumerate(unique_genes)}
|
28
|
+
|
29
|
+
graphs = []
|
30
|
+
for well_id in pd.unique(gene_df['well_id']):
|
31
|
+
well_genes = gene_df[gene_df['well_id'] == well_id]
|
32
|
+
well_cells = cell_df[cell_df['well_id'] == well_id]
|
33
|
+
|
34
|
+
# Skip wells with no cells or genes or with fewer cells than threshold
|
35
|
+
if well_cells.empty or well_genes.empty or len(well_cells) < cell_min:
|
36
|
+
continue
|
37
|
+
|
38
|
+
# Initialize gene features tensor with zeros for all unique genes
|
39
|
+
gene_features = torch.zeros((len(gene_id_to_index), 1), dtype=torch.float)
|
40
|
+
|
41
|
+
# Update gene features tensor with well_read_fraction for genes present in this well
|
42
|
+
for _, row in well_genes.iterrows():
|
43
|
+
gene_index = gene_id_to_index[row['gene_id']]
|
44
|
+
gene_features[gene_index] = torch.tensor([[row['well_read_fraction']]])
|
45
|
+
|
46
|
+
# Prepare cell features (scores)
|
47
|
+
cell_features = torch.tensor(well_cells['score'].values, dtype=torch.float).view(-1, 1)
|
48
|
+
|
49
|
+
num_genes = len(gene_id_to_index)
|
50
|
+
num_cells = cell_features.size(0)
|
51
|
+
num_nodes = num_genes + num_cells
|
52
|
+
|
53
|
+
# Create adjacency matrix connecting each cell to all genes in the well
|
54
|
+
adj = torch.zeros((num_nodes, num_nodes), dtype=torch.float)
|
55
|
+
for _, row in well_genes.iterrows():
|
56
|
+
gene_index = gene_id_to_index[row['gene_id']]
|
57
|
+
adj[num_genes:, gene_index] = 1
|
58
|
+
|
59
|
+
graph = {
|
60
|
+
'adjacency_matrix': adj,
|
61
|
+
'gene_features': gene_features,
|
62
|
+
'cell_features': cell_features,
|
63
|
+
'num_cells': num_cells,
|
64
|
+
'num_genes': num_genes
|
65
|
+
}
|
66
|
+
graphs.append(graph)
|
67
|
+
|
68
|
+
print(f'Generated dataset with {len(graphs)} graphs')
|
69
|
+
return graphs, gene_id_to_index
|
70
|
+
|
71
|
+
def print_graphs_info(graphs, gene_id_to_index):
|
72
|
+
# Invert the gene_id_to_index mapping for easy lookup
|
73
|
+
index_to_gene_id = {v: k for k, v in gene_id_to_index.items()}
|
74
|
+
|
75
|
+
for i, graph in enumerate(graphs, start=1):
|
76
|
+
print(f"Graph {i}:")
|
77
|
+
num_genes = graph['num_genes']
|
78
|
+
num_cells = graph['num_cells']
|
79
|
+
gene_features = graph['gene_features']
|
80
|
+
cell_features = graph['cell_features']
|
81
|
+
|
82
|
+
print(f" Number of Genes: {num_genes}")
|
83
|
+
print(f" Number of Cells: {num_cells}")
|
84
|
+
|
85
|
+
# Identify genes present in the graph based on non-zero feature values
|
86
|
+
present_genes = [index_to_gene_id[idx] for idx, feature in enumerate(gene_features) if feature.item() > 0]
|
87
|
+
print(" Genes present in this Graph:", present_genes)
|
88
|
+
|
89
|
+
# Display gene features for genes present in the graph
|
90
|
+
print(" Gene Features:")
|
91
|
+
for gene_id in present_genes:
|
92
|
+
idx = gene_id_to_index[gene_id]
|
93
|
+
print(f" {gene_id}: {gene_features[idx].item()}")
|
94
|
+
|
95
|
+
# Display a sample of cell features, for brevity
|
96
|
+
print(" Cell Features (sample):")
|
97
|
+
for idx, feature in enumerate(cell_features[:min(5, len(cell_features))]):
|
98
|
+
print(f" Cell {idx+1}: {feature.item()}")
|
99
|
+
|
100
|
+
print("-" * 40)
|
101
|
+
|
102
|
+
class Attention(nn.Module):
|
103
|
+
def __init__(self, feature_dim, attn_dim, dropout_rate=0.1):
|
104
|
+
super(Attention, self).__init__()
|
105
|
+
self.query = nn.Linear(feature_dim, attn_dim)
|
106
|
+
self.key = nn.Linear(feature_dim, attn_dim)
|
107
|
+
self.value = nn.Linear(feature_dim, feature_dim)
|
108
|
+
self.scale = 1.0 / (attn_dim ** 0.5)
|
109
|
+
self.dropout = nn.Dropout(dropout_rate)
|
110
|
+
|
111
|
+
def forward(self, gene_features, cell_features):
|
112
|
+
# Queries come from the cell features
|
113
|
+
q = self.query(cell_features)
|
114
|
+
# Keys and values come from the gene features
|
115
|
+
k = self.key(gene_features)
|
116
|
+
v = self.value(gene_features)
|
117
|
+
|
118
|
+
# Compute attention weights
|
119
|
+
attn_weights = torch.matmul(q, k.transpose(-2, -1)) * self.scale
|
120
|
+
attn_weights = F.softmax(attn_weights, dim=-1)
|
121
|
+
# Apply dropout to attention weights
|
122
|
+
attn_weights = self.dropout(attn_weights)
|
123
|
+
|
124
|
+
# Apply attention weights to the values
|
125
|
+
attn_output = torch.matmul(attn_weights, v)
|
126
|
+
|
127
|
+
return attn_output, attn_weights
|
128
|
+
|
129
|
+
class GraphTransformer(nn.Module):
|
130
|
+
def __init__(self, gene_feature_size, cell_feature_size, hidden_dim, output_dim, attn_dim, dropout_rate=0.1):
|
131
|
+
super(GraphTransformer, self).__init__()
|
132
|
+
self.gene_transform = nn.Linear(gene_feature_size, hidden_dim)
|
133
|
+
self.cell_transform = nn.Linear(cell_feature_size, hidden_dim)
|
134
|
+
self.dropout = nn.Dropout(dropout_rate)
|
135
|
+
|
136
|
+
# Attention layer to let each cell attend to all genes
|
137
|
+
self.attention = Attention(hidden_dim, attn_dim)
|
138
|
+
|
139
|
+
# This layer is used to transform the combined features after attention
|
140
|
+
self.combine_transform = nn.Linear(2 * hidden_dim, hidden_dim)
|
141
|
+
|
142
|
+
# Output layer for predicting cell scores, ensuring it matches the number of cells
|
143
|
+
self.cell_output = nn.Linear(hidden_dim, output_dim)
|
144
|
+
|
145
|
+
def forward(self, adjacency_matrix, gene_features, cell_features):
|
146
|
+
# Apply initial transformation to gene and cell features
|
147
|
+
transformed_gene_features = F.relu(self.gene_transform(gene_features))
|
148
|
+
transformed_cell_features = F.relu(self.cell_transform(cell_features))
|
149
|
+
|
150
|
+
# Incorporate attention mechanism
|
151
|
+
attn_output, attn_weights = self.attention(transformed_gene_features, transformed_cell_features)
|
152
|
+
|
153
|
+
# Combine the transformed cell features with the attention output features
|
154
|
+
combined_cell_features = torch.cat((transformed_cell_features, attn_output), dim=1)
|
155
|
+
|
156
|
+
# Apply dropout here as well
|
157
|
+
combined_cell_features = self.dropout(combined_cell_features)
|
158
|
+
|
159
|
+
combined_cell_features = F.relu(self.combine_transform(combined_cell_features))
|
160
|
+
|
161
|
+
# Combine gene and cell features for message passing
|
162
|
+
combined_features = torch.cat((transformed_gene_features, combined_cell_features), dim=0)
|
163
|
+
|
164
|
+
# Apply message passing via adjacency matrix multiplication
|
165
|
+
message_passed_features = torch.matmul(adjacency_matrix, combined_features)
|
166
|
+
|
167
|
+
# Predict cell scores from the post-message passed cell features
|
168
|
+
cell_scores = self.cell_output(message_passed_features[-cell_features.size(0):])
|
169
|
+
|
170
|
+
return cell_scores, attn_weights
|
171
|
+
|
172
|
+
def train_graph_transformer(graphs, lr=0.01, dropout_rate=0.1, weight_decay=0.00001, epochs=100, save_fldr='', acc_threshold = 0.1):
|
173
|
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
174
|
+
model = GraphTransformer(gene_feature_size=1, cell_feature_size=1, hidden_dim=256, output_dim=1, attn_dim=128, dropout_rate=dropout_rate).to(device)
|
175
|
+
|
176
|
+
criterion = nn.MSELoss()
|
177
|
+
#optimizer = torch.optim.Adam(model.parameters(), lr=lr)
|
178
|
+
optimizer = optim.AdamW(model.parameters(), lr=lr, weight_decay=weight_decay)
|
179
|
+
|
180
|
+
training_log = []
|
181
|
+
|
182
|
+
accumulate_grad_batches=1
|
183
|
+
threshold=acc_threshold
|
184
|
+
|
185
|
+
for epoch in range(epochs):
|
186
|
+
model.train()
|
187
|
+
total_loss = 0
|
188
|
+
total_correct = 0
|
189
|
+
total_samples = 0
|
190
|
+
optimizer.zero_grad()
|
191
|
+
batch_count = 0 # Initialize batch_count
|
192
|
+
|
193
|
+
for graph in graphs:
|
194
|
+
adjacency_matrix = graph['adjacency_matrix'].to(device)
|
195
|
+
gene_features = graph['gene_features'].to(device)
|
196
|
+
cell_features = graph['cell_features'].to(device)
|
197
|
+
num_cells = graph['num_cells']
|
198
|
+
predictions, attn_weights = model(adjacency_matrix, gene_features, cell_features)
|
199
|
+
predictions = predictions.squeeze()
|
200
|
+
true_scores = cell_features[:num_cells, 0]
|
201
|
+
loss = criterion(predictions, true_scores) / accumulate_grad_batches
|
202
|
+
loss.backward()
|
203
|
+
|
204
|
+
# Calculate "accuracy"
|
205
|
+
with torch.no_grad():
|
206
|
+
correct_predictions = (torch.abs(predictions - true_scores) / true_scores <= threshold).sum().item()
|
207
|
+
total_correct += correct_predictions
|
208
|
+
total_samples += num_cells
|
209
|
+
|
210
|
+
batch_count += 1 # Increment batch_count
|
211
|
+
if batch_count % accumulate_grad_batches == 0 or batch_count == len(graphs):
|
212
|
+
optimizer.step()
|
213
|
+
optimizer.zero_grad()
|
214
|
+
|
215
|
+
total_loss += loss.item() * accumulate_grad_batches
|
216
|
+
|
217
|
+
accuracy = total_correct / total_samples
|
218
|
+
training_log.append({"Epoch": epoch+1, "Average Loss": total_loss / len(graphs), "Accuracy": accuracy})
|
219
|
+
print(f"Epoch {epoch+1}, Loss: {total_loss / len(graphs)}, Accuracy: {accuracy}", end="\r", flush=True)
|
220
|
+
|
221
|
+
# Save the training log and model as before
|
222
|
+
os.makedirs(save_fldr, exist_ok=True)
|
223
|
+
log_path = os.path.join(save_fldr, 'training_log.csv')
|
224
|
+
training_log_df = pd.DataFrame(training_log)
|
225
|
+
training_log_df.to_csv(log_path, index=False)
|
226
|
+
print(f"Training log saved to {log_path}")
|
227
|
+
|
228
|
+
model_path = os.path.join(save_fldr, 'model.pth')
|
229
|
+
torch.save(model.state_dict(), model_path)
|
230
|
+
print(f"Model saved to {model_path}")
|
231
|
+
|
232
|
+
return model
|
233
|
+
|
234
|
+
def annotate_cells_with_genes(graphs, model, gene_id_to_index):
|
235
|
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
236
|
+
model.to(device)
|
237
|
+
model.eval()
|
238
|
+
annotated_data = []
|
239
|
+
|
240
|
+
with torch.no_grad():
|
241
|
+
for graph in graphs:
|
242
|
+
adjacency_matrix = graph['adjacency_matrix'].to(device)
|
243
|
+
gene_features = graph['gene_features'].to(device)
|
244
|
+
cell_features = graph['cell_features'].to(device)
|
245
|
+
|
246
|
+
predictions, attn_weights = model(adjacency_matrix, gene_features, cell_features)
|
247
|
+
predictions = np.atleast_1d(predictions.squeeze().cpu().numpy())
|
248
|
+
attn_weights = np.atleast_2d(attn_weights.squeeze().cpu().numpy())
|
249
|
+
|
250
|
+
# This approach assumes all genes in gene_id_to_index are used in the model.
|
251
|
+
# Create a list of gene IDs present in this specific graph.
|
252
|
+
present_gene_ids = [key for key, value in gene_id_to_index.items() if value < gene_features.size(0)]
|
253
|
+
|
254
|
+
for cell_idx in range(cell_features.size(0)):
|
255
|
+
true_score = cell_features[cell_idx, 0].item()
|
256
|
+
predicted_score = predictions[cell_idx]
|
257
|
+
|
258
|
+
# Find the index of the most probable gene.
|
259
|
+
most_probable_gene_idx = attn_weights[cell_idx].argmax()
|
260
|
+
|
261
|
+
if len(present_gene_ids) > most_probable_gene_idx: # Ensure index is within the range
|
262
|
+
most_probable_gene_id = present_gene_ids[most_probable_gene_idx]
|
263
|
+
most_probable_gene_score = attn_weights[cell_idx, most_probable_gene_idx] if attn_weights.ndim > 1 else attn_weights[most_probable_gene_idx]
|
264
|
+
|
265
|
+
annotated_data.append({
|
266
|
+
"Cell ID": cell_idx,
|
267
|
+
"Most Probable Gene": most_probable_gene_id,
|
268
|
+
"Cell Score": true_score,
|
269
|
+
"Predicted Cell Score": predicted_score,
|
270
|
+
"Probability Score for Highest Gene": most_probable_gene_score
|
271
|
+
})
|
272
|
+
else:
|
273
|
+
# Handle the case where the index is out of bounds - this should not happen but is here for robustness
|
274
|
+
print("Error: Gene index out of bounds. This might indicate a mismatch in the model's output.")
|
275
|
+
|
276
|
+
return pd.DataFrame(annotated_data)
|
@@ -0,0 +1,84 @@
|
|
1
|
+
import torch
|
2
|
+
import torch.nn as nn
|
3
|
+
import torch.nn.functional as F
|
4
|
+
from torch.utils.data import Dataset, DataLoader, TensorDataset
|
5
|
+
|
6
|
+
# Let's assume that the feature embedding part and the dataset loading part
|
7
|
+
# has already been taken care of, and your data is already in the format
|
8
|
+
# suitable for PyTorch (i.e., Tensors).
|
9
|
+
|
10
|
+
class FeatureEmbedder(nn.Module):
|
11
|
+
def __init__(self, vocab_sizes, embedding_size):
|
12
|
+
super(FeatureEmbedder, self).__init__()
|
13
|
+
self.embeddings = nn.ModuleDict({
|
14
|
+
key: nn.Embedding(num_embeddings=vocab_size+1,
|
15
|
+
embedding_dim=embedding_size,
|
16
|
+
padding_idx=vocab_size)
|
17
|
+
for key, vocab_size in vocab_sizes.items()
|
18
|
+
})
|
19
|
+
# Adding the 'visit' embedding
|
20
|
+
self.embeddings['visit'] = nn.Parameter(torch.zeros(1, embedding_size))
|
21
|
+
|
22
|
+
def forward(self, feature_map, max_num_codes):
|
23
|
+
# Implementation will depend on how you want to handle sparse data
|
24
|
+
# This is just a placeholder
|
25
|
+
embeddings = {}
|
26
|
+
masks = {}
|
27
|
+
for key, tensor in feature_map.items():
|
28
|
+
embeddings[key] = self.embeddings[key](tensor.long())
|
29
|
+
mask = torch.ones_like(tensor, dtype=torch.float32)
|
30
|
+
masks[key] = mask.unsqueeze(-1)
|
31
|
+
|
32
|
+
# Batch size hardcoded for simplicity in example
|
33
|
+
batch_size = 1 # Replace with actual batch size
|
34
|
+
embeddings['visit'] = self.embeddings['visit'].expand(batch_size, -1, -1)
|
35
|
+
masks['visit'] = torch.ones(batch_size, 1)
|
36
|
+
|
37
|
+
return embeddings, masks
|
38
|
+
|
39
|
+
class GraphConvolutionalTransformer(nn.Module):
|
40
|
+
def __init__(self, embedding_size=128, num_attention_heads=1, **kwargs):
|
41
|
+
super(GraphConvolutionalTransformer, self).__init__()
|
42
|
+
# Transformer Blocks
|
43
|
+
self.layers = nn.ModuleList([
|
44
|
+
nn.TransformerEncoderLayer(
|
45
|
+
d_model=embedding_size,
|
46
|
+
nhead=num_attention_heads,
|
47
|
+
batch_first=True)
|
48
|
+
for _ in range(kwargs.get('num_transformer_stack', 3))
|
49
|
+
])
|
50
|
+
# Output Layer for Classification
|
51
|
+
self.output_layer = nn.Linear(embedding_size, 1)
|
52
|
+
|
53
|
+
def feedforward(self, features, mask=None, training=None):
|
54
|
+
# Implement feedforward logic (placeholder)
|
55
|
+
pass
|
56
|
+
|
57
|
+
def forward(self, embeddings, masks, mask=None, training=False):
|
58
|
+
features = embeddings
|
59
|
+
attentions = [] # Storing attentions if needed
|
60
|
+
|
61
|
+
# Pass through each Transformer block
|
62
|
+
for layer in self.layers:
|
63
|
+
features = layer(features) # Apply transformer encoding here
|
64
|
+
|
65
|
+
if mask is not None:
|
66
|
+
features = features * mask
|
67
|
+
|
68
|
+
logits = self.output_layer(features[:, 0, :]) # Using the 'visit' embedding for classification
|
69
|
+
return logits, attentions
|
70
|
+
|
71
|
+
# Usage Example
|
72
|
+
vocab_sizes = {'dx_ints':3249, 'proc_ints':2210}
|
73
|
+
embedding_size = 128
|
74
|
+
gct_params = {
|
75
|
+
'embedding_size': embedding_size,
|
76
|
+
'num_transformer_stack': 3,
|
77
|
+
'num_attention_heads': 1
|
78
|
+
}
|
79
|
+
feature_embedder = FeatureEmbedder(vocab_sizes, embedding_size)
|
80
|
+
gct_model = GraphConvolutionalTransformer(**gct_params)
|
81
|
+
|
82
|
+
# Assume `feature_map` is a dictionary of tensors, and `max_num_codes` is provided
|
83
|
+
embeddings, masks = feature_embedder(feature_map, max_num_codes)
|
84
|
+
logits, attentions = gct_model(embeddings, masks)
|
@@ -0,0 +1,197 @@
|
|
1
|
+
import sys, ctypes, csv, matplotlib
|
2
|
+
import tkinter as tk
|
3
|
+
from tkinter import ttk, scrolledtext
|
4
|
+
from ttkthemes import ThemedTk
|
5
|
+
from matplotlib.figure import Figure
|
6
|
+
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
7
|
+
from matplotlib.figure import Figure
|
8
|
+
matplotlib.use('Agg')
|
9
|
+
from tkinter import filedialog
|
10
|
+
from multiprocessing import Process, Queue, Value
|
11
|
+
import traceback
|
12
|
+
|
13
|
+
try:
|
14
|
+
ctypes.windll.shcore.SetProcessDpiAwareness(True)
|
15
|
+
except AttributeError:
|
16
|
+
pass
|
17
|
+
|
18
|
+
from .logger import log_function_call
|
19
|
+
from .gui_utils import ScrollableFrame, StdoutRedirector, create_dark_mode, set_dark_style, set_default_font, generate_fields, process_stdout_stderr, safe_literal_eval, clear_canvas, main_thread_update_function
|
20
|
+
from .gui_utils import classify_variables, check_classify_gui_settings, train_test_model_wrapper, read_settings_from_csv, update_settings_from_csv
|
21
|
+
|
22
|
+
thread_control = {"run_thread": None, "stop_requested": False}
|
23
|
+
|
24
|
+
@log_function_call
|
25
|
+
def initiate_abort():
|
26
|
+
global thread_control
|
27
|
+
if thread_control.get("stop_requested") is not None:
|
28
|
+
thread_control["stop_requested"].value = 1
|
29
|
+
|
30
|
+
if thread_control.get("run_thread") is not None:
|
31
|
+
thread_control["run_thread"].join(timeout=5)
|
32
|
+
if thread_control["run_thread"].is_alive():
|
33
|
+
thread_control["run_thread"].terminate()
|
34
|
+
thread_control["run_thread"] = None
|
35
|
+
|
36
|
+
@log_function_call
|
37
|
+
def run_classify_gui(q, fig_queue, stop_requested):
|
38
|
+
global vars_dict
|
39
|
+
process_stdout_stderr(q)
|
40
|
+
try:
|
41
|
+
settings = check_classify_gui_settings(vars_dict)
|
42
|
+
for key in settings:
|
43
|
+
value = settings[key]
|
44
|
+
print(key, value, type(value))
|
45
|
+
train_test_model_wrapper(settings['src'], settings)
|
46
|
+
except Exception as e:
|
47
|
+
q.put(f"Error during processing: {e}")
|
48
|
+
traceback.print_exc()
|
49
|
+
finally:
|
50
|
+
stop_requested.value = 1
|
51
|
+
|
52
|
+
@log_function_call
|
53
|
+
def start_process(q, fig_queue):
|
54
|
+
global thread_control
|
55
|
+
if thread_control.get("run_thread") is not None:
|
56
|
+
initiate_abort()
|
57
|
+
|
58
|
+
stop_requested = Value('i', 0) # multiprocessing shared value for inter-process communication
|
59
|
+
thread_control["stop_requested"] = stop_requested
|
60
|
+
thread_control["run_thread"] = Process(target=run_classify_gui, args=(q, fig_queue, stop_requested))
|
61
|
+
thread_control["run_thread"].start()
|
62
|
+
|
63
|
+
def import_settings(scrollable_frame):
|
64
|
+
global vars_dict
|
65
|
+
|
66
|
+
csv_file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
|
67
|
+
csv_settings = read_settings_from_csv(csv_file_path)
|
68
|
+
variables = classify_variables()
|
69
|
+
new_settings = update_settings_from_csv(variables, csv_settings)
|
70
|
+
vars_dict = generate_fields(new_settings, scrollable_frame)
|
71
|
+
|
72
|
+
@log_function_call
|
73
|
+
def initiate_classify_root(width, height):
|
74
|
+
global root, vars_dict, q, canvas, fig_queue, canvas_widget, thread_control
|
75
|
+
|
76
|
+
theme = 'breeze'
|
77
|
+
|
78
|
+
if theme in ['clam']:
|
79
|
+
root = tk.Tk()
|
80
|
+
style = ttk.Style(root)
|
81
|
+
style.theme_use(theme) #plastik, clearlooks, elegance, default was clam #alt, breeze, arc
|
82
|
+
set_dark_style(style)
|
83
|
+
elif theme in ['breeze']:
|
84
|
+
root = ThemedTk(theme="breeze")
|
85
|
+
style = ttk.Style(root)
|
86
|
+
set_dark_style(style)
|
87
|
+
|
88
|
+
set_default_font(root, font_name="Arial", size=10)
|
89
|
+
#root.state('zoomed') # For Windows to maximize the window
|
90
|
+
root.attributes('-fullscreen', True)
|
91
|
+
root.geometry(f"{width}x{height}")
|
92
|
+
root.title("SpaCer: generate masks")
|
93
|
+
fig_queue = Queue()
|
94
|
+
|
95
|
+
def _process_fig_queue():
|
96
|
+
global canvas
|
97
|
+
try:
|
98
|
+
while not fig_queue.empty():
|
99
|
+
clear_canvas(canvas)
|
100
|
+
fig = fig_queue.get_nowait()
|
101
|
+
#set_fig_text_properties(fig, font_size=8)
|
102
|
+
for ax in fig.get_axes():
|
103
|
+
ax.set_xticks([]) # Remove x-axis ticks
|
104
|
+
ax.set_yticks([]) # Remove y-axis ticks
|
105
|
+
ax.xaxis.set_visible(False) # Hide the x-axis
|
106
|
+
ax.yaxis.set_visible(False) # Hide the y-axis
|
107
|
+
#ax.title.set_fontsize(14)
|
108
|
+
#disable_interactivity(fig)
|
109
|
+
fig.tight_layout()
|
110
|
+
fig.set_facecolor('#333333')
|
111
|
+
canvas.figure = fig
|
112
|
+
fig_width, fig_height = canvas_widget.winfo_width(), canvas_widget.winfo_height()
|
113
|
+
fig.set_size_inches(fig_width / fig.dpi, fig_height / fig.dpi, forward=True)
|
114
|
+
canvas.draw_idle()
|
115
|
+
except Exception as e:
|
116
|
+
traceback.print_exc()
|
117
|
+
#pass
|
118
|
+
finally:
|
119
|
+
canvas_widget.after(100, _process_fig_queue)
|
120
|
+
|
121
|
+
# Process queue for console output
|
122
|
+
def _process_console_queue():
|
123
|
+
while not q.empty():
|
124
|
+
message = q.get_nowait()
|
125
|
+
console_output.insert(tk.END, message)
|
126
|
+
console_output.see(tk.END)
|
127
|
+
console_output.after(100, _process_console_queue)
|
128
|
+
|
129
|
+
# Vertical container for settings and console
|
130
|
+
vertical_container = tk.PanedWindow(root, orient=tk.HORIZONTAL) #VERTICAL
|
131
|
+
vertical_container.pack(fill=tk.BOTH, expand=True)
|
132
|
+
|
133
|
+
# Scrollable Frame for user settings
|
134
|
+
scrollable_frame = ScrollableFrame(vertical_container, bg='#333333')
|
135
|
+
vertical_container.add(scrollable_frame, stretch="always")
|
136
|
+
|
137
|
+
# Setup for user input fields (variables)
|
138
|
+
variables = classify_variables()
|
139
|
+
vars_dict = generate_fields(variables, scrollable_frame)
|
140
|
+
|
141
|
+
# Horizontal container for Matplotlib figure and the vertical pane (for settings and console)
|
142
|
+
horizontal_container = tk.PanedWindow(vertical_container, orient=tk.VERTICAL) #HORIZONTAL
|
143
|
+
vertical_container.add(horizontal_container, stretch="always")
|
144
|
+
|
145
|
+
# Matplotlib figure setup
|
146
|
+
figure = Figure(figsize=(30, 4), dpi=100, facecolor='#333333')
|
147
|
+
plot = figure.add_subplot(111)
|
148
|
+
plot.plot([], []) # This creates an empty plot.
|
149
|
+
plot.axis('off')
|
150
|
+
|
151
|
+
# Embedding the Matplotlib figure in the Tkinter window
|
152
|
+
canvas = FigureCanvasTkAgg(figure, master=horizontal_container)
|
153
|
+
canvas.get_tk_widget().configure(cursor='arrow', background='#333333', highlightthickness=0)
|
154
|
+
#canvas.get_tk_widget().configure(cursor='arrow')
|
155
|
+
canvas_widget = canvas.get_tk_widget()
|
156
|
+
horizontal_container.add(canvas_widget, stretch="always")
|
157
|
+
canvas.draw()
|
158
|
+
canvas.figure = figure
|
159
|
+
|
160
|
+
# Console output setup below the settings
|
161
|
+
console_output = scrolledtext.ScrolledText(vertical_container, height=10)
|
162
|
+
vertical_container.add(console_output, stretch="always")
|
163
|
+
|
164
|
+
# Queue and redirection setup for updating console output safely
|
165
|
+
q = Queue()
|
166
|
+
sys.stdout = StdoutRedirector(console_output)
|
167
|
+
sys.stderr = StdoutRedirector(console_output)
|
168
|
+
|
169
|
+
# This is your GUI setup where you create the Run button
|
170
|
+
run_button = ttk.Button(scrollable_frame.scrollable_frame, text="Run",command=lambda: start_process(q, fig_queue))
|
171
|
+
run_button.grid(row=40, column=0, pady=10)
|
172
|
+
|
173
|
+
abort_button = ttk.Button(scrollable_frame.scrollable_frame, text="Abort", command=initiate_abort)
|
174
|
+
abort_button.grid(row=40, column=1, pady=10)
|
175
|
+
|
176
|
+
progress_label = ttk.Label(scrollable_frame.scrollable_frame, text="Processing: 0%", background="#333333", foreground="white")
|
177
|
+
progress_label.grid(row=41, column=0, columnspan=2, sticky="ew", pady=(5, 0))
|
178
|
+
|
179
|
+
# Create the Import Settings button
|
180
|
+
import_btn = tk.Button(root, text="Import Settings", command=lambda: import_settings(scrollable_frame))
|
181
|
+
import_btn.pack(pady=20)
|
182
|
+
|
183
|
+
_process_console_queue()
|
184
|
+
_process_fig_queue()
|
185
|
+
create_dark_mode(root, style, console_output)
|
186
|
+
|
187
|
+
root.after(100, lambda: main_thread_update_function(root, q, fig_queue, canvas_widget, progress_label))
|
188
|
+
|
189
|
+
return root, vars_dict
|
190
|
+
|
191
|
+
def gui_classify():
|
192
|
+
global vars_dict, root
|
193
|
+
root, vars_dict = initiate_classify_root(1000, 1500)
|
194
|
+
root.mainloop()
|
195
|
+
|
196
|
+
if __name__ == "__main__":
|
197
|
+
gui_classify()
|