lmpy.matrix

Module that contains a Matrix class that has header information.

Module Contents

Classes

Matrix

Create a new Matrix object from an existing ndarray.

class lmpy.matrix.Matrix(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)[source]

Bases: numpy.ndarray

Create a new Matrix object from an existing ndarray.

Parameters
  • input_array (numpy.ndarray) – An existing ndarray

  • headers (dict or list of list of str) –

    Optional headers for this matrix. This may be either a list of lists, where the index of a list in the lists will be treated as the axis:

    (ex. [['Row 1', 'Row 2', 'Row 3'],
          ['Column 1', 'Column 2']])
    

    Or this could be a dictionary where the key is used for the axis. (Ex:

    {

    ‘1’ : [‘Column 1’, ‘Column 2’], ‘0’ : [‘Row 1’, ‘Row 2’, ‘Row 3’]

    }

  • metadata (dict) – Optional metadata about his matrix.

Note

  • Triggers a call to Matrix.__array_finalize__.

Returns

A converted numpy array to Matrix.

Return type

Matrix

property T(self)[source]

Get the transpose of the matrix.

Returns

The matrix transpose.

Return type

Matrix

__array_finalize__(self, obj)[source]

Overridden function from ndarray.

self is a new object resulting from ndarray.__new__(Matrix, …), therefore it only has the attributes that the ndarray.__new__ constructor gave it.

Parameters

( (obj) – obj:`Matrix1): If the object is a matrix, pull out headers and metadata.

classmethod concatenate(cls, mtx_list, axis=0)[source]

Concatenates multiple Matrix objects together to form a new Matrix.

Parameters
  • mtx_list (list of Matrix) – A List of Matrix objects to concatenate together.

  • axis (int, optional) – The axis to concatenate these Matrix objects on. This should be an integer for the matrix axis of concatenation. This will be converted to a string where needed for headers.

Note

  • Assumes that headers for other axes are the same.

Returns

The concatenated Matrix objects.

Return type

Matrix

flatten_2d(self)[source]

Flattens a higher dimension Matrix object into a 2D matrix.

Returns

A Matrix object flattened to only have two dimensions.

Return type

Matrix

get_column_headers(self)[source]

Shortcut to get column headers.

Returns

A list of headers for each column.

get_headers(self, axis=None)[source]

Gets the headers associated with this Matrix.

Parameters

axis (int, optional) – If provided, return headers for this axis, else, return all.

Returns

If axis is None, a dictionary of all headers for the matrix. list: If axis is int, A list of headers for the specified axis.

Return type

dict

get_metadata(self)[source]

Retrieves matrix metadata.

Returns

A dictionary of metadata for the matrix.

Return type

dict

get_row_headers(self)[source]

Shortcut to get row headers.

Returns

A list of headers for the rows in the matrix.

classmethod load(cls, filename)[source]

Load a matrix from a filename.

Parameters

filename (str) – File location of matrix to load.

Returns

The matrix read from the file.

Return type

Matrix

classmethod load_csv(cls, flo, dtype=float, num_header_rows=0, num_header_cols=0)[source]

Attempts to load a Matrix object from a CSV file-like object.

Parameters
  • flo (File-like object) – A file like object containing csv data.

  • dtype (method, optional) – The data type for the data. Will be used to cast data when adding to matrix.

  • num_header_rows (int, optional) – The number of header rows in the CSV file.

  • num_header_cols (int, optional) – The number of header columns in the CSV file.

Returns

The newly loaded Matrix object.

Return type

Matrix

classmethod load_flo(cls, flo)[source]

Attempts to load a Matrix object from a file.

Parameters

flo (file-like) – A file-like object with matrix data.

Returns

The newly loaded Matrix object.

Return type

Matrix

save(self, flo)[source]

Saves the Matrix to a file-like object.

Saves the Matrix object in a JSON / Numpy zip file to the file-like object.

Parameters

flo (file-like) – The file-like object to write to.

set_column_headers(self, headers)[source]

Shortcut to set column headers.

Parameters

headers (list of str) – A list of new column headers.

set_headers(self, headers, axis=None)[source]

Sets the headers for this Matrix.

Parameters
  • headers (dict or list of list or list) – Matrix headers. Can be a list of lists, a dictionary of lists, or if axis is provided, a single list.

  • axis (int) – If provided, set the headers for a specific axis, else, process as if it is for the entire Matrix.

Note

Resets headers dictionary when setting values for all headers. Duck types to use list of lists or dictionary to set values for different

axes.

set_row_headers(self, headers)[source]

Shortcut to set row headers.

Parameters

headers (list of str) – A list of new row headers.

slice(self, *args)[source]

Subsets the matrix and returns a new instance.

Parameters

*args – A variable length argument list of iterables for the indices to retrieve for each axis.

Note

  • The first parameter will be for axis 0, second for axis 1, etc.

Returns

A new Matrix that is a subset of the original specified by

the slicing parameters.

Return type

Matrix

slice_by_header(self, header, axis)[source]

Gets a slice of the Matrix matching the header provided.

Parameters
  • header (str) – The name of a header to use for slicing

  • axis (int) – The axis to find this header.

Returns

A subset of the original Matrix specified by the header.

Return type

Matrix

transpose(self, *axes)[source]

Transposes the Matrix.

Parameters

axes (None, tuple of int, n :obj:`int`s) – The order of the axes in the transposition. see: ndarray.transpose

Returns

A transposed version of the original matrix

Return type

Matrix

write(self, filename)[source]

Write the matrix to the specified file location.

Parameters

filename (str) – The file location to save to.

write_csv(self, flo, *slice_args)[source]

Writes the Matrix object to a CSV file-like object.

Parameters
  • flo (file-like) – The file-like object to write to.

  • *slice_args – A variable length argument list of iterables to use for a slice operation prior to generating CSV content.

Note

Currently only works for 2-D tables.