Skip to content

Commit

Permalink
SDI support for CropImagesModule (#419)
Browse files Browse the repository at this point in the history
* Added IFS (4D data) support to CorpImageModule.

* Added testing of IFS data to CropImagesModule

Co-authored-by: Tomas Stolker <tomas.stolker@phys.ethz.ch>
  • Loading branch information
Kiefersv and Tomas Stolker committed Aug 10, 2020
1 parent f95f83d commit d9e538d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
36 changes: 30 additions & 6 deletions pynpoint/processing/resizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,21 @@ def run(self) -> None:
# Get memory and number of images to split the frames into chunks
memory = self._m_config_port.get_attribute('MEMORY')
nimages = self.m_image_in_port.get_shape()[0]
frames = memory_frames(memory, nimages)

# Get the numnber of dimensions and shape
ndim = self.m_image_in_port.get_ndim()
im_shape = self.m_image_in_port.get_shape()

if ndim == 3:
# Number of images
nimages = im_shape[-3]

# Split into batches to comply with memory constraints
frames = memory_frames(memory, nimages)

elif ndim == 4:
# Process all wavelengths per exposure at once
frames = np.linspace(0, im_shape[-3], im_shape[-3]+1)

# Convert size parameter from arcseconds to pixels
pixscale = self.m_image_in_port.get_attribute('PIXSCALE')
Expand All @@ -96,13 +110,23 @@ def run(self) -> None:

# Update progress bar
progress(i, len(frames[:-1]), 'Cropping images...', start_time)

# Select and crop images in the current chunk
images = self.m_image_in_port[frames[i]:frames[i+1], ]

# Select images in the current chunk
if ndim == 3:
images = self.m_image_in_port[frames[i]:frames[i+1], ]

elif ndim == 4:
# Process all wavelengths per exposure at once
images = self.m_image_in_port[:, i, ]

# crop images according to input parameters
images = crop_image(images, self.m_center, self.m_size, copy=False)

# Write cropped images to output port
self.m_image_out_port.append(images, data_dim=3)
# Write processed images to output port
if ndim == 3:
self.m_image_out_port.append(images, data_dim=3)
elif ndim == 4:
self.m_image_out_port.append(images, data_dim=4)

# Save history and copy attributes
history = f'image size (pix) = {self.m_size}'
Expand Down
32 changes: 30 additions & 2 deletions tests/test_processing/test_resizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pynpoint.readwrite.fitsreading import FitsReadingModule
from pynpoint.processing.resizing import CropImagesModule, ScaleImagesModule, \
AddLinesModule, RemoveLinesModule
from pynpoint.util.tests import create_config, create_star_data, remove_test_data
from pynpoint.util.tests import create_config, create_star_data, create_ifs_data, remove_test_data


class TestResizing:
Expand All @@ -19,13 +19,14 @@ def setup_class(self) -> None:
self.test_dir = os.path.dirname(__file__) + '/'

create_star_data(self.test_dir+'resize')
create_ifs_data(self.test_dir+'resize_ifs')
create_config(self.test_dir+'PynPoint_config.ini')

self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

def teardown_class(self) -> None:

remove_test_data(self.test_dir, folders=['resize'])
remove_test_data(self.test_dir, folders=['resize', 'resize_ifs'])

def test_read_data(self) -> None:

Expand All @@ -42,6 +43,20 @@ def test_read_data(self) -> None:
assert np.sum(data) == pytest.approx(105.54278879805277, rel=self.limit, abs=0.)
assert data.shape == (10, 11, 11)

module = FitsReadingModule(name_in='read_ifs',
image_tag='read_ifs',
input_dir=self.test_dir+'resize_ifs',
overwrite=True,
check=True,
ifs_data=True)

self.pipeline.add_module(module)
self.pipeline.run_module('read_ifs')

data = self.pipeline.get_data('read_ifs')
assert np.sum(data) == pytest.approx(749.8396528807369, rel=self.limit, abs=0.)
assert data.shape == (3, 10, 21, 21)

def test_crop_images(self) -> None:

module = CropImagesModule(size=0.2,
Expand All @@ -62,6 +77,15 @@ def test_crop_images(self) -> None:
self.pipeline.add_module(module)
self.pipeline.run_module('crop2')

module = CropImagesModule(size=0.2,
center=(4, 4),
name_in='crop_ifs',
image_in_tag='read_ifs',
image_out_tag='crop_ifs')

self.pipeline.add_module(module)
self.pipeline.run_module('crop_ifs')

data = self.pipeline.get_data('crop1')
assert np.sum(data) == pytest.approx(104.93318507061295, rel=self.limit, abs=0.)
assert data.shape == (10, 9, 9)
Expand All @@ -70,6 +94,10 @@ def test_crop_images(self) -> None:
assert np.sum(data) == pytest.approx(105.64863165433025, rel=self.limit, abs=0.)
assert data.shape == (10, 9, 9)

data = self.pipeline.get_data('crop_ifs')
assert np.sum(data) == pytest.approx(15.870936600122521, rel=self.limit, abs=0.)
assert data.shape == (3, 10, 9, 9)

def test_scale_images(self) -> None:

module = ScaleImagesModule(scaling=(2., 2., None),
Expand Down

0 comments on commit d9e538d

Please sign in to comment.