Ejemplo n.º 1
0
def test_fire_sim_chokepoint(netlogo_sh, netlogo_args, sess_file,
                             get_beshp_xml_interp2, result_field_map):
    def generate_speed_distro():
        '''Calculate a vector of distribution ranges, which we will then
             take as combinations against slow, medium and fast people.

           VBAR=(s+m+f)/3
           3*33-(s+m)=f
           100-(a[0]+a[1])=f
        '''
        VBAR = 100
        MAX_I = 35
        MAX_X = 61
        j = (i for i in range(4, MAX_I, 4))
        y = (x for x in range(5, MAX_X, 5))

        out0 = []
        for a in itertools.product(j, y):
            b = VBAR - a[0] - a[1]
            assert b > 0
            out0.append([a[0], a[1], b])

        return out0

    START_DATA = 8
    ELIDE_LAST = -1

    args = [netlogo_sh]
    for k, v in netlogo_args.items():
        args.append(f"--{k}")
        args.append(v)

#for p in range(100,501,100):
# for c,g in enumerate(generate_speed_distro()):
#     with open("setup-file.xml", "w") as f:
#         out=get_beshp_xml_interp2 % ('chokepoint.map',p,g[0],g[1],g[2])
#         f.write(out)
#         print(f'{c=}\n{out}\n\n')
    for c, slow in enumerate(range(1, 101, 5)):
        with open("setup-file.xml", "w") as f:
            out = get_beshp_xml_interp2 % ('chokepoint.map', 500, 100 - slow,
                                           slow, 0)
            f.write(out)
            print(f'{c=}\n{out}\n\n')
        result = subprocess.run(args, capture_output=True)
        lines = f"{result.stdout=}".split("\\n")[START_DATA:]

        fnames = result_field_map.values()
        dr = DictReader(lines[:ELIDE_LAST],
                        fieldnames=fnames,
                        restkey="restkey")

        for line in dr:
            if "restkey" in line:
                del line["restkey"]
            fix_bools(line)

            x = ResultsFireSim(**line)
            add_commit(sess_file, x)
            sess_file.add(x)
Ejemplo n.º 2
0
def test_fire_sim_persist_interp(netlogo_sh, netlogo_args, sess_file,
                                 get_beshp_xml_interp, result_field_map,
                                 map_file_list):
    START_DATA = 8
    ELIDE_LAST = -1

    args = [netlogo_sh]
    for k, v in netlogo_args.items():
        args.append(f"--{k}")
        args.append(v)

    for m in get_map_files(map_file_list):
        print(f'working on map {m}')
        with open("setup-file.xml", "w") as f:
            f.write(get_beshp_xml_interp % m)

        result = subprocess.run(args, capture_output=True)
        lines = f"{result.stdout=}".split("\\n")[START_DATA:]

        fnames = result_field_map.values()
        dr = DictReader(lines[:ELIDE_LAST],
                        fieldnames=fnames,
                        restkey="restkey")

        for line in dr:
            if "restkey" in line:
                del line["restkey"]
            fix_bools(line)

            x = ResultsFireSim(**line)
            add_commit(sess_file, x)
            sess_file.add(x)
Ejemplo n.º 3
0
def parsed_nlogo_class_file(string_fields, behsp_xml, sess):
    """The METRIC return from get_experiment_header is the additional outputs we store
    in addition to the UI widget values.
    """
    exps = Experiments(nlogo_file_path=str(filen.parents),
                       nlogo_file_name=filen.name)
    add_commit(sess, exps)
    soup = BeautifulSoup(behsp_xml, "lxml-xml")
    for e in soup.experiments:
        if e.name != None:
            (
                name,
                repetitions,
                runMetricsEveryStep,
                setup,
                go,
                metric,
                timeLimit,
            ) = get_exp_header(e)
            ex = Experiment(
                experiments_id=exps.experiments_id,
                name=name,
                repetitions=repetitions,
                setup=setup,
                go=go,
                metric=metric,
            )
            add_commit(sess, ex)

            result_table_name = f"results_{name}_{ex.experiment_id}"
            result_table_fields = []

            for evs in e.find_all("enumeratedValueSet"):
                vname = evs.attrs["variable"]
                result_table_fields.append(vname)
                enu = EnumeratedValueSet(experiment_id=ex.experiment_id,
                                         variable=vname)
                add_commit(sess, enu)
                for v in evs.find_all("value"):
                    w = Value(
                        enumerated_value_set_id=enu.enumerated_value_set_id,
                        value=v.attrs["value"],
                    )
                    sess.add(w)
                    sess.commit()
            return (
                string_fields,
                result_table_name,
                result_table_fields,
                runMetricsEveryStep,
                metric,
                e.name,
            )
Ejemplo n.º 4
0
def test_load(behsp_xml, sess):
    def get_result_table(result_table_name, result_table_fields):
        """Calculate a SQLAlchemy Class for the desired experiment.
        Return: the calculated name, the class, and a map of how the data fields
          in the NetLogo output map to what the python class's members are named.
        """
        def fix_name(f):
            return f.replace("-", "_").replace("?", "_p")

        attr_dict = {
            "__tablename__": result_table_name,
            f"{result_table_name}_id": Column(Integer, primary_key=True),
        }

        result_field_map = {}
        for f in result_table_fields:
            g = fix_name(f)
            result_field_map[f] = g
            attr_dict[g] = Column(Float)

        result_name = stringcase.pascalcase(
            fix_name(result_table_name).replace("_", ""))
        return result_name, type(result_name, (Base, ),
                                 attr_dict), result_field_map

    def add_commit(sess, obj):
        sess.add(obj)
        sess.commit()

    def get_exp_header(exp):
        name = repetitions = runMetricsEveryStep = setup = go = metric = None
        name = exp.attrs["name"]
        repetitions = exp.repetitions
        runMetricsEveryStep = exp.runMetricsEveryStep
        timeLimit = 0
        for c in exp.children:
            if c.name == "setup":
                setup = c.string
            elif c.name == "go":
                go = c.string
            elif c.name == "metric":
                metric = c.string
            elif c.name == "timeLimit":
                timeLimit = c.attrs["steps"]
        return name, repetitions, runMetricsEveryStep, setup, go, metric, timeLimit

    exps = Experiments(nlogo_file_path=str(filen.parents),
                       nlogo_file_name=filen.name)
    add_commit(sess, exps)
    soup = BeautifulSoup(behsp_xml, "lxml-xml")
    for e in soup.experiments:
        if e.name != None:
            (
                name,
                repetitions,
                runMetricsEveryStep,
                setup,
                go,
                metric,
                timeLimit,
            ) = get_exp_header(e)
            ex = Experiment(
                experiments_id=exps.experiments_id,
                name=name,
                repetitions=repetitions,
                setup=setup,
                go=go,
                metric=metric,
            )
            add_commit(sess, ex)

            result_table_name = f"results_{name}_{ex.experiment_id}"
            result_table_fields = []

            for evs in e.find_all("enumeratedValueSet"):
                vname = evs.attrs["variable"]
                result_table_fields.append(vname)
                enu = EnumeratedValueSet(experiment_id=ex.experiment_id,
                                         variable=vname)
                add_commit(sess, enu)
                for v in evs.find_all("value"):
                    w = Value(
                        enumerated_value_set_id=enu.enumerated_value_set_id,
                        value=v.attrs["value"],
                    )
                    sess.add(w)
                    sess.commit()

            for ex in sess.query(Experiment).filter(
                    Experiment.experiment_id == 1):
                print(ex.xml(sess))
            result_name, result_table, result_field_map = get_result_table(
                result_table_name, result_table_fields)
Ejemplo n.º 5
0
def parsed_nlogo(string_fields, behsp_xml, sess):
    """The METRIC return from get_experiment_header is the additional outputs we store
    in addition to the UI widget values.
    """
    exps = Experiments(nlogo_file_path=str(filen.parents),
                       nlogo_file_name=filen.name)
    add_commit(sess, exps)
    soup = BeautifulSoup(behsp_xml, "lxml-xml")
    for e in soup.experiments:
        if e.name != None:
            (
                name,
                repetitions,
                runMetricsEveryStep,
                setup,
                go,
                metric,
                timeLimit,
            ) = get_exp_header(e)
            ex = Experiment(
                experiments_id=exps.experiments_id,
                name=name,
                repetitions=repetitions,
                setup=setup,
                go=go,
                metric=metric,
            )
            add_commit(sess, ex)

            result_table_name = f"results_{name}_{ex.experiment_id}"
            result_table_fields = []

            for evs in e.find_all("enumeratedValueSet"):
                vname = evs.attrs["variable"]
                result_table_fields.append(vname)
                enu = EnumeratedValueSet(experiment_id=ex.experiment_id,
                                         variable=vname)
                add_commit(sess, enu)
                for v in evs.find_all("value"):
                    w = Value(
                        enumerated_value_set_id=enu.enumerated_value_set_id,
                        value=v.attrs["value"],
                    )
                    sess.add(w)
                    sess.commit()

            # for ex in sess.query(Experiment).filter(Experiment.experiment_id==1):
            #    print(ex.xml(sess))
            # insp = inspect(result_type)
            result_name, result_type, result_field_map = get_result_table(
                string_fields,
                result_table_name,
                result_table_fields,
                runMetricsEveryStep,
                metric,
            )
            sess.metadata.create_all(bind=sess.engine, tables=[result_type])
            sess.commit()
            for t in sess.metadata.sorted_tables:
                print(t.name)
            # result_type_reflected = Table(result_name, sess.metadata, autoload=True, autoload_with=sess.engine)
            return result_name, result_type, result_field_map