diff --git a/.gitignore b/.gitignore index 5b8c4e3..be2f6c6 100755 --- a/.gitignore +++ b/.gitignore @@ -4,10 +4,11 @@ *.project *.pydevproject .idea -dev +dev/ doc/build/ doc/source/_static/ doc/source/_templates/ build/ dist/ -*egg-info \ No newline at end of file +*egg-info +recipe/ \ No newline at end of file diff --git a/README.md b/README.md index 5a35269..4c79533 100755 --- a/README.md +++ b/README.md @@ -40,11 +40,21 @@ Install * Open command prompt, navigate to the source folder, run the following commands: ```commandline - $ conda create -n discorpy - $ conda activate discorpy - $ conda install python - $ python setup.py install + conda create -n discorpy + conda activate discorpy + conda install python + python setup.py install ``` + - Using conda: + + Install Miniconda as instructed above. + + Open terminal or command prompt and run the following command: + `conda install -c algotom discorpy` + +- Using pip: + + Install Miniconda as instructed above. + + Open terminal or command prompt and run the following command: + `pip install discorpy` + How to use ========== @@ -62,31 +72,31 @@ Demonstrations - Apply to a visible dot-target collected at [Beamline I12](https://www.diamond.ac.uk/Instruments/Imaging-and-Microscopy/I12/Detectors-at-I12.html), Diamond Light Source, UK: -![I12_before_after1](data/demo/i12_data_1.jpg) +![I12_before_after1](https://github.com/DiamondLightSource/discorpy/raw/master/data/demo/i12_data_1.jpg) -![I12_before_after2](data/demo/i12_data_2.jpg) +![I12_before_after2](https://github.com/DiamondLightSource/discorpy/raw/master/data/demo/i12_data_2.jpg) - Apply to an X-ray dot-target collected at [Beamline I13](https://www.diamond.ac.uk/Instruments/Imaging-and-Microscopy/I13/Diamond-Manchester_Imaging_Branchline/Facilities_and_equipment_Imaging.html), Diamond Light Source, UK: -![I13_before_after1](data/demo/i13_data_1.jpg) +![I13_before_after1](https://github.com/DiamondLightSource/discorpy/raw/master/data/demo/i13_data_1.jpg) -![I13_before_after2](data/demo/i13_data_2.jpg) +![I13_before_after2](https://github.com/DiamondLightSource/discorpy/raw/master/data/demo/i13_data_2.jpg) - Improvement of a tomographic reconstructed image after distortion correction. + Before the correction: - ![tomo_before](data/demo/recon_before.jpg) + ![tomo_before](https://github.com/DiamondLightSource/discorpy/raw/master/data/demo/recon_before.jpg) + After the correction: - ![tomo_before](data/demo/recon_after.jpg) + ![tomo_before](https://github.com/DiamondLightSource/discorpy/raw/master/data/demo/recon_after.jpg) - Apply to a hazard camera of the [Mars Perseverance Rover](https://mars.nasa.gov/mars2020/multimedia/raw-images/). Details of how to estimate distortion coefficients of that camera without using a calibration target are shown [here](https://github.com/DiamondLightSource/discorpy/blob/master/examples/Perseverance_distortion_correction/Distortion_correction_for_Perseverance_camera.md) -![Mars_rover](data/demo/Mars_Rover_camera.jpg) +![Mars_rover](https://github.com/DiamondLightSource/discorpy/raw/master/data/demo/Mars_Rover_camera.jpg) diff --git a/discorpy/losa/loadersaver.py b/discorpy/losa/loadersaver.py index 023496b..5a466b9 100644 --- a/discorpy/losa/loadersaver.py +++ b/discorpy/losa/loadersaver.py @@ -85,6 +85,114 @@ def _get_key(name, obj): return key_path +def get_hdf_information(file_path): + """ + Get information of datasets in a hdf/nxs file. + + Parameters + ---------- + file_path : str + Path to the file. + + Returns + ------- + list_key : str + Keys to the datasets. + list_shape : tuple of int + Shapes of the datasets. + list_type : str + Types of the datasets. + """ + ifile = h5py.File(file_path, 'r') + keys = [] + ifile.visit(keys.append) + list_key = [] + list_shape = [] + list_type = [] + for key in keys: + data = ifile[key] + if isinstance(data, h5py.Group): + for key2, _ in list(data.items()): + list_key.append(key + "/" + key2) + else: + list_key.append(data.name) + for i, key in enumerate(list_key): + data = ifile[list_key[i]] + try: + shape = data.shape + except AttributeError: + shape = "None" + try: + dtype = data.dtype + except AttributeError: + dtype = "None" + if isinstance(data, list): + if len(data) == 1: + if not isinstance(data, np.ndarray): + dtype = str(list(data)[0]) + dtype = dtype.replace("b'", "'") + list_shape.append(shape) + list_type.append(dtype) + ifile.close() + return list_key, list_shape, list_type + + +def find_hdf_key(file_path, pattern): + """ + Find datasets matching the pattern in a hdf/nxs file. + + Parameters + ---------- + file_path : str + Path to the file. + pattern : str + Pattern to find the full names of the datasets. + + Returns + ------- + list_key : str + Keys to the datasets. + list_shape : tuple of int + Shapes of the datasets. + list_type : str + Types of the datasets. + """ + ifile = h5py.File(file_path, 'r') + list_key = [] + keys = [] + ifile.visit(keys.append) + for key in keys: + data = ifile[key] + if isinstance(data, h5py.Group): + for key2, _ in list(data.items()): + list_key.append(data.name + "/" + key2) + else: + list_key.append(data.name) + list_dkey = [] + list_dshape = [] + list_dtype = [] + for _, key in enumerate(list_key): + if pattern in key: + list_dkey.append(key) + data = ifile[key] + try: + shape = data.shape + except AttributeError: + shape = "None" + list_dshape.append(shape) + try: + dtype = data.dtype + except AttributeError: + dtype = "None" + list_dtype.append(dtype) + if isinstance(data, list): + if len(data) == 1: + dtype = str(list(data)[0]) + dtype = dtype.replace("b'", "'") + ifile.close() + return list_dkey, list_dshape, list_dtype + + def load_hdf_file(file_path, key_path=None, index=None, axis=0): """ Load data from a hdf5/nxs file. diff --git a/setup.py b/setup.py index 399962a..1eee452 100755 --- a/setup.py +++ b/setup.py @@ -1,11 +1,17 @@ +import pathlib import setuptools +HERE = pathlib.Path(__file__).parent +README = (HERE / "README.md").read_text() + setuptools.setup( name="discorpy", version="1.3", author="Nghia Vo", author_email="nghia.vo@diamond.ac.uk", description='Radial lens distortion correction in Python', + long_description=README, + long_description_content_type="text/markdown", keywords=['Distortion correction', 'Tomography', 'Radial lens distortion', 'Camera calibration'], url="https://github.com/DiamondLightSource/discorpy",