Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create-meta subcommand #192

Merged
merged 6 commits into from
Oct 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion metasyn/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import pathlib
import pickle
import sys
from configparser import ConfigParser

try: # Python < 3.10 (backport)
from importlib_metadata import entry_points, version
except ImportError:
from importlib.metadata import entry_points, version # type: ignore [assignment]

import polars as pl

from metasyn import MetaFrame
from metasyn.validation import create_schema

Expand All @@ -19,7 +22,8 @@
Usage: metasyn [subcommand] [options]

Available subcommands:
synthesize - generate synthetic data from a .json file
create-meta - generate a GMF (.json) file.
synthesize - generate synthetic data from a GMF (.json) file
jsonschema - generate json schema from distribution providers

Program information:
Expand All @@ -46,11 +50,72 @@ def main() -> None:
elif subcommand == "schema":
schema()

elif subcommand == "create-meta":
create_metadata()

else:
print(f"Invalid subcommand ({subcommand}). For help see metasyn --help")
sys.exit(1)


def _parse_config(config_fp):
config = ConfigParser()
config.read(config_fp)
spec = {}
for section in config.sections():
if section.startswith("var."):
new_dict = {}
for key, val in dict(config[section]).items():
try:
new_dict[key] = config.getboolean(section, key)
except ValueError:
pass
try:
new_dict[key] = config.getfloat(section, key)
except ValueError:
pass
try:
new_dict[key] = config.getint(section, key)
except ValueError:
pass
if key not in new_dict:
new_dict[key] = val
spec[section[4:]] = new_dict
return spec


def create_metadata():
"""Program to create and export metadata from a DataFrame to a GMF file (.json)."""
parser = argparse.ArgumentParser(
prog="metasyn create-meta",
description="Create a Generative Metadata Format file from a CSV file.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to explain here that the input CSV has to be a tabular dataset? Not sure if the sentence would be too long then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't a CSV file always a tabular dataset though?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose they often are, you're right. That said, not everyone might be aware of that, in which case it is a lot more user-friendly to ask for an input tabular dataset, despite being redundant (IMO). It doesn't hurt, that's for sure. But if you like it more this way that is cool too.

)
parser.add_argument(
"input",
help="input file; a CSV file that you want to synthesize later.",
type=pathlib.Path,
)
parser.add_argument(
"output",
help="output file: .json",
type=pathlib.Path,
)
parser.add_argument(
"--config",
help="Configuration file to specify distribution behavior.",
type=pathlib.Path,
default=None,
)
args, _ = parser.parse_known_args()
if args.config is not None:
spec = _parse_config(args.config)
else:
spec = {}
data_frame = pl.read_csv(args.input, try_parse_dates=True)
meta_frame = MetaFrame.fit_dataframe(data_frame, spec=spec)
meta_frame.export(args.output)


def synthesize() -> None:
"""Program to generate synthetic data."""
parser = argparse.ArgumentParser(
Expand Down
Loading