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

226
get.py
View File

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