Modularize the source code (functions only)

This commit is contained in:
Jean-Christophe Dubacq 2024-10-27 11:11:32 +01:00
parent 474d2dffbd
commit b33370daaf

206
get.py
View File

@ -583,6 +583,10 @@ def analyse_depts():
studentsummary["bac"] = etud["admission"]["bac"]
else:
studentsummary["bac"] = "INCONNU"
if "civilite" in etud:
studentsummary["civilite"] = etud["civilite"]
else:
studentsummary["civilite"] = "?"
bacs.add(studentsummary["bac"])
# We skip non-techno students if we are in techno mode
# If we want a mixed reporting, maybe we should change this
@ -1015,12 +1019,12 @@ for etudid in student.keys():
if "old" in etud:
yearold = cache["sem"][etud["oldsem"]]["annee_scolaire"]
etud["nickshort"][firstyear - 1] = etud["old"]
yy = yearold
delta = firstyear + Options.base_year - yy - 2
for i in range(delta, firstyear - 1):
etud["nickshort"][i] = etud["nickshort"][firstyear - 1] + "*" * (
firstyear - 1 - i
)
# yy = yearold
# delta = firstyear + Options.base_year - yy - 2
# for i in range(delta, firstyear - 1):
# etud["nickshort"][i] = etud["nickshort"][firstyear - 1] + "*" * (
# firstyear - 1 - i
# )
else:
if (
str(firstyear * 2 - 2) in etud["cursus"]
@ -1042,15 +1046,33 @@ for etudid in student.keys():
)
displaynames[nick] = displayname
etud["nickshort"][firstyear - 1] = nick
for i in range(0, firstyear - 1):
etud["nickshort"][i] = nick + "*" * (firstyear - 1 - i)
# to get a better ordering in sankeymatic, false nodes ere required
# This is no more the case when building our own graphics
# for i in range(0, firstyear - 1):
# etud["nickshort"][i] = nick + "*" * (firstyear - 1 - i)
entries[ddd] += 1
bags = [{}, {}, {}, {}]
for etudid in student.keys():
class Filter:
TECHNO = 1
WOMAN = 2
def bags_from_students(student, filter=0):
bags = []
for etudid in student.keys():
# Filter
# 0 No filter
# 1 Keep only technological baccalaureate
# 2 Keep only women
# ... to be completed
if filter & Filter.TECHNO:
if student[etudid]["bac"][:2] != "ST":
continue
parc = student[etudid]["nickshort"]
previouslevels = []
for i in range(4):
for i in range(len(parc) - 1):
while len(bags) <= i:
bags.append({})
nstart = parc[i]
nend = parc[i + 1]
if nstart != None and nend != None:
@ -1060,6 +1082,7 @@ for etudid in student.keys():
bags[i][nstart][nend] = 1
else:
bags[i][nstart][nend] += 1
return bags
def node_color(x):
@ -1190,8 +1213,6 @@ def genetic_optimize(node_position, node_layer, edges, loops=1):
b = lays[i].copy()
b.sort(key=lambda x: best[x])
orders.append(b)
debug(orders)
debug(candidates[0][1])
return orders
@ -1221,65 +1242,7 @@ def ordernodes(layers, orders, edges):
return node_position, node_layer, newls
def printsvg():
padding = 4
unit_ratio = 96 / 72
thickness = conf_value("thickness")
fontsize_name = conf_value("fontsize_name")
fontsize_count = conf_value("fontsize_count")
spacing = conf_value("spacing")
height = conf_value("height")
hmargin = conf_value("hmargin")
width = conf_value("width")
node_structure = {}
layers = [[], [], [], [], []]
edges = []
for layer, layernodes in enumerate(bags):
for startnode in layernodes:
if startnode[-1] == "*":
continue
for endnode in layernodes[startnode]:
if endnode[-1] == "*":
continue
weight = layernodes[startnode][endnode]
if endnode not in node_structure:
node_structure[endnode] = {
"prev": [[startnode, weight]],
"next": [],
"layer": layer + 1,
}
layers[layer + 1].append(endnode)
else:
node_structure[endnode]["prev"].append([startnode, weight])
if startnode not in node_structure:
node_structure[startnode] = {
"prev": [],
"next": [[endnode, weight]],
"layer": layer,
}
layers[layer].append(startnode)
else:
node_structure[startnode]["next"].append([endnode, weight])
edges.append([startnode, endnode, weight])
filename = "best-" + orderkey
if Options.restart:
try:
os.remove(filename)
except OSError:
pass
if Options.optimize >= 0:
lastorders = read_conf(filename)
else:
lastorders = {}
node_position, node_layer, newls = ordernodes(layers, lastorders, edges)
if Options.optimize != 0:
orders = genetic_optimize(
node_position, node_layer, edges, loops=abs(Options.optimize)
)
else:
orders = newls
write_conf("best-" + orderkey, orders)
node_position, node_layer, newls = ordernodes(layers, orders, edges)
def get_layer_structure(newls, node_structure, spacing, hmargin, height):
layer_structure = []
density = []
for i in range(5):
@ -1305,13 +1268,16 @@ def printsvg():
ls["inout"] += k["size"]
layer_structure.append(ls)
if height == 0:
debug("coucou")
minheight = 0
for i in range(5):
ls = layer_structure[i]
new_minheight = spacing * (ls["num"] - 1) + 2 * hmargin + 2 * ls["inout"]
debug(f"{new_minheight} / {minheight}")
if new_minheight > minheight:
minheight = new_minheight
height = (1 + (minheight // 150)) * 150
debug(f"{height}")
for i in range(5):
ls = layer_structure[i]
@ -1320,11 +1286,6 @@ def printsvg():
)
density.append(ls["density"])
realdensity = max(density)
columns = []
l = 0
for i in range(5):
l += width / 6
columns.append(l)
for i in range(5):
ls = layer_structure[i]
supp_spacing = (
@ -1340,11 +1301,59 @@ def printsvg():
ns["top"] = supp_spacing + spacing + cs
h = ns["size"] / realdensity
cs = ns["bottom"] = ns["top"] + h
return realdensity, height, layer_structure
def nodestructure_from_bags(bags, sbags=None):
node_structure = {}
layers = [[], [], [], [], []]
edges = []
for layer, layernodes in enumerate(bags):
for startnode in layernodes:
# if startnode[-1] == "*":
# continue
for endnode in layernodes[startnode]:
# if endnode[-1] == "*":
# continue
weight = layernodes[startnode][endnode]
if endnode not in node_structure:
node_structure[endnode] = {
"prev": [[startnode, weight]],
"next": [],
"layer": layer + 1,
}
layers[layer + 1].append(endnode)
else:
node_structure[endnode]["prev"].append([startnode, weight])
if startnode not in node_structure:
node_structure[startnode] = {
"prev": [],
"next": [[endnode, weight]],
"layer": layer,
}
layers[layer].append(startnode)
else:
node_structure[startnode]["next"].append([endnode, weight])
edges.append([startnode, endnode, weight])
return node_structure, layers, edges
def compute_svg(height, padding, realdensity, node_structure):
unit_ratio = 96 / 72
thickness = conf_value("thickness")
fontsize_name = conf_value("fontsize_name")
fontsize_count = conf_value("fontsize_count")
width = conf_value("width")
columns = []
l = 0
for i in range(5):
l += width / 6
columns.append(l)
d = drawsvg.Drawing(width, height, origin=(0, 0), id_prefix=orderkey)
g1 = drawsvg.Group()
g2 = drawsvg.Group()
g3 = drawsvg.Group()
g4 = drawsvg.Group()
g1 = drawsvg.Group() # rectangles representing the student-sets
g2 = drawsvg.Group() # curves representing the transitions
g3 = drawsvg.Group() # texts (captions of each rectangle)
g4 = drawsvg.Group() # white rectangles below texts (for readability)
font_offset = max(fontsize_count, fontsize_name)
for n in node_structure:
ns = node_structure[n]
@ -1456,6 +1465,39 @@ def printsvg():
d.append(g1)
d.append(g4)
d.append(g3)
return d
def printsvg():
padding = 4
spacing = conf_value("spacing")
height = conf_value("height")
hmargin = conf_value("hmargin")
bags = bags_from_students(student, 0)
node_structure, layers, edges = nodestructure_from_bags(bags)
filename = "best-" + orderkey
if Options.restart:
try:
os.remove(filename)
except OSError:
pass
if Options.optimize >= 0:
lastorders = read_conf(filename)
else:
lastorders = {}
node_position, node_layer, newls = ordernodes(layers, lastorders, edges)
if Options.optimize != 0:
orders = genetic_optimize(
node_position, node_layer, edges, loops=abs(Options.optimize)
)
else:
orders = newls
write_conf("best-" + orderkey, orders)
node_position, node_layer, newls = ordernodes(layers, orders, edges)
realdensity, height, layer_structure = get_layer_structure(
newls, node_structure, spacing, hmargin, height
)
d = compute_svg(height, padding, realdensity, node_structure)
d.save_svg(orderkey + ".svg")