Skip to content

Commit d0ef4f5

Browse files
committed
Add slides for traits exercises
1 parent 2c3cc91 commit d0ef4f5

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

slides/02_traits.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,75 @@ f.data_changed = True
543543
<!-- #region slideshow={"slide_type": "slide"} -->
544544
## Exercise time!
545545

546-
- Take the simple example without traits
547-
- Create a simple Traits model for it
546+
- From the starting script (`stage1_starting_script/face_detect.py`), extract
547+
an object that represents an image file.
548+
- The class for the object should:
549+
- Be a traits model, i.e., inherit from `HasStrictTraits`, and, expose
550+
- Attributes:
551+
- `filepath`: the absolute path to the image file
552+
- `metadata`: a dictionary storing EXIF data
553+
- `data` a numpy array containing the RGB data
554+
- `faces`: a list containing detected faces
555+
- Methods:
556+
`detect_faces`: returns the list of detected faces
557+
- Be reactive:
558+
- Ensure `metadata` and `data` are updated with `filepath` is modified
559+
- Copy `stage1_starting_script/face_detect.py` to `stage2.1_traited_script` and
560+
work there
548561
- *Do not do any plotting in the model!*
549562

563+
- Hint for computing RGB data:
564+
565+
```python
566+
import numpy as np
567+
import PIL.Image
568+
569+
with PIL.Image.open(filepath) as img:
570+
data = np.asarray(img)
571+
```
572+
573+
<!-- #endregion -->
574+
575+
<!-- #region slideshow={"slide_type": "slide"} -->
576+
### Solution
577+
`stage2.1_traited_script/traited_face_detect.py`
578+
<!-- #endregion -->
579+
580+
<!-- #region slideshow={"slide_type": "slide"} -->
581+
## Exercise time!
582+
583+
- Develop another traits model, one that represents a folder containing several
584+
image files
585+
- The class for the object should expose:
586+
- Attributes:
587+
- `directory`: the absolute path to the folder
588+
- `images`: a list of `ImageFile` instances from the previous exercise
589+
- `data`: a pandas `DataFrame` to store metadata for each file in the folder
590+
- Be reactive:
591+
- Ensure `images` and `data` are updated when `directory` is modified
592+
- Override `__init__` to ensure directory exists at object initialization
593+
- Save work in `stage2.1_traited_script/image_folder.py`
594+
595+
- Hints:
596+
- Create a `DataFrame` from `List(Dict)`:
597+
```python
598+
import pandas as pd
599+
>>> records = [
600+
{'A': 5, 'B': 0, 'C': 3, 'D': 3},
601+
{'A': 7, 'B': 9, 'C': 3, 'D': 5},
602+
{'A': 2, 'B': 4, 'C': 7, 'D': 6}
603+
]
604+
>>> df = pd.DataFrame(records)
605+
A B C D
606+
0 5 0 3 3
607+
1 7 9 3 5
608+
2 2 4 7 6
609+
```
610+
- `os.path.isdir(directory)` to determine if `directory` is valid
611+
612+
<!-- #endregion -->
613+
614+
<!-- #region slideshow={"slide_type": "slide"} -->
615+
### Solution
616+
`stage2.1_traited_script/image_folder.py`
550617
<!-- #endregion -->

0 commit comments

Comments
 (0)