Skip to content

Commit

Permalink
Update builder_loader to supprt new JSON export format.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgpinkus committed Dec 22, 2024
1 parent adc62f0 commit 3392cb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
40 changes: 30 additions & 10 deletions device_kit/loaders/builder_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def load_data(data):

def load_load_device(d, basis: int):
device_id = d['title'] if 'title' in d else d['type']
bounds = np.array(d['bounds']).reshape((basis, 2))
bounds = run_to_array(d['bounds'])
cbounds = load_cbounds(d)
params = {}
cost_function = load_cost_function(d, bounds, cbounds, basis)
Expand All @@ -55,7 +55,7 @@ def load_load_device(d, basis: int):
def load_fixed_load_device(d, basis: int):
device_id = d['title'] if 'title' in d else d['type']
#, id={device_id}')
bounds = np.array(d['bounds']).reshape((basis, 2))
bounds = run_to_array(d['bounds'])
if (bounds[:,0] != bounds[:,1]).all():
raise Exception('Invalid fixed load device')
return device_kit.ADevice(device_id, basis, bounds)
Expand All @@ -75,7 +75,7 @@ def load_storage_device(d, basis: int):
# disChargeRateClippingFactor?: number,
}
device_id = d['title'] if 'title' in d else d['type']
bounds = np.array(d['bounds']).reshape((basis, 2))
bounds = run_to_array(d['bounds'])
params = { parameter_map[k]: v for k, v in d['parameters'].items() }
rate_clip = (None, None)
if 'disChargeRateClippingFactor' in d['parameters']:
Expand All @@ -88,7 +88,7 @@ def load_storage_device(d, basis: int):

def load_supply_device(d, basis: int):
device_id = d['title'] if 'title' in d else d['type']
bounds = -1*np.array(d['bounds']).reshape((basis, 2))
bounds = -1*run_to_array(d['bounds'])
bounds = np.array([bounds[:,1], bounds[:,0]])
cbounds = load_cbounds(d)
params = {}
Expand All @@ -99,25 +99,24 @@ def load_supply_device(d, basis: int):


def load_cbounds(d):
cbounds = [c[0:3] + [c[3]+1] for c in [flatten(c) for c in d['cumulative_bounds']]] if 'cumulative_bounds' in d else None
if cbounds:
logger.info(f'\tfound cbounds: {cbounds}')
return cbounds
if 'cumulative_bounds' in d:
logger.info(f'\tfound cbounds: {d['cumulative_bounds']}')
return run_to_cbounds_array(d['cumulative_bounds'])


def load_cost_function(d, bounds, cbounds, basis):
costs_data = d['costs']
costs = []
if 'flow' in costs_data:
logger.info('\tfound flow cost for %s' % (d['type'],))
coeffs = _reshape_offset_quad_coeffs(costs_data['flow'])
coeffs = _reshape_offset_quad_coeffs(run_to_array(costs_data['flow']))
costs += [Poly2DOffset(coeffs)]
if 'cumulative_flow' in costs_data:
logger.info('\tfound cumulative_flow for %s' % (d['type'],))
raise Exception('Not implemented')
if 'flow_bounds_relative' in costs_data:
logger.info('\tfound flow_bounds_relative cost for %s' % (d['type'],))
_functions = [HLQuadraticCost(v[0], v[1], bounds[i, 0], bounds[i, 1]) for i, v in enumerate(costs_data['flow_bounds_relative'])]
_functions = [HLQuadraticCost(v[0], v[1], bounds[i, 0], bounds[i, 1]) for i, v in enumerate(run_to_array(costs_data['flow_bounds_relative']))]
costs += [X2D(_functions)]
if 'cumulative_flow_bounds_relative' in costs_data:
logger.info('\tfound cumulative_flow_bounds_relative cost for %s: %s', (d['type'], costs_data['cumulative_flow_bounds_relative']))
Expand All @@ -135,5 +134,26 @@ def _reshape_offset_quad_coeffs(x):
return np.concatenate((x[:, 0:2], np.zeros((basis, 1)), np.array(x[:, 2]).reshape((basis, 1))), axis=1)


def run_to_array(run):
item_template = run['runs']['0']
shape = (run['basis'], len(item_template)) if hasattr(item_template, '__len__') else (run['basis'],)
_array = np.zeros(shape)
points = list(run['runs'].keys())
for i,v in enumerate(points):
e = int(points[i+1]) if i < len(points) - 1 else run['basis']
_array[int(v):e] = run['runs'][v]
return _array


def run_to_cbounds_array(run):
_array = []
points = list(run['runs'].keys())
for i,v in enumerate(points):
[l, h] = run['runs'][v]
e = int(points[i+1]) if i < len(points) - 1 else run['basis']
_array.append([l,h,int(v),e])
return _array


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "device_kit"
version = "1.0.6"
version = "1.0.7"
authors = [
{ name="Sam Pinkus", email="[email protected]" },
]
Expand Down

0 comments on commit 3392cb1

Please sign in to comment.