Skip to content

Commit

Permalink
Merge pull request #556 from SALib/problem_spec-output-fix-555
Browse files Browse the repository at this point in the history
Better, and explicit, support for IPython console display
  • Loading branch information
ConnectedSystems committed Mar 21, 2023
2 parents b3468b4 + 81bb019 commit 50e4bde
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/SALib/util/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,45 +594,61 @@ def _add_analyzers(self):
method_name, MethodType(self._method_creator(func, "analyze"), self)
)

def _repr_pretty_(self, p, cycle):
p.text(str(self) if not cycle else "...")

def __str__(self):
rep = ""
if self._samples is not None:
arr_shape = self._samples.shape
if len(arr_shape) == 1:
arr_shape = (arr_shape[0], 1)
nr, nx = arr_shape
print("Samples:")
print(f"\t{nx} parameters:", self["names"])
print(f"\t{nr} evaluations", "\n")
rep += (
"Samples:\n"
f"\t{nx} parameters: {self['names']}\n"
f"\t{nr} evaluations\n"
)
if self._results is not None:
arr_shape = self._results.shape
if len(arr_shape) == 1:
arr_shape = (arr_shape[0], 1)
nr, ny = arr_shape
print("Outputs:")
print(f"\t{ny} outputs:", self["outputs"])
print(f"\t{nr} evaluations", "\n")
rep += (
"Outputs:\n"
f"\t{ny} outputs: {self['outputs']}\n"
f"\t{nr} evaluations\n"
)
if self._analysis is not None:
print("Analysis:")
rep += "Analysis:\n"
an_res = self._analysis

allowed_types = (list, tuple)
if isinstance(an_res, ResultDict):
an_res = an_res.to_df()
if not isinstance(an_res, allowed_types):
print(an_res, "\n")
rep += f"{an_res}\n"
else:
for df in an_res:
print(df, "\n")
rep += f"{df}\n"
elif isinstance(an_res, dict):
for res_name in an_res:
print("{}:".format(res_name))
rep += "{}:\n".format(res_name)

dfs = an_res[res_name].to_df()
if isinstance(dfs, allowed_types):
for df in dfs:
print(df, "\n")
rep += f"{df}:\n"
else:
print(dfs, "\n")
return ""
rep += f"{dfs}:\n"

if len(rep) == 0:
rep = (
"ProblemSpec does not currently contain any samples, "
"evaluations or results."
)

return rep


def _check_spec_attributes(spec: ProblemSpec):
Expand Down

0 comments on commit 50e4bde

Please sign in to comment.