-
Notifications
You must be signed in to change notification settings - Fork 1
Enable zipped input/output in SEM mapping #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
c51e888
c35c9d2
e5cd04f
25c1e66
5cdd205
1e5270e
8422de8
2cccaf2
3353b0d
51f2b47
2347b10
67d80a0
80ff70d
f69dff7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,13 @@ | |
| import logging | ||
| import os | ||
| from sys import exit | ||
| from pathlib import Path | ||
|
|
||
| from src.IO.MappingAbortionError import MappingAbortionError | ||
| from src.IO.sem.InputReader import InputReader as InputReader_SEM | ||
| from src.IO.tomo.InputReader import InputReader as InputReader_TOMO | ||
| from src.IO.tomo.OutputWriter import OutputWriter | ||
| from src.IO.sem.OutputWriter import OutputWriter as OutputWriter_SEM | ||
| from src.IO.tomo.OutputWriter import OutputWriter as OutputWriter_TOMO | ||
| from src.resources.maps.parsing import map_from_flag | ||
|
|
||
| # make log level configurable from ENV, defaults to info level | ||
|
|
@@ -87,8 +89,8 @@ def run_tomo_mapper(args): | |
| #si = setup_infos if len(setup_infos) >= 1 else None | ||
| #ri = run_infos if len(run_infos) >= 1 else None | ||
|
|
||
| output = OutputWriter.stitch_together(setup_infos, run_infos, imgs) | ||
| OutputWriter.writeOutput(output, OUTPUT_PATH) | ||
| output = OutputWriter_TOMO.stitch_together(setup_infos, run_infos, imgs) | ||
| OutputWriter_TOMO.writeOutput(output, OUTPUT_PATH) | ||
| except MappingAbortionError as e: | ||
| reader.clean_up() | ||
| exit(e) | ||
|
|
@@ -103,18 +105,73 @@ def run_sem_mapper(args): | |
| MAP_SOURCE = argdict.get('map') | ||
| OUTPUT_PATH = argdict.get('output') | ||
|
|
||
| reader = None | ||
|
|
||
| try: | ||
| reader = InputReader_SEM(MAP_SOURCE, INPUT_SOURCE) | ||
|
|
||
| img_info = reader.retrieve_image_info(INPUT_SOURCE) | ||
| if not img_info: | ||
| logging.error('Could not retrieve image information due to unknown error. Aborting.') | ||
| exit(1) | ||
| with open(OUTPUT_PATH, 'w', encoding="utf-8") as f: | ||
| json.dump(img_info, f, indent=4, ensure_ascii=False) | ||
| reader = InputReader_SEM(MAP_SOURCE, INPUT_SOURCE, OUTPUT_PATH) | ||
| tmpdir = reader.temp_dir_path | ||
|
|
||
| if tmpdir: | ||
| # The case of a zipped input file | ||
| list_of_file_names = [] | ||
| success_count = 0 | ||
|
|
||
| for file_path in reader.filter_zipfile(tmpdir): | ||
| #if not file_path.is_file(): | ||
| # No directory path is allowed. Only process files | ||
| #logging.debug(f"Skipping {file_path} as it is probably a directory.") | ||
| #continue | ||
| #if '__MACOSX' in str(file_path): | ||
| #logging.debug(f"Skipping macOS metadata file: {file_path}") | ||
| #continue | ||
|
|
||
| logging.info(f"Processing extracted file: {file_path.name}") | ||
| try: | ||
| file_name = file_path.with_suffix('').name + ".json" | ||
| reader_ = InputReader_SEM(MAP_SOURCE, file_path, file_name) | ||
| img_info = reader_.retrieve_image_info(file_path) | ||
| logging.debug(f"IMAGE_INFO: {img_info}") | ||
|
|
||
| if not img_info: | ||
| raise MappingAbortionError(f"Could not retrieve image information for {file_path.name}.") | ||
|
|
||
| OutputWriter_SEM.save_the_file(img_info, file_name) | ||
| list_of_file_names.append(file_name) | ||
| success_count += 1 | ||
|
|
||
| except MappingAbortionError as e: | ||
| logging.warning(f"Skipping file {file_path.name} due to mapping error: {e}") | ||
| except Exception as e: | ||
| logging.exception(f"Unexpected error processing file {file_path.name}") | ||
|
|
||
| if success_count > 0: | ||
| logging.info(f"In total {success_count} file(s) were successfully processed.") | ||
| OutputWriter_SEM.save_to_zip(list_of_file_names, OUTPUT_PATH) | ||
GGoetzelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| raise MappingAbortionError("No files could be processed successfully. Aborting.") | ||
|
|
||
| else: | ||
| # The case of a single input file | ||
| logging.info("Processing input as single file.") | ||
| img_info = reader.retrieve_image_info(INPUT_SOURCE) | ||
| if not img_info: | ||
| raise MappingAbortionError("Could not retrieve image information. Aborting.") | ||
|
|
||
| #if not OUTPUT_PATH.lower().endswith('.json'): | ||
| #OUTPUT_PATH += '.json' # Ensure correct .json extension | ||
| #logging.warning(f"The output path has been updated to {OUTPUT_PATH} to match the correct extension.") | ||
|
|
||
| OutputWriter_SEM.save_the_file(img_info, OUTPUT_PATH) | ||
|
|
||
| #with open(OUTPUT_PATH, 'w', encoding="utf-8") as f: | ||
| #json.dump(img_info, f, indent=4, ensure_ascii=False) | ||
|
|
||
| except MappingAbortionError as e: | ||
| #logging.error(f"MappingAbortionError: {e}") | ||
| exit(e) | ||
|
|
||
| finally: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, I was not even aware that 'finally' would run even on exit call. TIL :) |
||
| if reader: | ||
| reader.clean_up() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to implement the clean_up function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a first quick solution, a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking more in the direction of an InputReader base class that either provides the interface for the clean_up method or (more likely) even implements it, since it likely always treats a working_dir used by inputReaders in the same way. Maybe there is even more overlap, especially in regards to parser handling. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The InputReader base class has been implement in a distinct branch dev_inputreader_base_class. |
||
|
|
||
| if __name__ == '__main__': | ||
| run_cli() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import os | ||
| import json | ||
| import logging | ||
| import zipfile | ||
|
|
||
| from src.IO.MappingAbortionError import MappingAbortionError | ||
|
|
||
|
|
||
| class OutputWriter: | ||
|
|
||
| @staticmethod | ||
| def save_the_file(mapped_metadata, file_path): | ||
| try: | ||
| with open(file_path, 'w', encoding="utf-8") as json_file: | ||
| json.dump(mapped_metadata, json_file, indent=4, ensure_ascii=False) | ||
| logging.info("The output document has been created successfully!") | ||
| except (FileNotFoundError, PermissionError, IsADirectoryError, OSError, TypeError, ValueError) as e: | ||
| logging.error(f"Unable to save {file_path}: {e}") | ||
| raise MappingAbortionError(f"Failed to save {file_path}.") | ||
|
|
||
| @staticmethod | ||
| def save_to_zip(file_path_list, zip_file_path): | ||
| try: | ||
| with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf: | ||
| # "ZIP_DEFLATED" is a lossless compression algorithm, meaning no data is lost during the compression process. | ||
| for file_path in file_path_list: | ||
| try: | ||
| zf.write(file_path, os.path.basename(file_path)) | ||
| logging.debug(f"Added {file_path} to zip.") | ||
| except (FileNotFoundError, PermissionError, IsADirectoryError, OSError, zipfile.BadZipFile) as e: | ||
| logging.error(f"Adding {file_path} to zip was not successful: {e}") | ||
| raise MappingAbortionError(f"Failed to add {file_path} to zip.") | ||
| logging.info(f"Files have been zipped into {zip_file_path} sucessfully!") | ||
| except MappingAbortionError as e: | ||
| logging.error(f"Failed to create zip file at {zip_file_path}: {e}") | ||
| raise MappingAbortionError(f"Failed to save to zip.") | ||
|
|
||
| # Delete the original files after zipping | ||
| for file_path in file_path_list: | ||
| try: | ||
| os.remove(file_path) | ||
| logging.info(f"{file_path} has been deleted.") | ||
| except (FileNotFoundError, PermissionError, IsADirectoryError, OSError) as e: | ||
| logging.warning(f"{file_path} to zip was not deleted: {e}") | ||
| raise MappingAbortionError(f"Failed to delete file {file_path} after zip.") |
Uh oh!
There was an error while loading. Please reload this page.