forked from alan-turing-institute/sqlsynthgen
-
Notifications
You must be signed in to change notification settings - Fork 1
install-stories command allows story file to specify its configuration #72
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
Open
tim-band
wants to merge
1
commit into
main
Choose a base branch
from
install-stories
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,7 +151,5 @@ docs/temp/* | |
| /src-stats.yaml | ||
| /config.yaml | ||
| *.yaml.gz | ||
|
|
||
| *_stories.py | ||
|
|
||
| /examples/opiates/* | ||
| ./*_stories.py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| """Functions to install Python file references in ``config.yaml``.""" | ||
| from collections.abc import Mapping, MutableMapping, Sequence | ||
| from inspect import Parameter, signature | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from datafaker.utils import import_file, logger | ||
|
|
||
|
|
||
| def _make_where_from_annotation( | ||
| query_def: Mapping[str, Any], | ||
| fn_name: str, | ||
| param_name: str, | ||
| ) -> str: | ||
| """Make a where clause from ``query`` value from the annotation.""" | ||
| if "where" not in query_def: | ||
| return "" | ||
| w = query_def["where"] | ||
| if isinstance(w, str): | ||
| return f" WHERE {w}" | ||
| if isinstance(w, Sequence): | ||
| return " WHERE " + " AND ".join(f'"({clause})"' for clause in w) | ||
| logger.warning( | ||
| '"where" in the query annotation of parameter "%s" of function "%s"' | ||
| " needs to be a string or a list of strings", | ||
| param_name, | ||
| fn_name, | ||
| ) | ||
| return "" | ||
|
|
||
|
|
||
| def _make_vars_from_annotation( | ||
| query_def: Mapping[str, Any], | ||
| fn_name: str, | ||
| param_name: str, | ||
| ) -> Mapping[str, Any]: | ||
| """Make a variables dict from ``query`` value from the annotation.""" | ||
| if "vars" not in query_def: | ||
| return {} | ||
| vars_def = query_def["vars"] | ||
| if isinstance(vars_def, Mapping): | ||
| return vars_def | ||
| if isinstance(vars_def, Sequence): | ||
| return {v: v for v in query_def["vars"]} | ||
| logger.warning( | ||
| '"vars" in the query annotation of parameter "%s" of function "%s"' | ||
| " needs to be a list of strings or a dict of strings to strings", | ||
| param_name, | ||
| fn_name, | ||
| ) | ||
| return {} | ||
|
|
||
|
|
||
| def _add_count_vars_from_annotation( | ||
| group_vars_out: MutableMapping[str, Any], | ||
| query_def: Mapping[str, Any], | ||
| fn_name: str, | ||
| param_name: str, | ||
| ) -> None: | ||
| """Add ``GROUP BY`` clauses from ``count_vars``.""" | ||
| if "count_vars" not in query_def: | ||
| return | ||
| cntv = query_def["count_vars"] | ||
| if isinstance(cntv, Mapping): | ||
| group_vars_out.update({k: f"COUNT({v})" for k, v in cntv}) | ||
| return | ||
| logger.warning( | ||
| '"count_vars" needs to be a dict in the annotation for parameter %s of function %s', | ||
| param_name, | ||
| fn_name, | ||
| ) | ||
|
|
||
|
|
||
| def _add_ms_vars_from_annotation( | ||
| group_vars_out: MutableMapping[str, Any], | ||
| query_def: Mapping[str, Any], | ||
| fn_name: str, | ||
| param_name: str, | ||
| ) -> None: | ||
| """Add ``GROUP BY`` clauses from ``ms_vars``.""" | ||
| if "ms_vars" not in query_def: | ||
| return | ||
| msv = query_def["ms_vars"] | ||
| if not isinstance(msv, Mapping): | ||
| logger.warning( | ||
| '"ms_vars" needs to be a dict in the annotation for parameter %s of function %s', | ||
| param_name, | ||
| fn_name, | ||
| ) | ||
| return | ||
| for k, v in msv.items(): | ||
| group_vars_out[k + "_count"] = f"COUNT({v})" | ||
| group_vars_out[k + "_mean"] = f"AVG({v})" | ||
| group_vars_out[k + "_stddev"] = f"STDDEV({v})" | ||
|
|
||
|
|
||
| def make_query_from_annotation( | ||
| annotation_data: Any, | ||
| fn_name: str, | ||
| param_name: str, | ||
| ) -> str | None: | ||
| """ | ||
| Make new configuration items describing a query. | ||
|
|
||
| The query's result will be passed as this parameter to this function. | ||
|
|
||
| The annotation must be a dict with the following keys: | ||
|
|
||
| ``comment``: A string describing the query in natural language. | ||
|
|
||
| ``query``: Either a string containing the SQL query required, or | ||
| a dict containing the following keys: | ||
|
|
||
| * ``table``: The table to query. Could be "tablename AS alias" if you like. | ||
| * ``vars`` (optional): Either a list of columns to extract from the table(s), | ||
| or a dict of keys (the names of the keys in the dict to be passed to the | ||
| annotated function) to values (the names of the columns to be extracted). | ||
| At least one of ``vars``, ``ms_vars``, ``count_vars`` must be present. | ||
| * ``where`` (optional): A SQL expression to filter the results. | ||
| * ``count_vars`` (optional): A dict of keys to be passed to the function | ||
| to values that are the names of the columns to be counted (could be | ||
| ``*``; if the name of a column the result will be the number of non-null | ||
| entries in that column). The query will be grouped by ``vars``. | ||
| * ``ms_vars`` (optional): A dict of value names to columns to be analysed. | ||
| The keys to be passed to the function will be name + ``_count`` for the | ||
| number of non-null values in that column, name + ``_mean`` for the | ||
| average value in that column and name + ``_stddev`` for the standard | ||
| deviation of values in that column. | ||
|
|
||
| :param annotation_data: The ``Annotation`` attached to the parameter. | ||
| :param fn_name: The name of the function that the parameter is of. | ||
| :param param_name: The name of the parameter with the annotation. | ||
| :return: A mapping of new configuration items to add to the configuration, | ||
| if the annotation had a well-defined query and comment value; otherwise | ||
| an empty dict. | ||
| """ | ||
| if not isinstance(annotation_data, Sequence): | ||
| return None | ||
| ann = annotation_data[0] | ||
| if not isinstance(ann, Mapping) or "query" not in ann: | ||
| return None | ||
| if isinstance(ann["query"], str): | ||
| return ann["query"] | ||
| query_def = ann["query"] | ||
| if "table" not in query_def: | ||
| logger.warning( | ||
| '"table" needs to be a key in the annotation for' | ||
| ' the "query" value of parameter "%s" of function "%s"', | ||
| param_name, | ||
| fn_name, | ||
| ) | ||
| return None | ||
| table = query_def["table"] | ||
| nongroup_vars = _make_vars_from_annotation(query_def, fn_name, param_name) | ||
| where = _make_where_from_annotation(query_def, fn_name, param_name) | ||
| group_vars: dict[str, Any] = {} | ||
| _add_count_vars_from_annotation(group_vars, query_def, fn_name, param_name) | ||
| _add_ms_vars_from_annotation(group_vars, query_def, fn_name, param_name) | ||
| if group_vars and nongroup_vars: | ||
| group_by = " GROUP BY " + ", ".join(f'"{v}"' for v in nongroup_vars) | ||
| else: | ||
| group_by = "" | ||
| vars_exprs = ", ".join( | ||
| f'{v} AS "{k}"' for k, v in {**nongroup_vars, **group_vars}.items() | ||
| ) | ||
| return f"SELECT {vars_exprs} FROM {table}{group_by}{where}" | ||
|
|
||
|
|
||
| def _add_kwarg( | ||
| kwargs_out: dict[str, Any], fn_name: str, param: Parameter | ||
| ) -> list[dict[str, Any]]: | ||
| """ | ||
| Add a kwargs configuration and return a ``src_stats`` query item. | ||
|
|
||
| :param kwargs_out: The story generator's ``kwargs`` value to be updated. | ||
| :param fn_name: The name of the story generator function. | ||
| :param param: The parameter to specify. | ||
| :return: A list of configuration items to add to the ``src_stats`` config, for | ||
| all the queries this parameter requires. | ||
| """ | ||
| if param.annotation is Parameter.empty: | ||
| return [] | ||
| meta = param.annotation.__metadata__ | ||
| query = make_query_from_annotation( | ||
| param.annotation.__metadata__, fn_name, param.name | ||
| ) | ||
| if query is None: | ||
| return [] | ||
| stat_name = f"story_auto__{fn_name}__{param.name}" | ||
| if "comments" in meta[0]: | ||
| comments = [meta[0]["comment"]] | ||
| else: | ||
| comments = [] | ||
| ssc = { | ||
| "name": stat_name, | ||
| "query": query, | ||
| "comments": comments, | ||
| } | ||
| kwargs_out[param.name] = f'SRC_STATS["{stat_name}"]["results"]' | ||
| return [ssc] | ||
|
|
||
|
|
||
| def install_stories_from(config: MutableMapping[str, Any], story_file: Path) -> bool: | ||
| """ | ||
| Configure datafaker with the stories in a Python file. | ||
|
|
||
| :param config: The contents of the configuration file, to be mutated. | ||
| :param story_file: Path to the Python file containing the story generators. | ||
| :return: True if the config was updated correctly, False if it was untouched | ||
| because problems were encountered. | ||
| """ | ||
| story_generators: list[Mapping[str, Any]] = [] | ||
| src_stats = [ | ||
| s | ||
| for s in config.get("src_stats", []) | ||
| if isinstance(s, Mapping) | ||
| and "name" in s | ||
| and not s["name"].startswith("story_auto__") | ||
| ] | ||
| story_module_name = story_file.stem | ||
| story_module = import_file(story_file, story_module_name) | ||
| for attr_name in dir(story_module): | ||
| attr = getattr(story_module, attr_name) | ||
| if ( | ||
| hasattr(attr, "__module__") | ||
| and attr.__module__ == story_module_name | ||
| and not attr_name.startswith("_") | ||
| and callable(attr) | ||
| ): | ||
| kwargs: dict[str, None] = {} | ||
| sig = signature(attr) | ||
| for param in sig.parameters.values(): | ||
| src_stats += _add_kwarg(kwargs, attr, param) | ||
| story_generators.append( | ||
| { | ||
| "name": f"{story_module_name}.{attr_name}", | ||
| "num_stories_per_pass": 1, | ||
| "kwargs": kwargs, | ||
| } | ||
| ) | ||
| config["story_generators_module"] = story_module_name | ||
| config["story_generators"] = story_generators | ||
| config["src-stats"] = src_stats | ||
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Story generators which describe their own queries and can therefore be installed.""" | ||
| from collections.abc import Iterable | ||
| from typing import Annotated, Any | ||
|
|
||
| def string_story_one_sd( | ||
| stats: Annotated[dict, { | ||
| "query": { | ||
| "ms_vars": {"freq": "frequency"}, | ||
| "table": "string", | ||
| }, | ||
| "comment": "Frequency mean and standard deviation", | ||
| }], | ||
| ) -> Iterable[tuple[str, dict[str, Any]]]: | ||
| man = yield("manufacturer", {"name": "one"}) | ||
| mod = yield ("model", { | ||
| "name": "one_sd", | ||
| "manufacturer_id": man["id"] | ||
| }) | ||
| yield("string", { | ||
| "model_id": mod["id"], | ||
| "position": 0, | ||
| "frequency": stats[0]["freq_mean"] - stats[0]["freq_stddev"], | ||
| }) | ||
| yield("string", { | ||
| "model_id": mod["id"], | ||
| "position": stats[0]["freq_count"], | ||
| "frequency": stats[0]["freq_mean"] + stats[0]["freq_stddev"], | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| tables: | ||
| string: | ||
| num_rows_per_pass: 0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config_filecannot be None because there is a default. It can in principle be a null string but that is an error, it doesn't mean "no configuration file".So this change (and the other similar ones below) are tidying up, not really part of this work.