Skip to content

factory

Parse json-encoded transform (experimental).

PARAMETER DESCRIPTION
filepath
        Filepath to json file to generate transform from.

TYPE: str

**kwargs
        Keyword arguments passed to the respective transform class.

DEFAULT: {}

RETURNS DESCRIPTION
transform
Source code in navis/transforms/factory.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def parse_json(filepath: str, **kwargs):
    """Parse json-encoded transform (experimental).

    Parameters
    ----------
    filepath :      str
                    Filepath to json file to generate transform from.
    **kwargs
                    Keyword arguments passed to the respective transform class.

    Returns
    -------
    transform

    """
    fp = pathlib.Path(filepath)

    with open(fp, 'r') as f:
        data = json.load(f)

    if not isinstance(data, list):
        data = []

    transforms = []
    for reg in data:
        if not isinstance(reg, type(dict)):
            raise TypeError(f'{filepath} expected data as dict or list of '
                            f'dicts, got "{type(reg)}"')

        if reg.get('type', None) == 'tpsreg':
            transforms.append(TPStransform(reg['refmat'].values,
                                           reg['tarmat'].values, **kwargs))
        elif reg.get('type', None) == 'affine':
            transforms.append(AffineTransform(reg['affine_matrix'], **kwargs))
        else:
            raise TypeError(f'{reg} has unknown "type"')

    return TransformSequence(*transforms)

Generate appropriate transform from file.

PARAMETER DESCRIPTION
filepath
        Filepath to generate transform from.

TYPE: str

**kwargs
        Keyword arguments passed to the respective transform class.

DEFAULT: {}

RETURNS DESCRIPTION
transform
Source code in navis/transforms/factory.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def transform_factory(filepath: str, **kwargs):
    """Generate appropriate transform from file.

    Parameters
    ----------
    filepath :      str
                    Filepath to generate transform from.
    **kwargs
                    Keyword arguments passed to the respective transform class.

    Returns
    -------
    transform

    """
    fp = pathlib.Path(filepath)

    # Check if file/path exists
    if not fp.is_dir() or not fp.is_file():
        raise ValueError(f'{fp} does not appear to exist')

    if fp.endswith('.list'):
        return CMTKtransform(fp, **kwargs)

    if fp.endswith('.h5'):
        return H5transform(fp, **kwargs)

    # Custom transforms
    if fp.endswith('.json'):
        return parse_json(fp, **kwargs)

    raise TypeError(f'Unknown transform format for {filepath}')