def _toggle_debug(_old_value, _new_value): # TODO: Toggle dosma logging to debug mode. if _old_value == _new_value: return if _new_value: preferences.set("nipype", value="stream", prefix="logging") else: preferences.set("nipype", value="file_stderr", prefix="logging")
def parse_args(f_input=None): """Parse arguments given through command line (argv) :raises ValueError if dicom path is not provided :raises NotADirectoryError if dicom path does not exist or is not a directory """ parser = argparse.ArgumentParser( prog='DOSMA', description='A deep-learning powered open source MRI analysis pipeline', epilog='Either `--dicom` or `--load` must be specified. ' 'If both are given, `--dicom` will be used') parser.add_argument('--%s' % DEBUG_KEY, action='store_true', help='use debug mode') # Dicom and results paths parser.add_argument('--d', '--%s' % DICOM_KEY, metavar='D', type=str, default=None, nargs='?', dest=DICOM_KEY, help='path to directory storing dicom files') parser.add_argument('--l', '--%s' % LOAD_KEY, metavar='L', type=str, default=None, nargs='?', dest=LOAD_KEY, help='path to data directory to load from') parser.add_argument('--s', '--%s' % SAVE_KEY, metavar='S', type=str, default=None, nargs='?', dest=SAVE_KEY, help='path to data directory to save to. Default: L/D') parser.add_argument( '--%s' % IGNORE_EXT_KEY, action='store_true', default=False, dest=IGNORE_EXT_KEY, help='ignore .dcm extension when loading dicoms. Default: False') parser.add_argument( '--%s' % SPLIT_BY_KEY, metavar='G', type=str, default=None, nargs='?', dest=SPLIT_BY_KEY, help='override dicom tag to split volumes by (eg. `EchoNumbers`)') parser.add_argument('--%s' % GPU_KEY, metavar='G', type=str, default=None, nargs='?', dest=GPU_KEY, help='gpu id. Default: None') # Add preferences preferences_flags = preferences.cmd_line_flags() for flag in preferences_flags.keys(): argparse_kwargs = preferences_flags[flag] argparse_kwargs['dest'] = flag aliases = argparse_kwargs.pop('aliases', None) name = argparse_kwargs.pop('name', None) parser.add_argument(*aliases, **argparse_kwargs) subparsers = parser.add_subparsers(help='sub-command help', dest=SCAN_KEY) add_scans(subparsers) # MSK knee parser knee.knee_parser(subparsers) start_time = time.time() if f_input: args = parser.parse_args(f_input) else: args = parser.parse_args() # Only initialize logger if called from command line. # If UI is using it, the logger should be initialized by the UI. io_utils.init_logger(fc.LOG_FILE_PATH, args.debug) vargin = vars(args) if vargin[DEBUG_KEY]: fc.set_debug() gpu = vargin[GPU_KEY] logging.debug(vargin) if gpu is not None: os.environ['CUDA_VISIBLE_DEVICES'] = gpu if not gpu: os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # parse and update preferences for flag in preferences_flags.keys(): preferences.set(flag, vargin[flag]) dicom_path = vargin[DICOM_KEY] load_path = vargin[LOAD_KEY] if not dicom_path and not load_path: raise ValueError( 'Must provide path to dicoms or path to load data from') save_path = vargin[SAVE_KEY] if not save_path: save_path = load_path if load_path else '%s/data' % dicom_path vargin[SAVE_KEY] = save_path if not os.path.isdir(save_path): os.makedirs(save_path) tissues = parse_tissues(vargin) vargin['tissues'] = tissues vargin[SPLIT_BY_KEY] = parse_dicom_tag_splitby(vargin[SPLIT_BY_KEY]) args.func(vargin) time_elapsed = (time.time() - start_time) logging.info("Time Elapsed: {:.2f} seconds".format(time.time() - start_time)) return time_elapsed
def parse_args(f_input=None): """Parse arguments given through command line (argv) :raises ValueError if dicom path is not provided :raises NotADirectoryError if dicom path does not exist or is not a directory """ parser = argparse.ArgumentParser( prog="DOSMA", description="A deep-learning powered open source MRI analysis pipeline", epilog="Either `--dicom` or `--load` must be specified. " "If both are given, `--dicom` will be used", ) parser.add_argument("--%s" % DEBUG_KEY, action="store_true", help="use debug mode") # Dicom and results paths parser.add_argument( "--d", "--%s" % DICOM_KEY, metavar="D", type=str, default=None, nargs="?", dest=DICOM_KEY, help="path to directory storing dicom files", ) parser.add_argument( "--l", "--%s" % LOAD_KEY, metavar="L", type=str, default=None, nargs="?", dest=LOAD_KEY, help="path to data directory to load from", ) parser.add_argument( "--s", "--%s" % SAVE_KEY, metavar="S", type=str, default=None, nargs="?", dest=SAVE_KEY, help="path to data directory to save to. Default: L/D", ) parser.add_argument( "--%s" % IGNORE_EXT_KEY, action="store_true", default=False, dest=IGNORE_EXT_KEY, help="ignore .dcm extension when loading dicoms. Default: False", ) parser.add_argument( "--%s" % SPLIT_BY_KEY, metavar="G", type=str, default=None, nargs="?", dest=SPLIT_BY_KEY, help="override dicom tag to split volumes by (eg. `EchoNumbers`)", ) parser.add_argument( "--%s" % GPU_KEY, metavar="G", type=str, default=None, nargs="?", dest=GPU_KEY, help="gpu id. Default: None", ) parser.add_argument( "--%s" % NUM_WORKERS_KEY, metavar="G", type=int, default=0, dest=NUM_WORKERS_KEY, help="num cpu workers. Default: 0", ) # Add preferences preferences_flags = preferences.cmd_line_flags() for flag in preferences_flags.keys(): argparse_kwargs = preferences_flags[flag] argparse_kwargs["dest"] = flag aliases = argparse_kwargs.pop("aliases", None) name = argparse_kwargs.pop("name", None) # noqa: F841 parser.add_argument(*aliases, **argparse_kwargs) subparsers = parser.add_subparsers(help="sub-command help", dest=SCAN_KEY) add_scans(subparsers) # MSK knee parser knee.knee_parser(subparsers) start_time = time.time() if f_input: args = parser.parse_args(f_input) else: args = parser.parse_args() # Only initialize logger if called from command line. # If UI is using it, the logger should be initialized by the UI. io_utils.init_logger(env.log_file_path(), args.debug) vargin = vars(args) if vargin[DEBUG_KEY]: env.debug(True) gpu = vargin[GPU_KEY] logging.debug(vargin) if gpu is not None: os.environ["CUDA_VISIBLE_DEVICES"] = gpu elif "CUDA_VISIBLE_DEVICES" not in os.environ: os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # parse and update preferences for flag in preferences_flags.keys(): preferences.set(flag, vargin[flag]) dicom_path = vargin[DICOM_KEY] load_path = vargin[LOAD_KEY] if not dicom_path and not load_path: raise ValueError("Must provide path to dicoms or path to load data from") save_path = vargin[SAVE_KEY] if not save_path: save_path = load_path if load_path else "%s/data" % dicom_path vargin[SAVE_KEY] = save_path if not os.path.isdir(save_path): os.makedirs(save_path) tissues = parse_tissues(vargin) vargin["tissues"] = tissues vargin[SPLIT_BY_KEY] = parse_dicom_tag_splitby(vargin[SPLIT_BY_KEY]) args.func(vargin) time_elapsed = time.time() - start_time logging.info("Time Elapsed: {:.2f} seconds".format(time.time() - start_time)) return time_elapsed
def _apply_settings(self): for preference in self._preference_elements.keys(): new_val = self._preference_elements[preference].tk_var.get() preferences.set(preference, new_val)