Tutorials (notebooks)¶
A guided tour through tdfs4ds, told on one set of data that the package
generates for you. You do not need a source table of your own: three small
tables are written in your database, and the thirteen modules build a feature
store on them, one idea at a time.
Before you start
Install the package (Installation) and have a Teradata Vantage account that may create tables and views in one database. Every page opens with the same short Before you start block, so any page runs alone in a fresh session.
Pick your industry¶
The data comes in eight flavors with the same three tables and the same shapes; only the
names, the categories and the amounts change. Pick the one you know best, change one word
in the first block of the page (tdfs4ds.tutorial_scenario("retail")), and every page
follows: the feature names are neutral (nb_events, total_amount, nb_partners), so the
outputs read the same everywhere.
| Flavor | Entity | Partner | Events table | Amount | Categories (examples) |
|---|---|---|---|---|---|
retail |
customer (CustomerID) |
merchant (MerchantID) |
transactions: one card transaction |
Transaction_Amount (euros) |
grocery, fuel, electronics |
banking |
bank account (AccountID) |
counterparty (CounterpartyID) |
payments: one payment |
Payment_Amount (euros) |
transfer, card, direct debit |
healthcare |
patient (PatientID) |
care provider (ProviderID) |
claims: one insurance claim |
Claim_Amount (euros) |
consultation, imaging, surgery |
pharma |
pharmacy (PharmacyID) |
medicinal product (ProductID) |
dispensings: one dispensing |
Dispensed_Units (packs) |
oncology, cardiology, diabetes |
manufacturing |
machine (MachineID) |
plant (PlantID) |
sensor_readings: one energy reading |
Energy_kWh (kWh) |
pressing, welding, painting |
assembly |
assembly station (StationID) |
assembly line (LineID) |
steps: one assembly step |
Cycle_Time_s (seconds) |
fastening, sub-assembly, inspection |
energy |
electricity meter (MeterID) |
substation (SubstationID) |
meter_readings: one meter reading |
Consumption_kWh (kWh) |
residential, commercial, industrial |
transportation |
vehicle (VehicleID) |
depot (DepotID) |
trips: one trip |
Distance_km (km) |
bus, tram, rail |
One example feature per flavor, all computed by the same SQL (nb_events, total_amount):
| Flavor | nb_events and total_amount mean... |
|---|---|
retail |
how many card transactions a customer made, and how many euros they spent |
banking |
how many payments an account made, and the total amount paid |
healthcare |
how many claims a patient filed, and the total claimed |
pharma |
how many dispensings a pharmacy made, and the packs dispensed |
manufacturing |
how many energy readings a machine produced, and the kWh consumed |
assembly |
how many steps a station performed, and the seconds spent |
energy |
how many readings a meter sent, and the kWh consumed |
transportation |
how many trips a vehicle made, and the kilometres driven |
Each table holds a year of data (2023): 2,000 entities, 200 partners and 50,000 events. About 15% of the entities stop before mid-year, about 15% start in May or later, each entity prefers one category, and about 1% of the events have no partner (a NULL partner id, the subject of module 9).
An experiment you can remove: a disposable data domain¶
The first block creates the tutorial's data domain with
create_data_domain(..., disposable=True). That records, in the store's settings table, that
this domain is an experiment, with who declared it and when. Nothing else changes: features,
processes and datasets work as in any domain.
What it gives you: at the end, tdfs4ds.remove_data_domain(domain, dry_run=False) removes in
one call everything the feature store holds for the domain. Only a disposable domain can be
removed this way:
- the call is a dry run by default: it prints the plan and writes nothing;
- it removes feature-store objects only: your source tables are never touched (here the
tutorial tables are dropped by
drop_tutorial_data); - what is shared with another domain (a dataset that also reads its features, a process that reads one of its views, a manager another process uses) is kept and listed with the reason; when keeping it would leave it broken, the removal refuses;
- a real domain is never removed in one call: plan it with
tdfs4ds.plan_domain_removal(orplan_removal), read the plan, thentdfs4ds.apply_removal(plan)(Maintain a workflow); - to keep an experiment that turned out useful:
tdfs4ds.set_data_domain_disposable(domain, False).
A domain that already holds processes cannot be made disposable by create_data_domain;
set_data_domain_disposable(domain, True) does it, on purpose.
import dataclasses
import getpass
import os
import tdfs4ds # importing it first also loads a .env file, if you have one
import teradataml as tdml
# 1. Open the Teradata session. Host and user come from the environment (TDFS4DS_TD_HOST,
# TDFS4DS_TD_USERNAME), the password from TDFS4DS_TD_PASSWORD or a prompt: nothing secret
# is written on this page.
host = os.environ["TDFS4DS_TD_HOST"]
user = os.environ["TDFS4DS_TD_USERNAME"]
password = os.environ.get("TDFS4DS_TD_PASSWORD") or getpass.getpass("Teradata password: ")
tdml.create_context(host=host, username=user, password=password)
# 2. Pick your industry. This is the ONLY line to change: retail, banking, healthcare, pharma,
# manufacturing, assembly, energy or transportation. Every name below comes from S.
S = tdfs4ds.tutorial_scenario("retail")
# 3. Connect to the feature store (created when missing) and choose a data domain. disposable=True
# records that this domain is an experiment, so the last page removes it in one call.
DATABASE = os.environ.get("TDFS4DS_TD_DATABASE") or user
tdfs4ds.connect(database=DATABASE, create_if_missing=True)
tdfs4ds.create_data_domain(f"TUTORIAL_{S.name.upper()}", disposable=True) # an experiment: removable in one call
# 4. The tutorial data: three tables written once; a second run keeps them.
try:
S = tdfs4ds.create_tutorial_data(S.name, DATABASE)
except ValueError:
S = dataclasses.replace(S, database=DATABASE)
def register_entity_features():
"""Page 1 in one call: (re)register the entity features and return the process id."""
sql = f"""
SELECT {S.entity}, COUNT(*) AS nb_events, SUM({S.amount}) AS total_amount,
AVG({S.amount}) AS avg_amount, COUNT(DISTINCT {S.partner}) AS nb_partners
FROM {S.table('events')}
GROUP BY 1"""
return tdfs4ds.upload_features(
df=None, sql_query=sql, target_view_name=f"{S.database}.{S.name}_entity_features",
entity_id=[S.entity], feature_names=["nb_events", "total_amount", "avg_amount", "nb_partners"],
replace=True, document=False, optimize_query=False, return_process_id=True)
print(S.describe())
You should see one row per table (entities, partners, events) with its
qualified name, its key column and a one-line description. The same story runs as one
script: python tdfs4ds/examples/tutorial/tutorial.py --scenario banking
(tdfs4ds.examples_path('tutorial') gives the folder).
The journey¶
| # | Module | What you will learn |
|---|---|---|
| 1 | First feature | Turn a SQL query on the events table into registered features |
| 2 | Documentation | Let an LLM write the business dictionary |
| 3 | Query optimization | Auto-rewrite a slow process view |
| 4 | Consumer agent | Chat with your feature store |
| 5 | Vector store backends | Chroma or Teradata: where the chatbot's memory lives |
| 6 | FilterManager | Segmented ingestion, one region at a time |
| 7 | TimeManager | A monthly calendar and a history with roll_out |
| 8 | Partitioning | Primary index and partitioning of feature tables |
| 9 | NULLs in entities | The events without a partner |
| 10 | Catalogs and datasets | The three catalogs and why datasets matter |
| 11 | Logs | The follow-up table and how to read a run |
| 12 | Lineage | Dependency graphs and Sankey diagrams |
| 13 | Migration manifest | Describe the store for another environment |
Modules 1 to 4 are the day-one path: ingest, document, optimize, chat. Module 5 is the architectural choice for production. Modules 6 to 9 are the operational toolbox. Modules 10 to 13 cover governance and lifecycle. Modules 2 to 5 need an instruct model (each says so at the top); the others need only the database.
After the tutorials¶
When you are done, remove what the tutorials created (the last page shows it): the
disposable tutorial domain with remove_data_domain(..., dry_run=False) and the data with
drop_tutorial_data. Then read Concepts for the model
behind it, and Configuration for every setting.