Skip to content

Commit

Permalink
Merge pull request #74 from Simcornelis/master
Browse files Browse the repository at this point in the history
Jupyter Notebook generation
  • Loading branch information
tbarbette authored Aug 29, 2024
2 parents 430612f + 0c86b1a commit b564ccf
Show file tree
Hide file tree
Showing 14 changed files with 864 additions and 186 deletions.
77 changes: 77 additions & 0 deletions npf/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from npf.types.dataset import convert_to_xyeb
from npf.variable import is_numeric, get_numeric
import numpy as np

class Graph:
"""
This is a structure holder for data to build a graph
"""
def __init__(self, grapher:'Grapher'):
self.grapher = grapher
self.subtitle = None
self.data_types = None

def statics(self):
return dict([(var,list(values)[0]) for var,values in self.vars_values.items() if len(values) == 1])

def dyns(self):
return [var for var,values in self.vars_values.items() if len(values) > 1]

#Convert the series into the XYEB format (see types.dataset)
def dataset(self, kind=None):
if not self.data_types:

self.data_types = convert_to_xyeb(
datasets = self.series,
run_list = self.vars_all,
key = self.key,
max_series=self.grapher.config('graph_max_series'),
do_x_sort=self.do_sort,
series_sort=self.grapher.config('graph_series_sort'),
options=self.grapher.options,
statics=self.statics(),
y_group=self.grapher.configdict('graph_y_group'),
color=[get_numeric(v) for v in self.grapher.configlist('graph_color')],
kind=kind
)

return self.data_types

# Divide all series by the first one, making a percentage of difference
@staticmethod
def series_prop(series, prop, exclusions = []):
if len(series) == 1:
raise Exception("Cannot make proportional series with only one serie !")
newseries = []
if not is_numeric(prop):
prop=1
if len(series[0]) < 3:
raise Exception("Malformed serie !")
base_results=series[0][2]
for i, (script, build, all_results) in enumerate(series[1:]):
new_results={}
for run,run_results in all_results.items():
if not run in base_results:
print(run,"FIXME is not in base")
continue

for result_type, results in run_results.items():
if not result_type in base_results[run]:
run_results[result_type] = None
print(result_type, "not in base for %s" % run)
continue
base = base_results[run][result_type]
if len(base) > len(results):
base = base[:len(results)]
elif len(results) > len(base):
results = results[:len(base)]
base = np.array(base)
results = np.array(results)
if result_type not in exclusions:
f = np.nonzero(base)
results = (results[f] / base[f] * float(abs(prop)) + (prop if prop < 0 else 0))
run_results[result_type] = results
new_results[run] = run_results
build._pretty_name = build._pretty_name + " / " + series[0][1]._pretty_name
newseries.append((script, build, new_results))
return newseries
40 changes: 40 additions & 0 deletions npf/graph_choice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from npf import npf

# types of graphs: bar, line, boxplot, simple_bar


def decide_graph_type(config, n_values, vars_values, key, result_type, ndyn, isubplot):
graph_type = "bar"
if ndyn == 0:
graph_type = "boxplot" if n_values == 1 else "simple_bar"
elif ndyn == 1 and n_values > 2 and npf.all_num(vars_values[key]):
graph_type = "line"
graph_types = config("graph_type", [])

if len(graph_types) > 0 and (type(graph_types[0]) is tuple or type(graph_types) is tuple):
if type(graph_types) is tuple:
graph_types = dict([graph_types])
else:
graph_types = dict(graph_types)
if result_type in graph_types:
graph_type = graph_types[result_type]
elif "default" in graph_types:
graph_type = graph_types["default"]
elif "result" in graph_types:
graph_type = graph_types["result"]
else:
graph_type = "line"

else:
if type(graph_types) is str:
graph_types = [graph_types]
graph_types.extend([graph_type, "line"])
graph_type = graph_types[isubplot if isubplot < len(
graph_types) else len(graph_types) - 1]

if ndyn == 0 and graph_type == "line":
print("WARNING: Cannot graph", graph_type,
"as a line without dynamic variables")
graph_type = "simple_bar"

return graph_type
Loading

0 comments on commit b564ccf

Please sign in to comment.