- ID
- 179e3554-1716-4ab5-ae72-91827d0417f4
Exp 236 v1 Backfill
Backfill quiz responses from exp_236 variant 1 that were never written to the
~customer_tag~ and ~occasion_survey~ tables. Data provided by the data science
team as CSV exports.
Skipping polarizing (disliked) tags — backfill positive preferences only.
CSV files
All five files are in the repo root:
- ~exp_236_v1_flavors_cuisines.csv~ → CustomerTag (preference=1)
- ~exp_236_v1_proteins.csv~ → CustomerTag (preference=1)
- ~exp_236_v1_quiz_food_preferences.csv~ → CustomerTag (preference=1)
- ~exp_236_v1_polarizing.csv~ → SKIPPED (negative preference, not backfilling)
- ~exp_236_v1_meal_frequency.csv~ → OccasionSurvey (breakfast/lunch/dinner/sweets)
Run instructions
From repo root with virtualenv active:
#+begin_src sh
./manage.py shell < /home/powellc/var/org/hungryroot/scripts/exp-236-backfill.py
#+end_src
Or tangle the block below to ~exp-236-backfill.py~, then run it.
Script
#+begin_src python :tangle exp-236-backfill.py
import csv
from collections import defaultdict
from pathlib import Path
from app.models import CustomerTag
from app.models import OccasionSurvey
BASE = Path("/home/powellc/src/github.com/hungryroot/hungryroot")
TAG_CSVS = [
BASE / "exp_236_v1_flavors_cuisines.csv",
BASE / "exp_236_v1_proteins.csv",
BASE / "exp_236_v1_quiz_food_preferences.csv",
# exp_236_v1_polarizing.csv skipped — negative preference, not backfilling
]
FREQ_CSV = BASE / "exp_236_v1_meal_frequency.csv"
CONTAINER_FIELD = {
"breakfast": "breakfast_days_per_week",
"lunch": "lunch_days_per_week",
"dinner": "dinner_days_per_week",
"sweets": "sweets_answer",
}
# --- CustomerTag backfill ---
tag_created = tag_updated = 0
for csv_path in TAG_CSVS:
with open(csv_path) as f:
for row in csv.DictReader(f):
_, created = CustomerTag.objects.update_or_create(
customer_id=int(row["customer_id"]),
tag_id=int(row["tag_id"]),
defaults={"preference": 1, "is_explicit": True},
)
if created:
tag_created += 1
else:
tag_updated += 1
print(f"CustomerTag: {tag_created} created, {tag_updated} updated")
# --- OccasionSurvey backfill ---
by_customer: dict[int, dict] = defaultdict(dict)
with open(FREQ_CSV) as f:
for row in csv.DictReader(f):
container = row["container_name"]
field = CONTAINER_FIELD.get(container)
if field is None:
# snacks has no OccasionSurvey field
continue
freq = row["frequency"]
if freq == "null":
continue
by_customer[int(row["customer_id"])][field] = int(freq)
occ_created = occ_updated = 0
for customer_id, fields in by_customer.items():
_, created = OccasionSurvey.objects.update_or_create(
customer_id=customer_id,
defaults=fields,
)
if created:
occ_created += 1
else:
occ_updated += 1
print(f"OccasionSurvey: {occ_created} created, {occ_updated} updated")
#+end_src