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

fix ruamel and pandas api #93

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 3 additions & 6 deletions ctwrap/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@
from multiprocessing import queues as mpq
from multiprocessing import synchronize as mps
import queue # imported for using queue.Empty exception

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml
from ruamel.yaml import YAML

# ctwrap specific import
from .parser import _parse, _write, Parser
Expand Down Expand Up @@ -169,7 +165,8 @@ def from_yaml(cls, yaml_file: str,
"".format(yaml_file))

with open(full_name) as stream:
content = yaml.load(stream, Loader=yaml.SafeLoader)
yaml = YAML(typ="safe")
content = yaml.load(stream)

output = content.get('output', {})

Expand Down
11 changes: 5 additions & 6 deletions ctwrap/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

from typing import Dict, List, Any, Optional, Union

import pkg_resources
import importlib

# avoid explicit dependence on cantera
try:
pkg_resources.get_distribution('cantera')
except pkg_resources.DistributionNotFound:
ct_spec = importlib.util.find_spec("cantera")
if ct_spec is None:
ct = ImportError('Method requires a working cantera installation.')
else:
import cantera as ct
ct = importlib.import_module("cantera")


class Output:
Expand Down Expand Up @@ -236,7 +235,7 @@ def save(self, data, entry, variation=None, mode=None, errored=False):
df = pd.read_csv(fname)
else:
df = pd.DataFrame(columns=row.keys())
df = df.append(row, ignore_index=True)
df = pd.concat([df, row.to_frame().T], ignore_index=True)
df.to_csv(fname, index=False)

def dir(self):
Expand Down
14 changes: 6 additions & 8 deletions ctwrap/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,10 @@
from typing import Optional, Dict, Any, Tuple, KeysView, Generator, Union
from copy import deepcopy
from pint import UnitRegistry
from ruamel.yaml import YAML
import warnings
import re

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml


__all__ = ['Parser']

Expand Down Expand Up @@ -253,12 +249,13 @@ def from_yaml(
elif path is not None:
fname = Path(path) / fname

yaml = YAML(typ="safe")
try:
_ = fname.is_file() # will raise error
_ = fname.is_file() # will raise error
with open(fname) as stream:
out = yaml.load(stream, Loader=yaml.SafeLoader)
out = yaml.load(stream)
except OSError:
out = yaml.load(yml, Loader=yaml.SafeLoader)
out = yaml.load(yml)

if keys is None:
return cls(out)
Expand All @@ -267,6 +264,7 @@ def from_yaml(

def to_yaml(self):
"""Convert Parser content to YAML string"""
yaml = YAML(typ="safe")
return yaml.dump(self.raw, Dumper=yaml.SafeDumper)

def get(self, key, default=None):
Expand Down
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ dependencies:
- numpy
- h5py
- pint
- ruamel.yaml
- pandas
- ruamel.yaml>=0.17.0
- pandas>=1.4
- cantera
- setuptools
- pytest
Expand Down
18 changes: 7 additions & 11 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import unittest
from pathlib import Path
import h5py

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml
from ruamel.yaml import YAML

import warnings
# add exception as pywintypes imports a deprecated module
Expand All @@ -21,15 +17,14 @@
# pylint: disable=no-member
import ctwrap.output as cwo

import pkg_resources
import importlib

# avoid explicit dependence on cantera
try:
pkg_resources.get_distribution('cantera')
except pkg_resources.DistributionNotFound:
ct_spec = importlib.util.find_spec("cantera")
if ct_spec is None:
ct = ImportError('Method requires a working cantera installation.')
else:
import cantera as ct
ct = importlib.import_module("cantera")


PWD = Path(__file__).parents[0]
Expand All @@ -46,7 +41,8 @@ class TestOutput(unittest.TestCase):
@classmethod
def setUpClass(cls):
with open(EXAMPLES / cls._yaml) as stream:
cls._config = yaml.load(stream, Loader=yaml.SafeLoader)
yaml = YAML(typ='safe')
cls._config = yaml.load(stream)

out = cls._config.get('output')
if out:
Expand Down
14 changes: 6 additions & 8 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

import unittest
from pathlib import Path
import pint.quantity as pq

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml
import pint as pq
from ruamel.yaml import YAML

import warnings
# add exception as pywintypes imports a deprecated module
Expand Down Expand Up @@ -202,7 +198,8 @@ class TestYAML(unittest.TestCase):
def test_minimal(self):

with open(EXAMPLES / 'minimal.yaml') as stream:
defaults = yaml.load(stream, Loader=yaml.SafeLoader)
yaml = YAML(typ='safe')
defaults = yaml.load(stream)
p = cw.Parser.from_yaml('minimal.yaml', path=EXAMPLES)
self.assertEqual(len(p), len(defaults))
self.assertEqual(p.keys(), defaults.keys())
Expand All @@ -216,7 +213,8 @@ def test_minimal(self):
def test_ignition(self):

with open(EXAMPLES / 'ignition.yaml') as stream:
yml = yaml.load(stream, Loader=yaml.SafeLoader)
yaml = YAML(typ='safe')
yml = yaml.load(stream)
initial = cw.Parser(yml['defaults']['initial'])
self.assertIsInstance(initial.T, pq.Quantity)
self.assertIsInstance(initial['fuel'], str)
Expand Down
6 changes: 0 additions & 6 deletions tests/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@

import unittest
from pathlib import Path
import pint.quantity as pq

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml

import warnings
# add exception as pywintypes imports a deprecated module
Expand Down
13 changes: 5 additions & 8 deletions tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
import unittest
from pathlib import Path
import subprocess
import pint.quantity as pq
import importlib
import h5py

try:
import ruamel_yaml as yaml
except ImportError:
from ruamel import yaml
from ruamel.yaml import YAML

import warnings
# add exception as pywintypes imports a deprecated module
Expand Down Expand Up @@ -54,7 +49,8 @@ class TestWrap(unittest.TestCase):

def setUp(self):
with open(EXAMPLES / self._yaml) as stream:
self.config = yaml.load(stream, Loader=yaml.SafeLoader)
yaml = YAML(typ='safe')
self.config = yaml.load(stream)
self.sim = cw.Simulation.from_module(self._module)
self.sh = cw.SimulationHandler.from_yaml(self._yaml, strategy=self._strategy, database=EXAMPLES)

Expand Down Expand Up @@ -247,7 +243,8 @@ class TestInvalid(TestWrap):

def setUp(self):
with open(EXAMPLES / self._yaml) as stream:
self.config = yaml.load(stream, Loader=yaml.SafeLoader)
yaml = YAML(typ='safe')
self.config = yaml.load(stream)
self.sim = cw.Simulation.from_module(self._module)
self.sh = cw.SimulationHandler.from_dict(self._dict)

Expand Down
Loading