Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for initializing MirParser from Path objects #1405

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
## [Unreleased]

### Added
- Added support for initializing `MirParser` and `MirMetaData` objects from `Path`
objects.
- Added a brief tutorial on how to convert SMA MIR data into MS format (with some notes
on SMA specific keywords than can be used).
- Added the `Mir.generate_sma_antpos_dict` method for reading in SMA-formatted antenna
Expand Down
30 changes: 16 additions & 14 deletions pyuvdata/uvdata/mir_meta_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import copy
import os
import warnings
from pathlib import Path

import numpy as np

Expand Down Expand Up @@ -536,13 +537,14 @@ def __init__(

Parameters
----------
obj : str or ndarray or int
Optional argument used to specify how to initialize the object. If a str is
supplied, then it is treated as the path to the Mir data folder containing
the metadata. If an int is supplied, a "blank" (zero-filled) array of
metadata is generated, with length of `obj`. If an ndarray is supplied, then
the supplied array is used as the underlying data set for the object (where
dtype of the array must match that appropriate for the object).
obj : str or Path or ndarray or int
Optional argument used to specify how to initialize the object. If a str or
Path is supplied, then it is treated as the path to the Mir data folder
containing the metadata. If an int is supplied, a "blank" (zero-filled)
array of metadata is generated, with length of `obj`. If an ndarray is
supplied, then the supplied array is used as the underlying data set for the
object (where dtype of the array must match that appropriate for the
object).
filetype : str
Name of the type MirMetaData object, which is then used as the file name
that is read from/written to within the folder specified by the path.
Expand Down Expand Up @@ -570,7 +572,7 @@ def __init__(

if obj is None:
return
if isinstance(obj, str):
if isinstance(obj, (str, Path)):
self.read(filepath=obj)
return

Expand Down Expand Up @@ -2196,7 +2198,7 @@ def _gen_filepath(self, filepath=None, *, check=True, invert_check=False):

Parameters
----------
filepath : str
filepath : str or Path
Either the file to write to, or if providing the name of an existing folder,
the name of the folder to write in (with the file name set by the _filetype
attribute, which is automatically set for various subclasses of
Expand All @@ -2222,8 +2224,8 @@ def _gen_filepath(self, filepath=None, *, check=True, invert_check=False):
FileNotFoundError
If running the check and no file is found (and `invert_check=False`).
"""
if not isinstance(filepath, str):
raise ValueError("filepath must be of type str.")
if not isinstance(filepath, (str, Path)):
raise ValueError("filepath must be of type str or Path.")

if os.path.isdir(filepath):
filepath = os.path.join(os.path.abspath(filepath), self._filetype)
Expand All @@ -2245,7 +2247,7 @@ def read(self, filepath=None):

Parameters
----------
filepath : str
filepath : str or Path
Path of the folder containing the metadata in question.
"""
self._data = np.fromfile(
Expand Down Expand Up @@ -2880,7 +2882,7 @@ def read(self, filepath=None):

Parameters
----------
filepath : str
filepath : str or Path
Path of the folder containing the metadata in question.
"""
with open(self._gen_filepath(filepath), "r") as antennas_file:
Expand Down Expand Up @@ -3414,7 +3416,7 @@ def read(self, filepath=None):

Parameters
----------
filepath : str
filepath : str or Path
Path of the folder containing the metadata in question.
"""
try:
Expand Down
2 changes: 1 addition & 1 deletion pyuvdata/uvdata/mir_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(

Parameters
----------
filepath : str
filepath : str or Path
Filepath is the path to the folder containing the Mir data set.
compass_soln : str
Optional argument, specifying the path of COMPASS-derived flagging and
Expand Down
13 changes: 13 additions & 0 deletions pyuvdata/uvdata/tests/test_mir_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2318,3 +2318,16 @@ def test_mir_remember_me_vis_data(mir_data):
np.all(sp_raw["data"] == check_arr) if (np.mod(idx, 5) == 0) else True
for idx, sp_raw in enumerate(mir_data.raw_data.values())
)


def test_mir_parser_read_path_vs_str():
from pathlib import Path

sma_data_path = str(os.path.join(DATA_PATH, "sma_test.mir"))
sma_str_init = MirParser(
sma_data_path, load_cross=True, load_auto=True, has_auto=True
)
sma_path_init = MirParser(
Path(sma_data_path), load_cross=True, load_auto=True, has_auto=True
)
assert sma_str_init == sma_path_init
Loading