#!/usr/bin/env python3
"""Generate a Feast feature repository (definitions + feature_store.yaml) from a feature spec.
Input: the JSON (or YAML with PyYAML) spec described in references/spec-format.md: entities,
sources (file | bigquery | snowflake | redshift), feature views (entities, source, ttl, online,
features with dtypes) and feature services. Output files:
features.py Entity / Source / FeatureView / FeatureService definitions (Feast >= 0.30 API)
feature_store.yaml a local provider config (SQLite online store, file offline store) to start from
Run `python lint_feature_spec.py spec.json` first; this generator refuses specs with lint errors
unless --force.
Usage:
python gen_feast_repo.py --template > spec.json
python gen_feast_repo.py spec.json --out feature_repo/
python gen_feast_repo.py spec.json --stdout # print features.py only
Exit codes: 0 generated, 1 spec has lint errors (without --force), 2 usage or read error.
"""
import argparse
import json
import os
import re
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from lint_feature_spec import lint, load, seconds # noqa: E402
TEMPLATE = {
"project": "churn_features",
"entities": [{"name": "customer", "join_keys": ["customer_id"], "description": "A subscriber account"}],
"sources": [
{"name": "customer_daily_activity", "type": "file", "path": "data/customer_daily_activity.parquet",
"timestamp_field": "event_ts", "created_timestamp_field": "created_ts", "cadence": "daily"},
{"name": "customer_profile", "type": "file", "path": "data/customer_profile.parquet",
"timestamp_field": "updated_ts", "cadence": "daily"},
],
"feature_views": [
{"name": "customer_activity", "entities": ["customer"], "source": "customer_daily_activity", "ttl": "2d",
"online": True, "freshness_sla": "1d", "owner": "retention-ds@example.com",
"description": "Rolling engagement aggregates, recomputed nightly",
"features": [
{"name": "logins_7d", "dtype": "int64", "description": "Logins in the 7 days before event_ts"},
{"name": "logins_30d", "dtype": "int64", "description": "Logins in the 30 days before event_ts"},
{"name": "spend_30d", "dtype": "float64", "description": "Spend in the 30 days before event_ts"},
{"name": "days_since_last_login", "dtype": "float32", "description": "Days from last login to event_ts"},
]},
{"name": "customer_profile", "entities": ["customer"], "source": "customer_profile", "ttl": "400d",
"online": True, "freshness_sla": "1d", "owner": "retention-ds@example.com",
"description": "Slowly changing account attributes",
"features": [
{"name": "plan", "dtype": "string", "description": "Current plan as of updated_ts"},
{"name": "tenure_days", "dtype": "int64", "description": "Days since signup as of updated_ts"},
]},
],
"feature_services": [{"name": "churn_model_v1", "description": "Features for the weekly churn model",
"features": ["customer_activity:*", "customer_profile:plan", "customer_profile:tenure_days"]}],
}
FEAST_TYPES = {"int32": "Int32", "int64": "Int64", "float32": "Float32", "float64": "Float64", "string": "String",
"bool": "Bool", "bytes": "Bytes", "unix_timestamp": "UnixTimestamp", "array<int64>": "Array(Int64)",
"array<float32>": "Array(Float32)", "array<float64>": "Array(Float64)", "array<string>": "Array(String)"}
def ident(name):
return re.sub(r"\W", "_", name)
def timedelta_expr(v):
s = seconds(v)
if s is None:
return "timedelta(0)"
if s % 86400 == 0:
return f"timedelta(days={int(s // 86400)})"
if s % 3600 == 0:
return f"timedelta(hours={int(s // 3600)})"
return f"timedelta(seconds={int(s)})"
def source_code(s):
var = f"{ident(s['name'])}_source"
common = f'name="{s["name"]}", timestamp_field="{s["timestamp_field"]}"'
if s.get("created_timestamp_field"):
common += f', created_timestamp_column="{s["created_timestamp_field"]}"'
kind = s.get("type", "file")
if kind == "bigquery":
return var, "BigQuerySource", f'{var} = BigQuerySource({common}, table="{s["table"]}")'
if kind == "snowflake":
db, schema, table = s["table"].split(".")
return var, "SnowflakeSource", (f'{var} = SnowflakeSource({common}, database="{db}", schema="{schema}", '
f'table="{table}")')
if kind == "redshift":
schema, table = s["table"].split(".")
return var, "RedshiftSource", f'{var} = RedshiftSource({common}, schema="{schema}", table="{table}")'
return var, "FileSource", f'{var} = FileSource({common}, path="{s["path"]}")'
def generate(spec):
imports = {"Entity", "FeatureView", "Field", "FeatureService"}
types = set()
body = []
for e in spec["entities"]:
keys = ", ".join(f'"{k}"' for k in e["join_keys"])
desc = json.dumps(e.get("description", ""))
body.append(f'{ident(e["name"])} = Entity(name="{e["name"]}", join_keys=[{keys}], description={desc})')
body.append("")
src_vars = {}
for s in spec["sources"]:
var, cls, code = source_code(s)
imports.add(cls)
src_vars[s["name"]] = var
body.append(code)
body.append("")
view_vars = {}
for v in spec["feature_views"]:
var = f"{ident(v['name'])}_fv"
view_vars[v["name"]] = var
fields = []
for f in v["features"]:
t = FEAST_TYPES[f["dtype"].lower()]
types.update(re.findall(r"[A-Z]\w+", t))
fields.append(f' Field(name="{f["name"]}", dtype={t}, description={json.dumps(f.get("description", ""))}),')
tags = {"owner": v.get("owner", ""), "freshness_sla": str(v.get("freshness_sla", ""))}
body += [f"{var} = FeatureView(",
f' name="{v["name"]}",',
f' entities=[{", ".join(ident(e) for e in v["entities"])}],',
f" ttl={timedelta_expr(v.get('ttl'))},",
" schema=[", *fields, " ],",
f" online={bool(v.get('online', True))},",
f" source={src_vars[v['source']]},",
f" description={json.dumps(v.get('description', ''))},",
f" tags={json.dumps(tags)},",
")", ""]
for svc in spec.get("feature_services", []):
parts = {}
for ref in svc["features"]:
view, _, feat = ref.partition(":")
parts.setdefault(view, [])
if feat and feat != "*":
parts[view].append(feat)
else:
parts[view] = None if parts[view] == [] else parts[view]
refs = []
for view, feats in parts.items():
refs.append(view_vars[view] if not feats else f"{view_vars[view]}[{json.dumps(feats)}]")
body.append(f'{ident(svc["name"])} = FeatureService(name="{svc["name"]}", features=[{", ".join(refs)}], '
f'description={json.dumps(svc.get("description", ""))})')
head = [f'"""Feast definitions for project {spec.get("project", "features")!r}. Generated by gen_feast_repo.py."""',
"from datetime import timedelta", "",
f"from feast import {', '.join(sorted(imports))}",
f"from feast.types import {', '.join(sorted(types))}", "", ""]
return "\n".join(head + body).rstrip() + "\n"
def store_yaml(spec):
return (f"project: {spec.get('project', 'features')}\nregistry: data/registry.db\nprovider: local\n"
"online_store:\n type: sqlite\n path: data/online_store.db\noffline_store:\n type: file\n"
"entity_key_serialization_version: 3\n")
def main():
ap = argparse.ArgumentParser(description="Generate a Feast feature repo from a spec.", epilog=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("spec", nargs="?")
ap.add_argument("--out", help="directory to write features.py and feature_store.yaml")
ap.add_argument("--stdout", action="store_true")
ap.add_argument("--template", action="store_true")
ap.add_argument("--force", action="store_true", help="generate even with lint errors")
a = ap.parse_args()
if a.template:
print(json.dumps(TEMPLATE, indent=2))
return 0
if not a.spec or not (a.out or a.stdout):
print("error: give a spec and --out DIR or --stdout (or --template)", file=sys.stderr)
return 2
try:
spec = load(a.spec)
except (OSError, ValueError) as e:
print(f"error: {e}", file=sys.stderr)
return 2
errors = [f for f in lint(spec) if f["severity"] == "error"]
if errors and not a.force:
for f in errors:
print(f"{f['code']} {f['where']}: {f['message']}", file=sys.stderr)
print(f"{len(errors)} lint error(s); fix them or pass --force", file=sys.stderr)
return 1
code = generate(spec)
if a.stdout:
sys.stdout.write(code)
return 0
os.makedirs(a.out, exist_ok=True)
with open(os.path.join(a.out, "features.py"), "w", encoding="utf-8") as f:
f.write(code)
with open(os.path.join(a.out, "feature_store.yaml"), "w", encoding="utf-8") as f:
f.write(store_yaml(spec))
print(f"wrote {a.out}/features.py and {a.out}/feature_store.yaml ({len(spec['feature_views'])} views)")
return 0
if __name__ == "__main__":
sys.exit(main())
anomaly-detection-model-builder
Build and evaluate anomaly detection for tabular, sensor and transaction data with few or no labels: define the anomaly type (point, contextual, collective), choose detectors (robust z-score/MAD and IQR rules, Mahalanobis with robust covariance, k-NN distance, LOF, Isolation Forest, one-class SVM, PCA reconstruction / autoencoders, forecasting residuals for time series), combine them by rank, explain each alert by its deviating features, set thresholds from an alert budget or the score distribution, and evaluate with precision@k, PR-AUC and recall on known incidents, or detector agreement when no labels exist. Includes pure-Python detectors (robust z, Mahalanobis, kNN, Isolation Forest, ensemble) and an evaluation script.
catboost-model-builder
Build CatBoost models that use its strengths correctly: raw categorical columns as cat_features (ordered target statistics, no manual encoding), text_features, Pool construction with NaN-safe categories, ordered boosting and has_time for temporal data, symmetric (oblivious) trees, overfitting detector with eval_set and use_best_model, losses for classification, regression (Quantile, Poisson, Tweedie) and ranking (YetiRank), class weights, monotone constraints, feature importance (PredictionValuesChange, LossFunctionChange) and SHAP values, GPU training and model export. Plans cat/text/numeric columns from a CSV and generates a runnable training script, and checks parameter sets for the conflicts CatBoost rejects (iterations vs n_estimators, Bayesian bootstrap with subsample, multiple class-weight options, depth > 16, use_best_model without eval_set).
categorical-encoding-selector
Choose and implement the right encoding for every categorical column given the model family and the data: one-hot (with infrequent-level grouping and handle_unknown), ordinal with an explicit order, cross-fitted target encoding (scikit-learn TargetEncoder, CatBoost ordered statistics, category_encoders inside CV), frequency/count encoding, feature hashing, native categorical support in LightGBM, HistGradientBoosting, XGBoost and CatBoost, prefix levels for codes (zip3, NAICS-2), and embeddings. Measures cardinality, rare-level share, unseen-level rate on a time-ordered holdout and target signal per column, emits the ColumnTransformer, and quantifies target-encoding leakage (in-sample vs out-of-fold vs holdout AUC).
classification-model-selector
Choose the right classification algorithm for tabular, text or mixed data and prove the choice with a fair bake-off: logistic regression (L2, L1, elastic net), linear SVM, naive Bayes, k-NN, RBF SVM, decision trees, random forest, extra trees, HistGradientBoosting, LightGBM, XGBoost, CatBoost, explainable boosting machines (EBM/GAM), points scorecards and MLPs, matched to data size, width, categorical and high-cardinality share, missingness, class imbalance, text, and constraints such as interpretability, monotonic constraints, calibrated probabilities, latency budgets and tiny deployable models. Ranks candidates with starting hyperparameters and cautions, then generates a bake-off script running every candidate on identical CV folds with per-fold metrics, fit time and prediction latency.
clustering-algorithm-selector
Choose the right clustering algorithm and check that clustering makes sense at all: cluster tendency (Hopkins statistic with a correlated-features caveat, nearest-neighbour isolation), then k-means / MiniBatchKMeans, Gaussian mixtures with BIC, agglomerative (Ward, average, complete, single) with dendrograms, DBSCAN, HDBSCAN, OPTICS, spectral clustering, BIRCH, mean shift, k-prototypes and k-modes for mixed or categorical data, and Gower distance, matched to data size, expected shapes, noise, varying density, known k, soft memberships, hierarchy and streaming. Covers preprocessing (scaling, encoding, dimensionality, distance metrics), validation (internal indices, bootstrap stability, external labels, business profiling) and assigning new points.
collaborative-filtering-implementer
Implement collaborative filtering recommenders that beat popularity: item-based kNN with cosine, Jaccard or asymmetric similarity, shrinkage, neighbourhood size, positive-only or mean-centred rating signals and 'because you liked X' explanations; implicit ALS with confidence weighting, BPR pairwise ranking and the closed-form EASE model in plain NumPy, tuned on a temporal validation split by recall@k and NDCG@k and refit on all data; plus user-kNN, explicit matrix factorisation, the implicit/LightFM libraries, hyperparameter ranges, cold-start fallbacks, scaling with sparse matrices and approximate nearest neighbours. Produces top-k lists in the format of the recommendation evaluator.