xradio 1.0.2__py3-none-any.whl → 1.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. xradio/_utils/_casacore/casacore_from_casatools.py +75 -9
  2. xradio/_utils/dict_helpers.py +38 -7
  3. xradio/_utils/list_and_array.py +26 -3
  4. xradio/_utils/schema.py +44 -0
  5. xradio/_utils/xarray_helpers.py +63 -0
  6. xradio/_utils/zarr/common.py +4 -2
  7. xradio/image/__init__.py +4 -2
  8. xradio/image/_util/_casacore/common.py +2 -1
  9. xradio/image/_util/_casacore/xds_from_casacore.py +144 -92
  10. xradio/image/_util/_casacore/xds_to_casacore.py +118 -53
  11. xradio/image/_util/_fits/xds_from_fits.py +125 -37
  12. xradio/image/_util/_zarr/common.py +0 -1
  13. xradio/image/_util/casacore.py +183 -25
  14. xradio/image/_util/common.py +10 -8
  15. xradio/image/_util/image_factory.py +469 -27
  16. xradio/image/image.py +72 -100
  17. xradio/image/image_xds.py +262 -0
  18. xradio/image/schema.py +85 -0
  19. xradio/measurement_set/__init__.py +5 -4
  20. xradio/measurement_set/_utils/_msv2/_tables/read.py +4 -3
  21. xradio/measurement_set/_utils/_msv2/conversion.py +6 -9
  22. xradio/measurement_set/_utils/_msv2/create_field_and_source_xds.py +1 -0
  23. xradio/measurement_set/_utils/_msv2/msv4_sub_xdss.py +1 -1
  24. xradio/measurement_set/_utils/_utils/interpolate.py +5 -0
  25. xradio/measurement_set/_utils/_utils/partition_attrs.py +0 -1
  26. xradio/measurement_set/convert_msv2_to_processing_set.py +9 -9
  27. xradio/measurement_set/load_processing_set.py +2 -2
  28. xradio/measurement_set/measurement_set_xdt.py +83 -93
  29. xradio/measurement_set/open_processing_set.py +1 -1
  30. xradio/measurement_set/processing_set_xdt.py +33 -26
  31. xradio/schema/check.py +70 -19
  32. xradio/schema/common.py +0 -1
  33. xradio/testing/__init__.py +0 -0
  34. xradio/testing/_utils/__template__.py +58 -0
  35. xradio/testing/measurement_set/__init__.py +58 -0
  36. xradio/testing/measurement_set/checker.py +131 -0
  37. xradio/testing/measurement_set/io.py +22 -0
  38. xradio/testing/measurement_set/msv2_io.py +1854 -0
  39. {xradio-1.0.2.dist-info → xradio-1.1.1.dist-info}/METADATA +65 -23
  40. xradio-1.1.1.dist-info/RECORD +75 -0
  41. {xradio-1.0.2.dist-info → xradio-1.1.1.dist-info}/WHEEL +1 -1
  42. xradio-1.0.2.dist-info/RECORD +0 -66
  43. {xradio-1.0.2.dist-info → xradio-1.1.1.dist-info}/licenses/LICENSE.txt +0 -0
  44. {xradio-1.0.2.dist-info → xradio-1.1.1.dist-info}/top_level.txt +0 -0
@@ -29,6 +29,24 @@ except ModuleNotFoundError as exc:
29
29
  f"{exc}"
30
30
  )
31
31
 
32
+ # Valid casacore ImageInfo enum values
33
+ # Reference: https://casacore.github.io/casacore/classcasacore_1_1ImageInfo.html
34
+ _VALID_IMAGE_TYPES = (
35
+ "Undefined",
36
+ "Intensity",
37
+ "Beam",
38
+ "ColumnDensity",
39
+ "DepolarizationRatio",
40
+ "KineticTemperature",
41
+ "MagneticField",
42
+ "OpticalDepth",
43
+ "RotationMeasure",
44
+ "RotationalTemperature",
45
+ "SpectralIndex",
46
+ "Velocity",
47
+ "VelocityDispersion",
48
+ )
49
+
32
50
  import numpy as np
33
51
  import toolviper.utils.logger as logger
34
52
 
@@ -166,6 +184,32 @@ def wrap_class_methods(cls: type) -> type:
166
184
  return cls
167
185
 
168
186
 
187
+ def _validate_image_type(value: str) -> str:
188
+ """Validate and normalize an image type string.
189
+
190
+ Parameters
191
+ ----------
192
+ value
193
+ The image type string to validate.
194
+
195
+ Returns
196
+ -------
197
+ str
198
+ A valid casacore ImageInfo enum value with proper capitalization.
199
+ Returns 'Intensity' if the input is not a valid type.
200
+
201
+ Notes
202
+ -----
203
+ Validation is case-insensitive. The returned string uses the
204
+ canonical capitalization from the casacore ImageInfo enum.
205
+ """
206
+ value_lower = value.lower()
207
+ for valid_type in _VALID_IMAGE_TYPES:
208
+ if valid_type.lower() == value_lower:
209
+ return valid_type
210
+ return "Intensity"
211
+
212
+
169
213
  @wrap_class_methods
170
214
  class table(casatools.table):
171
215
  """A wrapper for the casatools table object.
@@ -513,7 +557,7 @@ class image(casatools.image):
513
557
  self,
514
558
  imagename,
515
559
  axis=0,
516
- maskname="mask0",
560
+ maskname="MASK_0",
517
561
  images=(),
518
562
  values=None,
519
563
  coordsys=None,
@@ -702,18 +746,36 @@ class image(casatools.image):
702
746
  def imageinfo(self) -> dict:
703
747
  """Retrieve metadata from the image table.
704
748
 
705
- This method accesses the image table associated with the image name
706
- and attempts to retrieve information stored under the 'imageinfo'
707
- keyword. If the 'imageinfo' keyword is not found in the table,
708
- a default dictionary containing basic information is returned.
749
+ Accesses the image table and retrieves information stored under the
750
+ 'imageinfo' keyword. The 'imagetype' value is validated against
751
+ casacore's ImageInfo enumeration values to mimic python-casacore
752
+ `image.imageinfo()` behavior.
709
753
 
710
754
  Returns
711
755
  -------
712
756
  dict
713
- A dictionary containing image metadata. This is either the
714
- value associated with the 'imageinfo' keyword in the table,
715
- or a default dictionary {'imagetype': 'Intensity',
716
- 'objectname': ''} if the keyword is absent.
757
+ Image metadata dictionary containing:
758
+
759
+ - **imagetype** : str
760
+ Type of the image, validated against casacore ImageInfo enum.
761
+ Defaults to 'Intensity' if invalid or missing.
762
+ - **objectname** : str
763
+ Name of the observed object.
764
+
765
+ Notes
766
+ -----
767
+ image.info()['imageinfo'] and image.imageinfo() from python-casacore
768
+ always returns "imagetype" in a predefined enum value. When the "imageinfo"
769
+ keyword is missing from the image table, or a non-standard value of "imagetype"
770
+ (e.g. 'sky') was written into that keyword, image.info() will just return
771
+ "Intensity" as the imagetype.
772
+
773
+ Examples
774
+ --------
775
+ >>> img = image('my_image.im')
776
+ >>> info = img.imageinfo()
777
+ >>> info['imagetype']
778
+ 'Intensity'
717
779
  """
718
780
  with table(self._imagename) as tb:
719
781
  if "imageinfo" in tb.keywordnames():
@@ -721,6 +783,10 @@ class image(casatools.image):
721
783
  else:
722
784
  image_metadata = {"imagetype": "Intensity", "objectname": ""}
723
785
 
786
+ image_metadata["imagetype"] = _validate_image_type(
787
+ image_metadata.get("imagetype", "Intensity")
788
+ )
789
+
724
790
  return image_metadata
725
791
 
726
792
  def datatype(self):
@@ -1,3 +1,6 @@
1
+ from xradio._utils.list_and_array import to_python_type
2
+
3
+
1
4
  def make_quantity(value, units: str, dims: list = []) -> dict:
2
5
  """
3
6
  create a quantity dictionary given value and units
@@ -13,7 +16,11 @@ def make_quantity(value, units: str, dims: list = []) -> dict:
13
16
  -------
14
17
  dict
15
18
  """
16
- return {"data": value, "dims": dims, "attrs": make_quantity_attrs(units)}
19
+ return {
20
+ "data": to_python_type(value),
21
+ "dims": dims,
22
+ "attrs": make_quantity_attrs(units),
23
+ }
17
24
 
18
25
 
19
26
  def ensure_units_are_consistent(units):
@@ -67,7 +74,7 @@ def make_spectral_coord_reference_dict(
67
74
  u,
68
75
  observer.lower() if observer not in ["TOPO", "BARY", "REST"] else observer,
69
76
  ),
70
- "data": value,
77
+ "data": to_python_type(value),
71
78
  "dims": [],
72
79
  }
73
80
 
@@ -98,14 +105,38 @@ def make_skycoord_dict(data: list[float], units: str, frame: str) -> dict:
98
105
  "type": "sky_coord",
99
106
  "units": ensure_units_are_consistent(units),
100
107
  },
101
- "data": data,
102
- "dims": ["l", "m"],
108
+ "data": to_python_type(data),
109
+ "dims": "sky_dir_label",
110
+ "coords": {"sky_dir_label": {"data": ["ra", "dec"], "dims": "sky_dir_label"}},
111
+ }
112
+
113
+
114
+ def make_direction_location_dict(data: list[float], units: str, frame: str) -> dict:
115
+ return {
116
+ "attrs": {
117
+ "frame": frame.upper(),
118
+ "type": "location",
119
+ "units": ensure_units_are_consistent(units),
120
+ },
121
+ "data": to_python_type(data),
122
+ "dims": "ellipsoid_dir_label",
123
+ "coords": {
124
+ "ellipsoid_dir_label": {
125
+ "data": ["lon", "lat"],
126
+ "dims": "ellipsoid_dir_label",
127
+ }
128
+ },
103
129
  }
104
130
 
105
131
 
106
132
  def make_time_measure_attrs(units="s", scale="utc", time_format="mjd") -> dict:
107
133
  u = ensure_units_are_consistent(units)
108
- return {"units": u, "scale": scale, "format": time_format, "type": "time"}
134
+ return {
135
+ "units": u,
136
+ "scale": scale.lower(),
137
+ "format": time_format.lower(),
138
+ "type": "time",
139
+ }
109
140
 
110
141
 
111
142
  def make_time_measure_dict(data, units="s", scale="utc", time_format="mjd") -> dict:
@@ -127,7 +158,7 @@ def make_time_measure_dict(data, units="s", scale="utc", time_format="mjd") -> d
127
158
  """
128
159
  x = {}
129
160
  x["attrs"] = make_time_measure_attrs(units, scale, time_format)
130
- x["data"] = data
161
+ x["data"] = to_python_type(data)
131
162
  x["dims"] = []
132
163
  return x
133
164
 
@@ -149,7 +180,7 @@ def make_time_coord_attrs(units="s", scale="utc", time_format="mjd") -> dict:
149
180
  -------
150
181
  dict
151
182
  """
152
- x = make_time_measure_attrs(units, scale, time_format)
183
+ x = make_time_measure_attrs(units, scale.lower(), time_format.lower())
153
184
  del x["type"]
154
185
  return x
155
186
 
@@ -53,11 +53,34 @@ def get_pad_value(col_dtype: np.dtype) -> object:
53
53
  )
54
54
 
55
55
 
56
+ def to_python_type(x):
57
+ """
58
+ Convert any NumPy scalar, array, or nested structure to native Python types.
59
+
60
+ - np.float32, np.float64 → float
61
+ - np.int32, np.int64 → int
62
+ - np.bool_ → bool
63
+ - np.ndarray → list of Python-native types
64
+ - nested containers (list/tuple/dict) are handled recursively
65
+ """
66
+ if isinstance(x, np.generic): # covers all numpy scalar types
67
+ return x.item()
68
+ elif isinstance(x, np.ndarray):
69
+ return x.tolist()
70
+ elif isinstance(x, (list, tuple)):
71
+ return type(x)(to_python_type(v) for v in x)
72
+ elif isinstance(x, dict):
73
+ return {k: to_python_type(v) for k, v in x.items()}
74
+ else:
75
+ return x
76
+
77
+
56
78
  def to_list(x):
57
79
  if isinstance(x, np.ndarray):
58
- if x.ndim == 0:
59
- return [x.item()]
60
- return list(x) # needed for json serialization
80
+ z = x.astype(float)
81
+ if z.ndim == 0:
82
+ return [z.item()]
83
+ return list(z) # needed for json serialization
61
84
  elif isinstance(x, list):
62
85
  return x
63
86
  return [x]
xradio/_utils/schema.py CHANGED
@@ -224,3 +224,47 @@ casa_frequency_frames = [
224
224
  ]
225
225
 
226
226
  casa_frequency_frames_codes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 64]
227
+
228
+
229
+ def get_data_group_keys(schema_name: str) -> list[tuple[str, bool]]:
230
+ """Return (name, is_optional) pairs for data group keys for the given schema.
231
+
232
+ Parameters
233
+ ----------
234
+ schema_name : str
235
+ The name of the schema to retrieve data group keys for. Supported values are "msv4" and "image".
236
+
237
+ Returns
238
+ -------
239
+ list[tuple[str, bool]]
240
+ A list of tuples (key_name, is_optional) for the specified schema.
241
+
242
+ Raises
243
+ ------
244
+ ValueError
245
+ If the schema name is unknown.
246
+ """
247
+ from typing import get_type_hints, get_origin, get_args, Union
248
+
249
+ if schema_name == "msv4":
250
+ from xradio.measurement_set.schema import DataGroupDict
251
+
252
+ cls = DataGroupDict
253
+ elif schema_name == "image":
254
+ from xradio.image.schema import DataGroupDict
255
+
256
+ cls = DataGroupDict
257
+ else:
258
+ raise ValueError(f"Unknown schema name: {schema_name}")
259
+
260
+ annotations = get_type_hints(cls)
261
+ keys_with_optional = {}
262
+ for name, anno in annotations.items():
263
+ origin = get_origin(anno)
264
+ is_optional = False
265
+ if origin is Union:
266
+ args = get_args(anno)
267
+ is_optional = type(None) in args
268
+ keys_with_optional[name] = is_optional
269
+
270
+ return keys_with_optional
@@ -0,0 +1,63 @@
1
+ import xarray as xr
2
+ from xradio._utils.schema import get_data_group_keys
3
+ from collections.abc import Mapping, Iterable
4
+ from typing import Any, Union
5
+
6
+
7
+ def get_data_group_name(
8
+ xdx: Union[xr.Dataset, xr.DataTree], data_group_name: str = None
9
+ ) -> str:
10
+
11
+ if data_group_name is None:
12
+ if "base" in xdx.attrs["data_groups"]:
13
+ data_group_name = "base"
14
+ else:
15
+ data_group_name = list(xdx.attrs["data_groups"].keys())[0]
16
+
17
+ return data_group_name
18
+
19
+
20
+ def create_new_data_group(
21
+ xdx: Union[xr.Dataset, xr.DataTree],
22
+ schema_name: str,
23
+ new_data_group_name: str,
24
+ data_group: dict,
25
+ data_group_dv_shared_with: str = None,
26
+ ) -> xr.DataTree:
27
+ """Adds a data group to Xarray Data Structure (Dataset or DataTree).
28
+
29
+ Parameters
30
+ ----------
31
+ new_data_group_name : str
32
+ The name of the new data group to add.
33
+ data_group : dict
34
+ A dictionary containing the data group variables and their attributes.
35
+ data_group_dv_shared_with : str, optional
36
+ The name of the data group to share data variables with, by default "base"
37
+
38
+ Returns
39
+ -------
40
+ xr.DataTree
41
+ MSv4 DataTree with the new group added
42
+ """
43
+
44
+ data_group_dv_shared_with = get_data_group_name(xdx, data_group_dv_shared_with)
45
+
46
+ default_data_group = xdx.attrs["data_groups"][data_group_dv_shared_with]
47
+
48
+ new_data_group = {}
49
+
50
+ data_group_keys = get_data_group_keys(schema_name)
51
+
52
+ for key, optional in data_group_keys.items():
53
+ if key in data_group:
54
+ new_data_group[key] = data_group[key]
55
+ else:
56
+ if key not in default_data_group and not optional:
57
+ raise ValueError(
58
+ f"Data group key '{key}' is required but not provided and not present in shared data group '{data_group_dv_shared_with}'."
59
+ )
60
+ elif key in default_data_group:
61
+ new_data_group[key] = default_data_group[key]
62
+
63
+ return new_data_group_name, new_data_group
@@ -1,7 +1,5 @@
1
1
  import xarray as xr
2
- import s3fs
3
2
  import os
4
- from botocore.exceptions import NoCredentialsError
5
3
 
6
4
  # from xradio.vis._vis_utils._ms.msv2_to_msv4_meta import (
7
5
  # column_description_casacore_to_msv4_measure,
@@ -10,6 +8,9 @@ from botocore.exceptions import NoCredentialsError
10
8
 
11
9
  def _get_file_system_and_items(ps_store: str):
12
10
 
11
+ import s3fs
12
+ from botocore.exceptions import NoCredentialsError
13
+
13
14
  # default to assuming the data are accessible on local file system
14
15
  if os.path.isdir(ps_store):
15
16
  # handle a common shell convention
@@ -74,6 +75,7 @@ def _open_dataset(
74
75
  """
75
76
 
76
77
  import dask
78
+ import s3fs
77
79
 
78
80
  if isinstance(file_system, s3fs.core.S3FileSystem):
79
81
  mapping = s3fs.S3Map(root=store, s3=file_system, check=False)
xradio/image/__init__.py CHANGED
@@ -5,15 +5,17 @@ from .image import (
5
5
  make_empty_aperture_image,
6
6
  make_empty_lmuv_image,
7
7
  make_empty_sky_image,
8
- read_image,
8
+ open_image,
9
9
  write_image,
10
10
  )
11
11
 
12
+ from .image_xds import ImageXds
13
+
12
14
  __all__ = [
13
15
  "load_image",
14
16
  "make_empty_aperture_image",
15
17
  "make_empty_lmuv_image",
16
18
  "make_empty_sky_image",
17
- "read_image",
19
+ "open_image",
18
20
  "write_image",
19
21
  ]
@@ -42,7 +42,8 @@ def _create_new_image(
42
42
  del image
43
43
 
44
44
 
45
- _active_mask = "active_mask"
45
+ _image_flag = "flag"
46
+ _beam_fit_params = "beam_fit_params"
46
47
  # _native_types = ["FREQ", "VRAD", "VOPT", "BETA", "WAVE", "AWAV"]
47
48
  _object_name = "object_name"
48
49
  _pointing_center = "pointing_center"