Main - Snailz Docs

Skip to content

# Main

Synthesize data.

## `main()`

Main command-line driver.

Source code in `src/snailz/main.py`

    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    def main():
        """Main command-line driver."""

        args = _parse_args()
        if args.defaults:
            print(utils.json_dump(Parameters()))
            return 0

        params = _initialize(args)
        data = _synthesize(params)
        data["changes"] = do_all_effects(params, data)

        data["tidy_grids"] = Grid.tidy(params, data["grids"])
        _save_csv(args, data)
        _save_db(args, data)
        _save_params(args, params)

        return 0

## `_initialize(args)`

Initialize for data synthesis.

Source code in `src/snailz/main.py`

    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    def _initialize(args):
        """Initialize for data synthesis."""

        if args.params:
            with open(args.params, "r") as reader:
                params = Parameters.model_validate(json.load(reader))
        else:
            params = Parameters()

        random.seed(params.seed)

        return params

## `_parse_args()`

Parse command-line arguments.

Source code in `src/snailz/main.py`

    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    def _parse_args():
        """Parse command-line arguments."""

        parser = argparse.ArgumentParser()
        parser.add_argument(
            "--defaults", action="store_true", help="show default parameters"
        )
        parser.add_argument("--outdir", default=None, help="output directory")
        parser.add_argument("--params", default=None, help="JSON parameter file")
        return parser.parse_args()

## `_save_csv(args, data)`

Save synthesized data as CSV.

Source code in `src/snailz/main.py`

    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    def _save_csv(args, data):
        """Save synthesized data as CSV."""

        if not args.outdir:
            return

        elif args.outdir == "-":
            outdir = None

        else:
            outdir = Path(args.outdir)
            if not outdir.is_dir():
                outdir.mkdir(exist_ok=True)

        persist.grids_to_csv(outdir, data["grids"], data["tidy_grids"])
        for name, cls in (
            ("machines", Machine),
            ("persons", Person),
            ("ratings", Rating),
            ("samples", Sample),
        ):
            with utils.file_or_std(outdir, f"{name}.csv", "w") as writer:
                persist.objects_to_csv(writer, data[name])

        with utils.file_or_std(outdir, "changes.json", "w") as writer:
            json.dump(data["changes"], writer)

## `_save_db(args, data)`

Save synthesized data as CSV.

Source code in `src/snailz/main.py`

     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    def _save_db(args, data):
        """Save synthesized data as CSV."""

        if (not args.outdir) or (args.outdir == "-"):
            return

        outdir = Path(args.outdir)
        if not outdir.is_dir():
            outdir.mkdir(exist_ok=True)
        dbpath = outdir / DB_FILE
        dbpath.unlink(missing_ok=True)

        cnx = sqlite3.connect(dbpath)

        for table, name in (
            ("machine", "machines"),
            ("person", "persons"),
            ("rating", "ratings"),
            ("sample", "samples"),
        ):
            persist.objects_to_db(cnx, table, data[name])

        persist.grids_to_db(cnx, data["tidy_grids"])

        cnx.close()

## `_save_params(args, params)`

Save parameters.

Source code in `src/snailz/main.py`

    126
    127
    128
    129
    130
    131
    132
    def _save_params(args, params):
        """Save parameters."""
        if (args.outdir is None) or (args.outdir == "-"):
            return

        with open(Path(args.outdir, "params.json"), "w") as writer:
            writer.write(utils.json_dump(Parameters()))

## `_synthesize(params)`

Synthesize data.

Source code in `src/snailz/main.py`

    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    def _synthesize(params):
        """Synthesize data."""

        grids = Grid.make(params)
        persons = Person.make(params)
        samples = Sample.make(params, grids, persons)
        machines = Machine.make(params)
        ratings = Rating.make(persons, machines)
        return {
            "grids": grids,
            "persons": persons,
            "samples": samples,
            "machines": machines,
            "ratings": ratings,
        }