def read_image(model, expect_missing_file): try: image = skimage.io.imread(model.path) model.load(image) except Exception as e: terminal_error("Could not load image file: %s because: %s" % (model.path, str(e))) else: return True
def version(self, value): version_regex = re.compile(r"""\d+\.\d+\.\d+""") if version_regex.match(value): self._version = value else: log.critical( "Change the value in the VERSION file in the root directory of fylm_critic to 'x.y.z'." ) terminal_error("Invalid version number! %s" % value)
def load(self, data): try: self._last_state, self._last_state_time_period = self._parse_state(data[0]) if len(data) > 1: # We have some annotations already for line in data[1:]: self._parse_line(line) except Exception as e: terminal_error("Could not parse line for Channel Locator because of: %s" % e)
def load(self, data): try: header = data[0] self._top_left, self._bottom_right = self._parse_header(header) for line in data[1:]: channel_number, locations = self._parse_line(line) self._channels[channel_number] = locations except Exception as e: terminal_error("Could not parse line for Channel Locator because of: %s" % e)
def load(self, data): try: header = data[0] self._top_left, self._bottom_right = self._parse_header(header) for line in data[1:]: channel_number, locations = self._parse_line(line) self._channels[channel_number] = locations except Exception as e: terminal_error( "Could not parse line for Channel Locator because of: %s" % e)
def read_text(model, expect_missing_file): try: with open(model.path) as f: # strip raw data to remove trailing newlines, so splitting on newline can't produce an empty value data = f.read(-1).strip().split("\n") model.load(data) except Exception as e: if expect_missing_file: return None else: terminal_error("Could not load text file: %s because: %s" % (model.path, str(e))) else: return True
def _get_nd2_attributes(self, experiment): """ Determine several attributes of the ND2s used in this experiment. :type experiment: model.experiment.Experiment() """ experiment_log = self._load_experiment_log(experiment) found_an_nd2 = False for n, nd2_filename in enumerate(experiment.nd2s): try: nd2 = nd2reader.Nd2(nd2_filename) except IOError: pass else: # We need to know the absolute time that an experiment began so we can figure out the gap between # different files (as that could be any amount of time). timestamp = self._utc_timestamp(nd2.absolute_start) time_period = n + 1 experiment.set_time_period_start_time(time_period, timestamp) experiment_log['start_unix_timestamps'][str( time_period)] = timestamp experiment.field_of_view_count = nd2.field_of_view_count experiment_log['field_of_view_count'] = nd2.field_of_view_count experiment_log['has_fluorescent_channels'] = False for channel in nd2.channels: if channel.name != "": log.info("Experiment has fluorescent channels.") experiment.has_fluorescent_channels = True experiment_log['has_fluorescent_channels'] = True break else: log.info("Experiment does not have fluorescent channels.") self._save_experiment_log(experiment, experiment_log) found_an_nd2 = True if not found_an_nd2: # There are no ND2s so we load all the information we need from the log. if 'field_of_view_count' not in experiment_log.keys( ) or 'has_fluorescent_channels' not in experiment_log.keys(): terminal_error( "No ND2s found and no attributes saved. It seems like you haven't even started this experiment." ) experiment.field_of_view_count = int( experiment_log['field_of_view_count']) experiment.has_fluorescent_channels = experiment_log[ 'has_fluorescent_channels'] for time_period, timestamp in experiment_log[ 'start_unix_timestamps'].items(): experiment.set_time_period_start_time(time_period, timestamp)
def _get_nd2_attributes(self, experiment): """ Determine several attributes of the ND2s used in this experiment. :type experiment: model.experiment.Experiment() """ experiment_log = self._load_experiment_log(experiment) found_an_nd2 = False for n, nd2_filename in enumerate(experiment.nd2s): try: nd2 = nd2reader.Nd2(nd2_filename) except IOError: pass else: # We need to know the absolute time that an experiment began so we can figure out the gap between # different files (as that could be any amount of time). timestamp = self._utc_timestamp(nd2.absolute_start) time_period = n + 1 experiment.set_time_period_start_time(time_period, timestamp) experiment_log['start_unix_timestamps'][str(time_period)] = timestamp experiment.field_of_view_count = nd2.field_of_view_count experiment_log['field_of_view_count'] = nd2.field_of_view_count experiment_log['has_fluorescent_channels'] = False for channel in nd2.channels: if channel.name != "": log.info("Experiment has fluorescent channels.") experiment.has_fluorescent_channels = True experiment_log['has_fluorescent_channels'] = True break else: log.info("Experiment does not have fluorescent channels.") self._save_experiment_log(experiment, experiment_log) found_an_nd2 = True if not found_an_nd2: # There are no ND2s so we load all the information we need from the log. if 'field_of_view_count' not in experiment_log.keys() or 'has_fluorescent_channels' not in experiment_log.keys(): terminal_error("No ND2s found and no attributes saved. It seems like you haven't even started this experiment.") experiment.field_of_view_count = int(experiment_log['field_of_view_count']) experiment.has_fluorescent_channels = experiment_log['has_fluorescent_channels'] for time_period, timestamp in experiment_log['start_unix_timestamps'].items(): experiment.set_time_period_start_time(time_period, timestamp)
def _find_time_periods(self, experiment): """ Finds the time_periods of all available ND2 files associated with the experiment. """ regex = re.compile(r"""FYLM-%s-0(?P<time_period>\d+)\.nd2""" % experiment.start_date.clean_date) found = False for filename in sorted(self._os.listdir(experiment.base_dir)): match = regex.match(filename) if match: found = True time_period = int(match.group("time_period")) log.debug("time_period: %s" % time_period) experiment.add_time_period(time_period) experiment_log = self._load_experiment_log(experiment) for time_period in experiment_log['time_periods']: found = True experiment.add_time_period(time_period) if not found: terminal_error("No ND2s found!")
def get_experiment(self, experiment_start_date, base_dir, version, review): experiment = ExperimentModel() experiment.version = version experiment.review_annotations = review # set start date experiment.start_date = experiment_start_date if not experiment.start_date.is_valid: terminal_error("Invalid start date: %s (use the format: YYMMDD)" % experiment_start_date) log.debug("Experiment start date: %s" % experiment.start_date.clean_date) # set the base directory if not self._os.path.isdir(base_dir): terminal_error("Base directory does not exist: %s" % base_dir) experiment.base_dir = base_dir log.debug("Experiment base directory: %s" % experiment.base_dir) self._build_directories(experiment) self._find_time_periods(experiment) self._get_nd2_attributes(experiment) return experiment
def _find_time_periods(self, experiment): """ Finds the time_periods of all available ND2 files associated with the experiment. """ regex = re.compile(r"""FYLM-%s-0(?P<time_period>\d+)\.nd2""" % experiment.start_date.clean_date) found = False for filename in sorted(self._os.listdir(experiment.base_dir)): match = regex.match(filename) if match: found = True time_period = int(match.group("time_period")) log.debug("time_period: %s" % time_period) experiment.add_time_period(time_period) experiment_log = self._load_experiment_log(experiment) for time_period in experiment_log['time_periods']: found = True experiment.add_time_period(time_period) if not found: terminal_error("No ND2s found!")
def get_experiment(self, experiment_start_date, base_dir, version, review): experiment = ExperimentModel() experiment.version = version experiment.review_annotations = review # set start date experiment.start_date = experiment_start_date if not experiment.start_date.is_valid: terminal_error("Invalid start date: %s (use the format: YYMMDD)" % experiment_start_date) log.debug("Experiment start date: %s" % experiment.start_date.clean_date) # set the base directory if not self._os.path.isdir(base_dir): terminal_error("Base directory does not exist: %s" % base_dir) experiment.base_dir = base_dir log.debug("Experiment base directory: %s" % experiment.base_dir) self._build_directories(experiment) self._find_time_periods(experiment) self._get_nd2_attributes(experiment) return experiment