Fig. 1 in words
One batch, two tables, one sealed file · same data as code 05
- Batch · 0.91 · 'n/a' · 1.7
- No checks → ① Loose · 2 real · 1 text
- Type REAL → Check 0-1 → ② Strict · 1 real · 0.91 only
- Rejected · 2 · type · range
- Store: g.tmp · flush · fsync → os.replace · atomic ③ → g.csv · SHA-256 · 43fff085… (a check mark)
- ④ One edit: 0.91 → 0.19 · digest now 339535b6… · mismatch · refuse to read
01 Rules You Can Run Schema
- Write the schema: type, range, required, unique, per column
- Check every new batch against it before training
- Compare training data with what the model sees in use
Breck et al. (2019): Google's data validation checks several petabytes of production data per day.
02 Let the Table Say No Types
- SQLite: "Any column can still store any type of data"
- STRICT tables (3.37.0, 2021) raise an error on a wrong type
- A CHECK constraint guards the range
SQLite calls flexible typing "a feature, not a bug". Strictness is something I have to ask for.
03 Write Once, Whole Storage
- Temp file, flush, fsync, then os.replace: atomic on POSIX (Python docs)
- Never overwrite raw data. Keep dated, read-only snapshots
- FAIR: findable, accessible, interoperable, reusable
Wilkinson et al. (2016). os.replace may fail across two filesystems.
04 Seal With a Hash Integrity
- SHA-256 digests "detect whether messages have been changed" (FIPS 180-4)
- Store the digest beside the file. Recheck on every read
- Code 05: 0.91 → 0.19 turns 43fff085… into 339535b6…
A hash shows that a file changed. It cannot say which value is right.
05 Try It Yourself
sprint21_data_validation_and_storage.py
import hashlib, os, sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE loose(score REAL)")
db.execute("CREATE TABLE tight(score REAL"
" CHECK(score BETWEEN 0 AND 1)) STRICT")
for v in (0.91, "n/a", 1.7):
for t in ("loose", "tight"):
try:
db.execute(f"INSERT INTO {t}"
" VALUES (?)", (v,))
except sqlite3.IntegrityError as e:
print(t, v, "->", str(e)[:23])
print(db.execute("SELECT typeof(score),"
" COUNT(*) FROM loose GROUP BY 1")
.fetchall())
data = b"id,gene,score\n1,TP53,0.91\n"
with open("g.tmp", "wb") as f:
f.write(data); f.flush()
os.fsync(f.fileno())
os.replace("g.tmp", "g.csv") # atomic
sha = lambda b: hashlib.sha256(b).hexdigest()
print(sha(open("g.csv", "rb").read())[:16])
print(sha(data.replace(b"91", b"19"))[:16])
Output, as printed on the image
tight n/a -> cannot store TEXT value
tight 1.7 -> CHECK constraint failed
[('real', 2), ('text', 1)]
43fff0852dd0b5f8
339535b668796221
Run it. sprint21_data_validation_and_storage.py · versions, data and steps
Check: loose 2 real + 1 text = 3 rows. Strict 3 − 2 rejected = 1 row. Digests match sha256sum. Python 3.11.15 · sqlite3 3.45.1
06 General Notes Skepticism
- Validate on read as well as on write
- Log every rejection with its reason
- A CHECK in the table beats a note in the README
- Reinhart–Rogoff: a spreadsheet range left out five countries. With other fixes, growth above 90% debt was 2.2%, not −0.1% (Herndon et al. 2014)
07 Mini Project Hands-on
- Rebuild Sprint 19's table as STRICT with CHECKs
- Load its six rows again. Which fail, and why?
- Save the clean table atomically. Keep its SHA-256 in a .sha256 file
- Edit one digit and make your loader refuse the file
R Trusted Sources
- Breck, Polyzotis, Roy, Whang & Zinkevich (2019). Data Validation for Machine Learning. SysML
- SQLite docs: Datatypes In SQLite
- STRICT Tables
- Python docs: os.replace
- NIST (2015). FIPS 180-4, Secure Hash Standard
- Herndon, Ash & Pollin (2014). Camb. J. Econ. 38(2), 257–279
- Wilkinson et al. (2016). The FAIR Guiding Principles. Sci. Data 3, 160018
