def compute(self, args, same): """Perform all operations for this set of downsamplers. :param args: arguments :param args.dbase_file: database file :param args.dbase: connected database :param args.proj: name of the current project :param args.silent: `True` if using silent mode :param args.met_same: unchanged metrics :param args.metrics: current metrics :param args.do_op: updates the displayed progress :param args.image: name of the image :param args.image_dir: directory to store results for this image :param args.master: master image to downsample :param same: `True` if possibly accessing an existing table :type args: :class:`argparse.Namespace` :type args.dbase_file: `path` :type args.dbase: :class:`database.Database` :type args.proj: `string` :type args.silent: `boolean` :type args.met_same: `dict` :type args.metrics: `dict` :type args.do_op: `function` :type args.image: `string` :type args.image_dir: `path` :type args.master: `path` :type same: `boolean` """ is_same = self.same and same # Compute for all downsamplers. for args.downsampler in self.downsamplers: # Create a directory for this downsampler if necessary. if len(self): args.downsampler_dir = tools.create_dir(args.image_dir, args.downsampler) # Compute for all ratios. for ratio in self.ratios: ratio.compute(args, self.downsamplers, is_same) # Remove the directory for this downsampler. if len(self): shutil.rmtree(args.downsampler_dir, True)
def compute(self, args): """Perform all operations for this set of images. :param args: arguments :param args.dbase_file: database file :param args.dbase: connected database :param args.proj: name of the current project :param args.silent: `True` if using silent mode :param args.met_same: unchanged metrics :param args.metrics: current metrics :param args.do_op: updates the displayed progress :type args: :class:`argparse.Namespace` :type args.dbase_file: `path` :type args.dbase: :class:`database.Database` :type args.proj: `string` :type args.silent: `boolean` :type args.met_same: `dict` :type args.metrics: `dict` :type args.do_op: `function` """ # Compute for all images. for args.image in self.images: # Make a copy of the test image. if len(self): args.image_dir = tools.create_dir(args.proj, args.image) args.master = os.path.join(args.image_dir, 'master.tif') shutil.copyfile(self.images[args.image], args.master) # Compute for all downsamplers. for downsampler in self.downsamplers: downsampler.compute(args, self.same) # Remove the directory for this image. if len(self): shutil.rmtree(args.image_dir, True)
def compute(self, args, old=None): """Perform all operations. :param args: arguments :param args.prog: name of the calling program :param args.dbase_file: database file :param args.proj: name of the current project :param args.silent: `True` if using silent mode :param args.met_same: unchanged metrics :param args.metrics: current metrics :param args.config_file: current configuration file :param args.config_bak: previous configuration file :param old: old configuration entries to be removed :type args: :class:`argparse.Namespace` :type args.prog: `string` :type args.dbase_file: `path` :type args.proj: `string` :type args.silent: `boolean` :type args.met_same: `dict` :type args.metrics: `dict` :type args.config_file: `path` :type args.config_bak: `path` :type old: :class:`argparse.Namespace` """ # Setup verbose mode. if not args.silent: prg = progress.Progress(args.prog, args.proj, len(self)) args.do_op = prg.do_op cleanup = prg.cleanup complete = prg.complete else: prg = [] args.do_op = lambda *a, **k: None cleanup = lambda: None complete = lambda: None # Backup any existing database file. dbase_bak = '.'.join([args.dbase_file, 'bak']) if os.path.isfile(args.dbase_file): shutil.copyfile(args.dbase_file, dbase_bak) # Open the database connection. args.dbase = database.Database(args.dbase_file) success = True try: # Remove old database tables. if old: cleanup() args.dbase.drop_tables(old.images, old.downsamplers, old.ratios) # Create the project folder if it does not exist. tools.create_dir(args.proj) # Compute for all images. for image in self.images: image.compute(args) except StandardError as std_err: success = False error = std_err finally: # Remove the project directory and close the database. shutil.rmtree(args.proj, True) args.dbase.close() if success: # Backup the project file. shutil.copyfile(args.config_file, args.config_bak) # Delete the database backup. if os.path.isfile(dbase_bak): os.remove(dbase_bak) # Indicate completion and restore the console. complete() del prg else: # Restore the previous database file. os.remove(args.dbase_file) if os.path.isfile(dbase_bak): shutil.move(dbase_bak, args.dbase_file) # Restore the console del prg # Print an error message. print error
def compute(self, args, downsamplers, same): """Perform all operations for this set of ratios. :param args: arguments :param args.dbase_file: database file :param args.dbase: connected database :param args.proj: name of the current project :param args.silent: `True` if using silent mode :param args.met_same: unchanged metrics :param args.metrics: current metrics :param args.do_op: updates the displayed progress :param args.image: name of the image :param args.image_dir: directory to store results for this image :param args.master: master image to downsample :param args.downsampler: name of the downsampler :param args.downsampler_dir: directory to store dowsampled images :param downsamplers: downsamplers to use :param same: `True` if accessing an existing table :type args: :class:`argparse.Namespace` :type args.dbase_file: `path` :type args.dbase: :class:`database.Database` :type args.proj: `string` :type args.silent: `boolean` :type args.met_same: `dict` :type args.metrics: `dict` :type args.do_op: `function` :type args.image: `string` :type args.image_dir: `path` :type args.master: `path` :type args.downsampler: `string` :type args.downsampler_dir: `path` :type downsamplers: `dict` :type same: `boolean` """ is_same = self.same and same # Compute for all ratios. for args.ratio in self.ratios: if len(self): args.small = os.path.join(args.downsampler_dir, '.'.join([args.ratio, 'tif'])) # Create a directory for this ratio. ratio_dir = tools.create_dir(args.downsampler_dir, args.ratio) # Downsample master.tif by ratio using downsampler. # {0} input image path (master) # {1} output image path (small) # {2} downsampling ratio # {3} downsampled size (width or height) args.do_op(args) call( downsamplers[args.downsampler].format( args.master, args.small, args.ratio, self.ratios[args.ratio] ).split() ) if is_same: # Access the existing database table. args.table = '_'.join([args.image, args.downsampler, args.ratio]) args.table_bak = args.dbase.backup_table(args.table, args.metrics) else: # Create a new database table. args.table = args.dbase.add_table( args.image, args.downsampler, args.ratio, args.metrics) # Compute for all upsamplers. for upsampler in self.upsamplers: upsampler.compute(args, is_same) # Remove the directory for this ratio. if len(self): shutil.rmtree(ratio_dir, True) # Delete the backup table. if is_same: args.dbase.drop_backup(args.table_bak)