changeset 319:a07405bcd3f3 draft

Uploaded
author luca_milaz
date Mon, 05 Aug 2024 12:54:17 +0000
parents 75454fbbb893
children fbda3a0cc8b1
files marea_2/flux_to_map.py
diffstat 1 files changed, 23 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2/flux_to_map.py	Mon Aug 05 12:47:40 2024 +0000
+++ b/marea_2/flux_to_map.py	Mon Aug 05 12:54:17 2024 +0000
@@ -844,7 +844,9 @@
     rgb = (np.array(rgb) * 255).astype(int)
     return '#{:02x}{:02x}{:02x}'.format(rgb[0], rgb[1], rgb[2])
 
-def save_colormap_image(min_value: float, max_value: float, path: utils.FilePath):
+
+
+def save_colormap_image(min_value: float, max_value: float, path: utils.FilePath, colorMap:str="jet"):
     """
     Create and save an image of the colormap showing the gradient and its range.
 
@@ -855,7 +857,7 @@
     """
 
     # Create a colormap using matplotlib
-    cmap = plt.get_cmap("jet")
+    cmap = plt.get_cmap(colorMap)
 
     # Create a figure and axis
     fig, ax = plt.subplots(figsize=(6, 1))
@@ -866,8 +868,8 @@
     gradient = np.vstack((gradient, gradient))
 
     # Add min and max value annotations
-    ax.text(0, 0.5, f'{min_value}', va='center', ha='right', transform=ax.transAxes, fontsize=12, color='black')
-    ax.text(1, 0.5, f'{max_value}', va='center', ha='left', transform=ax.transAxes, fontsize=12, color='black')
+    ax.text(0, 0.5, f'{np.round(min_value, 3)}', va='center', ha='right', transform=ax.transAxes, fontsize=12, color='black')
+    ax.text(1, 0.5, f'{np.round(min_value, 3)}', va='center', ha='left', transform=ax.transAxes, fontsize=12, color='black')
 
 
     # Display the gradient image
@@ -879,10 +881,15 @@
     plt.close()
     pass
 
-def computeEnrichmentMeanMedian(metabMap: ET.ElementTree, class_pat: Dict[str, List[List[float]]], ids: List[str]) -> None:
+def min_nonzero_abs(arr):
+    # Flatten the array and filter out zeros, then find the minimum of the remaining values
+    non_zero_elements = np.abs(arr)[np.abs(arr) > 0]
+    return np.min(non_zero_elements) if non_zero_elements.size > 0 else None
+
+def computeEnrichmentMeanMedian(metabMap: ET.ElementTree, class_pat: Dict[str, List[List[float]]], ids: List[str], colormap:str) -> None:
     """
     Compute and visualize the metabolic map based on mean and median of the input fluxes.
-    The fluxes are normalised across classes/datasets and visualised using the jet colormap.
+    The fluxes are normalised across classes/datasets and visualised using the given colormap.
 
     Args:
         metabMap (ET.ElementTree): An XML tree representing the metabolic map.
@@ -904,16 +911,16 @@
     max_flux_medians = max(np.max(np.abs(arr)) for arr in medians.values())
     max_flux_means = max(np.max(np.abs(arr)) for arr in means.values())
 
-    min_flux_medians = min(np.min(np.abs(arr)) for arr in medians.values())
-    min_flux_means = min(np.min(np.abs(arr)) for arr in means.values())
+    min_flux_medians = min(min_nonzero_abs(arr) for arr in medians.values())
+    min_flux_means = min(min_nonzero_abs(arr) for arr in means.values())
 
     medians = {key: median/max_flux_medians for key, median in medians.items()}
     means = {key: mean/max_flux_means for key, mean in means.items()}
 
-    save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix="result"))
-    save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix="result"))
+    save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix="result"), colormap)
+    save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix="result"), colormap)
 
-    cmap = plt.get_cmap("jet")
+    cmap = plt.get_cmap(colormap)
 
     for key in class_pat:
         # Create color mappings for median and mean
@@ -999,14 +1006,16 @@
 
     ids, class_pat = getClassesAndIdsFromDatasets(ARGS.input_datas_fluxes, ARGS.input_data_fluxes, ARGS.input_class_fluxes, ARGS.names_fluxes)
 
+    ARGS.colormap="jet" #to add to user interface, choice amongst matplotlib colormaps
+
     if(ARGS.choice_map == utils.Model.HMRcore):
         temp_map = utils.Model.HMRcore_no_legend
-        computeEnrichmentMeanMedian(temp_map.getMap(ARGS.tool_dir), class_pat, ids)
+        computeEnrichmentMeanMedian(temp_map.getMap(ARGS.tool_dir), class_pat, ids, ARGS.colormap)
     elif(ARGS.choice_map == utils.Model.ENGRO2):
         temp_map = utils.Model.ENGRO2_no_legend
-        computeEnrichmentMeanMedian(temp_map.getMap(ARGS.tool_dir), class_pat, ids)
+        computeEnrichmentMeanMedian(temp_map.getMap(ARGS.tool_dir), class_pat, ids, ARGS.colormap)
     else:
-        computeEnrichmentMeanMedian(core_map, class_pat, ids)
+        computeEnrichmentMeanMedian(core_map, class_pat, ids, ARGS.colormap)
     
 
     computeEnrichment(core_map, class_pat, ids)