Skip to content

Commit

Permalink
Cleaning according to comments on merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Greer authored and Alexander Greer committed Aug 17, 2021
1 parent 45bbda7 commit 11d89eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
13 changes: 8 additions & 5 deletions gpkit/solution_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def savetxt(self, filename="solution.txt", *, printmodel=True, **kwargs):
f.write(self.modelstr + "\n")
f.write(self.table(**kwargs))

def savejson(self, filename="solution.json"):
def savejson(self, filename="solution.json", printjson=False):
"Saves solution table as a json file"
sol_dict = {}
# add appropriate data for each variable to the dictionary
Expand All @@ -603,14 +603,17 @@ def savejson(self, filename="solution.json"):
for i, (k, v) in enumerate(data.items()):
key = str(k)
if isinstance(v, np.ndarray):
val = {'v': v.tolist(), 'u': str(k.unitstr())}
val = {"v": v.tolist(), "u": k.unitstr()}
else:
val = {'v': v, 'u': str(k.unitstr())}
val = {"v": v, "u": k.unitstr()}
sol_dict[key] = val
for key in self.name_collision_varkeys():
del key.descr["necessarylineage"]
with open(filename, "w") as f:
json.dump(sol_dict, f)
if printjson:
return str(sol_dict)
else:
with open(filename, "w") as f:
json.dump(sol_dict, f)

def savecsv(self, filename="solution.csv", *, valcols=5, showvars=None):
"Saves primal solution as a CSV sorted by modelname, like the tables."
Expand Down
5 changes: 1 addition & 4 deletions gpkit/tests/t_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import pickle
import numpy as np
import json
import json

from gpkit import settings, Model, Variable, NamedVariables
from gpkit.tests.helpers import generate_example_tests
Expand Down Expand Up @@ -178,7 +178,6 @@ def test_model_var_access(self, example):

def test_performance_modeling(self, example):
m = Model(example.M.cost, Loose(example.M), example.M.substitutions)
NamedVariables.reset_modelnumbers()
sol = m.solve(verbosity=0)
sol.table()
sol.save("solution.pkl")
Expand All @@ -202,8 +201,6 @@ def test_performance_modeling(self, example):
== sol["variables"][var.key]))
self.assertEqual(json_dict[str(var.key)]['u'], var.unitstr())



def test_sp_to_gp_sweep(self, example):
sol = example.sol
cost = sol["cost"]
Expand Down

0 comments on commit 11d89eb

Please sign in to comment.