Example #1
0
def parse_tsv(tsv):
    kvs = {}
    for line in tsv.split("\\n"):
        parts = line.split("\\t")
        if len(parts) == 2:
            # These are in the form:
            # ("part1/part2/part3", "some value")
            # The "channel" is special because it is a LIST so that
            # has to be treated separately.

            k, v = parts
            k = k.replace("/", ".")

            # If v can be converted into a float, great, otherwise leave it alone
            try:
                v = float(v)
            except ValueError:
                pass

            try:
                # Remember if "channel" was present before the exception
                had_channel = "channel" in kvs
                utils.block_update(kvs, k, v)
            except ValueError:
                # This is probably because the "channel" key has
                # not been yet been constructed; add "channel" and try again
                n_channels = int(kvs.get("n_channels", 0))
                if not had_channel and n_channels > 0:
                    kvs["channel"] = [dict() for c in range(n_channels)]

                # RETRY
                utils.block_update(kvs, k, v)
    return kvs
Example #2
0
    def _write_runs(self, job_folder, run_descs, props=[]):
        """
        Convert the munch run_descs into folders
        """

        if not job_folder.exists():
            job_folder.mkdir()

        found_run_names = {}

        for i, run in enumerate(run_descs):
            # FIND or OVERRIDE run_name
            run_name = run.get("run_name")

            if run_name in found_run_names:
                raise Exception(
                    f"More than one run with name {run_name} found")
            found_run_names[run_name] = True

            # SETUP _erisyon block
            if "_erisyon" not in run:
                run._erisyon = Munch()
            run._erisyon.run_i = i
            run._erisyon.run_i_of = len(run_descs)
            run._erisyon.run_name = run_name

            # OVERRIDE with props
            for prop in props:
                k, v, t = prop.split("=")
                if t == "bool":
                    v = True if v == "true" else False
                elif t == "int":
                    v = int(v)
                elif t == "float":
                    v = float(v)
                elif t == "int_list":
                    v = [int(i) for i in v.split(",")]
                elif t == "int_dict":
                    v = v.split(",")
                    v = {v[i]: int(v[i + 1]) for i in range(0, len(v), 2)}
                else:
                    raise TypeError(f"Unknown type in prop coversion '{t}")
                utils.block_update(run, k, v)

            # Keep the run_name out
            run.pop("run_name", None)
            folder = job_folder / run_name
            folder.mkdir()
            RunExecutor(folder, tasks=run).save()

            tell(f"Wrote run to {folder}")
Example #3
0
 def it_adds_blocks_that_do_not_exist():
     a = {"sim": {"parameters": {"donotkill": 1}}}
     utils.block_update(a, "sim.parameters.peptides", [1, 2])
     assert a["sim"]["parameters"]["donotkill"] == 1
     assert a["sim"]["parameters"]["peptides"][0] == 1
     assert a["sim"]["parameters"]["peptides"][1] == 2
Example #4
0
 def it_adds_blocks_overwriting_scalar():
     a = {"a": [{"b": 1}, {"c": 2}]}
     utils.block_update(a, "a.0.b.d.f", 10)
     assert a["a"][0]["b"]["d"]["f"] == 10
Example #5
0
 def it_overwrites_existing_dict_block():
     a = {"a": [{"b": 1}, {"c": 2}]}
     utils.block_update(a, "a", 3)
     assert a["a"] == 3
Example #6
0
 def it_overwrites_existing_scalar():
     a = {"a": [{"b": 1}, {"c": 2}]}
     utils.block_update(a, "a.1.c", 3)
     assert a["a"][1]["c"] == 3