def process_czi_dir(self): """ After the CZI files are placed on birdstore, they need to be scanned to get the metadata for the tif files. Set the progress status here. """ lookup_ids = [1, 2, 3] scan_id = max(self.scan_ids) self.session.query(AlcSlide).filter( AlcSlide.scan_run_id.in_( self.scan_ids)).delete(synchronize_session=False) self.session.query(Task).filter(Task.lookup_id.in_(lookup_ids))\ .filter(Task.prep_id == self.animal.prep_id)\ .delete(synchronize_session=False) for i in lookup_ids: task = Task(self.animal.prep_id, i, True) self.session.add(task) self.session.commit() try: czi_files = sorted(os.listdir(self.CZI_FOLDER)) except OSError as e: print(e) sys.exit() section_number = 1 #czi_files = ['DK43_slide060_2020_01_23__8024.czi'] for i, czi_file in enumerate(czi_files): slide = AlcSlide() slide.scan_run_id = scan_id slide.slide_physical_id = int(re.findall(r'\d+', czi_file)[1]) slide.rescan_number = "1" slide.slide_status = 'Good' slide.processed = False slide.file_size = os.path.getsize( os.path.join(self.CZI_FOLDER, czi_file)) slide.file_name = czi_file slide.created = datetime.fromtimestamp( os.path.getmtime(os.path.join(self.CZI_FOLDER, czi_file))) self.session.add(slide) self.session.flush() # Get metadata from the czi file czi_file_path = os.path.join(self.CZI_FOLDER, czi_file) metadata_dict = get_czi_metadata(czi_file_path) series = get_fullres_series_indices(metadata_dict) scene_counter = 0 for j, series_index in enumerate(series): scene_counter += 1 channels = range(metadata_dict[series_index]['channels']) channel_counter = 0 width = metadata_dict[series_index]['width'] height = metadata_dict[series_index]['height'] for channel in channels: channel_counter += 1 newtif = os.path.splitext(czi_file)[0] newtif = '{}_S{}_C{}.tif'.format(czi_file, scene_counter, channel_counter) newtif = newtif.replace('.czi', '') tif = AlcSlideCziTif() tif.slide_id = slide.id tif.section_number = section_number tif.scene_number = scene_counter tif.channel = channel_counter tif.file_name = newtif tif.file_size = 0 tif.active = 1 tif.width = width tif.height = height tif.channel_index = channel tif.scene_index = series_index tif.processing_duration = 0 tif.created = time.strftime('%Y-%m-%d %H:%M:%S') print('{}\t{}\t{}\t{}\t{}\t{}'.format( newtif, tif.slide_id, tif.scene_number, tif.channel, width, height)) self.session.add(tif) section_number += 1 # lookup_id=3 is for the scanning CZI task = self.session.query(Task).filter(Task.lookup_id == 3)\ .filter(Task.prep_id == self.animal.prep_id).one() task.end_date = datetime.now() self.session.merge(task) self.session.commit()