Mercurial > repos > bimib > cobraxy
comparison COBRAxy/flux_to_map.py @ 147:3fca9b568faf draft
Uploaded
| author | bimib |
|---|---|
| date | Wed, 06 Nov 2024 13:57:24 +0000 |
| parents | 41f35c2f0c7b |
| children | 5683406a8cfd |
comparison
equal
deleted
inserted
replaced
| 146:88cf4543e210 | 147:3fca9b568faf |
|---|---|
| 20 import matplotlib.pyplot as plt | 20 import matplotlib.pyplot as plt |
| 21 | 21 |
| 22 ERRORS = [] | 22 ERRORS = [] |
| 23 ########################## argparse ########################################## | 23 ########################## argparse ########################################## |
| 24 ARGS :argparse.Namespace | 24 ARGS :argparse.Namespace |
| 25 def process_args() -> argparse.Namespace: | 25 def process_args(args:List[str] = None) -> argparse.Namespace: |
| 26 """ | 26 """ |
| 27 Interfaces the script of a module with its frontend, making the user's choices for various parameters available as values in code. | 27 Interfaces the script of a module with its frontend, making the user's choices for various parameters available as values in code. |
| 28 | 28 |
| 29 Args: | 29 Args: |
| 30 args : Always obtained (in file) from sys.argv | 30 args : Always obtained (in file) from sys.argv |
| 117 | 117 |
| 118 parser.add_argument( | 118 parser.add_argument( |
| 119 '-colorm', '--color_map', | 119 '-colorm', '--color_map', |
| 120 type = str, | 120 type = str, |
| 121 choices = ["jet", "viridis"]) | 121 choices = ["jet", "viridis"]) |
| 122 | 122 |
| 123 args :argparse.Namespace = parser.parse_args() | 123 parser.add_argument( |
| 124 '-idop', '--output_path', | |
| 125 type = str, | |
| 126 default='result', | |
| 127 help = 'output path for maps') | |
| 128 | |
| 129 args :argparse.Namespace = parser.parse_args(args) | |
| 124 args.net = True | 130 args.net = True |
| 125 | 131 |
| 126 return args | 132 return args |
| 127 | 133 |
| 128 ############################ dataset input #################################### | 134 ############################ dataset input #################################### |
| 641 f"{dataset1Name}_vs_{dataset2Name}" + (f" ({details})" if details else ""), | 647 f"{dataset1Name}_vs_{dataset2Name}" + (f" ({details})" if details else ""), |
| 642 # ^^^ yes this string is built every time even if the form is the same for the same 2 datasets in | 648 # ^^^ yes this string is built every time even if the form is the same for the same 2 datasets in |
| 643 # all output files: I don't care, this was never the performance bottleneck of the tool and | 649 # all output files: I don't care, this was never the performance bottleneck of the tool and |
| 644 # there is no other net gain in saving and re-using the built string. | 650 # there is no other net gain in saving and re-using the built string. |
| 645 ext, | 651 ext, |
| 646 prefix = "result") | 652 prefix = ARGS.output_path) |
| 647 | 653 |
| 648 FIELD_NOT_AVAILABLE = '/' | 654 FIELD_NOT_AVAILABLE = '/' |
| 649 def writeToCsv(rows: List[list], fieldNames :List[str], outPath :utils.FilePath) -> None: | 655 def writeToCsv(rows: List[list], fieldNames :List[str], outPath :utils.FilePath) -> None: |
| 650 fieldsAmt = len(fieldNames) | 656 fieldsAmt = len(fieldNames) |
| 651 with open(outPath.show(), "w", newline = "") as fd: | 657 with open(outPath.show(), "w", newline = "") as fd: |
| 920 min_flux_means = min(min_nonzero_abs(arr) for arr in means.values()) | 926 min_flux_means = min(min_nonzero_abs(arr) for arr in means.values()) |
| 921 | 927 |
| 922 medians = {key: median/max_flux_medians for key, median in medians.items()} | 928 medians = {key: median/max_flux_medians for key, median in medians.items()} |
| 923 means = {key: mean/max_flux_means for key, mean in means.items()} | 929 means = {key: mean/max_flux_means for key, mean in means.items()} |
| 924 | 930 |
| 925 save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix="result"), colormap) | 931 save_colormap_image(min_flux_medians, max_flux_medians, utils.FilePath("Color map median", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) |
| 926 save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix="result"), colormap) | 932 save_colormap_image(min_flux_means, max_flux_means, utils.FilePath("Color map mean", ext=utils.FileFormat.PNG, prefix=ARGS.output_path), colormap) |
| 927 | 933 |
| 928 cmap = plt.get_cmap(colormap) | 934 cmap = plt.get_cmap(colormap) |
| 929 | 935 |
| 930 for key in class_pat: | 936 for key in class_pat: |
| 931 # Create color mappings for median and mean | 937 # Create color mappings for median and mean |
| 979 key (str): The key identifying the specific map. | 985 key (str): The key identifying the specific map. |
| 980 | 986 |
| 981 Returns: | 987 Returns: |
| 982 None | 988 None |
| 983 """ | 989 """ |
| 984 svgFilePath = utils.FilePath(f"SVG Map {map_type} - {key}", ext=utils.FileFormat.SVG, prefix="result") | 990 svgFilePath = utils.FilePath(f"SVG Map {map_type} - {key}", ext=utils.FileFormat.SVG, prefix=ARGS.output_path) |
| 985 utils.writeSvg(svgFilePath, metabMap) | 991 utils.writeSvg(svgFilePath, metabMap) |
| 986 if ARGS.generate_pdf: | 992 if ARGS.generate_pdf: |
| 987 pngPath = utils.FilePath(f"PNG Map {map_type} - {key}", ext=utils.FileFormat.PNG, prefix="result") | 993 pngPath = utils.FilePath(f"PNG Map {map_type} - {key}", ext=utils.FileFormat.PNG, prefix=ARGS.output_path) |
| 988 pdfPath = utils.FilePath(f"PDF Map {map_type} - {key}", ext=utils.FileFormat.PDF, prefix="result") | 994 pdfPath = utils.FilePath(f"PDF Map {map_type} - {key}", ext=utils.FileFormat.PDF, prefix=ARGS.output_path) |
| 989 convert_to_pdf(svgFilePath, pngPath, pdfPath) | 995 convert_to_pdf(svgFilePath, pngPath, pdfPath) |
| 990 if not ARGS.generate_svg: | 996 if not ARGS.generate_svg: |
| 991 os.remove(svgFilePath.show()) | 997 os.remove(svgFilePath.show()) |
| 992 | 998 |
| 993 | 999 |
| 994 | 1000 |
| 995 | 1001 |
| 996 ############################ MAIN ############################################# | 1002 ############################ MAIN ############################################# |
| 997 def main() -> None: | 1003 def main(args:List[str] = None) -> None: |
| 998 """ | 1004 """ |
| 999 Initializes everything and sets the program in motion based on the fronted input arguments. | 1005 Initializes everything and sets the program in motion based on the fronted input arguments. |
| 1000 | 1006 |
| 1001 Returns: | 1007 Returns: |
| 1002 None | 1008 None |
| 1004 Raises: | 1010 Raises: |
| 1005 sys.exit : if a user-provided custom map is in the wrong format (ET.XMLSyntaxError, ET.XMLSchemaParseError) | 1011 sys.exit : if a user-provided custom map is in the wrong format (ET.XMLSyntaxError, ET.XMLSchemaParseError) |
| 1006 """ | 1012 """ |
| 1007 | 1013 |
| 1008 global ARGS | 1014 global ARGS |
| 1009 ARGS = process_args() | 1015 ARGS = process_args(args) |
| 1010 | 1016 |
| 1011 if os.path.isdir('result') == False: os.makedirs('result') | 1017 if os.path.isdir(ARGS.output_path) == False: os.makedirs(ARGS.output_path) |
| 1012 | 1018 |
| 1013 core_map :ET.ElementTree = ARGS.choice_map.getMap( | 1019 core_map :ET.ElementTree = ARGS.choice_map.getMap( |
| 1014 ARGS.tool_dir, | 1020 ARGS.tool_dir, |
| 1015 utils.FilePath.fromStrPath(ARGS.custom_map) if ARGS.custom_map else None) | 1021 utils.FilePath.fromStrPath(ARGS.custom_map) if ARGS.custom_map else None) |
| 1016 # TODO: ^^^ ugly but fine for now, the argument is None if the model isn't custom because no file was given. | 1022 # TODO: ^^^ ugly but fine for now, the argument is None if the model isn't custom because no file was given. |
