Programming labs
Write the program yourself, then compare with the model solution. The problems follow the chapters, from the first programs to object-oriented design.
CPS 5745 · 84 labs
Chapter 1 Visualization in the Age of AI 6 labs
Anscombe's quartet: when statistics lie
Read a CSV from standard input with columns dataset,x,y (several datasets, in order of first appearance). For each dataset print one line: [code] — th...
Your first chart specification with Altair
CityPulse gives you daily PM2.5 readings: a CSV on standard input with columns date,district,pm25. Build an Altair chart titled "Daily PM2.5 by distri...
Audit an AI-generated chart
A chatbot produced Vega-Lite specifications for CityPulse. Write an automatic auditor: read one JSON specification from standard input and apply these...
A scatter plot from scratch: scales and marks in text
To understand what every plotting library does internally, draw a scatter plot as text. Input: first line W H (grid width and height in characters), t...
Critique an AI-generated CityPulse chart
A city official typed into a chatbot: "Make a chart showing how air quality changed in each district this year." The assistant returned a chart and th...
Where can AI help in the visualization pipeline?
For each step of the visualization pipeline — (1) acquiring data, (2) cleaning and transforming it, (3) choosing the visual mapping, (4) writing the c...
Chapter 2 Data Abstraction: Datasets, Attributes and Tidy Data 6 labs
Classify the attributes of a dataset
Read a CSV from standard input and classify every column: - numeric column: key if its name ends with id (case-insensitive) and its values are unique,...
Wide to long and back: melt and pivot
Read a wide CSV from standard input: the first column identifies the unit (e.g., building), the other columns are periods (e.g., Jan,Feb,Mar) holding...
A data-quality report before the first chart
Read sensor readings (sensor_id,district,timestamp,pm25) from standard input and print a quality report: [code] Order: drop exact duplicates first; th...
Flatten nested JSON from a bike-share API
A bike-share API (in the style of the GBFS standard) returns JSON like: [code] Read it from standard input, flatten the stations with pd.json_normaliz...
Review AI-written cleaning code
An assistant produced this pandas code to compute "average PM2.5 per district per month" for CityPulse: [code] 1. Find at least five problems (correct...
Data abstraction of the whole CityPulse platform
CityPulse has four sources: (a) air-quality sensors (hourly PM2.5 and temperature per sensor), (b) bike-share trips and live station status, (c) month...
Chapter 3 Task Abstraction: Why Is the User Looking? 6 labs
A task-vocabulary checker
Write a checker that accepts only well-formed tasks. Standard input: a line columns: date district pm25 … (the data abstraction), then one task per li...
Answer the task with numbers before drawing it
A chart must make the answer to a task visible. Compute the answers first, so you can check the chart. Standard input: a CSV with columns date,<item,…...
Does the validation match the level?
Check a project's evaluation plan against the nested model. Each input line is claim: LEVEL method: METHOD. Known methods and the level they validate:...
From a task chain to linked views
Each input line is a chain of tasks separated by , each task ACTION TARGET ATTRIBUTE…. Map actions to view types: summarize, discover, explore, browse...
Task analysis of a real tool
Pick a visualization tool you use (a weather app, a fitness app, a flight-search site, GitHub Insights, a stock app, a bike-share app, a university po...
Critique an AI-generated task analysis
A team asked a chatbot: "List the visualization tasks for a bike-share operations dashboard." It answered: 1. Show bike usage over time. 2. Visualize...
Chapter 4 Getting Data: Files, Databases, APIs and Web Scraping 6 labs
SQL joins and groups from pandas
Standard input contains a stations CSV (station_id,name,district), a line ---, and a trips CSV (trip_id,start_station,end_station,duration_min). Load...
Scrape a station table robustly
Read an HTML page from standard input. The data is in <table id="stations" with a header row (Station, District, Bikes, Capacity); there may be other...
Follow API pagination safely
Simulate a paginated API. Standard input: the allowed requests per minute, then one JSON page per line (in any order) like {"page": 1, "next": 2, "res...
Read robots.txt before you crawl
Standard input: the content of a robots.txt file, a line ---, a line agent: NAME, then URLs (one per line). Use urllib.robotparser.RobotFileParser (pa...
Modernize a legacy scraper
A scraper written a few years ago (in the style of popular GitHub projects) collects a film list: [code] 1. List every technical, legal and data-quali...
A data acquisition plan for CityPulse
Write a one-page data acquisition plan for the CityPulse project you will build this semester. For each source (air quality, bike share, energy, buses...
Chapter 5 Marks, Channels and Perception 6 labs
Assign channels by type and importance
Write a tiny encoding recommender for point/line charts. Input lines: name type distinct (type is quantitative, ordinal or categorical), in order of i...
Stevens' power law: how big does it look?
Input lines: channel value1 value2, where channel is one of length, area, depth, brightness, saturation, electric shock (exponents 1.0, 0.7, 0.67, 0.5...
Measure the lie of a truncated axis
Input: a line axis_min M, then bars label value (the first bar is the baseline for comparisons). Print baseline bar: Lucheng = 42, axis starts at 40,...
An expressiveness checker for chart specifications
Read one Vega-Lite specification (JSON) from standard input. For each encoding channel, in alphabetical order, print x: t (quantitative) - magnitude r...
Encoding studio: one dataset, five encodings
Use the CityPulse table date, district, pm25 (6 districts × 30 days; any similar dataset of your own also works). In Altair, build five different char...
Redesign a misleading infographic
A city press release shows "Bike trips up 300 %!" with two bicycle icons: the 2026 icon is twice as tall and twice as wide as the 2025 icon. The data:...
Chapter 6 Colour, Accessibility and Honest Charts 6 labs
WCAG contrast checker
Input lines: foreground background (6-digit hex). Compute each colour's relative luminance L = 0.2126 R + 0.7152 G + 0.0722 B, where each channel c (0...
Is my palette safe for colour-blind readers?
Simulate colour-vision deficiency and find confusable colour pairs. Input: threshold T, then lines name hex. For each colour: linearize sRGB (as in La...
Pick the colour scheme from the attribute type
Input lines: name type=KIND [n=COUNT] [mid=VALUE] where KIND is categorical, sequential, diverging or cyclic. Rules: categorical with n ≤ 10 → scheme...
Detect a cherry-picked time window
Input: a line window START END (months YYYY-MM), then a CSV month,value (the full series). Compute least-squares slopes (value against position 0, 1,...
Accessibility audit of a dashboard
Audit a CityPulse-style dashboard (or any public dashboard, e.g., a city's open-data portal or a weather site) for accessibility. Check and document:...
Honest or misleading? Four charts
Judge each chart, compute where possible, and propose an honest version: 1. A bar chart of average commute time by bus line, y axis from 25 to 32 minu...
Chapter 7 The Grammar of Graphics: Tables, Time and Distributions 6 labs
How bin width changes a histogram
Input: a line steps W1 W2 … (bin widths to try), then a CSV with a column duration_min. Print n=16, min 4, max 31, mean 16.9, median 11.5. For each wi...
Verify what the chart computes
Input: a line valid_max M, then a CSV district,pm25. Build an Altair bar chart with transforms: filter 0 ≤ pm25 ≤ M, aggregate mean_pm25 = mean(pm25)...
Find the weekly rhythm: weekday × hour
Input: a CSV trip_id,start_time. Derive the weekday (three-letter English name, e.g. Mon) and the hour. Print the weekday × hour count matrix (rows in...
Layers, facets and concatenation
Input: a line guideline G, then a CSV date,district,pm25. Build: line = line chart (x = date, y = pm25, colour = district); rule = a dashed horizontal...
From sketch to specification
Sketch on paper (or describe precisely) three charts for the CityPulse question: "When and where are bike stations empty?" using the status table (sta...
Review AI-generated Altair code
An assistant produced this code for "show average trip duration by station and month": [code] Find the problems (correctness, performance, design, rob...
Chapter 8 Interaction and Multiple Linked Views 6 labs
Brushing and linking: predict the linked view
Input: a line brush FROM TO (dates), then a CSV date,district,pm25. Build in Altair: an interval selection named brush on the x encoding, added to a l...
A dropdown filter without a server (Altair)
Rebuild the classic "sales by region" case study in Altair. Input: a line choices R1 R2 … (regions a user will pick), then a CSV Date,Region,Sales. Cr...
The Bokeh 3 sales dashboard, done right
Rebuild the Bokeh case study with the current API. Input: a CSV Date,Region,Sales. - Two ColumnDataSources: all rows, and the rows of the first region...
Choose the level of detail for zooming
Input: a line width W (pixels), then lines view START END (dates). Data is recorded every 5 minutes. For each view, choose the finest aggregation leve...
Interaction design for the bike-operations view
Operators rebalance bikes using a tablet in a van. Data: station status every 5 minutes (bikes, docks, capacity), station locations, and predicted bik...
Static, small multiples or animation?
CityPulse wants to show how PM2.5 in its 6 districts changed month by month over 2026, in two settings: (a) an analysts' report where readers compare...
Chapter 9 Dashboards: Design, Development and Deployment 6 labs
KPI cards with context and sparklines
Input: a header line kpi NAME target T higher_is_betterlower_is_better, then a CSV with date and the KPI column (daily values). Using the last 7 days...
Test a Streamlit dashboard automatically
Write a small Streamlit dashboard and an automated test for it, in one program. Standard input is the CSV date,district,pm25. The dashboard (a Streaml...
Caching with a time-to-live
Simulate st.cache_data(ttl=...). Input: ttl T (seconds), cost C (seconds per database query), then requests TIME KEY in increasing time. A request is...
A dashboard design linter
Read a JSON description of a dashboard: title, audience, shows_last_updated (boolean) and panels, each with id, kind (kpi, chart, table, map, image…),...
Build and deploy a CityPulse dashboard
Build a small Streamlit (or Dash) dashboard for one CityPulse audience using a dataset you acquired in Chapter 4 (or a provided CSV). Requirements: 1....
Critique an AI-generated dashboard
A manager typed into a BI copilot: "Create a dashboard about our bike-share system." The copilot produced: 12 charts in a 4 × 3 grid (a donut of trips...
Chapter 10 Maps, Networks and Trees 6 labs
Great-circle distances and the nearest station
Input: a stations CSV name,lat,lon,bikes, a line ---, and a users CSV name,lat,lon. Implement the haversine distance with R = 6371 km. For each user p...
Normalize and classify for a choropleth
Input: a line classes K, then a CSV district,cases,population. Compute rate = cases / population × 10,000. Classify the rates with K equal-interval cl...
Network metrics for bike flows
Input: a CSV start,end with one row per trip. Aggregate trips into directed links (weight = number of trips), ignoring round trips (start = end) for t...
A slice-and-dice treemap
Input: a line canvas W H, then a CSV district,building,kwh (rows grouped by district, in display order). Compute the slice-and-dice layout: districts...
Map design for the air-quality network
CityPulse has 60 air-quality sensors in 6 districts of very different sizes (the largest covers 40 % of the city's area but has 15 % of its population...
From hairball to insight: the station network
An AI assistant drew the full CityPulse bike network (80 stations, all 2,500 station pairs with trips) as a force-directed node-link diagram with node...
Chapter 11 Scale: Reduction, Aggregation and High-Dimensional Data 6 labs
Overplotting and 2D binning
Input: a line grid NX NY, then a CSV x,y. Compute a 2D histogram with np.histogram2d(x, y, bins=[NX, NY]) and print 11 points, x [0, 8], y [0, 9], gri...
PCA: what standardization changes
Input: a line scaling standard or scaling none, then a CSV with a station column and numeric attributes. Optionally standardize (StandardScaler), fit...
Clustering stations as aggregation
Input: a line k K, then the stations CSV (station + numeric attributes). Standardize the attributes. For k = 1…4 print k=2: within-cluster sum of squa...
Sampling can delete the outliers
Input: fraction F, seed S, then a CSV sensor,pm25. Outliers are readings above Q3 + 1.5 × IQR. 1. Print 200 readings, 4 outliers above 51. 2. Draw a r...
Visualize 40 attributes of 120 buildings
CityPulse has 120 public buildings, each described by 40 attributes: monthly energy use (12), floor area, age, building type, occupancy, heating type,...
Read an embedding map produced by an AI pipeline
A city chatbot logs citizens' questions. The vendor's analytics dashboard embeds all 50,000 questions with a language model and shows a UMAP map: poin...
Chapter 12 AI for Visualization: From Natural Language to Charts 6 labs
A tiny natural-language-to-chart translator
Build a rule-based NL2VIS translator to understand what such systems must decide. Input: schema field:TNOQ …, then one question per line (lower-case i...
Guardrail: validate an AI-generated specification
Input: schema field:TNOQ …, then a Vega-Lite specification produced by an AI (possibly invalid JSON). Checks: valid JSON (else print REJECT: not valid...
Evaluate NL2VIS outputs: exact vs. execution accuracy
Input: a small CSV dataset, a line ---, then one JSON object per line: {"q": question, "gold": spec, "pred": spec} (Vega-Lite specs with mark and enco...
Fact-check a generated caption
Input: a CSV district,month,pm25, a line ---, then a caption (sentences ending with . or !). Check each sentence with these rules (first matching rule...
An experiment: does prompting with the schema help?
Design (and, if you have access to an AI assistant, run on a small scale) an experiment comparing three ways of asking an AI tool for CityPulse charts...
Should CityPulse put a chatbot on its open-data portal?
The city wants an "Ask CityPulse" chatbot that answers citizens' questions with charts and short texts from the open data. Write a one-page recommenda...
Chapter 13 Visualization for AI and Real-Time Systems 6 labs
Confusion matrix and per-group performance
Input: a CSV actual,predicted,group (1 = the station became/was predicted empty). Using scikit-learn (confusion_matrix(..., labels=[0, 1]), precision_...
ROC, precision–recall and choosing a threshold
Input: target_recall R, then a CSV actual,score. Print 10 cases, 5 positives; ROC AUC 0.720, average precision 0.794 (scikit-learn roc_auc_score, aver...
Streaming percentiles and alerts with hysteresis
Input: a line window=W enter=E exit=X k=K, then request latencies in ms (whitespace-separated, in arrival order). Process tumbling windows of W reques...
Detect data drift with the population stability index
Input: a reference CSV, a line ---, and a current CSV with the same numeric columns. For each column compute the PSI with 4 bins defined by the refere...
Explain the demand model to the city council
CityPulse's model predicts hourly bike demand per station. The council (non-experts) must decide whether to use it to plan where to add 200 bikes. Des...
An operations dashboard for an edge–cloud platform
CityPulse processes camera and sensor streams on 12 edge servers (one per district, near the sensors) and offloads heavy analytics to a public cloud....
Chapter 14 Evaluation, Storytelling and Reproducibility 6 labs
Analyse a usability test
Input: a CSV with one row per task attempt: participant,design,task,success,seconds,errors,sus1,…,sus10 (the SUS answers are repeated on each row of a...
Report a comparison with a confidence interval
Input: a CSV participant,table,chart with each participant's time (s) for the same kind of question with a table and with a chart (within-subjects). C...
Detect Simpson's paradox
Input: a CSV group,option,successes,total. Print the overall success rate of each option (alphabetical): overall: app_routing 78.0%, manual 82.6%; the...
A reproducible bootstrap confidence interval
Input: a line seed=S resamples=B, then a CSV district,pm25 with two districts. Let a and b be the readings of the alphabetically first and second dist...
Plan an evaluation study for your project
Write the evaluation plan for your semester project (industry or research track). Include: 1. The claim(s) you want to support, and the nested-model l...
From analysis to data story
Take one finding from your CityPulse analysis (or use: "Weekend bike demand near the river grew 40 % after the new riverside path opened, while weekda...