Skip to content

Commit

Permalink
Merge pull request #316 from tomasstolker/angle
Browse files Browse the repository at this point in the history
Improved angle calculation in StackAndSubsetModule
  • Loading branch information
tomasstolker committed Mar 1, 2019
2 parents b897690 + 83a4c86 commit 08691b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
49 changes: 32 additions & 17 deletions pynpoint/processing/stacksubset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from __future__ import absolute_import

import sys
import math
import cmath
import warnings

import numpy as np

from six.moves import range

from pynpoint.core.processing import ProcessingModule
from pynpoint.util.module import progress, memory_frames
from pynpoint.util.module import progress, memory_frames, number_images_port
from pynpoint.util.image import rotate_images


Expand Down Expand Up @@ -55,15 +57,23 @@ def __init__(self,
self.m_stacking = stacking

if self.m_stacking is None and self.m_random is None:
warnings.warn("Both 'stacking' and 'random' are set to None.")
warnings.warn("Both 'stacking' and 'random' are set to None. No data will be written.")

def run(self):
"""
Run method of the module. Stacks subsets of images and/or selects a random subset.
Run method of the module. Stacks subsets of images and/or selects a random subset. Also
the parallactic angles are mean-combined if images are stacked.
:return: None
"""

def _mean_angle(angles):
cmath_rect = sum(cmath.rect(1, math.radians(ang)) for ang in angles)
cmath_phase = cmath.phase(cmath_rect/len(angles))

return math.degrees(cmath_phase)


def _stack(nimages, im_shape, parang):
im_new = None
parang_new = None
Expand All @@ -84,7 +94,8 @@ def _stack(nimages, im_shape, parang):
progress(i, nimages_new, "Running StackAndSubsetModule...")

if parang is not None:
parang_new[i] = np.mean(parang[frames[i]:frames[i+1]])
# parang_new[i] = np.mean(parang[frames[i]:frames[i+1]])
parang_new[i] = _mean_angle(parang[frames[i]:frames[i+1]])

im_new[i, ] = np.mean(self.m_image_in_port[frames[i]:frames[i+1], ], axis=0)

Expand All @@ -99,19 +110,21 @@ def _stack(nimages, im_shape, parang):
def _subset(im_shape, im_new, parang_new):
if self.m_random is not None:
choice = np.random.choice(im_shape[0], self.m_random, replace=False)
choice = np.sort(choice)
choice = list(np.sort(choice))

if parang_new is None:
parang_new = None
else:
parang_new = parang_new[choice]

if self.m_stacking is None:
im_new = self.m_image_in_port[choice, ]
im_new = self.m_image_in_port[list(choice), ]
else:
im_new = im_new[choice, ]

if im_new.ndim == 2:
if self.m_random is None and self.m_stacking is None:
nimages = 0
elif im_new.ndim == 2:
nimages = 1
elif im_new.ndim == 3:
nimages = im_new.shape[0]
Expand All @@ -121,7 +134,7 @@ def _subset(im_shape, im_new, parang_new):
non_static = self.m_image_in_port.get_all_non_static_attributes()

im_shape = self.m_image_in_port.get_shape()
nimages = im_shape[0]
nimages = number_images_port(self.m_image_in_port)

if self.m_random is not None:
if self.m_stacking is None and im_shape[0] < self.m_random:
Expand All @@ -144,18 +157,20 @@ def _subset(im_shape, im_new, parang_new):
sys.stdout.write("Running StackAndSubsetModule... [DONE]\n")
sys.stdout.flush()

self.m_image_out_port.set_all(im_new, keep_attributes=True)
self.m_image_out_port.copy_attributes(self.m_image_in_port)
self.m_image_out_port.add_attribute("INDEX", np.arange(0, nimages, 1), static=False)
if self.m_random or self.m_stacking:
self.m_image_out_port.set_all(im_new, keep_attributes=True)
self.m_image_out_port.copy_attributes(self.m_image_in_port)
self.m_image_out_port.add_attribute("INDEX", np.arange(0, nimages, 1), static=False)

if parang is not None:
self.m_image_out_port.add_attribute("PARANG", parang_new, static=False)
if parang_new is not None:
self.m_image_out_port.add_attribute("PARANG", parang_new, static=False)

if "NFRAMES" in non_static:
self.m_image_out_port.del_attribute("NFRAMES")
if "NFRAMES" in non_static:
self.m_image_out_port.del_attribute("NFRAMES")

history = "stacking ="+str(self.m_stacking)+", random ="+str(self.m_random)
self.m_image_out_port.add_history("StackAndSubsetModule", history)

history = "stacking ="+str(self.m_stacking)+", random ="+str(self.m_random)
self.m_image_out_port.add_history("StackAndSubsetModule", history)
self.m_image_out_port.close_port()


Expand Down
4 changes: 2 additions & 2 deletions tests/test_processing/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ def test_psf_subtraction(self):
self.pipeline.run_module("psf_subtraction")

data = self.pipeline.get_data("res_mean")
assert np.allclose(data[0, 38, 22], 2.073746596517549e-05, rtol=limit, atol=0.)
assert np.allclose(np.mean(data), -1.5133345435496935e-08, rtol=limit, atol=0.)
assert np.allclose(data[0, 38, 22], 2.073747383344391e-05, rtol=limit, atol=0.)
assert np.allclose(np.mean(data), -1.5133372249623263e-08, rtol=limit, atol=0.)
assert data.shape == (1, 46, 46)

def test_write_fits(self):
Expand Down

0 comments on commit 08691b2

Please sign in to comment.