Exemple #1
0
    def __init__(
            self,
            raw_dtype: Union[str, numpy.dtype],
            magnitude_lookup_table: numpy.ndarray,
            raw_shape: Optional[Tuple[int, ...]] = None,
            formatted_shape: Optional[Tuple[int, ...]] = None,
            reverse_axes: Optional[Tuple[int, ...]] = None,
            transpose_axes: Optional[Tuple[int, ...]] = None,
            band_dimension: int = -1):
        """

        Parameters
        ----------
        raw_dtype : str|numpy.dtype
            The raw datatype. Must be `uint8` up to endianness.
        magnitude_lookup_table : numpy.ndarray
        raw_shape : None|Tuple[int, ...]
        formatted_shape : None|Tuple[int, ...]
        reverse_axes : None|Tuple[int, ...]
        transpose_axes : None|Tuple[int, ...]
        band_dimension : int
            Which band is the complex dimension, **after** the transpose operation.
        """

        ComplexFormatFunction.__init__(
            self, raw_dtype, 'MP', raw_shape=raw_shape, formatted_shape=formatted_shape,
            reverse_axes=reverse_axes, transpose_axes=transpose_axes, band_dimension=band_dimension)
        self._magnitude_lookup_table = None
        self.set_magnitude_lookup(magnitude_lookup_table)
Exemple #2
0
    def test_basic_write(self):
        data = numpy.reshape(numpy.arange(24, dtype='int16'), (3, 4, 2))
        complex_data = numpy.empty((3, 4), dtype='complex64')
        complex_data.real = data[:, :, 0]
        complex_data.imag = data[:, :, 1]

        with self.subTest(msg='write_raw'):
            empty = numpy.empty((3, 4, 2), dtype='int16')
            data_segment = NumpyArraySegment(
                empty,
                formatted_dtype='complex64',
                formatted_shape=(3, 4),
                format_function=ComplexFormatFunction('int16',
                                                      'IQ',
                                                      band_dimension=2),
                mode='w')

            data_segment.write_raw(data, start_indices=0)
            self.assertTrue(numpy.all(empty == data))

        with self.subTest(msg='write'):
            empty = numpy.empty((3, 4, 2), dtype='int16')
            data_segment = NumpyArraySegment(
                empty,
                formatted_dtype='complex64',
                formatted_shape=(3, 4),
                format_function=ComplexFormatFunction('int16',
                                                      'IQ',
                                                      band_dimension=2),
                mode='w')

            data_segment.write(complex_data, start_indices=0)
            self.assertTrue(numpy.all(empty == data))

        with self.assertRaises(ValueError, msg='read_raw attempt'):
            _ = data_segment.read_raw(0)

        with self.assertRaises(ValueError, msg='read attempt'):
            _ = data_segment.read(0)

        with self.subTest(msg='close'):
            empty = numpy.empty((3, 4, 2), dtype='int16')
            data_segment = NumpyArraySegment(
                empty,
                formatted_dtype='complex64',
                formatted_shape=(3, 4),
                format_function=ComplexFormatFunction('int16',
                                                      'IQ',
                                                      band_dimension=2),
                mode='w')
            self.assertFalse(data_segment.closed)
            data_segment.close()
            self.assertTrue(data_segment.closed)

        with self.assertRaises(ValueError, msg='write access when closed'):
            data_segment.write(complex_data)

        with self.assertRaises(ValueError, msg='write_raw access when closed'):
            data_segment.write(complex_data)
Exemple #3
0
 def _forward_magnitude_theta(self, data: numpy.ndarray, out: numpy.ndarray,
                              magnitude: numpy.ndarray,
                              theta: numpy.ndarray,
                              subscript: Tuple[slice, ...]) -> None:
     if self._scaling_function is not None:
         magnitude = self._scaling_function(magnitude)
     ComplexFormatFunction._forward_magnitude_theta(self, data, out,
                                                    magnitude, theta,
                                                    subscript)
Exemple #4
0
 def _forward_magnitude_theta(
         self,
         data: numpy.ndarray,
         out: numpy.ndarray,
         magnitude: numpy.ndarray,
         theta: numpy.ndarray,
         subscript: Tuple[slice, ...]) -> None:
     magnitude = self.magnitude_lookup_table[magnitude]
     ComplexFormatFunction._forward_magnitude_theta(
         self, data, out, magnitude, theta, subscript)
Exemple #5
0
    def _reverse_magnitude_theta(
            self,
            data: numpy.ndarray,
            out: numpy.ndarray,
            magnitude: numpy.ndarray,
            theta: numpy.ndarray,
            slice0: Tuple[slice, ...],
            slice1: Tuple[slice, ...]) -> None:
        magnitude = numpy.digitize(
            numpy.round(magnitude.ravel()), self.magnitude_lookup_table, right=False).reshape(data.shape)

        ComplexFormatFunction._reverse_magnitude_theta(self, data, out, magnitude, theta, slice0, slice1)
Exemple #6
0
    def __init__(self, sio_details):
        """

        Parameters
        ----------
        sio_details : str|SIODetails
            filename or SIODetails object
        """

        if isinstance(sio_details, str):
            sio_details = SIODetails(sio_details)
        if not isinstance(sio_details, SIODetails):
            raise TypeError(
                'The input argument for SIOReader must be a filename or '
                'SIODetails object.')
        self._sio_details = sio_details
        sicd_meta = sio_details.get_sicd()

        if sicd_meta.ImageData.PixelType == 'AMP8I_PHS8I':
            format_function = ComplexFormatFunction(
                sio_details.raw_data_type,
                order='MP',
                band_dimension=-1,
                magnitude_lookup_table=sicd_meta.ImageData.AmpTable)
        else:
            format_function = ComplexFormatFunction(sio_details.raw_data_type,
                                                    order='IQ',
                                                    band_dimension=-1)
        reverse_axes, transpose_axes = sio_details.get_symmetry()
        data_segment = NumpyMemmapSegment(sio_details.file_name,
                                          sio_details.data_offset,
                                          sio_details.raw_data_type,
                                          sio_details.raw_data_size,
                                          'complex64',
                                          sio_details.formatted_data_size,
                                          reverse_axes=reverse_axes,
                                          transpose_axes=transpose_axes,
                                          format_function=format_function,
                                          mode='r',
                                          close_file=True)

        SICDTypeReader.__init__(self,
                                data_segment,
                                sicd_meta,
                                close_segments=True)
        self._check_sizes()
Exemple #7
0
    def __init__(self,
                 raw_dtype: Union[str, numpy.dtype],
                 order: str,
                 scaling_function: Optional[Callable] = None,
                 raw_shape: Optional[Tuple[int, ...]] = None,
                 formatted_shape: Optional[Tuple[int, ...]] = None,
                 reverse_axes: Optional[Tuple[int, ...]] = None,
                 transpose_axes: Optional[Tuple[int, ...]] = None,
                 band_dimension: int = -1):
        """

        Parameters
        ----------
        raw_dtype : str|numpy.dtype
            The raw datatype. Valid options dependent on the value of order.
        order : str
            One of `('MP', 'PM')`, with allowable raw_dtype
            `('uint8', 'uint16', 'uint32', 'float32', 'float64')`.
        scaling_function : Optional[Callable]
        raw_shape : None|Tuple[int, ...]
        formatted_shape : None|Tuple[int, ...]
        reverse_axes : None|Tuple[int, ...]
        transpose_axes : None|Tuple[int, ...]
        band_dimension : int
            Which band is the complex dimension, **after** the transpose operation.
        """

        self._scaling_function = None
        ComplexFormatFunction.__init__(self,
                                       raw_dtype,
                                       order,
                                       raw_shape=raw_shape,
                                       formatted_shape=formatted_shape,
                                       reverse_axes=reverse_axes,
                                       transpose_axes=transpose_axes,
                                       band_dimension=band_dimension)
        self._set_scaling_function(scaling_function)
Exemple #8
0
    def __init__(self, csk_details):
        """

        Parameters
        ----------
        csk_details : str|CSKDetails
            file name or CSKDetails object
        """

        if isinstance(csk_details, str):
            csk_details = CSKDetails(csk_details)
        if not isinstance(csk_details, CSKDetails):
            raise TypeError('The input argument for a CSKReader must be a '
                            'filename or CSKDetails object')
        self._csk_details = csk_details
        sicd_data, shape_dict, dtype_dict, reverse_axes, transpose_axes = csk_details.get_sicd_collection(
        )
        data_segments = []
        sicds = []
        for band_name in sicd_data:
            if self._csk_details.mission_id in ['CSK', 'KMPS']:
                the_band = '{}/SBI'.format(band_name)
            elif self._csk_details.mission_id == 'CSG':
                the_band = '{}/IMG'.format(band_name)
            else:
                raise ValueError(
                    _unhandled_id_text.format(self._csk_details.mission_id))

            sicds.append(sicd_data[band_name])
            basic_shape = shape_dict[band_name]
            data_segments.append(
                HDF5DatasetSegment(csk_details.file_name,
                                   the_band,
                                   formatted_dtype='complex64',
                                   formatted_shape=(basic_shape[1],
                                                    basic_shape[0]),
                                   reverse_axes=reverse_axes,
                                   transpose_axes=transpose_axes,
                                   format_function=ComplexFormatFunction(
                                       raw_dtype=dtype_dict[band_name],
                                       order='IQ',
                                       band_dimension=2),
                                   close_file=True))

        SICDTypeReader.__init__(self,
                                data_segments,
                                sicds,
                                close_segments=True)
        self._check_sizes()
Exemple #9
0
    def test_read_with_transpose_and_reverse(self):
        data = numpy.reshape(numpy.arange(60, dtype='int16'), (5, 6, 2))

        for axis in [None, (0, ), (1, ), (0, 1)]:
            complex_data = numpy.empty((5, 6), dtype='complex64')
            complex_data.real = data[:, :, 0]
            complex_data.imag = data[:, :, 1]
            if axis is None:
                complex_data = numpy.transpose(complex_data)
            else:
                complex_data = numpy.transpose(
                    numpy.flip(complex_data, axis=axis))

            data_segment = NumpyArraySegment(
                data,
                formatted_dtype='complex64',
                formatted_shape=(6, 5),
                reverse_axes=axis,
                transpose_axes=(1, 0, 2),
                format_function=ComplexFormatFunction('int16',
                                                      'IQ',
                                                      band_dimension=2),
                mode='r')

            with self.subTest(msg='read full, axis {}'.format(axis)):
                test_data = data_segment.read(None)
                self.assertTrue(numpy.all(complex_data == test_data))

            with self.subTest(msg='read subscript, axis {}'.format(axis)):
                subscript = (slice(1, 3, 1), slice(0, 2, 1))
                test_data = data_segment.read(subscript)
                self.assertTrue(
                    numpy.all(test_data == complex_data[subscript]))

            with self.subTest(msg='corners :3,:3, axis {}'.format(axis)):
                test_data = data_segment[:3, :3]
                self.assertTrue(test_data.shape == (3, 3))

            with self.subTest(msg='corners -3:,:3, axis {}'.format(axis)):
                test_data = data_segment[-3:, :3]
                self.assertTrue(test_data.shape == (3, 3))

            with self.subTest(msg='corners :3,-3:, axis {}'.format(axis)):
                test_data = data_segment[:3, -3:]
                self.assertTrue(test_data.shape == (3, 3))

            with self.subTest(msg='corners -3:,-3:, axis {}'.format(axis)):
                test_data = data_segment[-3:, -3:]
                self.assertTrue(test_data.shape == (3, 3))
Exemple #10
0
    def test_uint(self):
        for bit_depth in [8, 16]:
            raw_type = 'uint{}'.format(bit_depth)
            base_data = numpy.reshape(numpy.arange(2, 14, dtype=raw_type),
                                      (2, 3, 2))

            with self.subTest(msg='MP {}'.format(raw_type)):
                func = ComplexFormatFunction(raw_type,
                                             'MP',
                                             raw_shape=(2, 3, 2),
                                             formatted_shape=(2, 3),
                                             band_dimension=2)
                out_data = func(
                    base_data,
                    (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
                magnitude = base_data[:, :, 0]
                theta = base_data[:, :, 1] * 2 * numpy.pi / (1 << bit_depth)
                test_data = numpy.empty((2, 3), dtype='complex64')
                test_data.real = magnitude * numpy.cos(theta)
                test_data.imag = magnitude * numpy.sin(theta)
                self.assertTrue(numpy.all(out_data == test_data),
                                msg='MP {} forward'.format(raw_type))

                inv_data = func.inverse(out_data,
                                        (slice(0, 2, 1), slice(0, 3, 1)))
                self.assertTrue(numpy.all(base_data == inv_data),
                                msg='MP {} inverse'.format(raw_type))

            with self.subTest(msg='PM {}'.format(raw_type)):
                func = ComplexFormatFunction(raw_type,
                                             'PM',
                                             raw_shape=(2, 3, 2),
                                             formatted_shape=(2, 3),
                                             band_dimension=2)
                out_data = func(
                    base_data,
                    (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
                magnitude = base_data[:, :, 1]
                theta = base_data[:, :, 0] * 2 * numpy.pi / (1 << bit_depth)
                test_data = numpy.empty((2, 3), dtype='complex64')
                test_data.real = magnitude * numpy.cos(theta)
                test_data.imag = magnitude * numpy.sin(theta)
                self.assertTrue(numpy.all(out_data == test_data),
                                msg='PM {} forward'.format(raw_type))

                inv_data = func.inverse(out_data,
                                        (slice(0, 2, 1), slice(0, 3, 1)))
                self.assertTrue(numpy.all(base_data == inv_data),
                                msg='PM {} inverse'.format(raw_type))
Exemple #11
0
    def test_bad_typing(self):
        for order in ['IQ', 'QI']:
            with self.assertRaises(ValueError, msg='{} typing'.format(order)):
                _ = ComplexFormatFunction('uint8', order)
            with self.assertRaises(ValueError, msg='{} typing'.format(order)):
                _ = ComplexFormatFunction('uint16', order)
            with self.assertRaises(ValueError, msg='{} typing'.format(order)):
                _ = ComplexFormatFunction('uint32', order)

        for order in ['MP', 'PM']:
            with self.assertRaises(ValueError, msg='{} typing'.format(order)):
                _ = ComplexFormatFunction('int8', order)
            with self.assertRaises(ValueError, msg='{} typing'.format(order)):
                _ = ComplexFormatFunction('int16', order)
            with self.assertRaises(ValueError, msg='{} typing'.format(order)):
                _ = ComplexFormatFunction('int32', order)
Exemple #12
0
    def __init__(self, nisar_details: Union[str, NISARDetails]):
        """

        Parameters
        ----------
        nisar_details : str|NISARDetails
            file name or NISARDetails object
        """

        if isinstance(nisar_details, str):
            nisar_details = NISARDetails(nisar_details)
        if not isinstance(nisar_details, NISARDetails):
            raise TypeError('The input argument for NISARReader must be a '
                            'filename or NISARDetails object')
        self._nisar_details = nisar_details
        sicd_data, shape_dict, reverse_axes, transpose_axes = nisar_details.get_sicd_collection()
        data_segments = []
        sicds = []
        for band_name in sicd_data:
            sicds.append(sicd_data[band_name])
            raw_shape, raw_dtype = shape_dict[band_name]
            formatted_shape = (raw_shape[1], raw_shape[0]) if transpose_axes is not None \
                else raw_shape[:2]
            if raw_dtype.name == 'complex64':
                formatted_dtype = raw_dtype
                format_function = None
            else:
                formatted_dtype = 'complex64'
                format_function = ComplexFormatFunction(raw_dtype=raw_dtype, order='IQ', band_dimension=-1)

            data_segments.append(
                HDF5DatasetSegment(
                    nisar_details.file_name, band_name,
                    formatted_dtype=formatted_dtype, formatted_shape=formatted_shape,
                    reverse_axes=reverse_axes, transpose_axes=transpose_axes,
                    format_function=format_function, close_file=True))

        SICDTypeReader.__init__(self, data_segments, sicds, close_segments=True)
        self._check_sizes()
Exemple #13
0
def get_iceye_data_segment(
        file_name: str,
        reverse_axes: Union[None, int, Sequence[int]],
        transpose_axes: Union[None, Tuple[int, ...]],
        real_group: str = 's_i',
        imaginary_grop: str = 's_q') -> BandAggregateSegment:
    real_dataset = HDF5DatasetSegment(file_name,
                                      real_group,
                                      reverse_axes=reverse_axes,
                                      transpose_axes=transpose_axes,
                                      close_file=True)
    imag_dataset = HDF5DatasetSegment(file_name,
                                      imaginary_grop,
                                      reverse_axes=reverse_axes,
                                      transpose_axes=transpose_axes,
                                      close_file=True)
    return BandAggregateSegment(
        (real_dataset, imag_dataset),
        band_dimension=2,
        formatted_dtype='complex64',
        formatted_shape=real_dataset.formatted_shape,
        format_function=ComplexFormatFunction(real_dataset.formatted_dtype,
                                              order='IQ',
                                              band_dimension=-1))
Exemple #14
0
    def test_int(self):
        for raw_type in ['int8', 'int16']:
            base_data = numpy.reshape(numpy.arange(2, 14, dtype=raw_type),
                                      (2, 3, 2))

            with self.subTest(msg='IQ {}'.format(raw_type)):
                func = ComplexFormatFunction('int16',
                                             'IQ',
                                             raw_shape=(2, 3, 2),
                                             formatted_shape=(2, 3),
                                             band_dimension=2)
                out_data = func(
                    base_data,
                    (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
                test_data = numpy.empty((2, 3), dtype='complex64')
                test_data.real = base_data[:, :, 0]
                test_data.imag = base_data[:, :, 1]
                self.assertTrue(numpy.all(out_data == test_data),
                                msg='IQ {} forward'.format(raw_type))

                inv_data = func.inverse(out_data,
                                        (slice(0, 2, 1), slice(0, 3, 1)))
                self.assertTrue(numpy.all(base_data == inv_data),
                                msg='IQ {} inverse'.format(raw_type))

            with self.subTest(msg='QI {}'.format(raw_type)):
                func = ComplexFormatFunction('int16',
                                             'QI',
                                             raw_shape=(2, 3, 2),
                                             formatted_shape=(2, 3),
                                             band_dimension=2)
                out_data = func(
                    base_data,
                    (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
                test_data = numpy.empty((2, 3), dtype='complex64')
                test_data.real = base_data[:, :, 1]
                test_data.imag = base_data[:, :, 0]
                self.assertTrue(numpy.all(out_data == test_data),
                                msg='QI {} forward'.format(raw_type))
                inv_data = func.inverse(out_data,
                                        (slice(0, 2, 1), slice(0, 3, 1)))
                self.assertTrue(numpy.all(base_data == inv_data),
                                msg='QI {} inverse'.format(raw_type))
Exemple #15
0
def _extract_transform_data(image_header: Union[ImageSegmentHeader,
                                                ImageSegmentHeader0],
                            band_dimension: int):
    """
    Helper function for defining necessary transform_data definition for
    interpreting image segment data.

    Parameters
    ----------
    image_header : ImageSegmentHeader|ImageSegmentHeader0

    Returns
    -------
    None|str|callable
    """

    if len(image_header.Bands) != 2:
        raise ValueError('Got unhandled case of {} image bands'.format(
            len(image_header.Bands)))

    complex_order = image_header.Bands[0].ISUBCAT + image_header.Bands[
        1].ISUBCAT
    if complex_order not in ['IQ', 'QI', 'MP', 'PM']:
        raise ValueError(
            'Got unhandled complex order `{}`'.format(complex_order))

    bpp = int(image_header.NBPP / 8)
    pv_type = image_header.PVTYPE
    if pv_type == 'INT':
        raw_dtype = '>u{}'.format(bpp)
    elif pv_type == 'SI':
        raw_dtype = '>i{}'.format(bpp)
    elif pv_type == 'R':
        raw_dtype = '>f{}'.format(bpp)
    else:
        raise ValueError('Got unhandled PVTYPE {}'.format(pv_type))

    # noinspection PyUnresolvedReferences
    tre = None if img_header.ExtendedHeader.data is None else \
        img_header.ExtendedHeader.data['CMETAA']  # type: Optional[CMETAA]

    if tre is None:
        return ComplexFormatFunction(raw_dtype,
                                     complex_order,
                                     band_dimension=band_dimension)

    cmetaa = tre.DATA
    if cmetaa.CMPLX_PHASE_SCALING_TYPE.strip() != 'NS':
        raise ValueError('Got unsupported CMPLX_PHASE_SCALING_TYPE {}'.format(
            cmetaa.CMPLX_PHASE_SCALING_TYPE))

    remap_type = cmetaa.CMPLX_MAG_REMAP_TYPE.strip()
    if remap_type == 'NS':
        if complex_order in ['IQ', 'QI']:
            return ComplexFormatFunction(raw_dtype,
                                         complex_order,
                                         band_dimension=band_dimension)
        else:
            raise ValueError(
                'Got unexpected state where cmetaa.CMPLX_MAG_REMAP_TYPE is "NS",\n\t '
                'but Band[0].ISUBCAT/Band[1].ISUBCAT = `{}`'.format(
                    complex_order))
    elif remap_type not in ['LINM', 'LINP', 'LOGM', 'LOGP', 'LLM']:
        raise ValueError(
            'Got unsupported CMETAA.CMPLX_MAG_REMAP_TYPE {}'.format(
                remap_type))

    if complex_order not in ['MP', 'PM']:
        raise ValueError(
            'Got unexpected state where cmetaa.CMPLX_MAG_REMAP_TYPE is `{}`,\n\t'
            'but Band[0].ISUBCAT/Band[1].ISUBCAT = `{}`'.format(
                remap_type, complex_order))

    scale_factor = float(cmetaa.CMPLX_LIN_SCALE)
    if remap_type == 'LINM':
        scaling_function = get_linear_magnitude_scaling(scale_factor)
    elif remap_type == 'LINP':
        scaling_function = get_linear_power_scaling(scale_factor)
    elif remap_type == 'LOGM':
        # NB: there is nowhere in the CMETAA structure to define
        #   the db_per_step value. Strangely, the use of this value is laid
        #   out in the STDI-0002 standards document, which defines CMETAA
        #   structure. We will generically use a value which maps the
        #   max uint8 value to the max int16 value.
        db_per_step = 300 * numpy.log(2) / 255.0
        scaling_function = get_log_magnitude_scaling(scale_factor, db_per_step)
    elif remap_type == 'LOGP':
        db_per_step = 300 * numpy.log(2) / 255.0
        scaling_function = get_log_power_scaling(scale_factor, db_per_step)
    elif remap_type == 'LLM':
        scaling_function = get_linlog_magnitude_scaling(
            scale_factor, int(cmetaa.CMPLX_LINLOG_TP))
    else:
        raise ValueError(
            'Got unhandled CMETAA.CMPLX_MAG_REMAP_TYPE {}'.format(remap_type))
    return ApplyAmplitudeScalingFunction(raw_dtype,
                                         complex_order,
                                         scaling_function,
                                         band_dimension=band_dimension)
Exemple #16
0
    def __init__(self,
                 file_object: Union[str, BinaryIO],
                 sicd_meta: SICDType,
                 user_data: Optional[Dict[str, str]] = None,
                 check_older_version: bool = False,
                 check_existence: bool = True):
        """

        Parameters
        ----------
        file_object : str|BinaryIO
        sicd_meta : SICDType
        user_data : None|Dict[str, str]
        check_older_version : bool
            Try to use an older version (1.1) of the SICD standard, for possible
            application compliance issues?
        check_existence : bool
            Should we check if the given file already exists, and raises an exception if so?
        """

        self._data_written = True
        if isinstance(file_object, str):
            if check_existence and os.path.exists(file_object):
                raise SarpyIOError(
                    'Given file {} already exists, and a new SIO file cannot be created here.'
                    .format(file_object))
            file_object = open(file_object, 'wb')

        if not is_file_like(file_object):
            raise ValueError(
                'file_object requires a file path or BinaryIO object')

        self._file_object = file_object
        if is_real_file(file_object):
            self._file_name = file_object.name
            self._in_memory = False
        else:
            self._file_name = None
            self._in_memory = True

        # choose magic number (with user data) and corresponding endian-ness
        magic_number = 0xFD7F02FF
        endian = SIODetails.ENDIAN[magic_number]

        # define basic image details
        raw_shape = (sicd_meta.ImageData.NumRows, sicd_meta.ImageData.NumCols,
                     2)
        pixel_type = sicd_meta.ImageData.PixelType
        if pixel_type == 'RE32F_IM32F':
            raw_dtype = numpy.dtype('{}f4'.format(endian))
            element_type = 13
            element_size = 8
            format_function = ComplexFormatFunction(raw_dtype,
                                                    order='MP',
                                                    band_dimension=2)
        elif pixel_type == 'RE16I_IM16I':
            raw_dtype = numpy.dtype('{}i2'.format(endian))
            element_type = 12
            element_size = 4
            format_function = ComplexFormatFunction(raw_dtype,
                                                    order='MP',
                                                    band_dimension=2)
        else:
            raw_dtype = numpy.dtype('{}u1'.format(endian))
            element_type = 11
            element_size = 2
            format_function = ComplexFormatFunction(
                raw_dtype,
                order='MP',
                band_dimension=2,
                magnitude_lookup_table=sicd_meta.ImageData.AmpTable)

        # construct the sio header
        header = numpy.array([
            magic_number, raw_shape[0], raw_shape[1], element_type,
            element_size
        ],
                             dtype='>u4')
        # construct the user data - must be {str : str}
        if user_data is None:
            user_data = {}
        uh_args = sicd_meta.get_des_details(check_older_version)
        user_data['SICDMETA'] = sicd_meta.to_xml_string(tag='SICD',
                                                        urn=uh_args['DESSHTN'])

        # write the initial things to the buffer
        self._file_object.seek(0, os.SEEK_SET)
        self._file_object.write(struct.pack('{}5I'.format(endian), *header))
        # write the user data - name size, name, value size, value
        for name in user_data:
            name_bytes = name.encode('utf-8')
            self._file_object.write(
                struct.pack('{}I'.format(endian), len(name_bytes)))
            self._file_object.write(
                struct.pack('{}{}s'.format(endian, len(name_bytes)),
                            name_bytes))
            val_bytes = user_data[name].encode('utf-8')
            self._file_object.write(
                struct.pack('{}I'.format(endian), len(val_bytes)))
            self._file_object.write(
                struct.pack('{}{}s'.format(endian, len(val_bytes)), val_bytes))
        self._data_offset = self._file_object.tell()
        # initialize the single data segment
        if self._in_memory:
            underlying_array = numpy.full(raw_shape,
                                          fill_value=0,
                                          dtype=raw_dtype)
            data_segment = NumpyArraySegment(underlying_array,
                                             'complex64',
                                             raw_shape[:2],
                                             format_function=format_function,
                                             mode='w')
            self._data_written = False
        else:
            data_segment = NumpyMemmapSegment(self._file_object,
                                              self._data_offset,
                                              raw_dtype,
                                              raw_shape,
                                              'complex64',
                                              raw_shape[:2],
                                              format_function=format_function,
                                              mode='w',
                                              close_file=False)
            self._data_written = True
        BaseWriter.__init__(self, data_segment)
Exemple #17
0
    def test_float(self):
        base_data = numpy.reshape(numpy.arange(2, 14, dtype='float32'),
                                  (2, 3, 2))

        with self.subTest(msg='IQ float'):
            func = ComplexFormatFunction('float32',
                                         'IQ',
                                         raw_shape=(2, 3, 2),
                                         formatted_shape=(2, 3),
                                         band_dimension=2)
            out_data = func(base_data,
                            (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
            test_data = numpy.empty((2, 3), dtype='complex64')
            test_data.real = base_data[:, :, 0]
            test_data.imag = base_data[:, :, 1]
            self.assertTrue(numpy.all(out_data == test_data), msg='IQ forward')

            inv_data = func.inverse(out_data, (slice(0, 2, 1), slice(0, 3, 1)))
            self.assertTrue(numpy.all(numpy.abs(base_data - inv_data) < 1e-10),
                            msg='IQ inverse')

        with self.subTest(msg='QI float'):
            func = ComplexFormatFunction('float32',
                                         'QI',
                                         raw_shape=(2, 3, 2),
                                         formatted_shape=(2, 3),
                                         band_dimension=2)
            out_data = func(base_data,
                            (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
            test_data = numpy.empty((2, 3), dtype='complex64')
            test_data.real = base_data[:, :, 1]
            test_data.imag = base_data[:, :, 0]
            self.assertTrue(numpy.all(out_data == test_data), msg='QI forward')
            inv_data = func.inverse(out_data, (slice(0, 2, 1), slice(0, 3, 1)))
            self.assertTrue(numpy.all(numpy.abs(base_data - inv_data) < 1e-10),
                            msg='QI inverse')

        with self.subTest(msg='MP float'):
            func = ComplexFormatFunction('float32',
                                         'MP',
                                         raw_shape=(2, 3, 2),
                                         formatted_shape=(2, 3),
                                         band_dimension=2)
            out_data = func(base_data,
                            (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
            magnitude = base_data[:, :, 0]
            theta = base_data[:, :, 1]
            test_data = numpy.empty((2, 3), dtype='complex64')
            test_data.real = magnitude * numpy.cos(theta)
            test_data.imag = magnitude * numpy.sin(theta)
            self.assertTrue(numpy.all(out_data == test_data), msg='MP forward')

            inv_data = func.inverse(out_data, (slice(0, 2, 1), slice(0, 3, 1)))
            inv_array_check = (base_data - inv_data)
            self.assertTrue(
                numpy.all(numpy.abs(inv_array_check[:, :, 0]) < 1e-5),
                msg='M of MP inverse')
            self.assertTrue(numpy.all(
                numpy.abs(numpy.sin(inv_array_check[:, :, 1])) < 1e-5),
                            msg='P of MP inverse')

        with self.subTest(msg='PM float'):
            func = ComplexFormatFunction('float32',
                                         'PM',
                                         raw_shape=(2, 3, 2),
                                         formatted_shape=(2, 3),
                                         band_dimension=2)
            out_data = func(base_data,
                            (slice(0, 2, 1), slice(0, 3, 1), slice(0, 2, 1)))
            magnitude = base_data[:, :, 1]
            theta = base_data[:, :, 0]
            test_data = numpy.empty((2, 3), dtype='complex64')
            test_data.real = magnitude * numpy.cos(theta)
            test_data.imag = magnitude * numpy.sin(theta)
            self.assertTrue(numpy.all(out_data == test_data), msg='PM forward')

            inv_data = func.inverse(out_data, (slice(0, 2, 1), slice(0, 3, 1)))
            inv_array_check = (base_data - inv_data)
            self.assertTrue(
                numpy.all(numpy.abs(inv_array_check[:, :, 1]) < 1e-5),
                msg='M of PM inverse')
            self.assertTrue(numpy.all(
                numpy.abs(numpy.sin(inv_array_check[:, :, 0])) < 1e-5),
                            msg='P of PM inverse')
Exemple #18
0
    def test_read(self):
        data = numpy.reshape(numpy.arange(24, dtype='int16'), (3, 4, 2))
        complex_data = numpy.empty((3, 4), dtype='complex64')
        complex_data.real = data[:, :, 0]
        complex_data.imag = data[:, :, 1]

        file_object = BytesIO(data.tobytes())
        data_segment = FileReadDataSegment(
            file_object,
            0,
            'int16', (3, 4, 2),
            'complex64', (3, 4),
            format_function=ComplexFormatFunction('int16',
                                                  'IQ',
                                                  band_dimension=2))

        with self.subTest(msg='read_raw full'):
            test_data = data_segment.read_raw(None)
            self.assertTrue(numpy.all(data == test_data))

        with self.subTest(msg='read_raw subscript'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment.read_raw(subscript)
            self.assertTrue(numpy.all(data[subscript] == test_data))

        with self.subTest(msg='read_raw index with squeeze'):
            test_data = data_segment.read_raw((0, 1, 1), squeeze=True)
            self.assertTrue(test_data.ndim == 0, msg='{}'.format(test_data))
            self.assertTrue(data[0, 1, 1] == test_data)

        with self.subTest(msg='read_raw index without squeeze'):
            test_data = data_segment.read_raw((0, 1, 1), squeeze=False)
            self.assertTrue(test_data.ndim == 3)
            self.assertTrue(data[0, 1, 1] == test_data)

        with self.subTest(msg='read full'):
            test_data = data_segment.read(None)
            self.assertTrue(numpy.all(complex_data == test_data))

        with self.subTest(msg='read subscript'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment.read(subscript)
            self.assertTrue(numpy.all(complex_data[subscript] == test_data))

        with self.subTest(msg='read subscript with ellipsis'):
            subscript = (..., slice(1, 3, 1))
            test_data = data_segment.read(subscript)
            self.assertTrue(numpy.all(complex_data[subscript] == test_data))

        with self.subTest(msg='read index with squeeze'):
            test_data = data_segment.read((0, 1), squeeze=True)
            self.assertTrue(test_data.ndim == 0)
            self.assertTrue(complex_data[0, 1] == test_data)

        with self.subTest(msg='read index without squeeze'):
            test_data = data_segment.read((0, 1), squeeze=False)
            self.assertTrue(test_data.ndim == 2)
            self.assertTrue(complex_data[0, 1] == test_data)

        with self.subTest(msg='read using __getitem__ subscript'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment[0:2, 1:3]
            self.assertTrue(numpy.all(complex_data[subscript] == test_data))

        with self.subTest(msg='read using __getitem__ and specifiying raw'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment[0:2, 1:3, 'raw']
            self.assertTrue(numpy.all(data[subscript] == test_data))

        with self.assertRaises(ValueError, msg='write_raw attempt'):
            data_segment.write_raw(data, start_indices=0)

        with self.assertRaises(ValueError, msg='write attempt'):
            data_segment.write(complex_data, start_indices=0)

        with self.subTest(msg='close functionality test'):
            self.assertFalse(data_segment.closed)
            data_segment.close()
            self.assertTrue(data_segment.closed)

        with self.assertRaises(ValueError, msg='read access when closed'):
            _ = data_segment.read(None)

        with self.assertRaises(ValueError, msg='read_raw access when closed'):
            _ = data_segment.read_raw(None)
Exemple #19
0
    def test_basic_read(self):
        data = numpy.reshape(numpy.arange(60, dtype='int16'), (5, 6, 2))
        complex_data = numpy.empty((5, 6), dtype='complex64')
        complex_data.real = data[:, :, 0]
        complex_data.imag = data[:, :, 1]

        data_segment = NumpyArraySegment(data,
                                         formatted_dtype='complex64',
                                         formatted_shape=(5, 6),
                                         format_function=ComplexFormatFunction(
                                             'int16', 'IQ', band_dimension=2),
                                         mode='r')

        with self.subTest(msg='read_raw full'):
            test_data = data_segment.read_raw(None)
            self.assertTrue(numpy.all(data == test_data))

        with self.subTest(msg='read_raw subscript'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment.read_raw(subscript)
            self.assertTrue(numpy.all(data[subscript] == test_data))

        with self.subTest(msg='read_raw index with squeeze'):
            test_data = data_segment.read_raw((0, 1, 1), squeeze=True)
            self.assertTrue(test_data.ndim == 0, msg='{}'.format(test_data))
            self.assertTrue(data[0, 1, 1] == test_data)

        with self.subTest(msg='read_raw index without squeeze'):
            test_data = data_segment.read_raw((0, 1, 1), squeeze=False)
            self.assertTrue(test_data.ndim == 3)
            self.assertTrue(data[0, 1, 1] == test_data)

        with self.subTest(msg='read full'):
            test_data = data_segment.read(None)
            self.assertTrue(numpy.all(complex_data == test_data))

        with self.subTest(msg='read subscript'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment.read(subscript)
            self.assertTrue(numpy.all(complex_data[subscript] == test_data))

        with self.subTest(msg='read subscript with ellipsis'):
            subscript = (..., slice(1, 3, 1))
            test_data = data_segment.read(subscript)
            self.assertTrue(numpy.all(complex_data[subscript] == test_data))

        with self.subTest(msg='read index with squeeze'):
            test_data = data_segment.read((0, 1), squeeze=True)
            self.assertTrue(test_data.ndim == 0)
            self.assertTrue(complex_data[0, 1] == test_data)

        with self.subTest(msg='read index without squeeze'):
            test_data = data_segment.read((0, 1), squeeze=False)
            self.assertTrue(test_data.ndim == 2)
            self.assertTrue(complex_data[0, 1] == test_data)

        with self.subTest(msg='read using __getitem__ subscript'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment[0:2, 1:3]
            self.assertTrue(numpy.all(complex_data[subscript] == test_data))

        with self.subTest(msg='read using __getitem__ and specifying raw'):
            subscript = (slice(0, 2, 1), slice(1, 3, 1))
            test_data = data_segment[0:2, 1:3, 'raw']
            self.assertTrue(numpy.all(data[subscript] == test_data))

        with self.subTest(msg='corners :3,:3'):
            test_data = data_segment[:3, :3]
            self.assertTrue(test_data.shape == (3, 3))

        with self.subTest(msg='corners -3:,:3'):
            test_data = data_segment[-3:, :3]
            self.assertTrue(test_data.shape == (3, 3))

        with self.subTest(msg='corners :3,-3:'):
            test_data = data_segment[:3, -3:]
            self.assertTrue(test_data.shape == (3, 3))

        with self.subTest(msg='corners -3:,-3:'):
            test_data = data_segment[-3:, -3:]
            self.assertTrue(test_data.shape == (3, 3))

        with self.assertRaises(ValueError, msg='write_raw attempt'):
            data_segment.write_raw(data, start_indices=0)

        with self.assertRaises(ValueError, msg='write attempt'):
            data_segment.write(complex_data, start_indices=0)

        with self.subTest(msg='close functionality test'):
            self.assertFalse(data_segment.closed)
            data_segment.close()
            self.assertTrue(data_segment.closed)

        with self.assertRaises(ValueError, msg='read access when closed'):
            _ = data_segment.read(None)

        with self.assertRaises(ValueError, msg='read_raw access when closed'):
            _ = data_segment.read_raw(None)