# HG changeset patch # User luca_milaz # Date 1722798763 0 # Node ID e4d02f9b41a1c1821e16b59f2be007a35c48aa0e # Parent b501ac3585a1b414361ce7539c750d4d998e7818 Uploaded diff -r b501ac3585a1 -r e4d02f9b41a1 marea_2/flux_to_map.py --- a/marea_2/flux_to_map.py Sun Aug 04 19:00:56 2024 +0000 +++ b/marea_2/flux_to_map.py Sun Aug 04 19:12:43 2024 +0000 @@ -851,6 +851,57 @@ # Convert RGB values (0-1 range) to hexadecimal format rgb = (rgb * 255).astype(int) return '#{:02x}{:02x}{:02x}'.format(rgb[0], rgb[1], rgb[2]) + +def jet_colormap(value): + """ + Generate an RGB color based on a single numeric value using a colormap similar to MATLAB's 'jet'. + + Args: + value (float): A numeric value to be mapped to a color. + + Returns: + tuple: An (R, G, B) tuple with color components in the range [0, 255]. + """ + # Normalize value to range [0, 1] + value = np.clip(value, 0, 1) + + # Define the red, green, and blue color components + red = np.clip(1.5 - np.abs(4 * (value - 0.75)), 0, 1) + green = np.clip(1.5 - np.abs(4 * (value - 0.5)), 0, 1) + blue = np.clip(1.5 - np.abs(4 * (value - 0.25)), 0, 1) + + # Convert to 0-255 range + return int(red * 255), int(green * 255), int(blue * 255) + +def save_colormap_image(min_value:float, max_value:float, path:str): + """ + Create and save an image of the colormap showing the gradient and its range. + + Args: + min_value (float): The minimum value of the colormap range. + max_value (float): The maximum value of the colormap range. + filename (str): The filename for saving the image. + """ + # Define image size and create a new image + width = 256 + height = 50 + image = Image.new('RGB', (width, height), color='white') + draw = ImageDraw.Draw(image) + + # Create the gradient + for x in range(width): + value = (x / (width - 1)) * (max_value - min_value) + min_value + color = jet_colormap((value - min_value) / (max_value - min_value)) + draw.line([(x, 0), (x, height)], fill=color) + + # Add text for min and max values + font = ImageFont.load_default() + draw.text((10, height - 20), f'{min_value:.2f}', fill='black', font=font) + draw.text((width - 50, height - 20), f'{max_value:.2f}', fill='black', font=font) + + # Save the image + image.save(path.show()) + pass def computeEnrichmentMeanMedian(metabMap: ET.ElementTree, class_pat: Dict[str, List[List[float]]], ids: List[str]) -> None: @@ -878,9 +929,15 @@ 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()) + 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")) + 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)} @@ -937,6 +994,8 @@ if not ARGS.generate_svg: os.remove(svgFilePath.show()) + + ############################ MAIN ############################################# def main() -> None: