Skip to content

Commit

Permalink
Merge branch 'master' into functions_refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Jul 7, 2018
2 parents df14160 + 0d1321f commit 38260dc
Show file tree
Hide file tree
Showing 16 changed files with 314 additions and 2,506 deletions.
7 changes: 0 additions & 7 deletions .idea/misc.xml

This file was deleted.

21 changes: 0 additions & 21 deletions .idea/pysd.iml

This file was deleted.

2,279 changes: 0 additions & 2,279 deletions .idea/workspace.xml

This file was deleted.

Binary file removed dist/pysd-0.8.3-py2.py3-none-any.whl
Binary file not shown.
Binary file removed dist/pysd-0.8.3.tar.gz
Binary file not shown.
Binary file added dist/pysd-0.9.0-py2.py3-none-any.whl
Binary file not shown.
Binary file added dist/pysd-0.9.0.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion pysd/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.8.3"
__version__ = "0.9.0"

20 changes: 8 additions & 12 deletions pysd/py_backend/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

from __future__ import absolute_import

import os
import os.path
import pkg_resources
import textwrap
import warnings

import yapf

from io import open
from .._version import __version__
from ..py_backend import utils

Expand Down Expand Up @@ -93,7 +93,7 @@ def time():
'outfile': os.path.basename(outfile_name),
'version': __version__}

style_file = os.path.dirname(os.path.realpath(__file__)) + '/output_style.yapf'
style_file = pkg_resources.resource_filename("pysd", "py_backend/output_style.yapf")
text = text.replace('\t', ' ')
text, changed = yapf.yapf_api.FormatCode(textwrap.dedent(text),
style_config=style_file)
Expand Down Expand Up @@ -162,15 +162,11 @@ def build_element(element, subscript_dict):
%(cache)s
def %(py_name)s(%(arguments)s):
"""
%(real_name)s
%(eqn)s
%(unit)s
%(lims)s
%(kind)s
Real Name: %(real_name)s
Original Eqn: %(eqn)s
Units: %(unit)s
Limits: %(lims)s
Type: %(kind)s
%(doc)s
"""
Expand Down
48 changes: 32 additions & 16 deletions pysd/py_backend/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ def __init__(self, delay_input, delay_time, initial_value, order):
self.order = None

def initialize(self):
self.order = self.order_func() # The order can only be set once
order = self.order_func()
if order != int(order):
warnings.warn('Casting delay order from %f to %i' % (order, int(order)))
self.order = int(order) # The order can only be set once
init_state_value = self.init_func() * self.delay_time_func() / self.order
self.state = np.array([init_state_value] * self.order)

Expand Down Expand Up @@ -429,19 +432,32 @@ def set_state(self, t, state):
self.time.update(t)

for key, value in state.items():
# TODO Implement map with reference between component and stateful element?
if key in self.components._namespace.keys():
element_name = 'integ_%s' % self.components._namespace[key]
component_name = self.components._namespace[key]
stateful_name = 'integ_%s' % self.components._namespace[key]
elif key in self.components._namespace.values():
element_name = 'integ_%s' % key
component_name = key
stateful_name = 'integ_%s' % key
else: # allow the user to specify the stateful object directly
element_name = key
component_name = key
stateful_name = key

try:
element = getattr(self.components, element_name)
element.update(value)
except AttributeError:
print("'%s' has no state elements, assignment failed")
raise
# Try to update stateful component
if hasattr(self.components, stateful_name):
try:
element = getattr(self.components, stateful_name)
element.update(value)
except AttributeError:
print("'%s' has no state elements, assignment failed")
raise
else:
# Try to override component
try:
setattr(self.components, component_name, self._constant_component(value))
except AttributeError:
print("'%s' has no component, assignment failed")
raise

def clear_caches(self):
""" Clears the Caches for all model elements """
Expand Down Expand Up @@ -471,11 +487,11 @@ def doc(self):
lines = docstring.split('\n')
collector.append({'Real Name': name,
'Py Name': varname,
'Eqn': lines[3].strip(),
'Unit': lines[5].strip(),
'Lims': lines[7].strip(),
'Type': lines[9].strip(),
'Comment': '\n'.join(lines[9:]).strip()})
'Eqn': lines[2].replace("Original Eqn:", "").strip(),
'Unit': lines[3].replace("Units:", "").strip(),
'Lims': lines[4].replace("Limits:", "").strip(),
'Type': lines[5].replace("Type:", "").strip(),
'Comment': '\n'.join(lines[7:]).strip()})
except:
pass

Expand Down Expand Up @@ -574,7 +590,7 @@ def _format_return_timestamps(self, return_timestamps=None):
self.components.final_time() + self.components.saveper(),
self.components.saveper(), dtype=np.float64
)
elif isinstance(return_timestamps, (list, int, float, range, np.ndarray)):
elif isinstance(return_timestamps, (list, int, float, np.ndarray)):
return_timestamps_array = np.array(return_timestamps, ndmin=1)
elif isinstance(return_timestamps, _pd.Series):
return_timestamps_array = return_timestamps.as_matrix()
Expand Down
2 changes: 1 addition & 1 deletion pysd/py_backend/vensim/table2py.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
import warnings
from ...pysd import read_vensim

from io import open

def read_tabular(table_file, sheetname='Sheet1'):
"""
Expand Down
79 changes: 48 additions & 31 deletions pysd/py_backend/vensim/vensim2py.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pysd/pysd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
--------
August 15, 2014: created
June 6 2015: Major updates - version 0.2.5
Jan 2016: Rework to handle subscripts
Jan 2016: Rework to handle subscripts
May 2016: Updates to handle grammar refactoring
Sept 2016: Major refactor, putting most internal code into the Model and Macro objects
"""
Expand Down
Loading

0 comments on commit 38260dc

Please sign in to comment.