Skip to content

Commit

Permalink
add md stats reader
Browse files Browse the repository at this point in the history
  • Loading branch information
alinelena committed Apr 3, 2024
1 parent 2b6f0df commit ecaa7d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
33 changes: 20 additions & 13 deletions janus_core/stats.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
"""
Module that reads the md stats output timeseries
Module that reads the md stats output timeseries.
"""

import re

from numpy import genfromtxt

from janus_core.janus_types import PathLike

class Stats():

class Stats:
"""
Configure shared molecular dynamics simulation options.
Parameters
----------
source: PathLike
source : PathLike
File that contains the stats of a molecular dynamics simulation.
"""

Expand All @@ -21,7 +25,7 @@ def __init__(self, source: PathLike = None) -> None:
Parameters
----------
source: PathLike
source : PathLike
File that contains the stats of a molecular dynamics simulation.
"""

Expand All @@ -34,20 +38,23 @@ def __init__(self, source: PathLike = None) -> None:
self.source = source
self.read(source)

def read(self,filename: PathLike = None)-> None:
def read(self, filename: PathLike = None) -> None:
"""
Read MD stats and store them.
Parameters
----------
source: PathLike
filename : PathLike
File that contains the stats of a molecular dynamics simulation.
"""
self.data = genfromtxt(filename,skip_header=1)
f = open(filename,"r+")
head = f.readline().split('|')
self.units = [ re.search("\[.+?\]", x).group(0) if re.search("\[.+?\]", x) else '' for x in head ]
self.labels = [ re.sub("[\[].*?[\]]", "", x).rstrip().lstrip() for x in head ]
f.close()
self.data = genfromtxt(filename, skip_header=1)
with open(filename, "r+", encoding="utf-8") as f:
head = f.readline().split("|")
self.units = [
re.search(r"\[.+?\]", x).group(0) if re.search(r"\[.+?\]", x) else ""
for x in head
]
self.labels = [
re.sub(r"[\[].*?[\]]", "", x).rstrip().lstrip() for x in head
]
self.rows, self.columns = self.data.shape

12 changes: 7 additions & 5 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"""Test stats reader."""

from pathlib import Path

import pytest

from janus_core.stats import Stats

DATA_PATH = Path(__file__).parent / "data"


def test_stats():
"""Test readind md stats"""
data_path = DATA_PATH / "md-stats.dat"

s = Stats(data_path)_
s = Stats(data_path)

assert s.rows == 100
assert s.columns == 18
assert s.units[0] == ''
assert s.units[17] == '[K]'
assert s.units[0] == 'Steps'
assert s.units[17] == 'T*'
assert s.units[0] == ""
assert s.units[17] == "[K]"
assert s.units[0] == "Steps"
assert s.units[17] == "T*"

0 comments on commit ecaa7d8

Please sign in to comment.