def get_subdirs(dirpath, wildcard='*', outputfile='experiment_data.yaml'): dirlist = [] if os.path.exists(dirpath): logger.info('dirpath = ' + dirpath) # print completedirpath else: logger.error('Wrong path: ' + dirpath) raise Exception('Wrong path : ' + dirpath) dirpath = os.path.abspath(dirpath) # print 'copmpletedirpath = ', completedirpath # import pdb; pdb.set_trace() dirlist = { o: { 'abspath': os.path.abspath(os.path.join(dirpath, o)) } for o in os.listdir(dirpath) if os.path.isdir(os.path.join(dirpath, o)) } # import pdb; pdb.set_trace() # print [o for o in os.listdir(dirpath) if # os.path.isdir(os.path.abspath(o))] # dirlist.append(infile) # print "current file is: " + infile misc.obj_to_file(dirlist, 'experiment_data.yaml', 'yaml') return dirlist
def get_subdirs(dirpath, wildcard='*', outputfile='experiment_data.yaml'): dirlist = [] if os.path.exists(dirpath): logger.info('dirpath = ' + dirpath) # print completedirpath else: logger.error('Wrong path: ' + dirpath) raise Exception('Wrong path : ' + dirpath) dirpath = os.path.abspath(dirpath) # print 'copmpletedirpath = ', completedirpath # import pdb; pdb.set_trace() dirlist = { o: {'abspath': os.path.abspath(os.path.join(dirpath, o))} for o in os.listdir(dirpath) if os.path.isdir( os.path.join(dirpath, o)) } # import pdb; pdb.set_trace() # print [o for o in os.listdir(dirpath) if # os.path.isdir(os.path.abspath(o))] # dirlist.append(infile) # print "current file is: " + infile misc.obj_to_file(dirlist, 'experiment_data.yaml', 'yaml') return dirlist
def delete_config_records(filename, records_to_save=[]): """ All records of config file are deleted except records_to_save. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') new_cfg = subdict(cfg, records_to_save) print(cfg) misc.obj_to_file(new_cfg, "test_" + filename, filetype='yaml')
def delete_config_records(filename, records_to_save=[]): """ All records of config file are deleted except records_to_save. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') new_cfg = subdict(cfg, records_to_save) print cfg misc.obj_to_file(new_cfg, "test_" + filename , filetype='yaml')
def update_config_records(filename, new_cfg): """ All records of config file are updated exept records_to_save. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') cfg.update(new_cfg) print cfg misc.obj_to_file(new_cfg, "test_" + filename , filetype='yaml')
def update_config_records(filename, new_cfg): """ All records of config file are updated exept records_to_save. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') cfg.update(new_cfg) print(cfg) misc.obj_to_file(new_cfg, "test_" + filename, filetype='yaml')
def test_obj_to_and_from_file_pklz_with_auto(self): testdata = np.random.random([4, 4, 3]) test_object = {'a': 1, 'data': testdata} filename = 'test_obj_to_and_from_file_with_auto.pklz' misc.obj_to_file(test_object, filename, 'auto') saved_object = misc.obj_from_file(filename, 'auto') self.assertTrue(saved_object['a'] == 1) self.assertTrue(saved_object['data'][1, 1, 1] == testdata[1, 1, 1]) os.remove(filename)
def test_obj_to_and_from_file_pickle(self): testdata = np.random.random([4, 4, 3]) test_object = {'a': 1, 'data': testdata} filename = 'test_obj_to_and_from_file.pkl' misc.obj_to_file(test_object, filename, 'pickle') saved_object = misc.obj_from_file(filename, 'pickle') self.assertTrue(saved_object['a'] == 1) self.assertTrue(saved_object['data'][1, 1, 1] == testdata[1, 1, 1]) os.remove(filename)
def test_obj_to_and_from_file_yaml_with_ndarray_to_yaml(self): testdata = np.random.random([4, 4, 3]) test_object = {'a': 1, 'data': testdata, "lst":[1, 2, 3]} filename = 'test_obj_to_and_from_file.yaml' misc.obj_to_file(test_object, filename, 'yaml', ndarray_to_list=True) saved_object = misc.obj_from_file(filename, 'yaml') self.assertTrue(saved_object['a'] == 1) self.assertTrue(saved_object['data'][1][1][1] == testdata[1, 1, 1]) os.remove(filename)
def test_obj_to_and_from_file_yaml_with_ndarray_to_yaml(self): testdata = np.random.random([4, 4, 3]) test_object = {'a': 1, 'data': testdata, "lst": [1, 2, 3]} filename = 'test_obj_to_and_from_file.yaml' misc.obj_to_file(test_object, filename, 'yaml', ndarray_to_list=True) saved_object = misc.obj_from_file(filename, 'yaml') self.assertTrue(saved_object['a'] == 1) self.assertTrue(saved_object['data'][1][1][1] == testdata[1, 1, 1]) os.remove(filename)
def test_obj_to_and_from_file_with_directories(self): import shutil testdata = np.random.random([4, 4, 3]) test_object = {'a': 1, 'data': testdata} dirname = '__test_write_and_read' filename = '__test_write_and_read/test_obj_to_and_from_file.pkl' misc.obj_to_file(test_object, filename, 'pickle') saved_object = misc.obj_from_file(filename, 'pickle') self.assertTrue(saved_object['a'] == 1) self.assertTrue(saved_object['data'][1, 1, 1] == testdata[1, 1, 1]) shutil.rmtree(dirname)
def test_obj_to_and_from_file_with_directories(self): import shutil testdata = np.random.random([4, 4, 3]) test_object = {'a': 1, 'data': testdata} dirname = '__test_write_and_read' filename = '__test_write_and_read/test_obj_to_and_from_file.pkl' misc.obj_to_file(test_object, filename, 'pickle') saved_object = misc.obj_from_file(filename, 'pickle') self.assertTrue(saved_object['a'] == 1) self.assertTrue(saved_object['data'][1, 1, 1] == testdata[1, 1, 1]) shutil.rmtree(dirname)
def get_config(filename, default_cfg): """ Looks config file and update default_cfg values. If file does not exist it is created. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') default_cfg.update(cfg) cfg_out = default_cfg else: misc.obj_to_file(default_cfg, filename, filetype='yaml') cfg_out = default_cfg # default config return cfg_out
def get_config(filename, default_cfg): """ Looks config file and update default_cfg values. If file does not exist it is created. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') default_cfg.update(cfg) cfg_out = default_cfg else: misc.obj_to_file(default_cfg, filename, filetype='yaml') cfg_out = default_cfg # default config return cfg_out
def check_config_version_and_remove_old_records(filename, version, records_to_save): """ Check if config file version is ok. If it is not all records except records_to_save are deleted and config_version in file is set to version. It is used to update user configuration. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') if ('config_version' in cfg and (cfg['config_version'] == version)): # everything is ok, no need to panic return else: # older version of config file cfg = misc.obj_from_file(filename, filetype='yaml') misc.obj_to_file(cfg, filename + '.old', filetype='yaml') print 'cfg ', cfg new_cfg = subdict(cfg, records_to_save) new_cfg['config_version'] = version print 'ncfg ', new_cfg misc.obj_to_file(new_cfg, filename, filetype='yaml')
def check_config_version_and_remove_old_records(filename, version, records_to_save): """ Check if config file version is ok. If it is not all records except records_to_save are deleted and config_version in file is set to version. It is used to update user configuration. """ if os.path.isfile(filename): cfg = misc.obj_from_file(filename, filetype='yaml') if ('config_version' in cfg and (cfg['config_version'] == version)): # everything is ok, no need to panic return else: # older version of config file cfg = misc.obj_from_file(filename, filetype='yaml') misc.obj_to_file(cfg, filename + '.old', filetype='yaml') print('cfg ', cfg) new_cfg = subdict(cfg, records_to_save) new_cfg['config_version'] = version print('ncfg ', new_cfg) misc.obj_to_file(new_cfg, filename, filetype='yaml')
def main(): #logger = logging.getLogger(__name__) logger = logging.getLogger() logger.setLevel(logging.WARNING) ch = logging.StreamHandler() logger.addHandler(ch) #logger.debug('input params') # input parser parser = argparse.ArgumentParser(description= 'Segmentation of bones, lungs and heart.') parser.add_argument('-i','--datadir', default=None, help='path to data dir') parser.add_argument('-o','--output', default=None, help='output file') parser.add_argument('-d', '--debug', action='store_true', help='run in debug mode') parser.add_argument('-ss', '--segspine', action='store_true', help='run spine segmentaiton') parser.add_argument('-sl', '--seglungs', action='store_true', help='run lungs segmentation') parser.add_argument('-sh', '--segheart', action='store_true', help='run heart segmentation') parser.add_argument('-sb', '--segbones', action='store_true', help='run bones segmentation') parser.add_argument('-exd', '--exampledata', action='store_true', help='run unittest') parser.add_argument('-so', '--show_output', action='store_true', help='Show output data in viewer') args = parser.parse_args() if args.debug: logger.setLevel(logging.DEBUG) if args.exampledata: args.dcmdir = '../sample_data/liver-orig001.raw' # if dcmdir == None: #else: #dcm_read_from_dir('/home/mjirik/data/medical/data_orig/46328096/') #data3d, metadata = dcmr.dcm_read_from_dir(args.dcmdir) data3d , metadata = io3d.datareader.read(args.datadir) sseg = SupportStructureSegmentation(data3d = data3d, voxelsize_mm = metadata['voxelsize_mm'], ) #sseg.orientation() if args.segbones: sseg.bone_segmentation() if args.segspine: sseg.spine_segmentation() if args.seglungs or args.segheart: sseg.lungs_segmentation() if args.segheart: sseg.heart_segmentation() sseg.resize_back_to_orig() #print ("Data size: " + str(data3d.nbytes) + ', shape: ' + str(data3d.shape) ) #igc = pycut.ImageGraphCut(data3d, zoom = 0.5) #igc.interactivity() #igc.make_gc() #igc.show_segmentation() # volume #volume_mm3 = np.sum(oseg.segmentation > 0) * np.prod(oseg.voxelsize_mm) #pyed = sed3.sed3(oseg.data3d, contour = oseg.segmentation) #pyed.show() if args.show_output: sseg.visualization() #savestring = raw_input ('Save output data? (y/n): ') #sn = int(snstring) if args.output is not None: # savestring in ['Y','y']: import misc data = sseg.export() misc.obj_to_file(data, args.output, filetype = 'pickle')
def main(): #logger = logging.getLogger(__name__) logger = logging.getLogger() logger.setLevel(logging.WARNING) ch = logging.StreamHandler() logger.addHandler(ch) #logger.debug('input params') # input parser parser = argparse.ArgumentParser(description= 'Segmentation of bones, lungs and heart.') parser.add_argument('-i','--datadir', default=None, help='path to data dir') parser.add_argument('-o','--output', default=None, help='output file') parser.add_argument('-d', '--debug', action='store_true', help='run in debug mode') parser.add_argument('-ss', '--segspine', action='store_true', help='run spine segmentaiton') parser.add_argument('-sl', '--seglungs', action='store_true', help='run lungs segmentation') parser.add_argument('-sh', '--segheart', action='store_true', help='run heart segmentation') parser.add_argument('-sb', '--segbones', action='store_true', help='run bones segmentation') parser.add_argument('-exd', '--exampledata', action='store_true', help='run unittest') parser.add_argument('-so', '--show_output', action='store_true', help='Show output data in viewer') args = parser.parse_args() if args.debug: logger.setLevel(logging.DEBUG) if args.exampledata: args.dcmdir = '../sample_data/liver-orig001.raw' # if dcmdir == None: #else: #dcm_read_from_dir('/home/mjirik/data/medical/data_orig/46328096/') #data3d, metadata = dcmr.dcm_read_from_dir(args.dcmdir) data3d , metadata = io3d.datareader.read(args.datadir, dataplus_format=False) sseg = SupportStructureSegmentation(data3d = data3d, voxelsize_mm = metadata['voxelsize_mm'], ) #sseg.orientation() if args.segbones: sseg.bone_segmentation() if args.segspine: sseg.spine_segmentation() if args.seglungs or args.segheart: sseg.lungs_segmentation() if args.segheart: sseg.heart_segmentation() sseg.resize_back_to_orig() #print("Data size: " + str(data3d.nbytes) + ', shape: ' + str(data3d.shape) ) #igc = pycut.ImageGraphCut(data3d, zoom = 0.5) #igc.interactivity() #igc.make_gc() #igc.show_segmentation() # volume #volume_mm3 = np.sum(oseg.segmentation > 0) * np.prod(oseg.voxelsize_mm) #pyed = sed3.sed3(oseg.data3d, contour = oseg.segmentation) #pyed.show() if args.show_output: sseg.visualization() #savestring = raw_input ('Save output data? (y/n): ') #sn = int(snstring) if args.output is not None: # savestring in ['Y','y']: import misc data = sseg.export() misc.obj_to_file(data, args.output, filetype = 'pickle')
def save_config(cfg, filename): misc.obj_to_file(cfg, filename, filetype='yaml')
def save_config(cfg, filename): misc.obj_to_file(cfg, filename, filetype='yaml')