|
| 1 | +"""Extension module to process files with Voxcell.""" |
| 2 | +from dir_content_diff import register_comparator |
| 3 | +from dir_content_diff.base_comparators import BaseComparator |
| 4 | +from dir_content_diff.comparators.pandas import DataframeComparator |
| 5 | +from dir_content_diff.util import import_error_message |
| 6 | + |
| 7 | +try: |
| 8 | + import numpy as np |
| 9 | + from voxcell import CellCollection |
| 10 | + from voxcell import VoxelData |
| 11 | +except ImportError: # pragma: no cover |
| 12 | + import_error_message(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class NrrdComparator(BaseComparator): |
| 16 | + """Comparator for NRRD files.""" |
| 17 | + |
| 18 | + def load(self, path, **kwargs): |
| 19 | + """Load a NRRD file into a :class:`numpy.ndarray`.""" |
| 20 | + return VoxelData.load_nrrd(str(path), **kwargs) |
| 21 | + |
| 22 | + def save(self, data, path, **kwargs): |
| 23 | + """Save data to a NRRD file.""" |
| 24 | + return data.save_nrrd(str(path), **kwargs) |
| 25 | + |
| 26 | + def format_diff(self, difference, **kwargs): |
| 27 | + """Format one element difference.""" |
| 28 | + k, v = difference |
| 29 | + return f"\n{k}: {v}" |
| 30 | + |
| 31 | + def sort(self, differences, **kwargs): |
| 32 | + """Do not sort the entries to keep voxel dimensions as first entry.""" |
| 33 | + return differences |
| 34 | + |
| 35 | + def diff(self, ref, comp, *args, precision=None, **kwargs): |
| 36 | + """Compare data from two NRRD files. |
| 37 | +
|
| 38 | + Note: NRRD files can contain their creation date, so their hashes are depends on |
| 39 | + this creation date, even if the actual data are the same. This comparator only compares the |
| 40 | + actual data in the files. |
| 41 | +
|
| 42 | + Args: |
| 43 | + ref_path (str): The path to the reference CSV file. |
| 44 | + comp_path (str): The path to the compared CSV file. |
| 45 | + precision (int): The desired precision, default is exact precision. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + bool or list(str): ``False`` if the DataFrames are considered as equal or a list of |
| 49 | + strings explaining why they are not considered as equal. |
| 50 | + """ |
| 51 | + errors = {} |
| 52 | + |
| 53 | + try: |
| 54 | + if precision is not None: |
| 55 | + np.testing.assert_array_almost_equal( |
| 56 | + ref.voxel_dimensions, comp.voxel_dimensions, *args, decimal=precision, **kwargs |
| 57 | + ) |
| 58 | + else: |
| 59 | + np.testing.assert_array_equal( |
| 60 | + ref.voxel_dimensions, comp.voxel_dimensions, *args, **kwargs |
| 61 | + ) |
| 62 | + except AssertionError as exception: |
| 63 | + errors["Voxel dimensions"] = exception.args[0] |
| 64 | + |
| 65 | + try: |
| 66 | + if precision is not None: |
| 67 | + np.testing.assert_array_almost_equal( |
| 68 | + ref.raw, comp.raw, *args, decimal=precision, **kwargs |
| 69 | + ) |
| 70 | + else: |
| 71 | + np.testing.assert_array_equal(ref.raw, comp.raw, *args, **kwargs) |
| 72 | + except AssertionError as exception: |
| 73 | + errors["Internal raw data"] = exception.args[0] |
| 74 | + |
| 75 | + if len(errors) == 0: |
| 76 | + return False |
| 77 | + return errors |
| 78 | + |
| 79 | + def report(self, ref_file, comp_file, formatted_differences, diff_args, diff_kwargs, **kwargs): |
| 80 | + """Create a report from the formatted differences.""" |
| 81 | + # pylint: disable=arguments-differ |
| 82 | + if "precision" not in diff_kwargs: |
| 83 | + diff_kwargs["precision"] = None |
| 84 | + return super().report( |
| 85 | + ref_file, |
| 86 | + comp_file, |
| 87 | + formatted_differences, |
| 88 | + diff_args, |
| 89 | + diff_kwargs, |
| 90 | + **kwargs, |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +class Mvd3Comparator(DataframeComparator): |
| 95 | + """Comparator for MVD3 files. |
| 96 | +
|
| 97 | + Note: MVD3 files can contain their creation date, so their hashes are depends on |
| 98 | + this creation date, even if the data are the same. |
| 99 | +
|
| 100 | + This comparator inherits from the :class:`dir_content_diff.pandas.DataframeComparator`, read |
| 101 | + the doc of this comparator for details on args and kwargs. |
| 102 | + """ |
| 103 | + |
| 104 | + def load(self, path, **kwargs): |
| 105 | + """Load a MVD3 file into a :class:`pandas.DataFrame`.""" |
| 106 | + return CellCollection.load_mvd3(path, **kwargs).as_dataframe() |
| 107 | + |
| 108 | + def save(self, data, path, **kwargs): |
| 109 | + """Save data to a CellCollection file.""" |
| 110 | + return CellCollection.from_dataframe(data).save_mvd3(path, **kwargs) |
| 111 | + |
| 112 | + |
| 113 | +class CellCollectionComparator(DataframeComparator): |
| 114 | + """Comparator for any type of CellCollection file. |
| 115 | +
|
| 116 | + This comparator inherits from the :class:`dir_content_diff.pandas.DataframeComparator`, read |
| 117 | + the doc of this comparator for details on args and kwargs. |
| 118 | + """ |
| 119 | + |
| 120 | + def load(self, path, **kwargs): |
| 121 | + """Load a CellCollection file into a :class:`pandas.DataFrame`.""" |
| 122 | + return CellCollection.load(path, **kwargs).as_dataframe() |
| 123 | + |
| 124 | + def save(self, data, path, **kwargs): |
| 125 | + """Save data to a CellCollection file.""" |
| 126 | + return CellCollection.from_dataframe(data).save(path, **kwargs) |
| 127 | + |
| 128 | + |
| 129 | +def register(): |
| 130 | + """Register Voxcell extensions.""" |
| 131 | + register_comparator(".nrrd", NrrdComparator()) |
| 132 | + register_comparator(".mvd3", Mvd3Comparator()) |
0 commit comments