Skip to content
Snippets Groups Projects
json_write.py 3.18 KiB
Newer Older
Simon Wittl's avatar
dev
Simon Wittl committed
import numpy as np
import json 
from pathlib import Path

Simon Wittl's avatar
Simon Wittl committed
from ..writer import BaseDataWriter, PyProjection, PyProjectionGeometry, PyRegionOfIntrest, PyVolume
Simon Wittl's avatar
dev
Simon Wittl committed
from PIL import Image
Simon Wittl's avatar
Simon Wittl committed
import pyometiff
Simon Wittl's avatar
dev
Simon Wittl committed


class RqJsonWriter(BaseDataWriter):
    def __init__(self):
Simon Wittl's avatar
Simon Wittl committed
        super().__init__('.geom-json', '.tif', '.roi-json', '.ome.tiff')
Simon Wittl's avatar
dev
Simon Wittl committed

    def write_json(self, save_path: Path, data_dict: dict):
        with open(str(save_path), 'w') as f:
            json.dump(data_dict, f, indent=2)

    def write_projection_geometry(self, save_path: Path, projection_geometry: PyProjectionGeometry):
        data_dict = dict()
Simon Wittl's avatar
Simon Wittl committed

        #geometry
Simon Wittl's avatar
dev
Simon Wittl committed
        data_dict['focal_spot_mm'] = projection_geometry.focal_spot_mm.tolist()
        data_dict['detector_postion_mm'] = projection_geometry.detector_postion_mm.tolist()
        data_dict['detector_orientation_quad'] = projection_geometry.detector_orientation_quad.tolist()
Simon Wittl's avatar
Simon Wittl committed
        data_dict['focal_spot_orientation_quad'] = projection_geometry.focal_spot_orientation_quad.tolist()
        
Simon Wittl's avatar
dev
Simon Wittl committed
        data_dict['frame_id'] = projection_geometry.frame_id

        self.write_json(save_path, data_dict)

    def write_projection(self, save_path: Path, projection: PyProjection):
Simon Wittl's avatar
Simon Wittl committed
        save_path_projection_geometry = save_path.parent / f'{save_path.stem}{self.porjection_geometry_suffix}'
Simon Wittl's avatar
dev
Simon Wittl committed
        data_dict = dict()
Simon Wittl's avatar
Simon Wittl committed

        # geometry
Simon Wittl's avatar
dev
Simon Wittl committed
        data_dict['focal_spot_mm'] = projection.focal_spot_mm.tolist()
        data_dict['detector_postion_mm'] = projection.detector_postion_mm.tolist()
        data_dict['detector_orientation_quad'] = projection.detector_orientation_quad.tolist()
Simon Wittl's avatar
Simon Wittl committed
        data_dict['focal_spot_orientation_quad'] = projection.focal_spot_orientation_quad.tolist()
        
        # detector
Simon Wittl's avatar
dev
Simon Wittl committed
        data_dict['pixel_pitch_x_mm'] = projection.pixel_pitch_x_mm
        data_dict['pixel_pitch_y_mm'] = projection.pixel_pitch_y_mm
        data_dict['detector_heigth_mm'] = projection.detector_heigth_mm
        data_dict['detector_width_mm'] = projection.detector_width_mm
Simon Wittl's avatar
Simon Wittl committed

        # xray
        data_dict['exposure_time_ms'] = projection.exposure_time_ms
        data_dict['voltage_kv'] = projection.voltage_kv
        data_dict['current_ua'] = projection.current_ua
Simon Wittl's avatar
dev
Simon Wittl committed
        data_dict['frame_id'] = projection.frame_id

        self.write_json(save_path_projection_geometry, data_dict)

        image = Image.fromarray(projection.image)
        image.save(save_path)

    def write_region_of_intrest(self, save_path: Path, region_of_intrest: PyRegionOfIntrest):
        data_dict = dict()
Simon Wittl's avatar
Simon Wittl committed

        # geometry
        data_dict['center_points_mm'] = region_of_intrest.center_points_mm.tolist()
        data_dict['dimensions_mm'] = region_of_intrest.dimensions_mm.tolist()
Simon Wittl's avatar
dev
Simon Wittl committed
        data_dict['frame_id'] = region_of_intrest.frame_id
Simon Wittl's avatar
Simon Wittl committed
        data_dict['resolution_mm'] = region_of_intrest.resolution_mm.tolist()
Simon Wittl's avatar
dev
Simon Wittl committed

        self.write_json(save_path, data_dict)
Simon Wittl's avatar
Simon Wittl committed

    def write_volume(self, save_path: Path, volume: PyVolume):
        save_path_region_of_intrest = save_path.parent / f'{save_path.stem}{self.region_of_intrest_suffix}'
        self.write_region_of_intrest(save_path_region_of_intrest, volume.roi)
        writer = pyometiff.OMETIFFWriter(save_path, volume.array, dict())
        writer.write()