Ejemplo n.º 1
0
 def test_from_info(self):
     arr = info_header.InfoArray(self.data)
     mat_arr = matrix.make_mat(arr, (0, 1), (0, 2))
     self.assertTrue(isinstance(mat_arr, matrix.MatrixObject))
     mem = info_header.InfoMemmap(self.memmap_data)
     vect_mem = vector.make_vect(mem)
     self.assertTrue(isinstance(vect_mem, vector.VectorObject))
Ejemplo n.º 2
0
 def test_from_memmap(self):
     # Works if constructed from array.
     data = npfor.open_memmap('temp.npy', mode='w+', shape=(4, 3, 3))
     data[:] = 5.0
     Mat = info_header.InfoMemmap(data, {'a': 'b'})
     Mat.flush()
     self.assertEqual(Mat.shape, (4, 3, 3))
     self.assertEqual(Mat.info['a'], 'b')
     self.assertTrue(sp.allclose(Mat, 5.0))
     self.assertTrue(isinstance(Mat, sp.memmap))
     del Mat
     os.remove('temp.npy')
Ejemplo n.º 3
0
 def test_assert_info(self):
     """Test the assert_info function."""
     # info_memaps should pass.
     data = npfor.open_memmap('temp.npy', mode='w+', shape=(4, 3, 3))
     data[:] = 5.0
     Mat = info_header.InfoMemmap(data)
     info_header.assert_info(Mat)
     del Mat
     os.remove('temp.npy')
     # info_arrays should pass.
     data = sp.empty((5, 6, 6))
     data[:] = 4.0
     Mat = info_header.InfoArray(data)
     info_header.assert_info(Mat)
     # arrays should fail.
     self.assertRaises(TypeError, info_header.assert_info, data)
Ejemplo n.º 4
0
def make_mat(array, row_axes=None, col_axes=None, axis_names=None):
    """Do what ever it takes to make a mat out of an array.

    Convert any class that can be converted to a mat (array, InfoArray,
    memmap, InfoMemmap) to the appropriate mat object (mat_array or
    mat_memmap).

    This convieiance function just simplifies the constructor heirarchy.
    Normally to get an mat out of an array, you would need to construct an
    intermediate InfoArray object.  This bypasses that step.

    Parameters
    ----------
    array: array_like
        Array to be converted to mat object (if possible).
    row_axes: tuple of ints
        Sequence contains the axis numbers of the array to identify as
        varying over the matrix rows. This sequence is stored in the
        `rows` attribute.  This parameter is ignored if
        `input_array`'s info attribute already contains the rows.
    col_axis: tuple of ints
        Sequence contains the axis numbers of the array to identify as
        varying over the matrix columns. This sequence is stored in the
        `cols` attribute.  This parameter is ignored if
        `input_array`'s info attribute already contains the cols.
    axis_names: tuple of strings, optional
        The sequence contains the name of each axis.  This sequence will be
        stored in the `axes` attribute.  This parameter is ignored if
        `input_array`'s info attribute already contains the axis names.

    Returns
    -------
    mat_arr: mat_array or mat_memmap
        A view of `array` converted to a mat object.
    """
    if isinstance(array, sp.memmap):
        if not isinstance(array, info_header.InfoMemmap):
            array = info_header.InfoMemmap(array)
        return mat_memmap(array, row_axes, col_axes, axis_names)
    elif isinstance(array, sp.ndarray):
        if not isinstance(array, info_header.InfoArray):
            array = info_header.InfoArray(array)
        return mat_array(array, row_axes, col_axes, axis_names)
    else:
        raise TypeError("Object cannot be converted to a matrix.")
Ejemplo n.º 5
0
def make_vect(array, axis_names=None):
    """Do whatever it takes to make a vect out of an array.

    Convert any class that can be converted to a vect (array, InfoArray,
    memmap, InfoMemmap) to the appropriate vect object (vect_array,
    vect_memmap).

    This convenience function just simplifies the constructor hierarchy.
    Normally to get a vect out of an array, you would need to construct an
    intermediate InfoArray object.  This bypasses that step.

    Parameters
    ----------
    array: array_like
        Array to be converted to vect object (if possible).
    axis_names: tuple of strings, optional
        The sequence contains the name of each axis.  This sequence will be
        stored in the `axes` attribute.  This parameter is ignored if
        `input_array`'s info attribute already contains the axis names.

    Returns
    -------
    vect_arr: vect_array or vect_memmap
        A view of `array` converted to a vect object.

    """

    if isinstance(array, sp.memmap):
        if not isinstance(array, info_header.InfoMemmap):
            array = info_header.InfoMemmap(array)
        return vect_memmap(array, axis_names)
    elif isinstance(array, sp.ndarray):
        if not isinstance(array, info_header.InfoArray):
            array = info_header.InfoArray(array)
        return vect_array(array, axis_names)
    else:
        raise TypeError("Object cannot be converted to a vector.")
Ejemplo n.º 6
0
def open_memmap(filename,
                mode='r+',
                dtype=None,
                shape=None,
                fortran_order=False,
                version=(1, 0),
                metafile=None):
    """Open a file and memory map it to an InfoMemmap object.

    This is similar to the numpy.lib.format.openmemmap() function but also
    deals with the meta data dictionary, which is read and written from a
    meta data file.

    The only extra argument over the numpy version is the meta data file name
    `metafile`.

    Parameters
    ----------
    metafile: str
        File name for which the `info` attribute of the returned InfoMemmap
        will be read from and written to. Default is None, where the it is
        assumed to be `filename` + ".meta".

    Returns
    -------
    marray: InfoMemmap
        The `info` is intialized as an empty dictionary if `mode` is 'w' or if
        the file corresponding to `metafile` does not exist.  The `metafile`
        attribute of marray is set to the `metafile` parameter unless `mode` is
        'r' or 'c' in which case it is set to None.
    """

    # Restrict to version (1,0) because we've only written write_header for
    # this version.
    if version != (1, 0):
        raise ValueError("Only version (1,0) is safe from this function.")

    # Memory map the data part.
    marray = npfor.open_memmap(filename, mode, dtype, shape, fortran_order,
                               version)

    # Get the file name for the meta data.
    if metafile is None:
        metafile = filename + '.meta'

    # Read the meta data if need be.
    if ('r' in mode or mode is 'c') and os.path.isfile(metafile):
        info_fid = open(metafile, 'r')
        try:
            infostring = info_fid.readline()
        finally:
            info_fid.close()
        info = safe_eval(infostring)
    else:
        info = {}

    # In read mode don't pass a metafile to protect the meta data.
    if mode is 'r' or mode is 'c':
        metafile = None

    marray = info_header.InfoMemmap(marray, info, metafile)

    return marray