topskill.dev
CoursesTracks
0
All courses/Intro to NumPy: Pocket ObservatoryExercise: save and read CSV
7. Observation Lab: Shapes, CSV, and Final Project
10 / 12
← Intro to NumPy: Pocket Observatory
○What is NumPy?○Import NumPy as np○Turn a list into an array○Exercise: Decode a sensor feed○Build a two-dimensional grid○Exercise: Map station rows○Stack observation frames○Exercise: Stack sky snapshots○Read shape and size○Exercise: Label any observation✦Module Quiz
○Mark ticks and intervals○Exercise: Schedule sensor samples○Reserve blank and active grids○Exercise: Prime observation panels○Build patterns without loops○Exercise: Wire a sensor pattern○Replay seeded samples○Exercise: Simulate three station feeds○Choose and convert element types○Exercise: Label observation types✦Module Quiz
○Pick readings by position○Exercise: choose a signal pair○Read a sky grid○Exercise: trace a grid column○Cut a stepped window○Exercise: mute alternating samples○Mark noisy readings○Exercise: censor weak signals○See a view change its source○Exercise: preserve a calibration snapshot✦Module Quiz
○Scale every reading○Exercise: calibrate a pressure series○Combine matching readings○Exercise: find sensor agreement○Find a usable band○Exercise: flag an observation window○Apply functions to a full array○Exercise: transform a light curve○Reuse column offsets across rows○Exercise: correct a horizon map○Give each grid row its own baseline○Exercise: calibrate rows and reject bad shapes✦Module Quiz
○Find the loudest reading○Locate sensor extremes○Choose an axis○Summarize both directions○Keep a running tally○Build an interval counter○Describe a series○Compare center and spread○Sort within rows○Order each observation○Project: read the panel○Project: flag variability✦Module Quiz
○Read the grid○Select a sensor history○Transpose a grid○Turn a scoreboard○Weight a reading○Compute a weighted score○Combine matrix dimensions○Transform a whole panel○Project: calibrate the panel○Project: report calibrated peaks✦Module Quiz
○Fold a signal○Exercise: pack star counts○Trace shared readings○Exercise: audit a view○Raise a new axis○Exercise: align calibration○Stamp a pattern○Exercise: build exposure plan○Read a tiny CSV●Exercise: save and read CSV○Final project: Pocket Observatory report✦Module Quiz

Exercise: save and read CSV

The weather sensor needs to send a grid as text and recover it later. Write two helpers so you can check each direction on its own.

StringIO() creates an empty text buffer. np.savetxt(buffer, values, delimiter=",", fmt="%d") writes numeric rows into it. Here, fmt="%d" writes whole numbers, and buffer.getvalue() returns the finished string, including a newline after each row.

For example, this separate one-row grid becomes the text '8,1\n':

python
from io import StringIO
import numpy as np

buffer = StringIO()
np.savetxt(buffer, np.array([[8, 1]]), delimiter=",", fmt="%d")
text = buffer.getvalue()
print(repr(text))
print(np.genfromtxt(StringIO(text), delimiter=",", dtype=int, ndmin=2))

When reading, ndmin=2 keeps at least two dimensions. Without it, a one-row CSV usually becomes a one-dimensional array rather than a grid. A fresh StringIO(text) starts reading at the beginning; if you reuse the written buffer instead, rewind it with buffer.seek(0) first.

The buffer is temporary practice data created by your code, not a downloaded or saved file. Edit main.py, Run after each helper, then Submit. The supplied repr(text) print makes newline characters visible.

Your Task

  1. Implement save_grid(grid) with np.savetxt and StringIO to return comma-separated integer CSV text ending in a newline.
  2. Implement read_grid(text) with np.genfromtxt and StringIO to return a two-dimensional integer array, including for one-row inputs.
Hint 1
1 / 1
HINT 1

Write with np.savetxt(buffer, grid, delimiter=",", fmt="%d"); read with ndmin=2.

Files
  • ⋄main.py
Loading editor…
Press Run to execute your code.