From b33370daafb2b0497ba96c77dcb1b1e3301f3736 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Dubacq Date: Sun, 27 Oct 2024 11:11:32 +0100 Subject: [PATCH] Modularize the source code (functions only) --- get.py | 226 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 134 insertions(+), 92 deletions(-) diff --git a/get.py b/get.py index 08f3e50..283cd4e 100755 --- a/get.py +++ b/get.py @@ -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,24 +1046,43 @@ 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(): - parc = student[etudid]["nickshort"] - previouslevels = [] - for i in range(4): - 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 + +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"] + 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): @@ -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")