|
227 | 227 | "2) Set a breakpoint inside `main` to use the debugger\n", |
228 | 228 | "3) Step through the code using `n (next)` and another time using `s (step)`\n", |
229 | 229 | "4) Set a second breakpoint inside `main`and run again the code but this time use `c (continue)`\n", |
230 | | - "5) Download [this public dataset](https://github.com/fabridamicelli/ds005588/archive/refs/heads/broken-data.zip) into the folder `/pycourse/data/` (create it if you don't yet have it).\n", |
| 230 | + "5) Download [this public dataset](https://github.com/OpenNeuroDatasets/ds005588) as zip file into the folder `/pycourse/data/` (create it if you don't yet have it)\n", |
231 | 231 | "\n", |
232 | | - "This dataset was modified and has some problems apparently.\n", |
233 | | - "Here's a bit of code to unzip it and read through the files." |
| 232 | + "Here's a bit of code to unzip the file." |
234 | 233 | ] |
235 | 234 | }, |
236 | 235 | { |
237 | 236 | "cell_type": "code", |
238 | | - "execution_count": 6, |
239 | | - "id": "bbe54d2f-f5a4-44f1-a015-8a4d19e1097a", |
| 237 | + "execution_count": 1, |
| 238 | + "id": "6ed1ee58-e172-48ca-8d02-735cefc5ec1e", |
240 | 239 | "metadata": {}, |
241 | 240 | "outputs": [], |
242 | 241 | "source": [ |
243 | | - "path = \"/home/fdamicel/projects/pycourse/data/ds005588-broken-data.zip\"\n", |
244 | | - "target = \"/home/fdamicel/projects/pycourse/data\"" |
| 242 | + "from pathlib import Path\n", |
| 243 | + "import zipfile\n", |
| 244 | + "\n", |
| 245 | + "def unzip(source_file, target_dir):\n", |
| 246 | + " with zipfile.ZipFile(source_file) as file:\n", |
| 247 | + " file.extractall(target_dir)\n", |
| 248 | + "\n", |
| 249 | + "project_path = Path(\"pycourse\")\n", |
| 250 | + "\n", |
| 251 | + "source = project_path/\"data/ds005588-main.zip\"\n", |
| 252 | + "target = project_path/\"data\"\n", |
| 253 | + "unzip(source, target)" |
245 | 254 | ] |
246 | 255 | }, |
247 | 256 | { |
248 | | - "cell_type": "code", |
249 | | - "execution_count": 7, |
250 | | - "id": "e7ad9b30-c041-4edc-83d4-2cfdf984dac7", |
| 257 | + "cell_type": "markdown", |
| 258 | + "id": "2ef4d196-964a-49ae-9c50-3b4c772f14f5", |
251 | 259 | "metadata": {}, |
252 | | - "outputs": [], |
253 | 260 | "source": [ |
254 | | - "import zipfile" |
| 261 | + "Now, your collaborator has written this script to extract the mean value of the \"SAR\" entry from across all subjects bold data.\n", |
| 262 | + "\n", |
| 263 | + "6) Put this script into the folder (create if it does not yet exist) `pycourse/scripts/sar_mean.py` and make the necessary modifications to make it run as a script.\n", |
| 264 | + "\n", |
| 265 | + "7) Run it using `uv run scripts/sar_mean.py` and see it fail. Set a breakpoint inside the `get_subjects_sar_mean` function. Run it again and try to find the bug inside the debugger.\n", |
| 266 | + "\n", |
| 267 | + "8) Add some error handling to make sure the script runs." |
255 | 268 | ] |
256 | 269 | }, |
257 | 270 | { |
258 | 271 | "cell_type": "code", |
259 | 272 | "execution_count": 8, |
260 | | - "id": "123149ab-4410-4444-9348-099cbf067f91", |
| 273 | + "id": "4877305d-66bb-47ab-95c9-ae9377323bef", |
261 | 274 | "metadata": {}, |
262 | 275 | "outputs": [], |
263 | 276 | "source": [ |
264 | | - "with zipfile.ZipFile(path) as file:\n", |
265 | | - " file.extractall(target)" |
| 277 | + "import json\n", |
| 278 | + "from glob import glob\n", |
| 279 | + "from pathlib import Path\n", |
| 280 | + "\n", |
| 281 | + "def get_subjects_sar_mean(data_dir):\n", |
| 282 | + " # Grab all files matching this filename pattern\n", |
| 283 | + " files = glob(str(data_dir/\"**/*_bold.json\"), recursive=True)\n", |
| 284 | + " \n", |
| 285 | + " sar_sum = 0\n", |
| 286 | + " n = 0\n", |
| 287 | + " for file in files:\n", |
| 288 | + " content = json.loads(Path(file).read_text())\n", |
| 289 | + " sar_sum += content[\"SAR\"]\n", |
| 290 | + " n += 1\n", |
| 291 | + " return sar_sum/n\n", |
| 292 | + "\n", |
| 293 | + "# TODO: add whatever code you need to make this code a proper script\n", |
| 294 | + "# that prints the sar-mean when run" |
266 | 295 | ] |
267 | 296 | }, |
268 | 297 | { |
269 | | - "cell_type": "code", |
270 | | - "execution_count": 13, |
271 | | - "id": "8d95f7c4-977f-4cc5-8f8e-80e87ec7d40a", |
272 | | - "metadata": {}, |
273 | | - "outputs": [], |
274 | | - "source": [] |
275 | | - }, |
276 | | - { |
277 | | - "cell_type": "code", |
278 | | - "execution_count": 14, |
279 | | - "id": "3f764de2-552c-49ab-ae8c-8d8455d254c3", |
280 | | - "metadata": {}, |
281 | | - "outputs": [], |
282 | | - "source": [] |
283 | | - }, |
284 | | - { |
285 | | - "cell_type": "code", |
286 | | - "execution_count": 15, |
287 | | - "id": "4fbd3efe-bd52-4414-99a5-42058984a3c8", |
| 298 | + "cell_type": "markdown", |
| 299 | + "id": "1e080ee5-eea8-4f16-bb1d-a992e4eed409", |
288 | 300 | "metadata": {}, |
289 | | - "outputs": [ |
290 | | - { |
291 | | - "ename": "UnicodeDecodeError", |
292 | | - "evalue": "'utf-8' codec can't decode byte 0xb9 in position 10: invalid start byte", |
293 | | - "output_type": "error", |
294 | | - "traceback": [ |
295 | | - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", |
296 | | - "\u001b[31mUnicodeDecodeError\u001b[39m Traceback (most recent call last)", |
297 | | - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[15]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m lines = \u001b[43mf\u001b[49m\u001b[43m.\u001b[49m\u001b[43mreadlines\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", |
298 | | - "\u001b[36mFile \u001b[39m\u001b[32m<frozen codecs>:325\u001b[39m, in \u001b[36mdecode\u001b[39m\u001b[34m(self, input, final)\u001b[39m\n", |
299 | | - "\u001b[31mUnicodeDecodeError\u001b[39m: 'utf-8' codec can't decode byte 0xb9 in position 10: invalid start byte" |
300 | | - ] |
301 | | - } |
302 | | - ], |
303 | | - "source": [] |
| 301 | + "source": [ |
| 302 | + "To recap, so far our project should have these files:\n", |
| 303 | + "\n", |
| 304 | + "\n", |
| 305 | + "\n", |
| 306 | + "" |
| 307 | + ] |
304 | 308 | }, |
305 | 309 | { |
306 | 310 | "cell_type": "code", |
307 | 311 | "execution_count": null, |
308 | | - "id": "4877305d-66bb-47ab-95c9-ae9377323bef", |
| 312 | + "id": "43472f65-ea2f-4706-bb6a-7cf9a28e05b3", |
309 | 313 | "metadata": {}, |
310 | 314 | "outputs": [], |
311 | 315 | "source": [] |
|
0 commit comments