diff --git a/gpkit/solution_array.py b/gpkit/solution_array.py index 27fdf926..2f17e734 100644 --- a/gpkit/solution_array.py +++ b/gpkit/solution_array.py @@ -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 @@ -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." diff --git a/gpkit/tests/t_examples.py b/gpkit/tests/t_examples.py index ea4a23a1..68952ec2 100644 --- a/gpkit/tests/t_examples.py +++ b/gpkit/tests/t_examples.py @@ -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 @@ -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") @@ -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"]