Skip to content

Commit e3d8845

Browse files
committed
Fix #55 create model meta class
* bump to version 0.2.6 Bicycle Bears * update model attribute if layers declared as classes in model class so that it can be saved as JSON and remove FIXME * fix import issues in carousel.test - don't use imp, instead use importlib, since imp causes import issues with pvpower.tests * change name of import from "pvpower_models" to "sandia_performance_model" in tests, test_model and test_calcs * use dirname(path) instead of join(path, '..')
1 parent 337c9b5 commit e3d8845

6 files changed

Lines changed: 55 additions & 29 deletions

File tree

carousel/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
__author__ = u'Mark Mikofski'
88
__email__ = u'mark.mikofski@sunpowercorp.com'
99
__url__ = u'https://github.com/SunPower/Carousel'
10-
__version__ = u'0.2.5'
11-
__release__ = u'Brass Rings'
10+
__version__ = u'0.2.6'
11+
__release__ = u'Bicycle Bears'

carousel/core/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def _initialize(self):
178178
# initialize layers
179179
# FIXME: move import inside loop for custom layers in different modules
180180
mod = importlib.import_module(self.layers_mod, self.layers_pkg)
181+
src_model = {}
181182
for layer, value in self.model.iteritems():
182183
# from layers module get the layer's class definition
183184
layer_cls = getattr(mod, self.layer_cls_names[layer]) # class def
@@ -200,10 +201,12 @@ def _initialize(self):
200201
src_value[src.__name__].update(kwargs)
201202
# use layer values generated from source class
202203
if src_value:
203-
value = src_value
204-
# FIXME: update model with dictionary, can't save
204+
value = src_model[layer] = src_value
205205
# set layer attribute with model data
206206
setattr(self, layer, layer_cls(value))
207+
# update model with layer values generated from source classes
208+
if src_model:
209+
self.model.update(src_model)
207210
self._update()
208211
self._state = 'initialized'
209212

@@ -329,4 +332,5 @@ def command(self, cmd, progress_hook=None, *args, **kwargs):
329332
sim_names = self.cmd_layer.reg.iterkeys()
330333
for sim_name in sim_names:
331334
sim_cmd = getattr(self.cmd_layer.reg[sim_name], cmd)
332-
sim_cmd(self.registries, progress_hook=progress_hook, *args, **kwargs)
335+
sim_cmd(self.registries, progress_hook=progress_hook,
336+
*args, **kwargs)

carousel/tests/__init__.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from carousel.core import logging
6-
import imp
6+
import importlib
77
import os
88
import sys
99

@@ -17,10 +17,29 @@
1717
))
1818

1919
sys.path.append(PROJ_PATH)
20-
try:
21-
fid, fn, info = imp.find_module(
22-
MODEL, [os.path.join(PROJ_PATH, PROJECT.lower())]
23-
)
24-
pvpower_models = imp.load_module(MODEL, fid, fn, info)
25-
finally:
26-
fid.close()
20+
sandia_performance_model = importlib.import_module(
21+
'.%s' % MODEL, PROJECT.lower()
22+
)
23+
24+
# using imp interferes with pvpower tests so comment out
25+
26+
# import imp
27+
# PVPOWER_PKG = PROJECT.lower()
28+
#
29+
# # pvpower package
30+
# fid, fn, info = imp.find_module(PVPOWER_PKG, [PROJ_PATH])
31+
# LOGGER.debug('filename: %s', fn)
32+
# try:
33+
# PVPOWER_PKG = imp.load_module(PVPOWER_PKG, fid, fn, info)
34+
# finally:
35+
# if fid:
36+
# fid.close()
37+
# LOGGER.debug('package: %r', PVPOWER_PKG)
38+
# PVPOWER_MOD = '%s.%s' % (PVPOWER_PKG.__name__, MODEL)
39+
# # pvpower model
40+
# fid, fn, info = imp.find_module(MODEL, PVPOWER_PKG.__path__)
41+
# try:
42+
# sandia_performance_model = imp.load_module(PVPOWER_MOD, fid, fn, info)
43+
# finally:
44+
# if fid:
45+
# fid.close()

carousel/tests/test_calcs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from nose.tools import ok_, eq_
66
from carousel.core.calculations import Calc
7-
from carousel.tests import PROJ_PATH, pvpower_models
7+
from carousel.tests import PROJ_PATH, sandia_performance_model
88
import os
99
import uncertainties
1010
from pvlib.solarposition import get_solarposition as solpos
@@ -73,7 +73,7 @@ def test_static_calc_unc():
7373
# FIXME: this shouldn't have to run a model to test the uncertainty
7474
test_model_file = os.path.join(PROJ_PATH, 'models',
7575
'sandia_performance_model-Tuscon.json')
76-
test_model = pvpower_models.SAPM(test_model_file) # create model
76+
test_model = sandia_performance_model.SAPM(test_model_file) # create model
7777
test_model.command('start') # start simulation
7878
# get parameters from model
7979
dt = test_model.outputs.reg['timestamps'] # timestamps

carousel/tests/test_model.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from nose.tools import ok_
77
from carousel.core.models import Model
8-
from carousel.tests import PROJ_PATH, pvpower_models, logging
8+
from carousel.tests import PROJ_PATH, sandia_performance_model, logging
99
import os
1010

1111
LOGGER = logging.getLogger(__name__)
@@ -18,7 +18,7 @@ def test_carousel_model():
1818
"""
1919

2020
model_test_file = os.path.join(PROJ_PATH, 'models', MODELFILE)
21-
carousel_model_test1 = pvpower_models.SAPM(model_test_file)
21+
carousel_model_test1 = sandia_performance_model.SAPM(model_test_file)
2222
ok_(isinstance(carousel_model_test1, Model))
2323

2424
carousel_model_test2 = PVPowerSAPM2()
@@ -98,23 +98,26 @@ class PVPowerSAPM2(Model):
9898
class PVPowerSAPM3(Model):
9999
modelpath = PROJ_PATH
100100
outputs = [
101-
pvpower_models.PVPowerOutputs,
102-
pvpower_models.PerformanceOutputs,
103-
pvpower_models.IrradianceOutputs
101+
sandia_performance_model.PVPowerOutputs,
102+
sandia_performance_model.PerformanceOutputs,
103+
sandia_performance_model.IrradianceOutputs
104104
]
105105
formulas = [
106-
pvpower_models.UtilityFormulas,
107-
pvpower_models.PerformanceFormulas,
108-
pvpower_models.IrradianceFormulas
106+
sandia_performance_model.UtilityFormulas,
107+
sandia_performance_model.PerformanceFormulas,
108+
sandia_performance_model.IrradianceFormulas
109+
]
110+
data = [
111+
(sandia_performance_model.PVPowerData,
112+
{"path": None, "filename": "Tuscon.json"})
109113
]
110-
data = [(pvpower_models.PVPowerData, {"filename": "Tuscon.json"})]
111114
calculations = [
112-
pvpower_models.UtilityCalcs,
113-
pvpower_models.PerformanceCalcs,
114-
pvpower_models.IrradianceCalcs
115+
sandia_performance_model.UtilityCalcs,
116+
sandia_performance_model.PerformanceCalcs,
117+
sandia_performance_model.IrradianceCalcs
115118
]
116119
simulations = [
117-
(pvpower_models.Standalone,
120+
(sandia_performance_model.Standalone,
118121
{"path": "Standalone", "filename": "Tuscon.json"})
119122
]
120123

examples/PVPower/pvpower/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
__email__ = 'mark.mikofski@sunpowercorp.com'
1010

1111
PKG_PATH = os.path.abspath(os.path.dirname(__file__))
12-
PROJ_PATH = os.path.abspath(os.path.join(PKG_PATH, '..'))
12+
PROJ_PATH = os.path.dirname(PKG_PATH)

0 commit comments

Comments
 (0)