cc-mapping 0.1.0__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.
cc_mapping/__init__.py ADDED
@@ -0,0 +1,23 @@
1
+ """
2
+ cc_mapping: GMM-based thresholding for single-cell analysis.
3
+
4
+ This package provides Gaussian Mixture Model (GMM) based thresholding
5
+ for gene expression data within AnnData objects. It supports both
6
+ single-feature thresholding and sequential refinement operations.
7
+
8
+ Main functionality:
9
+ - GMMThresholding: Single-feature GMM thresholding
10
+ - SequentialGMM: Sequential refinement across multiple features
11
+ - create_boolean_label_combination: Boolean operations on categorical labels
12
+ """
13
+
14
+ from .thresholding import GMMThresholding, SequentialGMM
15
+ from .utils import create_boolean_label_combination
16
+
17
+ __all__ = [
18
+ "GMMThresholding",
19
+ "SequentialGMM",
20
+ "create_boolean_label_combination",
21
+ ]
22
+
23
+ __version__ = "0.1.0"
cc_mapping/core.py ADDED
@@ -0,0 +1,360 @@
1
+ from typing import Optional
2
+ from collections import OrderedDict
3
+ import warnings
4
+ warnings.simplefilter(action="ignore", category=FutureWarning)
5
+
6
+ import numpy as np
7
+ np.seterr(all="ignore")
8
+
9
+ import re
10
+ import anndata as ad
11
+ from scipy import stats as st
12
+ from tqdm import tqdm
13
+
14
+ import matplotlib
15
+ import matplotlib.pyplot as plt
16
+ import matplotlib.patches as mpatches
17
+ from matplotlib import cm
18
+
19
+ from sklearn.svm import SVC
20
+ from sklearn.mixture import GaussianMixture
21
+ from sklearn import metrics
22
+ from sklearn.preprocessing import LabelEncoder
23
+ from sklearn.model_selection import train_test_split
24
+ from sklearn.ensemble import RandomForestClassifier
25
+
26
+ from .utils import get_str_idx
27
+ from .preprocess import row_data_partitioning
28
+
29
+
30
+ def train_random_forest_model(
31
+ features,
32
+ labels,
33
+ rf_params: dict,
34
+ random_state: int,
35
+ train_test_split_params: dict,
36
+ feature_set_description: str = "",
37
+ verbose: bool = True,
38
+ ):
39
+ """
40
+ Trains a random forest model using the given features and labels.
41
+
42
+ Parameters:
43
+ - features: The input features for training the model.
44
+ - labels: The target labels for training the model.
45
+ - rf_params: A dictionary of parameters for the random forest classifier.
46
+ - random_state: A boolean value indicating whether to use a random state for reproducibility.
47
+ - train_test_split_params: A dictionary of parameters for the train-test split.
48
+ - verbose: A boolean value indicating whether to print the classification report.
49
+
50
+ Returns:
51
+ - rf_classifier: The trained random forest classifier.
52
+ - accuracy: The accuracy of the model on the test set.
53
+ """
54
+ train_features, test_features, train_labels, test_labels = train_test_split(
55
+ features, labels, random_state=random_state, **train_test_split_params
56
+ )
57
+ rf_classifier = RandomForestClassifier(random_state=random_state, **rf_params)
58
+
59
+ rf_classifier.fit(train_features, train_labels)
60
+
61
+ rf_pred_labels = rf_classifier.predict(test_features)
62
+
63
+ accuracy = metrics.accuracy_score(test_labels, rf_pred_labels)
64
+
65
+ if verbose:
66
+ print(f"Classification Report for RF model trained with {feature_set_description} feature set")
67
+ print("##################################################################")
68
+ print()
69
+ print(metrics.classification_report(test_labels, rf_pred_labels))
70
+
71
+ return rf_classifier, accuracy
72
+
73
+
74
+ def __random_forest_increment_counter(
75
+ acc_list, optim_feat_num, counter, stable_counter, threshold
76
+ ):
77
+ """
78
+ Increment the counter based on the accuracy difference between the current feature and the previous feature.
79
+
80
+ Parameters:
81
+ acc_list (list): List of accuracy values.
82
+ optim_feat_num (int): Index of the current feature.
83
+ counter (int): Counter value.
84
+ stable_counter (int): Number of stable features.
85
+ threshold (float): Threshold value for accuracy difference.
86
+
87
+ Returns:
88
+ tuple: A tuple containing the updated optim_feat_num, counter, and continue_bool values.
89
+ """
90
+ trun_acc_list = np.array(
91
+ acc_list[optim_feat_num : optim_feat_num + stable_counter + 1]
92
+ )
93
+ acc_diff_list = trun_acc_list - acc_list[optim_feat_num]
94
+
95
+ diff_surpass_threshold = np.where(acc_diff_list > threshold)[0]
96
+
97
+ if len(diff_surpass_threshold) == 0:
98
+ continue_bool = False
99
+ else:
100
+ if counter != 0:
101
+ counter -= 1
102
+
103
+ optim_feat_num += 1
104
+ continue_bool = True
105
+
106
+ return optim_feat_num, counter, continue_bool
107
+
108
+
109
+ def random_forest_feature_selection(
110
+ adata: ad.AnnData,
111
+ training_feature_set: str,
112
+ training_labels: str,
113
+ feature_set_name: str = None,
114
+ method: str = "RF_min_max",
115
+ random_state: int = 42,
116
+ threshold: float = 0.01,
117
+ stable_counter: int = 3,
118
+ plot: bool = True,
119
+ verbose: bool = True,
120
+ save_path: str = None,
121
+ cutoff_method: str = "increment",
122
+ train_test_split_params: Optional[dict]= None,
123
+ rf_params: Optional[dict] = None,
124
+ ) -> ad.AnnData:
125
+ """
126
+ Trains a random forest classifier on the training feature set and labels using one of two methods:
127
+ RF_min_30: Selects the top 30 features based on the random forest feature importance
128
+ RF_min_max: Selects the minimum number of features that maximizes the accuracy of the random forest classifier
129
+ This is done by iteratively adding features to the feature set until the accuracy of the classifier
130
+ does not improve for x number of iterations
131
+
132
+ Args:
133
+ adata (ad.AnnData): The AnnData object containing the data.
134
+ training_feature_set (str): The name of the feature set to be used for training.
135
+ training_labels (str): The name of the labels to be used for training.
136
+ feature_set_name (str, optional): The name of the feature set to be added to the .var attribute of the adata object. Defaults to None.
137
+ method (str, optional): The method to be used for feature selection. Defaults to 'RF_min_max'.
138
+ random_state (int, optional): The random state for reproducibility. Defaults to 42.
139
+ threshold (float, optional): The threshold for determining when to stop adding features. Defaults to 0.01.
140
+ stable_counter (int, optional): The number of stable iterations before stopping. Defaults to 3.
141
+ plot (bool, optional): Whether to plot the accuracy vs. number of features graph. Defaults to True.
142
+ cutoff_method (str, optional): The method for determining when to stop adding features. Defaults to 'increment'.
143
+ train_test_split_params (dict, optional): The parameters for train test split. Defaults to {'test_size':0.25}.
144
+ rf_params (dict, optional): The parameters for the random forest classifier. Defaults to {'min_samples_leaf':50, 'n_estimators':150, 'bootstrap':True, 'oob_score':True, 'n_jobs':-1}.
145
+
146
+ Returns:
147
+ ad.AnnData: The adata object with the feature set added to the .var attribute.
148
+ """
149
+
150
+ if rf_params is None:
151
+ rf_params = { "min_samples_leaf": 50,
152
+ "n_estimators": 150,
153
+ "bootstrap": True,
154
+ "oob_score": True,
155
+ "n_jobs": -1,
156
+ }
157
+ if train_test_split_params is None:
158
+ train_test_split_params = {"test_size": 0.25}
159
+
160
+ if feature_set_name is None:
161
+ feature_set_name = f"{method}_feature_set"
162
+
163
+ # Get the indices of the features in the training feature set
164
+ feature_set_idxs, _ = get_str_idx(training_feature_set, adata.var_names.values)
165
+
166
+ # remove the nan values from the feature set
167
+ # TODO: I need to make this more generalizable because there are other forms of nan in the data
168
+ try:
169
+ phase_nan_idx, _ = get_str_idx("nan", adata.obs[training_labels])
170
+ except KeyError:
171
+ phase_nan_idx = []
172
+
173
+ # isolates the feature set from the adata object
174
+ feature_set = adata.X[:, feature_set_idxs].copy()
175
+
176
+ # removes the nan values from the feature set and labels
177
+ feature_set = np.delete(feature_set, phase_nan_idx, axis=0)
178
+ labels = np.delete(adata.obs[training_labels].values, phase_nan_idx, axis=0)
179
+
180
+ # remove the inf values from the feature set
181
+ feature_set[feature_set == np.inf] = np.nan
182
+ feature_set[feature_set == -np.inf] = np.nan
183
+ nan_data_idx = np.isnan(feature_set).any(axis=1)
184
+
185
+ feature_set = feature_set[~nan_data_idx]
186
+ labels = labels[~nan_data_idx]
187
+
188
+ # train the random forest model on all the features to get the feature importances
189
+ rf_classifier, _ = train_random_forest_model(
190
+ features=feature_set,
191
+ labels=labels,
192
+ rf_params=rf_params,
193
+ train_test_split_params=train_test_split_params,
194
+ verbose=verbose,
195
+ feature_set_description="intial",
196
+ random_state=random_state,
197
+ )
198
+
199
+ # negative to have it sort from highest to lowest
200
+ sorted_idxs = np.argsort(-rf_classifier.feature_importances_)
201
+ sorted_feature_set = np.array(training_feature_set)[sorted_idxs]
202
+ sorted_features = feature_set[:, sorted_idxs]
203
+
204
+ # if the method is RF_min_30, then the optimal feature set is the top 30 features
205
+ # this works with any number of features
206
+ if re.search("(?<=RF_min_)[0-9]+", method):
207
+ optim_feat_num = int(re.search("(?<=RF_min_)[0-9]+", method).group(0))
208
+ optimum_random_forest_feature_set = sorted_feature_set[:optim_feat_num]
209
+
210
+ elif method == "RF_min_max":
211
+
212
+ counter = 0
213
+ max_acc_arg = 0
214
+ acc_list = [0]
215
+ for num_feats in tqdm(
216
+ range(1, sorted_features.shape[1] + 1),
217
+ total=sorted_features.shape[1],
218
+ desc=f"Training RF model iteratively using most important RF features until {stable_counter} stable iterations",
219
+ disable=not verbose,
220
+ ):
221
+
222
+ if counter > stable_counter:
223
+ break
224
+
225
+ # truncates the feature set to the current number of features
226
+ trunc_feature_set = sorted_features[:, :num_feats]
227
+
228
+ # trains the random forest model on the truncated feature set
229
+ _, accuracy = train_random_forest_model(
230
+ features=trunc_feature_set,
231
+ labels=labels,
232
+ rf_params=rf_params,
233
+ train_test_split_params=train_test_split_params,
234
+ random_state=random_state,
235
+ verbose=False,
236
+ )
237
+ acc_list.append(accuracy)
238
+
239
+ # calulate the difference between the current accuracy and the maximum accuracy determined before this iteration
240
+ acc_difference = np.abs(acc_list[max_acc_arg] - accuracy)
241
+
242
+ # if the maximum accuracy is the same as the current accuracy, pass on
243
+ if max_acc_arg == np.argmax(acc_list):
244
+ pass
245
+
246
+ # if the difference between the current accuracy and the maximum accuracy is greater than the threshold
247
+ # and the number of features is greater than 1 (to avoid the 0th index in the accuracy list)
248
+ # elif acc_difference > threshold and num_feats > 1:
249
+ elif acc_difference > threshold:
250
+
251
+ # if the cuttoff method is jump, then if the difference is greater than the threshold,
252
+ # then set the maximum accuracy argument to the current number of features and reset counter
253
+ if cutoff_method == "jump":
254
+ max_acc_arg = np.argmax(acc_list)
255
+ counter = 0
256
+
257
+ # if the cuttoff method is increment, then if the difference is greater than the threshold,
258
+ # then the optimal number of features is increased by 1 and the check occurs again until the condition is not met
259
+ # when the acc_diccerece is less than the threshold, the counter also decreased by 1
260
+ elif cutoff_method == "increment":
261
+
262
+ temp_max_acc_arg, temp_counter = max_acc_arg, counter
263
+ for _ in range(stable_counter):
264
+ temp_max_acc_arg, temp_counter, continue_bool = (
265
+ __random_forest_increment_counter(
266
+ acc_list,
267
+ temp_max_acc_arg,
268
+ temp_counter,
269
+ stable_counter,
270
+ threshold,
271
+ )
272
+ )
273
+
274
+ if not continue_bool:
275
+ break
276
+
277
+ if (temp_max_acc_arg, temp_counter) != (max_acc_arg, counter):
278
+ max_acc_arg = temp_max_acc_arg
279
+ counter = temp_counter
280
+
281
+ counter += 1
282
+
283
+ acc_list = np.array(acc_list)
284
+ optim_feat_num = max_acc_arg
285
+
286
+ optimum_random_forest_feature_set = sorted_feature_set[:optim_feat_num]
287
+
288
+ optim_feature_set = sorted_features[:, :optim_feat_num]
289
+
290
+ # train the random forest model on the optimal feature set for output performance metrics
291
+ _ = train_random_forest_model(
292
+ features=optim_feature_set,
293
+ labels=labels,
294
+ rf_params=rf_params,
295
+ train_test_split_params=train_test_split_params,
296
+ verbose=verbose,
297
+ feature_set_description="optimal",
298
+ random_state=random_state,
299
+ )
300
+
301
+ if verbose:
302
+ print()
303
+ print(
304
+ "##################################################################################"
305
+ )
306
+ print()
307
+
308
+ print("Optimal Feature Set sorted by RF feature importance")
309
+ print("###################################################")
310
+ print(optimum_random_forest_feature_set)
311
+
312
+ # converts the optimal feature set to a boolean array
313
+ feat_idxs, _ = get_str_idx(optimum_random_forest_feature_set, adata.var_names.values)
314
+ fs_bool = np.repeat(False, adata.shape[1])
315
+ fs_bool[feat_idxs] = True
316
+
317
+ adata.var[feature_set_name] = fs_bool
318
+
319
+ if plot and method == "RF_min_max":
320
+ plt.figure(figsize=(10, 5))
321
+
322
+ x_axis = np.arange(len(acc_list), dtype=int)
323
+ plt.plot(x_axis, acc_list)
324
+ plt.axvline(
325
+ optim_feat_num,
326
+ color="r",
327
+ linestyle="--",
328
+ label=f"Optimal Feature Set Size: {optim_feat_num}",
329
+ )
330
+ plt.title(
331
+ f"Stable Counter {stable_counter} - Stable Threshold {threshold*100}% - Cutoff Method: {cutoff_method}"
332
+ )
333
+ plt.xticks(x_axis)
334
+
335
+ percentages = np.arange(0, 110, 10, dtype=int).tolist()
336
+ rounded_percentages = [f"{elem} %" for elem in percentages]
337
+ plt.grid(visible=True, alpha=0.5, linestyle="--", which="both")
338
+ plt.yticks(np.arange(0, 1.1, 0.1), rounded_percentages)
339
+
340
+ xtick_labels = np.insert(sorted_feature_set[: len(acc_list) - 1], 0, "")
341
+ plt.xticks(
342
+ np.arange(0, len(acc_list)),
343
+ xtick_labels,
344
+ rotation=45,
345
+ ha="right",
346
+ fontsize=8,
347
+ )
348
+ plt.ylabel("Accuracy")
349
+ plt.xlim(0, len(acc_list) - 1)
350
+ plt.ylim(0, 1.05)
351
+ plt.legend(loc="lower right")
352
+ plt.tight_layout()
353
+
354
+ if save_path is not None:
355
+ plt.savefig(save_path)
356
+
357
+ plt.close()
358
+
359
+ return adata
360
+
cc_mapping/manifold.py ADDED
@@ -0,0 +1,314 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+
5
+ import anndata as ad
6
+ import numpy as np
7
+ import pandas as pd
8
+ import phate
9
+
10
+ np.seterr(all="ignore")
11
+
12
+ import matplotlib as mpl
13
+ import matplotlib.pyplot as plt
14
+ from tqdm import tqdm
15
+
16
+ from .plot import combine_Lof_plots, general_plotting_function, get_legend
17
+
18
+
19
+ def run_phate(
20
+ adata: ad.AnnData,
21
+ feature_set: str,
22
+ layer: str,
23
+ phate_param_dict: dict = None,
24
+ obsm_save_key: str = "X_phate",
25
+ hyperparam: bool = False,
26
+ ):
27
+ """
28
+ Run the PHATE algorithm on the specified data.
29
+
30
+ Parameters:
31
+ adata (ad.AnnData): Annotated data object.
32
+ feature_set (str): Name of the feature set to use.
33
+ layer (str): Name of the layer to use.
34
+ phate_param_dict (dict, optional): Dictionary of PHATE algorithm parameters. Defaults to an empty dictionary.
35
+ obsm_save_key (str, optional): Key to save the PHATE coordinates in `adata.obsm`. Defaults to 'X_phate'.
36
+ hyperparam (bool, optional): If True, only return the PHATE coordinates. Defaults to False.
37
+
38
+ Returns:
39
+ ad.AnnData or np.ndarray: Annotated data object with PHATE coordinates in `adata.obsm[obsm_save_key]` if `hyperparam` is False,
40
+ otherwise returns the PHATE coordinates as a numpy array.
41
+ """
42
+ if phate_param_dict is None:
43
+ phate_param_dict = {}
44
+
45
+ feature_set_bool = adata.var[feature_set].values
46
+ data = adata.layers[layer][:, feature_set_bool].copy()
47
+
48
+ phate_operator = phate.PHATE(**phate_param_dict)
49
+ phate_coords = phate_operator.fit_transform(
50
+ data
51
+ ) # Reduce the dimensions of the data using the PHATE algorithm
52
+
53
+ if hyperparam:
54
+ return phate_coords
55
+
56
+ adata.obsm[obsm_save_key] = phate_coords
57
+
58
+ return adata
59
+
60
+
61
+ def plot_phate_coords(
62
+ adata: ad.AnnData = None,
63
+ colors: np.ndarray | list = None,
64
+ phate_coords: np.ndarray = None,
65
+ kwargs: dict = None,
66
+ axe: mpl.axes = None,
67
+ hyperparam: bool = False,
68
+ unit_size: int = 5,
69
+ obsm_embedding: str = "X_phate",
70
+ blank: bool = False,
71
+ return_fig: bool = False,
72
+ ):
73
+ """
74
+ Plot PHATE coordinates.
75
+
76
+ Parameters:
77
+ adata (ad.AnnData): Annotated data object.
78
+ colors (Union[np.ndarray,list]): Array-like object containing colors for each data point.
79
+ phate_coords (np.ndarray): Array-like object containing PHATE coordinates.
80
+ kwargs (dict): Additional keyword arguments for scatter plot.
81
+ axe (mpl.axes): Matplotlib axes object to plot on.
82
+ hyperparam (bool): Flag indicating whether to use hyperparameter values for color mapping.
83
+ obsm_embedding (str): Key for accessing the PHATE coordinates in `adata.obsm`.
84
+ return_fig (bool): Flag indicating whether to return the figure and axes objects.
85
+
86
+ Returns:
87
+ mpl.figure.Figure, mpl.axes.Axes or mpl.axes.Axes: If `return_fig` is True, returns the figure and axes objects. Otherwise, returns the axes object.
88
+ """
89
+
90
+ if not isinstance(kwargs, dict):
91
+ raise ValueError("kwargs is not a dict")
92
+
93
+ if kwargs is None:
94
+ kwargs = {}
95
+
96
+ if not axe:
97
+ fig, axe = plt.subplots(1, 1, figsize=(unit_size, unit_size))
98
+
99
+ if blank:
100
+ axe.axis("off")
101
+ axe.set_yticks([])
102
+ axe.set_xticks([])
103
+ return axe
104
+
105
+ if not hyperparam:
106
+ phate_coords = adata.obsm[obsm_embedding]
107
+
108
+ colors = adata.obs_vector(colors)
109
+
110
+ if colors.dtype != "object" and not isinstance(
111
+ colors, pd.core.arrays.categorical.Categorical
112
+ ):
113
+ vmin = np.percentile(colors, 1)
114
+ vmax = np.percentile(colors, 99)
115
+ kwargs.update(
116
+ {
117
+ "vmin": vmin,
118
+ "vmax": vmax,
119
+ "cmap": "rainbow",
120
+ }
121
+ )
122
+
123
+ axe.scatter(phate_coords[:, 0], phate_coords[:, 1], c=colors, **kwargs)
124
+
125
+ axe.axis("off")
126
+ axe.set_yticks([])
127
+ axe.set_xticks([])
128
+
129
+ if return_fig:
130
+ return fig, axe
131
+
132
+ return axe
133
+
134
+
135
+ def phate_hyperparameter_search_plotting_function(axe, idx_dict, plotting_dict):
136
+ """
137
+ Plot PHATE coordinates for hyperparameter search.
138
+
139
+ Args:
140
+ axe (matplotlib.axes.Axes): The axes object to plot on.
141
+ idx_dict (dict): Dictionary containing the row and column indices.
142
+ plotting_dict (dict): Dictionary containing the plotting parameters.
143
+
144
+ Returns:
145
+ matplotlib.axes.Axes: The updated axes object.
146
+
147
+ """
148
+ col_idx = idx_dict["col_idx"] - 1
149
+ row_idx = idx_dict["row_idx"] - 1
150
+
151
+ adata = plotting_dict["adata"]
152
+ feature_set = plotting_dict["feature_set"]
153
+ color_name = plotting_dict["color_name"]
154
+ layer = plotting_dict["layer"]
155
+ unit_size = plotting_dict["unit_size"]
156
+ kwargs = plotting_dict["kwargs"]
157
+
158
+ hyperparam_dict = plotting_dict["hyperparam_dict"].copy()
159
+ hyperparam_info_dict = plotting_dict["hyperparam_info_dict"].copy()
160
+
161
+ row_param_name = hyperparam_info_dict["row_label"]
162
+ col_param_name = hyperparam_info_dict["col_label"]
163
+
164
+ row_param_list = hyperparam_dict[row_param_name]
165
+ col_param_list = hyperparam_dict[col_param_name]
166
+
167
+ hyperparam_dict[row_param_name] = row_param_list[row_idx]
168
+ hyperparam_dict[col_param_name] = col_param_list[col_idx]
169
+
170
+ phate_coords = run_phate(
171
+ adata, feature_set, layer, hyperparam_dict, hyperparam=True
172
+ )
173
+
174
+ # colors = adata.obs[color_name].values
175
+ axe = plot_phate_coords(
176
+ adata=adata,
177
+ phate_coords=phate_coords,
178
+ colors=color_name,
179
+ kwargs=kwargs,
180
+ axe=axe,
181
+ hyperparam=True,
182
+ )
183
+
184
+ return axe
185
+
186
+
187
+ def perform_phate_hyperparameter_search(
188
+ adata: ad.AnnData,
189
+ feature_set: str,
190
+ hyperparam_dict: dict,
191
+ hyperparam_info_dict: dict,
192
+ additional_plotting_dict_params: dict = None,
193
+ layer: str = None,
194
+ plotting_function: callable = None,
195
+ color_name: list = None,
196
+ save_path: str = None,
197
+ legend: bool = False,
198
+ unit_size: int = 10,
199
+ kwargs: dict = None,
200
+ ):
201
+ """
202
+ Perform hyperparameter search for PHATE visualization.
203
+
204
+ Parameters:
205
+ adata (ad.AnnData): Annotated data object.
206
+ feature_set (str): Name of the feature set.
207
+ layer (str): Name of the layer.
208
+ hyperparam_dict (dict): Dictionary of hyperparameters.
209
+ hyperparam_info_dict (dict): Dictionary of hyperparameter information.
210
+ color_name (list, optional): List of color names. Defaults to None.
211
+ save_path (str, optional): Path to save the figure. Defaults to None.
212
+ legend (bool, optional): Whether to include a legend. Defaults to False.
213
+ unit_size (int, optional): Size of the units. Defaults to 10.
214
+ kwargs (dict, optional): Additional keyword arguments. Defaults to {}.
215
+
216
+ Returns:
217
+ matplotlib.figure.Figure: Combined figure of hyperparameter search plots.
218
+ """
219
+
220
+ if kwargs is None:
221
+ kwargs = {}
222
+
223
+ if save_path is not None:
224
+ save_dir = os.path.dirname(save_path)
225
+ if not os.path.exists(save_dir):
226
+ raise ValueError(f"{save_dir} does not exist")
227
+
228
+ if not plotting_function:
229
+ plotting_function = phate_hyperparameter_search_plotting_function
230
+
231
+ constant_param_name = hyperparam_info_dict["constant_label"]
232
+
233
+ backend = mpl.get_backend()
234
+ mpl.use("agg")
235
+
236
+ final_figure_dims = hyperparam_info_dict["final_figure_dims"]
237
+ number_param_plots = len(hyperparam_dict[constant_param_name])
238
+ final_num_rows = final_figure_dims[0]
239
+ final_num_cols = final_figure_dims[1]
240
+
241
+ total_num_plots = final_num_rows * final_num_cols
242
+
243
+ if total_num_plots < number_param_plots:
244
+ raise ValueError(
245
+ f"Number of plots ({number_param_plots}) exceeds the number of subplots ({total_num_plots})"
246
+ )
247
+
248
+ figure_list = []
249
+ for idx in tqdm(
250
+ range(total_num_plots),
251
+ total=number_param_plots,
252
+ desc="Generating hyperparameter search plots",
253
+ ):
254
+ plotting_dict = {
255
+ "adata": adata,
256
+ "feature_set": feature_set,
257
+ "color_name": color_name,
258
+ "layer": layer,
259
+ "unit_size": unit_size,
260
+ "kwargs": kwargs,
261
+ }
262
+
263
+ if additional_plotting_dict_params:
264
+ plotting_dict.update(additional_plotting_dict_params)
265
+
266
+ # -1 is needed for correct indexing
267
+ if idx <= number_param_plots - 1:
268
+ temp_hyperparam_dict = hyperparam_dict.copy()
269
+ temp_hyperparam_info_dict = hyperparam_info_dict.copy()
270
+ temp_hyperparam_dict[constant_param_name] = hyperparam_dict[
271
+ constant_param_name
272
+ ][idx]
273
+ temp_hyperparam_info_dict["param_dict"] = temp_hyperparam_dict
274
+
275
+ plotting_dict["hyperparam_dict"] = temp_hyperparam_dict
276
+ plotting_dict["hyperparam_info_dict"] = temp_hyperparam_info_dict
277
+
278
+ fig = general_plotting_function(
279
+ plotting_function,
280
+ temp_hyperparam_info_dict,
281
+ plotting_dict,
282
+ hyperparam_search=True,
283
+ unit_size=unit_size,
284
+ )
285
+
286
+ else:
287
+ # generate a blank plot to fill in the empty space
288
+ fig = general_plotting_function(
289
+ plotting_function,
290
+ temp_hyperparam_info_dict,
291
+ plotting_dict,
292
+ blank=True,
293
+ hyperparam_search=True,
294
+ )
295
+
296
+ figure_list.append(fig)
297
+
298
+ combined_fig = combine_Lof_plots(figure_list, final_figure_dims)
299
+
300
+ if color_name is not None:
301
+ color_vector = adata.obs_vector(color_name)
302
+
303
+ if (
304
+ color_vector.dtype == "object" or isinstance(color_vector, pd.Categorical)
305
+ ) and legend:
306
+ patches, colors = get_legend(adata, color_name)
307
+ plt.legend(handles=patches, fontsize=unit_size)
308
+
309
+ if save_path is not None:
310
+ plt.savefig(save_path, dpi=300, bbox_inches="tight")
311
+
312
+ mpl.use(backend)
313
+
314
+ return combined_fig