|
1 | 1 | # SPDX-License-Identifier: BSD-3-Clause |
2 | | -# Copyright 2025 MoDaCor Authors |
3 | | -# |
4 | | -# Redistribution and use in source and binary forms, with or without modification, |
5 | | -# are permitted provided that the following conditions are met: |
6 | | -# 1. Redistributions of source code must retain the above copyright notice, this |
7 | | -# list of conditions and the following disclaimer. |
8 | | -# 2. Redistributions in binary form must reproduce the above copyright notice, |
9 | | -# this list of conditions and the following disclaimer in the documentation |
10 | | -# and/or other materials provided with the distribution. |
11 | | -# 3. Neither the name of the copyright holder nor the names of its contributors |
12 | | -# may be used to endorse or promote products derived from this software without |
13 | | -# specific prior written permission. |
14 | | -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND |
15 | | -# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 | | -# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
17 | | -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
18 | | -# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
19 | | -# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
20 | | -# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
21 | | -# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | | -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
23 | | -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | | - |
25 | | -__license__ = "BSD-3-Clause" |
26 | | -__copyright__ = "Copyright 2025 MoDaCor Authors" |
27 | | -__status__ = "Alpha" |
28 | | -__all__ = ["IoSource"] |
29 | | - |
30 | | - |
31 | | -from typing import Any |
| 2 | +# /usr/bin/env python3 |
| 3 | +# -*- coding: utf-8 -*- |
32 | 4 |
|
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +__coding__ = "utf-8" |
| 8 | +__authors__ = ["Malte Storm", "Brian R. Pauw"] # add names to the list as appropriate |
| 9 | +__copyright__ = "Copyright 2025, The MoDaCor team" |
| 10 | +__date__ = "14/06/2025" |
| 11 | +__status__ = "Development" # "Development", "Production" |
| 12 | +# end of header and standard imports |
| 13 | + |
| 14 | +from typing import Any, Optional, Tuple, Union |
| 15 | + |
| 16 | +import numpy as np |
33 | 17 | from attrs import define, field |
34 | 18 |
|
35 | | -from modacor.dataclasses.source_data import SourceData |
| 19 | +# for type hinting of slicing: |
| 20 | +Index = Union[int, slice, type(Ellipsis)] |
| 21 | +ArraySlice = Union[Index, Tuple[Index, ...]] |
36 | 22 |
|
37 | 23 |
|
38 | 24 | def default_config() -> dict[str, Any]: |
@@ -78,21 +64,76 @@ class IoSource: |
78 | 64 |
|
79 | 65 | configuration: dict[str, Any] = field(factory=default_config) |
80 | 66 |
|
81 | | - def get_data(self, index: int, data_key: str) -> SourceData: |
| 67 | + def get_data(self, data_key: str, load_slice: Optional[ArraySlice] = None) -> np.ndarray: |
82 | 68 | """ |
83 | | - Get data and metadata from the IO source using the provided data key. |
| 69 | + Get data from the IO source using the provided data key. |
84 | 70 |
|
85 | 71 | Parameters |
86 | 72 | ---------- |
87 | | - index : int |
88 | | - The index to access the data. |
89 | 73 | data_key : str |
90 | | - The key to access the data. |
| 74 | + The key to access the data, e.g. '/entry1/instrument/detector00/data'. |
| 75 | + load_slice : Optional[ArraySlice] |
| 76 | + A slice or tuple of slices to apply to the data. If None, the entire data is returned. |
| 77 | + Slicing is not yet implemented, so this will raise NotImplementedError if used. |
| 78 | + Consider using the numpy.s_ or numpy.index_exp for simplifying the slicing syntax. |
91 | 79 |
|
92 | 80 | Returns |
93 | 81 | ------- |
94 | 82 | np.ndarray : |
95 | | - The data associated with the provided key. |
| 83 | + The data array associated with the provided key. For scalars, this is a 0-d array. |
| 84 | + """ |
| 85 | + if load_slice is not None: |
| 86 | + raise NotImplementedError("Slicing is not yet implemented.") |
| 87 | + raise NotImplementedError("This method should be implemented in subclasses.") |
| 88 | + |
| 89 | + def get_data_shape(self, data_key: str) -> Tuple[int, ...]: |
| 90 | + """ |
| 91 | + Get the shape of the data from the IO source if the format supports it else empty tuple. |
| 92 | +
|
| 93 | + Parameters |
| 94 | + ---------- |
| 95 | + data_key : str |
| 96 | + The key to the data for which the shape is requested. |
| 97 | +
|
| 98 | + Returns |
| 99 | + ------- |
| 100 | + Tuple[int, ...] : |
| 101 | + The shape of the data associated with the provided key. |
| 102 | + Returns an empty tuple if nothing available or unsupported. |
| 103 | + """ |
| 104 | + raise NotImplementedError("This method should be implemented in subclasses.") |
| 105 | + |
| 106 | + def get_data_dtype(self, data_key: str) -> Optional[np.dtype]: |
| 107 | + """ |
| 108 | + Get the data type of the data from the IO source if the format supports it else None. |
| 109 | +
|
| 110 | + Parameters |
| 111 | + ---------- |
| 112 | + data_key : str |
| 113 | + The key to the data for which the dtype is requested. |
| 114 | +
|
| 115 | + Returns |
| 116 | + ------- |
| 117 | + Optional[np.dtype] : |
| 118 | + The data type of the data associated with the provided key. |
| 119 | + Returns None if nothing available or unsupported. |
| 120 | + """ |
| 121 | + raise NotImplementedError("This method should be implemented in subclasses.") |
| 122 | + |
| 123 | + def get_data_attributes(self, data_key: str) -> dict[str, Any]: |
| 124 | + """ |
| 125 | + Get data attributes from the IO source if the format supports it else empty dict. |
| 126 | +
|
| 127 | + Parameters |
| 128 | + ---------- |
| 129 | + data_key : str |
| 130 | + The key to the data for which attributes are requested. |
| 131 | +
|
| 132 | + Returns |
| 133 | + ------- |
| 134 | + dict[str, Any] : |
| 135 | + The attributes associated with the data. |
| 136 | + Returns an empty dictionary if nothing available or unsupported. |
96 | 137 | """ |
97 | 138 | raise NotImplementedError("This method should be implemented in subclasses.") |
98 | 139 |
|
|
0 commit comments