Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a pandas reader #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions openmatrix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ def open_file(filename, mode='r', title='', root_uep='/',

return f

def read_pandas(filename, mapping=None, matrix_name=None):
"""
Reads the .omx file, gets the mapping and matrix values
and return the pandas DataFrame
Parameters
----------
filename - path to the .omx file
mapping - str of mapping in the omx file (if None takes the first mapping)
matrix_name - str of the matrix name (if None takes the first matrix name)

Returns
-------
pandas DataFrame. index = mapping, columns = mapping, values = matrix values

author: Rafal Kucharski rafal.kucharski@uj.edu.pl
"""
import pandas as pd
try:
omx_file = open_file(filename)
except:
raise Exception('Error whle reading a file: ', filename)
if mapping is None:
mapping = omx_file.list_mappings()[0]
if matrix_name is None:
matrix_name = omx_file.list_matrices()[0]

try:
values = np.array(omx_file[matrix_name])
except:
raise Exception('Wrong matrix name in the omx file')
try:
mapping = omx_file.mapping(mapping)
except:
raise Exception('Wrong mapping name name in the omx file')

return pd.DataFrame(index=mapping, columns=mapping, data=values)


if __name__ == "__main__":
print('OMX!')
Expand Down