예제 #1
0
def write_selected_data(images: np.ndarray, indices: np.ndarray,
                        port_selected: OutputPort,
                        port_removed: OutputPort) -> None:
    """
    Function to write a selected number of images from a data set.

    Parameters
    ----------
    images : numpy.ndarray
        Stack of images.
    indices : numpy.ndarray
        Indices that are removed.
    port_selected : pynpoint.core.dataio.OutputPort
        Port to store the selected images.
    port_removed : pynpoint.core.dataio.OutputPort
        Port to store the removed images.

    Returns
    -------
    NoneType
        None
    """

    if np.size(indices) > 0:
        if port_removed is not None:
            port_removed.append(images[indices])

        if images.ndim == 2:
            images = None

        elif images.ndim == 3:
            images = np.delete(images, indices, axis=0)

    if port_selected is not None and images is not None:
        port_selected.append(images)
예제 #2
0
    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]')