Mercurial > repos > bimib > marea_2
changeset 313:376afe557f90 draft
Uploaded
author | luca_milaz |
---|---|
date | Mon, 05 Aug 2024 12:23:58 +0000 |
parents | ecfd72888221 |
children | e8c20e0b4f27 |
files | marea_2/flux_to_map.py |
diffstat | 1 files changed, 26 insertions(+), 61 deletions(-) [+] |
line wrap: on
line diff
--- a/marea_2/flux_to_map.py Mon Aug 05 12:17:15 2024 +0000 +++ b/marea_2/flux_to_map.py Mon Aug 05 12:23:58 2024 +0000 @@ -831,31 +831,6 @@ dataset = dataset.drop(dataset.columns[0], axis = "columns").to_dict("list") return { id : list(map(utils.Float("Dataset values, not an argument"), values)) for id, values in dataset.items() }, IDs -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: - numpy.ndarray: An array of RGB color components (in the range [0, 1]). - """ - value = abs(value) - - if(np.round(value, 6) == 0.0): - return np.array([0.745, 0.745, 0.745]) #grey - - # 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) - - # Combine the color components into an RGB array - rgb = np.array([red, green, blue]) - - return rgb - def rgb_to_hex(rgb): """ Convert RGB values (0-1 range) to hexadecimal color format. @@ -870,9 +845,7 @@ rgb = (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:str): +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. @@ -881,28 +854,27 @@ 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_arr = jet_colormap((value - min_value) / (max_value - min_value)) - color = tuple(int(c * 255) for c in color_arr) - 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='white', font=font) - draw.text((width - 50, height - 20), f'{max_value:.2f}', fill='white', font=font) - + # Generate a range of values + values = np.linspace(min_value, max_value, 256) + + # Create a colormap using seaborn + cmap = sns.color_palette("jet", as_cmap=True) + + # Create a figure and axis + fig, ax = plt.subplots(figsize=(6, 1)) + fig.subplots_adjust(bottom=0.5) + + # Create a gradient image + gradient = np.linspace(0, 1, 256) + gradient = np.vstack((gradient, gradient)) + + # Display the gradient image + ax.imshow(gradient, aspect='auto', cmap=cmap) + ax.set_axis_off() + # Save the image - image.save(path.show()) - pass - + plt.savefig(path, bbox_inches='tight', pad_inches=0) + plt.close() def computeEnrichmentMeanMedian(metabMap: ET.ElementTree, class_pat: Dict[str, List[List[float]]], ids: List[str]) -> None: """ @@ -935,20 +907,16 @@ 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, "colormap_median.png") + save_colormap_image(min_flux_means, max_flux_means, "colormap_mean.png") 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)} + colors_median = {rxn_id: rgb_to_hex(sns.color_palette("jet", as_cmap=True)((medians[key][i] + 1) / 2)) for i, rxn_id in enumerate(ids)} + colors_mean = {rxn_id: rgb_to_hex(sns.color_palette("jet", as_cmap=True)((means[key][i] + 1) / 2)) for i, rxn_id in enumerate(ids)} for i, rxn_id in enumerate(ids): - - isNegative = False - - if(medians[key][i] <0): - isNegative = True + isNegative = medians[key][i] < 0 # Apply median arrows apply_arrow(metabMap_median, rxn_id, colors_median[rxn_id], isNegative) @@ -960,9 +928,6 @@ save_and_convert(metabMap_mean, "mean", key) save_and_convert(metabMap_median, "median", key) - - - def apply_arrow(metabMap, rxn_id, color, isNegative): """ Apply an arrow to a specific reaction in the metabolic map with a given color.