changeset 282:ebc6bb8526c9 draft

Uploaded
author luca_milaz
date Sun, 04 Aug 2024 18:16:37 +0000
parents d0a2043eb4d9
children 33e499061029
files marea_2/flux_to_map.py
diffstat 1 files changed, 38 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/marea_2/flux_to_map.py	Sun Aug 04 18:09:33 2024 +0000
+++ b/marea_2/flux_to_map.py	Sun Aug 04 18:16:37 2024 +0000
@@ -834,61 +834,55 @@
     return '#{:02x}{:02x}{:02x}'.format(rgb[0], rgb[1], rgb[2])
     
 
-def computeEnrichmentMeanMedian(metabMap :ET.ElementTree, class_pat :Dict[str, List[List[float]]], ids :List[str]) -> None:
-
+def computeEnrichmentMeanMedian(metabMap: ET.ElementTree, class_pat: Dict[str, List[List[float]]], ids: List[str]) -> None:
+    # Create copies only if they are needed
     metabMap_mean = copy.deepcopy(metabMap)
     metabMap_median = copy.deepcopy(metabMap)
-    
-    medians = {} # dict, for each class an array (the median)
-    means = {}
-    for key, value in class_pat.items():
-        values = np.array(class_pat[key])
-        median = np.median(values, axis=1)
-        mean = np.mean(values, axis=1)
-        medians[key] = median
-        means[key] = mean
 
+    # Compute medians and means
+    medians = {key: np.median(np.array(value), axis=1) for key, value in class_pat.items()}
+    means = {key: np.mean(np.array(value), axis=1) for key, value in class_pat.items()}
+
+    # Normalize medians and means
     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())
 
-    for key, value in medians.items():
-        medians[key] = medians[key] / max_flux_medians
-    
-    for key, value in means.items():
-        means[key] = means[key] / max_flux_means
-
-    for key, value in class_pat.items():
+    medians = {key: median / max_flux_medians for key, median in medians.items()}
+    means = {key: mean / max_flux_means for key, mean in means.items()}
 
-        colors_median={}
-        i=0
-        for rxn_id in ids:
-             colors_median[rxn_id] = rgb_to_hex(jet_colormap(medians[key][i]))
-             i+=1
-
-        colors_mean={}
-        i=0
-        for rxn_id in ids:
-             colors_mean[rxn_id] = rgb_to_hex(jet_colormap(means[key][i]))
-             i+=1
+    for key in class_pat:
+        # Create color mappings for median and mean
+        colors_median = {rxn_id: rgb_to_hex(jet_colormap(medians[key][i])) for i, rxn_id in enumerate(ids)}
+        colors_mean = {rxn_id: rgb_to_hex(jet_colormap(means[key][i])) for i, rxn_id in enumerate(ids)}
 
         for rxn_id in ids:
-            arrow = Arrow(width=5, col=colors_median[rxn_id])
-            arrow.styleReactionElements(metabMap_median, rxn_id, mindReactionDir=False)
-            idOpt1, idOpt2 = getArrowHeadElementId(rxn_id)
-            arrow.applyTo(idOpt1, metabMap_median, arrow.toStyleStr(downSizedForTips = True))
-            if idOpt2: arrow.applyTo(idOpt2, metabMap_median, arrow.toStyleStr(downSizedForTips = True))
+            # Apply median arrows
+            apply_arrow(metabMap_median, rxn_id, colors_median[rxn_id])
+
+            # Apply mean arrows
+            apply_arrow(metabMap_mean, rxn_id, colors_mean[rxn_id])
+
+        # Save and convert the SVG files
+        save_and_convert(metabMap_mean, "mean", key)
+        save_and_convert(metabMap_median, "median", key)
 
-            arrow = Arrow(width=5, col=colors_mean[rxn_id])
-            arrow.styleReactionElements(metabMap_mean, rxn_id, mindReactionDir=False)
-            idOpt1, idOpt2 = getArrowHeadElementId(rxn_id)
-            arrow.applyTo(idOpt1, metabMap_mean, arrow.toStyleStr(downSizedForTips = True))
-            if idOpt2: arrow.applyTo(idOpt2, metabMap_mean, arrow.toStyleStr(downSizedForTips = True))
+def apply_arrow(metabMap, rxn_id, color):
+    arrow = Arrow(width=5, col=color)
+    arrow.styleReactionElements(metabMap, rxn_id, mindReactionDir=False)
+    idOpt1, idOpt2 = getArrowHeadElementId(rxn_id)
+    arrow.applyTo(idOpt1, metabMap, arrow.toStyleStr(downSizedForTips=True))
+    if idOpt2:
+        arrow.applyTo(idOpt2, metabMap, arrow.toStyleStr(downSizedForTips=True))
 
-        svgFilePath = utils.FilePath("SVG Map mean - " + str(key), ext = utils.FileFormat.SVG, prefix="result")
-        utils.writeSvg(svgFilePath, metabMap_mean)
-
-        svgFilePath = utils.FilePath("SVG Map median - " + str(key), ext = utils.FileFormat.SVG, prefix="result")
-        utils.writeSvg(svgFilePath, metabMap_median)
+def save_and_convert(metabMap, map_type, key):
+    svgFilePath = utils.FilePath(f"SVG Map {map_type} - {key}", ext=utils.FileFormat.SVG, prefix="result")
+    utils.writeSvg(svgFilePath, metabMap)
+    if ARGS.generate_pdf:
+        pngPath = utils.FilePath(f"PNG Map {map_type} - {key}", ext=utils.FileFormat.PNG, prefix="result")
+        pdfPath = utils.FilePath(f"PDF Map {map_type} - {key}", ext=utils.FileFormat.PDF, prefix="result")
+        convert_to_pdf(svgFilePath, pngPath, pdfPath)
+    if not ARGS.generate_svg:
+        os.remove(svgFilePath.show())
 
     
 ############################ MAIN #############################################