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

fix error in npol_to_mzp transformation #158

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Changes from 1 commit
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
53 changes: 27 additions & 26 deletions solpolpy/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,40 @@
Notes
------
Equation 44 in DeForest et al. 2022.

"""""
"""
input_keys = list(input_collection.keys())
input_dict = {}
in_list = list(input_collection)
phi = [input_collection[key].meta['POLAR'] for key in input_keys if key != 'alpha']*u.degree
jmbhughes marked this conversation as resolved.
Show resolved Hide resolved
mzp_angles = [-60, 0, 60] * u.degree # theta angle in Eq 44

for p_angle in in_list:
if p_angle == "alpha":
break
input_dict[u.Quantity(p_angle)] = input_collection[p_angle].data
data_shape = input_collection[input_keys[0]].data.shape
data_npol = np.zeros([data_shape[0], data_shape[1], 3, 1])

mzp_angles = [-60, 0, 60] * u.degree
Bmzp = {}
for target_angle in mzp_angles:
Bmzp[target_angle] = ((1 / 3) * np.sum([ith_polarizer_brightness * (1 + 2 * np.cos(2 * (target_angle - (ith_angle-offset_angle))))
for ith_angle, ith_polarizer_brightness in input_dict.items()], axis=0))

# TODO: update header properly; time info?
metaM, metaZ, metaP = (copy.copy(input_collection[input_keys[0]].meta),
copy.copy(input_collection[input_keys[0]].meta),
copy.copy(input_collection[input_keys[0]].meta))
metaM.update(POLAR=-60*u.degree)
metaZ.update(POLAR=0*u.degree)
metaP.update(POLAR=60*u.degree)
conv_matrix = np.array([[(4 * np.cos(phi[i] - mzp_angles[j] - offset_angle) ** 2 - 1) / 3
for j in range(3)] for i in range(3)])

for i, key in enumerate(key for key in input_keys if key != 'alpha'):
data_npol[:, :, i, 0] = input_collection[key].data

try:
conv_matrix_inv = np.linalg.inv(conv_matrix)
except np.linalg.LinAlgError as err:
if "Singular matrix" in str(err):
raise ValueError("Conversion matrix is degenerate")

Check warning on line 83 in solpolpy/transforms.py

View check run for this annotation

Codecov / codecov/patch

solpolpy/transforms.py#L81-L83

Added lines #L81 - L83 were not covered by tests
jmbhughes marked this conversation as resolved.
Show resolved Hide resolved
else:
raise

Check warning on line 85 in solpolpy/transforms.py

View check run for this annotation

Codecov / codecov/patch

solpolpy/transforms.py#L85

Added line #L85 was not covered by tests
jmbhughes marked this conversation as resolved.
Show resolved Hide resolved

data_mzp_solar = np.matmul(conv_matrix_inv, data_npol)

meta = copy.copy(input_collection[input_keys[0]].meta)
metas = [{**meta, 'POLAR': angle} for angle in [-60, 0, 60] * u.degree]
mask = combine_all_collection_masks(input_collection)
Bmzp_cube = [("M", NDCube(Bmzp[-60 * u.degree], wcs=input_collection[input_keys[0]].wcs, mask=mask, meta=metaM)),
("Z", NDCube(Bmzp[0 * u.degree], wcs=input_collection[input_keys[0]].wcs, mask=mask, meta=metaZ)),
("P", NDCube(Bmzp[60 * u.degree], wcs=input_collection[input_keys[0]].wcs, mask=mask, meta=metaP))]
for p_angle in in_list:
Bmzp_cube = [(key, NDCube(data_mzp_solar[:, :, i, 0], wcs=input_collection[input_keys[0]].wcs,
jmbhughes marked this conversation as resolved.
Show resolved Hide resolved
mask=mask, meta=metas[i])) for i, key in enumerate(["M", "Z", "P"])]
for p_angle in input_keys:
if p_angle.lower() == "alpha":
Bmzp_cube.append(("alpha", NDCube(input_collection["alpha"].data * u.radian,
wcs=input_collection[input_keys[0]].wcs,
meta=metaP)))
meta=input_collection["alpha"].meta)))
return NDCollection(Bmzp_cube, meta={}, aligned_axes="all")


Expand Down
Loading