def test_create_instance(self): with pytest.raises(ValueError) as error: OutputPort("config", self.storage) assert str(error.value) == "The tag name 'config' is reserved for the central " \ "configuration of PynPoint." with pytest.raises(ValueError) as error: OutputPort("fits_header", self.storage) assert str(error.value) == "The tag name 'fits_header' is reserved for storage of the " \ "FITS headers." active_port = OutputPort("test", self.storage, activate_init=True) deactive_port = OutputPort("test", self.storage, activate_init=False) control_port = InputPort("test", self.storage) deactive_port.open_port() deactive_port.set_all(np.asarray([0, 1, 2, 3])) deactive_port.flush() with pytest.warns(UserWarning) as warning: control_port.get_all() assert len(warning) == 1 assert warning[0].message.args[0] == "No data under the tag which is linked by the " \ "InputPort." active_port.set_all(np.asarray([0, 1, 2, 3])) active_port.flush() assert np.array_equal(np.asarray([0, 1, 2, 3]), control_port.get_all()) active_port.del_all_data()
def subtract_psf(image: np.ndarray, im_index: int, parang_thres: Optional[float], nref: Optional[int], reference: Optional[np.ndarray], ang_diff: np.ndarray, image_in_port: InputPort) -> np.ndarray: if parang_thres: index_thres = np.where(ang_diff > parang_thres)[0] if index_thres.size == 0: reference = image_in_port.get_all() warnings.warn('No images meet the rotation threshold. Creating a reference ' 'PSF from the median of all images instead.') else: if nref: index_diff = np.abs(im_index - index_thres) index_near = np.argsort(index_diff)[:nref] index_sort = np.sort(index_thres[index_near]) reference = image_in_port[index_sort, :, :] else: reference = image_in_port[index_thres, :, :] reference = np.median(reference, axis=0) return image-reference
def test_create_instance_access_data(self): with pytest.raises(ValueError) as error: InputPort('config', self.storage) assert str(error.value) == 'The tag name \'config\' is reserved for the central ' \ 'configuration of PynPoint.' with pytest.raises(ValueError) as error: InputPort('fits_header', self.storage) assert str(error.value) == 'The tag name \'fits_header\' is reserved for storage of the ' \ 'FITS headers.' port = InputPort('images', self.storage) assert np.allclose(port[0, 0, 0], 0.00032486907273264834, rtol=limit, atol=0.) assert np.allclose(np.mean(port.get_all()), 1.0506056979365338e-06, rtol=limit, atol=0.) arr_tmp = np.asarray((0.00032486907273264834, -2.4494781298462809e-05, -0.00038631277795631806), dtype=np.float64) assert np.allclose(port[0:3, 0, 0], arr_tmp, rtol=limit, atol=0.) assert len(port[0:2, 0, 0]) == 2 assert port.get_shape() == (10, 100, 100) assert port.get_attribute('PIXSCALE') == 0.01 assert port.get_attribute('PARANG')[0] == 1 with pytest.warns(UserWarning): assert port.get_attribute('none') is None
def test_create_instance_access_data(self) -> None: with pytest.raises(ValueError) as error: InputPort('config', self.storage) assert str(error.value) == 'The tag name \'config\' is reserved for the central ' \ 'configuration of PynPoint.' with pytest.raises(ValueError) as error: InputPort('fits_header', self.storage) assert str(error.value) == 'The tag name \'fits_header\' is reserved for storage of the ' \ 'FITS headers.' port = InputPort('images', self.storage) assert port[0, 0, 0] == pytest.approx(0.00032486907273264834, rel=self.limit, abs=0.) data = np.mean(port.get_all()) assert data == pytest.approx(1.1824138000882435e-05, rel=self.limit, abs=0.) assert len(port[0:2, 0, 0]) == 2 assert port.get_shape() == (5, 11, 11) assert port.get_attribute('PIXSCALE') == 0.01 assert port.get_attribute('PARANG')[0] == 0. with pytest.warns(UserWarning): assert port.get_attribute('none') is None
def test_create_instance_no_data_storage(self): port = InputPort("test") with pytest.warns(UserWarning): assert port[0, 0, 0] is None with pytest.warns(UserWarning): assert port.get_all() is None with pytest.warns(UserWarning): assert port.get_shape() is None with pytest.warns(UserWarning): assert port.get_all_non_static_attributes() is None with pytest.warns(UserWarning): assert port.get_all_static_attributes() is None
def test_create_instance_access_non_existing_data(self): port = InputPort("test", self.storage) with pytest.warns(UserWarning): assert port[0, 0, 0] is None with pytest.warns(UserWarning): assert port.get_all() is None with pytest.warns(UserWarning): assert port.get_shape() is None with pytest.warns(UserWarning): assert port.get_attribute("num_files") is None with pytest.warns(UserWarning): assert port.get_all_non_static_attributes() is None with pytest.warns(UserWarning): assert port.get_all_static_attributes() is None
def apply_function_to_images(self, func: Callable[..., np.ndarray], image_in_port: InputPort, image_out_port: OutputPort, message: str, func_args: Optional[tuple] = None) -> None: """ Function which applies a function to all images of an input port. Stacks of images are processed in parallel if the CPU and MEMORY attribute are set in the central configuration. The number of images per process is equal to the value of MEMORY divided by the value of CPU. Note that the function *func* is not allowed to change the shape of the images if the input and output port have the same tag and ``MEMORY`` is not set to None. Parameters ---------- func : function The function which is applied to all images. Its definitions should be similar to:: def function(image_in, parameter1, parameter2, parameter3) The function must return a numpy array. image_in_port : pynpoint.core.dataio.InputPort Input port which is linked to the input data. image_out_port : pynpoint.core.dataio.OutputPort Output port which is linked to the results. message : str Progress message. func_args : tuple Additional arguments that are required by the input function. Returns ------- NoneType None """ memory = self._m_config_port.get_attribute('MEMORY') cpu = self._m_config_port.get_attribute('CPU') nimages = image_in_port.get_shape()[0] if memory == 0: memory = nimages if image_out_port.tag == image_in_port.tag: # load all images in the memory at once if the input and output tag are the # same or if the MEMORY attribute is set to None in the configuration file images = image_in_port.get_all() result = [] start_time = time.time() for i in range(nimages): progress(i, nimages, message + '...', start_time) args = update_arguments(i, nimages, func_args) if args is None: result.append(func(images[i, ], i)) else: result.append(func(images[i, ], i, *args)) image_out_port.set_all(np.asarray(result), keep_attributes=True) elif cpu == 1: # process images one-by-one with a single process if CPU is set to 1 start_time = time.time() for i in range(nimages): progress(i, nimages, message + '...', start_time) args = update_arguments(i, nimages, func_args) if args is None: result = func(image_in_port[i, ], i) else: result = func(image_in_port[i, ], i, *args) if result.ndim == 1: image_out_port.append(result, data_dim=2) elif result.ndim == 2: image_out_port.append(result, data_dim=3) else: # process images in parallel in stacks of MEMORY/CPU images print(message, end='') args = update_arguments(0, nimages, func_args) result = apply_function(image_in_port[0, :, :], 0, func, args) result_shape = result.shape out_shape = [nimages] for item in result_shape: out_shape.append(item) image_out_port.set_all(data=np.zeros(out_shape), data_dim=len(result_shape) + 1, keep_attributes=False) image_in_port.close_port() image_out_port.close_port() capsule = StackProcessingCapsule(image_in_port=image_in_port, image_out_port=image_out_port, num_proc=cpu, function=func, function_args=func_args, stack_size=math.ceil(memory / cpu), result_shape=result_shape, nimages=nimages) capsule.run() print(' [DONE]')