Skip to content

Commit f2765b9

Browse files
codelionnhuet
andauthored
Replace -inf, +inf, NaN in programs metrics by None before visualizing (#384)
When the visualizer import data from a checkpoint, this is sent to the javascript via a response object decoded with `resp.json()` in `fetchAndRender()` from "main.js". This is crashing if it does not respect fully json specs (and NaN, Infinity are not json valid even though js objects). This is useful for evolutions based on positive metrics to minimize (like a cost). In that case, we want to put -metric in combined_score (which will then be negative). Thus an evolved program not working should be given a worse score during evaluation. An easy way to do it is to put -inf (instead of not outputing any metric, which will be replaced by a 0 by default by the database when requesting a fitness). Doing so works well during evolution (ranking the top programs as expected), but during visualization, it was raising an error when fetching data. Co-authored-by: Nolwen <nolwen.huet@imacs.polytechnique.fr>
1 parent 75505f6 commit f2765b9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

scripts/visualizer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import math
12
import os
23
import json
34
import glob
45
import shutil
56
import logging
67
import re as _re
8+
from numbers import Number
9+
from typing import Optional, Any
710

811
from flask import Flask, render_template, jsonify
912

@@ -63,6 +66,7 @@ def load_evolution_data(checkpoint_folder):
6366
if os.path.exists(prog_path):
6467
with open(prog_path) as pf:
6568
prog = json.load(pf)
69+
sanitize_program_for_visualization(prog)
6670
prog["id"] = pid
6771
prog["island"] = island_idx
6872
nodes.append(prog)
@@ -84,6 +88,18 @@ def load_evolution_data(checkpoint_folder):
8488
"checkpoint_dir": checkpoint_folder,
8589
}
8690

91+
def sanitize_program_for_visualization(program: dict[str, Any]) -> None:
92+
for k, v in program["metrics"].items():
93+
if not check_json_float(v):
94+
program["metrics"][k] = None
95+
if "parent_metrics" in program["metadata"]:
96+
for k, v in program["metadata"]["parent_metrics"].items():
97+
if not check_json_float(v):
98+
program["metadata"]["parent_metrics"][k] = None
99+
100+
def check_json_float(v: Optional[float]) -> bool:
101+
return isinstance(v, Number) and not (math.isinf(v) or math.isnan(v))
102+
87103

88104
@app.route("/")
89105
def index():

0 commit comments

Comments
 (0)