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

Framework location work #47

Merged
merged 7 commits into from
Apr 1, 2022
Merged
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
10 changes: 5 additions & 5 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from TEAL.src import Amortization
from TEAL.src import CashFlow_ExtMod
from TEAL.src import CashFlows
from TEAL.src import CashFlowUser
from TEAL.src import main as CashFlow
from .src import Amortization
from .src import CashFlow_ExtMod
from .src import CashFlows
from .src import CashFlowUser
from .src import main as CashFlow
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshua-cogliati-inl do we need to add the raven dir to the system path here so python knows about ravenframework?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, considering we have to adjust the python path to find __init__.py for TEAL, we can probably just add both the RAVEN path and the TEAL path at the same time before we get to this file.

7 changes: 1 addition & 6 deletions src/CashFlowUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@
import os
import sys

# NOTE this import exception is ONLY to allow RAVEN to directly import this module.
try:
from TEAL.src.CashFlows import Component
except ImportError:
from CashFlows import Component
from ..src.CashFlows import Component

# This plugin imports RAVEN modules. if run in stand-alone, RAVEN needs to be installed and this file
# needs to be in the propoer plugin directory.
sys.path.append(os.path.expanduser(os.path.dirname(__file__) + '/../../../framework'))

class CashFlowUser:
"""
Expand Down
17 changes: 4 additions & 13 deletions src/CashFlow_ExtMod.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,15 @@
import warnings
warnings.simplefilter('default', DeprecationWarning)

# NOTE this import exception is ONLY to allow RAVEN to directly import this module.
try:
from TEAL.src import main
from TEAL.src import CashFlows
except ImportError:
import main

from ..src import main
from ..src import CashFlows

# This plugin imports RAVEN modules. if run in stand-alone, RAVEN needs to be installed and this file
# needs to be in the propoer plugin directory.
dirPath = os.path.dirname(os.path.realpath(__file__))
# TODO fix with plugin relative path
ravenPath = os.path.dirname(__file__) + '/../../../framework'
sys.path.append(os.path.expanduser(ravenPath))

try:
from utils.graphStructure import graphObject
from PluginBaseClasses.ExternalModelPluginBase import ExternalModelPluginBase
from ravenframework.utils.graphStructure import graphObject
from ravenframework.PluginBaseClasses.ExternalModelPluginBase import ExternalModelPluginBase
except:
raise IOError("CashFlow ERROR (Initialisation): RAVEN needs to be installed and CashFlow needs to be in its plugin directory for the plugin to work!'")

Expand Down
44 changes: 16 additions & 28 deletions src/CashFlows.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,10 @@
import numpy as np
import time

# NOTE this import exception is ONLY to allow RAVEN to directly import this module.
try:
from TEAL.src import Amortization
except ImportError:
import Amortization
# TODO fix with plugin relative path
path1 = os.path.dirname(__file__)
path2 = '/../raven/framework'
path3=os.path.abspath(os.path.expanduser(path1+'/..'+path2))
path4=os.path.abspath(os.path.expanduser(path1+path2))
path5=os.path.abspath(os.path.expanduser(path1+'/../../../framework'))
sys.path.extend([path3,path4,path5])

from utils import mathUtils as utils
from utils import InputData, InputTypes, TreeStructure, xmlUtils
from ..src import Amortization

from ravenframework.utils import mathUtils
from ravenframework.utils import InputData, InputTypes, TreeStructure, xmlUtils

class GlobalSettings:
"""
Expand Down Expand Up @@ -842,13 +830,13 @@ def setVariableOrFloats(self, value):
if len(value) == 1:
# single entry should be either a float (price) or string (raven variable)
value = value[0]
if utils.isAString(value) or utils.isAFloatOrInt(value):
if mathUtils.isAString(value) or mathUtils.isAFloatOrInt(value):
ret = value
else:
raise IOError('Unrecognized alpha/driver type: "{}" with type "{}"'.format(value, type(value)))
else:
# should be floats; InputData assures the entries are the same type already
if not utils.isAFloatOrInt(value[0]):
if not mathUtils.isAFloatOrInt(value[0]):
raise IOError('Multiple non-number entries for alpha/driver found, but require either a single variable name or multiple float entries: {}'.format(value))
ret = np.asarray(value)
return ret
Expand All @@ -864,7 +852,7 @@ def loadFromVariables(self, need, variables, cashflows, lifetime):
"""
# load variable values from variables or other cash flows, as needed (ha!)
for name, source in need.items():
if utils.isAString(source):
if mathUtils.isAString(source):
# as a string, this is either from the variables or other cashflows
# look in variables first
value = variables.get(source, None)
Expand Down Expand Up @@ -1017,13 +1005,13 @@ def extendParameters(self, toExtend, t):
# for capex, both the Driver and Alpha are nonzero in year 1 and zero thereafter
for name, value in toExtend.items():
if name.lower() in ['alpha', 'driver']:
if utils.isAFloatOrInt(value):
if mathUtils.isAFloatOrInt(value):
new = np.zeros(t)
new[0] = float(value)
toExtend[name] = new
elif type(value) in [list, np.ndarray]:
if len(value) == 1:
if utils.isAFloatOrInt(value[0]):
if mathUtils.isAFloatOrInt(value[0]):
new = np.zeros(t)
new[0] = float(value)
toExtend[name] = new
Expand Down Expand Up @@ -1068,7 +1056,7 @@ def calculateCashflow(self, variables, lifetimeCashflows, lifetime, verbosity):
mult = self.getMultiplier()
if mult is None:
mult = 1.0
elif utils.isAString(mult):
elif mathUtils.isAString(mult):
mult = float(variables[mult])
result = mult * alpha * (driver / reference) ** scale
if verbosity > 1:
Expand All @@ -1092,7 +1080,7 @@ def checkParamLengths(self, lifetime, compName=None):
for param in ['alpha', 'driver']:
val = self.getParam(param)
# if a string, then it's probably a variable, so don't check it now
if utils.isAString(val):
if mathUtils.isAString(val):
continue
# if it's valued, then it better be the same length as the lifetime (which is comp lifetime + 1)
elif len(val) != lifetime:
Expand Down Expand Up @@ -1160,7 +1148,7 @@ def computeIntrayearCashflow(self, year, alpha, driver):
mult = self.getMultiplier()
if mult is None:
mult = 1.0
elif utils.isAString(mult):
elif mathUtils.isAString(mult):
raise NotImplementedError
try:
self._yearlyCashflow[year] = mult * (alpha * driver).sum() # +1 is for initial construct year
Expand All @@ -1181,7 +1169,7 @@ def computeYearlyCashflow(self, alpha, driver):
mult = self.getMultiplier()
if mult is None:
mult = 1.0
elif utils.isAString(mult):
elif mathUtils.isAString(mult):
raise NotImplementedError
try:
self._yearlyCashflow = mult * (alpha * driver)
Expand Down Expand Up @@ -1229,13 +1217,13 @@ def extendParameters(self, toExtend, t):
# FIXME: we're going to integrate alpha * D over time (not year time, intrayear time)
for name, value in toExtend.items():
if name.lower() in ['alpha', 'driver']:
if utils.isAFloatOrInt(value):
if mathUtils.isAFloatOrInt(value):
new = np.ones(t) * float(value)
new[0] = 0
toExtend[name] = new
elif type(value) in [list, np.ndarray]:
if len(value) == 1:
if utils.isAFloatOrInt(value[0]):
if mathUtils.isAFloatOrInt(value[0]):
new = np.ones(t) * float(value)
new[0] = 0
toExtend[name] = new
Expand Down Expand Up @@ -1275,12 +1263,12 @@ def extendParameters(self, toExtend, t):
driver = toExtend['driver']
# how we treat the driver depends on if this is the amortizer or the depreciator
if self.name.split('_')[-2] == 'amortize':
if not utils.isAString(driver):
if not mathUtils.isAString(driver):
toExtend['driver'] = np.ones(t) * driver[0] * -1.0
toExtend['driver'][0] = 0.0
for name, value in toExtend.items():
if name.lower() in ['driver']:
if utils.isAFloatOrInt(value) or (len(value) == 1 and utils.isAFloatOrInt(value[0])):
if mathUtils.isAFloatOrInt(value) or (len(value) == 1 and mathUtils.isAFloatOrInt(value[0])):
new = np.zeros(t)
new[1:] = float(value)
toExtend[name] = new
Expand Down
19 changes: 6 additions & 13 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,11 @@

import numpy as np
import numpy_financial as npf
try:
from TEAL.src import CashFlows
# NOTE this import exception is ONLY to allow RAVEN to directly import this extmod.
# In general, this should not exist, and RAVEN should import TEAL.CashFlow instead of importing Teal directly, implicitly.
except (ImportError, ModuleNotFoundError):
import CashFlows

raven_path= os.path.abspath(os.path.dirname(__file__)) + '/../../raven/framework'
sys.path.append(raven_path) #'~/projects/raven/framework') # TODO generic RAVEN location
from ..src import CashFlows

from utils.graphStructure import graphObject
from utils import mathUtils as utils
from ravenframework.utils.graphStructure import graphObject
from ravenframework.utils import mathUtils

#=====================
# UTILITIES
Expand Down Expand Up @@ -143,7 +136,7 @@ def _createEvalProcess(components, variables, pyomoVar=False):
# does the driver come from the variable list, or from another cashflow, or is it already evaluated?
cfn = '{}|{}'.format(comp.name, cf.name)
found = False
if driver is None or utils.isAFloatOrInt(driver) or isinstance(driver, np.ndarray) or pyomoVar:
if driver is None or mathUtils.isAFloatOrInt(driver) or isinstance(driver, np.ndarray) or pyomoVar:
found = True
# TODO assert it's already filled?
evaluated.append(cfn)
Expand Down Expand Up @@ -217,11 +210,11 @@ def componentLifeCashflow(comp, cf, variables, lifetimeCashflows, v=100, pyomoVa
for item, value in results.items():
if item == 'result':
continue
if utils.isAFloatOrInt(value):
if mathUtils.isAFloatOrInt(value):
vprint(v, 1, m, paramText.format(item, value))
else:
orig = cf.getParam(item)
if utils.isSingleValued(orig):
if mathUtils.isSingleValued(orig):
name = orig
else:
name = '(from input)'
Expand Down
4 changes: 3 additions & 1 deletion tests/HourlyObjectOrientedTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import sys
import numpy as np
import pandas as pd
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) #Plugins (including TEAL)
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))) #RAVEN
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'raven'))) #RAVEN (if TEAL and RAVEN in same directory)
from TEAL import CashFlows
from TEAL import CashFlow as RunCashFlow

Expand Down
1 change: 1 addition & 0 deletions tests/tests
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
[./CashFlow_NPV_RunAsCode]
type = 'RavenPython'
input = 'CashFlow_test_NPV_RunAsCode.py'
skip = 'Need to create driver to make this work'
[../]

[./CashFlow_NPV_customTime]
Expand Down