def end_unpacking(self, path: Path, unpacked_path: Path, *args, format: AbstractFormat = None, is_collection: bool = False, **kwargs): parent = self.get_uf(path) parent.status = UploadedFile.UNPACKED parent.update() if not is_collection: uf = UploadedFile() uf.status = UploadedFile.UPLOADED # Better status ? uf.contentType = format.get_identifier() # TODO uf.size = unpacked_path.size uf.filename = str(unpacked_path.relative_to(FILE_ROOT_PATH)) uf.originalFilename = str(format.main_path.name) uf.ext = "" uf.storage = parent.storage uf.user = parent.user uf.parent = parent.id uf.imageServer = parent.imageServer uf.save() self.path_uf_mapping[str(unpacked_path)] = uf
def cached_tifffile_baseseries(format: AbstractFormat) -> TiffPageSeries: tf = cached_tifffile(format) def get_baseseries(tf: TiffFile) -> TiffPageSeries: idx = np.argmax([np.prod(s.shape) for s in tf.series]) return tf.series[idx] return format.get_cached('_tf_baseseries', get_baseseries, tf)
def cached_palette_converted_pillow_file( format: AbstractFormat, pil_format_slug: Optional[str]) -> PILImage: """Palette converted pillow image""" def _open_palette_converted(_format, _pil_format_slug): image = cached_pillow_file(format, pil_format_slug) palette = getattr(image, "palette", None) if palette: image = image.convert(palette.mode) return image return format.get_cached('_pil_palette_converted', _open_palette_converted, format.path, pil_format_slug)
def cached_pillow_file(format: AbstractFormat, pil_format_slug: Optional[str]) -> PILImage: slugs = [pil_format_slug] if pil_format_slug else None return format.get_cached('_pil', PILImage.open, format.path, formats=slugs)
def cached_omedict(format: AbstractFormat) -> dict: tf = cached_tifffile(format) return format.get_cached('_omedict', xml2dict, tf.pages[0].description)
def cached_omexml(format: AbstractFormat) -> OMEXML: tf = cached_tifffile(format) return format.get_cached('_omexml', parse_ome, tf.pages[0].description)
def deploy_spatial(self, format: AbstractFormat) -> Image: """ Deploy a spatial representation of the image so that it can be used for efficient spatial requests. """ self.notify(ImportEventType.START_SPATIAL_DEPLOY, self.original_path) if format.need_conversion: # Do the spatial conversion try: ext = format.conversion_format().get_identifier() spatial_filename = Path(f"{SPATIAL_STEM}.{ext}") self.spatial_path = self.processed_dir / spatial_filename self.notify( ImportEventType.START_CONVERSION, self.spatial_path, self.upload_path ) r = format.convert(self.spatial_path) if not r or not self.spatial_path.exists(): self.notify( ImportEventType.ERROR_CONVERSION, self.spatial_path ) raise FormatConversionProblem() except Exception as e: self.notify( ImportEventType.ERROR_CONVERSION, self.spatial_path, exception=e ) raise FormatConversionProblem() self.notify(ImportEventType.END_CONVERSION, self.spatial_path) # Check format of converted file self.notify(ImportEventType.START_FORMAT_DETECTION, self.spatial_path) spatial_format = SpatialReadableFormatFactory().match(self.spatial_path) if not spatial_format: self.notify(ImportEventType.ERROR_NO_FORMAT, self.spatial_path) raise NoMatchingFormatProblem(self.spatial_path) self.notify( ImportEventType.END_FORMAT_DETECTION, self.spatial_path, spatial_format ) self.spatial = Image(self.spatial_path, format=spatial_format) # Check spatial image integrity self.notify(ImportEventType.START_INTEGRITY_CHECK, self.spatial_path) errors = self.spatial.check_integrity(check_metadata=True) if len(errors) > 0: self.notify( ImportEventType.ERROR_INTEGRITY_CHECK, self.spatial_path, integrity_errors=errors ) raise ImageParsingProblem(self.spatial) self.notify(ImportEventType.END_INTEGRITY_CHECK, self.spatial) else: # Create spatial role spatial_filename = Path(f"{SPATIAL_STEM}.{format.get_identifier()}") self.spatial_path = self.processed_dir / spatial_filename self.mksymlink(self.spatial_path, self.original_path) self.spatial = Image(self.spatial_path, format=format) assert self.spatial.has_spatial_role() self.notify(ImportEventType.END_SPATIAL_DEPLOY, self.spatial) return self.spatial
def cached_tifffile_baseline(format: AbstractFormat) -> TiffPage: tf = cached_tifffile(format) return format.get_cached('_tf_baseline', tf.pages.__getitem__, 0)
def cached_vips_file(format: AbstractFormat) -> VIPSImage: """Get cached vips object for the image format.""" return format.get_cached('_vips', VIPSImage.new_from_file, str(format.path))
def end_format_detection(self, path: Path, format: AbstractFormat, *args, **kwargs): uf = self.get_uf(path) uf.contentType = format.get_identifier() # TODO: not the content type uf.update()