Saltar al contenido

Lección 24 · Projects and Reference

Project: Build a Versioned Dataset with DVC

A hands-on DVC project: create a small dataset, track two versions, add a local remote, tag releases, roll back safely and validate data before publishing.

In this project you create a small "support tickets" dataset, publish two versions, roll back to the first one and prove that it worked. Do it in your own terminal with real DVC and Git, in a scratch folder. All data is invented, so nothing sensitive is involved.

Goal

By the end you have:

  • a Git repository with DVC initialized;
  • a local folder acting as the DVC remote;
  • two tagged data versions;
  • a validation script that runs before every release;
  • a documented rollback.

Step 1: set up

Terminal
# Read-only: create the project in a scratch location
mkdir tickets-lab && cd tickets-lab
mkdir dvc-storage
mkdir tickets-data && cd tickets-data
git init
python -m venv .venv && source .venv/bin/activate
pip install dvc
echo ".venv/" > .gitignore
dvc init
dvc remote add -d local "$(cd ../dvc-storage && pwd)"
mkdir data scripts
git add .gitignore .dvc .dvcignore
git commit -m "Initialize project"

The remote uses an absolute path, so it also works from a second clone on the same machine. An absolute path is only right for a practice lab. For a team, use shared storage that everyone can reach. On Windows, type the full path to dvc-storage yourself.

Step 2: the data generator

Save this as scripts/make_data.py:

Python
# Read-only: scripts/make_data.py
import csv, random, sys

rows, seed = int(sys.argv[1]), int(sys.argv[2])
random.seed(seed)
topics = ["billing", "login", "shipping"]
with open("data/tickets.csv", "w", newline="") as f:
    w = csv.writer(f, lineterminator="\n")
    w.writerow(["id", "topic", "minutes_to_resolve"])
    for i in range(1, rows + 1):
        w.writerow([i, random.choice(topics), random.randint(5, 240)])

Step 3: validate before releasing

Write checks that fail loudly. The stand-in below runs the checks on in-memory data. Copy the function into scripts/validate.py, add code that reads the file named on the command line, and exit with a non-zero status when there are problems.

Python Stand-in: validate a ticket dataset

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 4: publish version 1

Terminal
# Read-only: first release
python scripts/make_data.py 100 1
python scripts/validate.py data/tickets.csv
dvc add data/tickets.csv
git add data/tickets.csv.dvc data/.gitignore scripts
git commit -m "tickets v1: 100 rows"
git tag -a tickets-v1 -m "First release: 100 rows"
dvc push

Step 5: publish version 2

Terminal
# Read-only: second release
python scripts/make_data.py 150 2
python scripts/validate.py data/tickets.csv
dvc diff tickets-v1
dvc add data/tickets.csv
git add data/tickets.csv.dvc
git commit -m "tickets v2: 150 rows, new random sample"
git tag -a tickets-v2 -m "150 rows, resampled"
dvc push

Write a short change note: how many rows, what changed, and whether columns changed. The second version uses a different seed, so it is a new random sample, not "the old 100 rows plus 50 new ones". Your note should say so.

Step 6: roll back

Terminal
# Read-only: restore version 1 data, keep current code
git checkout tickets-v1 -- data/tickets.csv.dvc
dvc checkout data/tickets.csv.dvc
wc -l data/tickets.csv          # a header plus 100 rows
git commit -m "Roll back to tickets v1"

Step 7: prove the remote works

Simulate a new teammate:

Terminal
# Read-only: clean clone test
cd .. && git clone tickets-data tickets-clone && cd tickets-clone
source ../tickets-data/.venv/bin/activate
dvc remote list
dvc pull
python scripts/validate.py data/tickets.csv

A fresh clone has an empty cache, so a successful pull shows that the data really reached your remote. If the pull fails, check the remote path and make sure you pushed.

Extensions

  1. Add a README.md describing columns, the owner and the license of your invented data.
  2. Track a folder of monthly files and see how only changed files create new objects.
  3. Install hooks with dvc install and watch the post-checkout hook restore data.
  4. Run dvc gc -w -a -T --dry and explain the result.

Reflection questions

  • Which files did Git track, and which did DVC track?
  • What would happen if you deleted the cache after pushing?
  • Tags are Git objects. What must you do so a teammate on another machine sees them?

Common mistake: releasing without validation, then fixing the data under the same tag. Never move a published tag; create a new version instead, so history stays trustworthy.

What each step teaches

It helps to name the idea behind each step, so you can apply it to your own data.

Step Idea
Set up Configuration that others need goes into Git
Generator with a seed The same inputs give the same file, so results can be repeated
Validation A release is a promise about quality, so check it first
dvc add and tag A version is a pointer file plus a human name
dvc push A version that only exists on your disk is not shared
Rollback History is something you can use, not only read
Clean clone Trust a result only after testing it from nothing

Check your work

Before you call the project done, answer these with commands rather than memory. Does git log --oneline show your releases in order? Does git tag list both tags? Does git ls-files data list the .dvc file and .gitignore but not the CSV? Does dvc data status --not-in-remote report nothing missing?

If one answer is surprising, go back and find out why. Typical causes are a CSV that Git already tracked, a remote path that does not resolve from the new folder, or a push that ran before the latest dvc add.

Reset and try again

Because this is a scratch folder with invented data, you can delete the whole tickets-lab directory and start over. Repeating the project once without looking at the instructions is an excellent test of whether the workflow has become familiar.

Apply it to your own data

Once the lab works, choose a small real dataset that you are allowed to copy, preferably one without personal or confidential information. Write its description first: where it comes from, who owns it, what each column means and how often it changes. Then repeat the same steps with your own validation rules, such as required columns, allowed values, unique keys and sensible number ranges.

Decide where the shared storage will live and who may write to it before the first push. Agree on a tag naming scheme, such as the dataset name plus a version number. Finally, ask one colleague to do the clean clone test from their own machine with their own access. That single test exposes most gaps in permissions and documentation, and it is far better to find them in the first week than during an urgent investigation.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Why run validation before dvc add and tagging?

    A release should only contain data that passed your checks.
  2. 2. How did the project restore version 1 without changing the code?

    Restoring only the pointer file changes only that dataset.
  3. 3. What does the clean clone test verify?

    A fresh clone has no local cache, so pull must succeed from the remote.

Summary

  • You tracked two data versions, tagged them and pushed them.
  • A validation script guards releases.
  • Rolling back means restoring a pointer file and running dvc checkout.
  • A clean clone is a strong test of your remote.
  • Never move published tags; release a new version instead.

Next: Project: A Reproducible Training Pipeline

Tu progreso se guarda en este navegador. No necesitas una cuenta.