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
Make pyedr also return a dictionary of units #56
Changes from 8 commits
dcd7cfd
8876a55
14f3c4b
8a9bfa9
fa82cf7
780a93e
6bd1e6e
c81aefb
d078436
8e5f566
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .panedr import edr_to_df, get_unit_dictionary | ||
import pbr.version | ||
__version__ = pbr.version.VersionInfo('panedr').release_string() | ||
del pbr | ||
|
||
from .panedr import edr_to_df |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .pyedr import edr_to_dict, read_edr, get_unit_dictionary | ||
import pbr.version | ||
__version__ = pbr.version.VersionInfo('pyedr').release_string() | ||
del pbr | ||
|
||
from .pyedr import edr_to_dict, read_edr |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ | |
Enxnm = collections.namedtuple('Enxnm', 'name unit') | ||
ENX_VERSION = 5 | ||
|
||
__all__ = ['edr_to_dict', 'read_edr'] | ||
__all__ = ['edr_to_dict', 'read_edr', 'get_unit_dictionary'] | ||
|
||
class EDRFile(object): | ||
def __init__(self, path): | ||
|
@@ -409,7 +409,10 @@ def is_frame_magic(data): | |
all_energies_type = List[List[float]] | ||
all_names_type = List[str] | ||
times_type = List[float] | ||
read_edr_return_type = Tuple[all_energies_type, all_names_type, times_type] | ||
read_edr_return_type = Tuple[all_energies_type, | ||
all_names_type, | ||
times_type, | ||
Dict[str, str]] | ||
|
||
|
||
def read_edr(path: str, verbose: bool = False) -> read_edr_return_type: | ||
|
@@ -436,6 +439,8 @@ def read_edr(path: str, verbose: bool = False) -> read_edr_return_type: | |
A list containing the names of the energy terms found in the file | ||
times: list[float] | ||
A list containing the time of each step/frame. | ||
unit_dict: Dict[str, str] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not in the return signature? |
||
A dictionary mapping the term names to their units. | ||
""" | ||
begin = time.time() | ||
edr_file = EDRFile(str(path)) | ||
|
@@ -464,7 +469,30 @@ def read_edr(path: str, verbose: bool = False) -> read_edr_return_type: | |
return all_energies, all_names, times | ||
|
||
|
||
def edr_to_dict(path: str, verbose: bool = False) -> Dict[str, np.ndarray]: | ||
def get_unit_dictionary(path: str) -> Dict[str, str]: | ||
"""Creates an EDRFile object which executes the :func:`do_enxnms` | ||
method. This reads the names and units of the EDR data, which is returned | ||
as a dictionary mapping column names (str) to unit names (str). | ||
|
||
Parameters | ||
---------- | ||
path : str | ||
path to EDR file to be read | ||
|
||
Returns | ||
------- | ||
unit_dict: Dict[str, str] | ||
A dictionary mapping the term names to their units. | ||
""" | ||
edr_file = EDRFile(str(path)) | ||
unit_dict = {'Time': "ps"} | ||
for nm in edr_file.nms: | ||
unit_dict[nm.name] = nm.unit | ||
return unit_dict | ||
|
||
|
||
def edr_to_dict(path: str, verbose: bool = False) -> (Dict[str, np.ndarray], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand what's going on with this PR, didn't you say that you wanted to not return a tuple here, and therefore added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really need to stop changing code without changing the documentation to match it..... thanks! |
||
Dict[str, str]): | ||
"""Calls :func:`read_edr` and packs its return values into a dictionary | ||
|
||
The returned dictionary's keys are the names of the energy terms present in | ||
|
@@ -481,6 +509,8 @@ def edr_to_dict(path: str, verbose: bool = False) -> Dict[str, np.ndarray]: | |
------- | ||
enery_dict: dict[str, np.ndarray] | ||
dictionary that holds all energy terms found in the EDR file. | ||
unit_dict: Dict[str, str] | ||
A dictionary mapping the term names to their units. | ||
""" | ||
all_energies, all_names, times = read_edr(path, verbose=verbose) | ||
energy_dict = {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,17 @@ | |
import re | ||
import sys | ||
import unittest | ||
import pickle | ||
|
||
import pytest | ||
import numpy as np | ||
from numpy.testing import assert_allclose | ||
|
||
import pyedr | ||
from pyedr.tests.datafiles import ( | ||
EDR, EDR_XVG, EDR_IRREGULAR, EDR_IRREGULAR_XVG, | ||
EDR_DOUBLE, EDR_DOUBLE_XVG, EDR_BLOCKS, EDR_BLOCKS_XVG | ||
EDR, EDR_XVG, EDR_UNITS, EDR_IRREG, EDR_IRREG_XVG, | ||
EDR_IRREG_UNITS, EDR_DOUBLE, EDR_DOUBLE_XVG, EDR_DOUBLE_UNITS, | ||
EDR_BLOCKS, EDR_BLOCKS_XVG, EDR_BLOCKS_UNITS | ||
) | ||
|
||
|
||
|
@@ -28,39 +30,47 @@ | |
NDEC_PATTERN = re.compile(r'[\.eE]') | ||
|
||
# Data constants | ||
EDR_Data = namedtuple('EDR_Data', | ||
['edr_dict', 'xvgdata', 'xvgtime', 'xvgnames', | ||
'xvgcols', 'xvgprec', 'edrfile', 'xvgfile']) | ||
EDR_Data = namedtuple('EDR_Data', | ||
['edr_dict', 'edr_units', 'xvgdata', 'xvgtime', | ||
'xvgnames', 'xvgcols', 'xvgprec', 'true_units', | ||
'edrfile', 'xvgfile']) | ||
|
||
|
||
@pytest.fixture(scope='module', | ||
params=[(EDR, EDR_XVG), | ||
(EDR_IRREGULAR, EDR_IRREGULAR_XVG), | ||
(EDR_DOUBLE, EDR_DOUBLE_XVG), | ||
(EDR_BLOCKS, EDR_BLOCKS_XVG), | ||
(Path(EDR), EDR_XVG),]) | ||
params=[(EDR, EDR_XVG, EDR_UNITS), | ||
(EDR_IRREG, EDR_IRREG_XVG, EDR_IRREG_UNITS), | ||
(EDR_DOUBLE, EDR_DOUBLE_XVG, EDR_DOUBLE_UNITS), | ||
(EDR_BLOCKS, EDR_BLOCKS_XVG, EDR_BLOCKS_UNITS), | ||
(Path(EDR), EDR_XVG, EDR_UNITS), ]) | ||
def edr(request): | ||
edrfile, xvgfile = request.param | ||
edrfile, xvgfile, unitfile = request.param | ||
edr_dict = pyedr.edr_to_dict(edrfile) | ||
edr_units = pyedr.get_unit_dictionary(edrfile) | ||
xvgdata, xvgnames, xvgprec = read_xvg(xvgfile) | ||
with open(unitfile, "rb") as f: | ||
true_units = pickle.load(f) | ||
xvgtime = xvgdata[:, 0] | ||
xvgdata = xvgdata[:, 1:] | ||
xvgcols = np.insert(xvgnames, 0, u'Time') | ||
return EDR_Data(edr_dict, xvgdata, xvgtime, xvgnames, | ||
xvgcols, xvgprec, edrfile, xvgfile) | ||
return EDR_Data(edr_dict, edr_units, xvgdata, xvgtime, xvgnames, | ||
xvgcols, xvgprec, true_units, edrfile, xvgfile) | ||
|
||
|
||
class TestEdrToDict(object): | ||
""" | ||
Tests for :fun:`pyedr.edr_to_dict`. | ||
""" | ||
|
||
def test_output_type(self, edr): | ||
""" | ||
Test that the function returns a dictionary of ndarrays | ||
""" | ||
assert isinstance(edr.edr_dict, dict) | ||
assert isinstance(edr.edr_dict['Time'], np.ndarray) | ||
|
||
def test_units(self, edr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class name doesn't really match what this is doing, but I don't think it's worth being pedantic about it. |
||
assert edr.edr_units == edr.true_units | ||
|
||
def test_columns(self, edr): | ||
""" | ||
Test that the dictionary names match | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll let it go here because the likelihood is that no one was using these downstream, but arbitrarily renaming file object names like this is a breaking change, please avoid doing this.