Skip to content

Commit 48df106

Browse files
committed
MV - module03 refacto + indent
1 parent 94a524e commit 48df106

10 files changed

+757
-826
lines changed

module03/exercises/m03ex00.tex

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
\chapter{Exercise 00}
2+
\extitle{NumPyCreator}
3+
\turnindir{ex00}
4+
\exnumber{00}
5+
\exfiles{NumPyCreator.py}
6+
\exforbidden{None}
7+
\makeheaderfilesforbidden
8+
9+
% ================================== %
10+
\section*{Objective}
11+
% ---------------------------------- %
12+
Introduction to the Numpy library.
13+
14+
% ================================== %
15+
\section*{Instructions}
16+
% ---------------------------------- %
17+
Write a class named \texttt{NumPyCreator}, that implements all of the following methods.\\
18+
\\
19+
Each method receives as an argument a different type of data structure and transforms it into a Numpy array:
20+
\begin{itemize}
21+
\item \texttt{from\_list(self, lst)}: takes a list or nested lists and returns its corresponding Numpy array.
22+
\item \texttt{from\_tuple(self, tpl)}: takes a tuple or nested tuples and returns its corresponding Numpy array.
23+
\item \texttt{from\_iterable(self, itr)}: takes an iterable and returns an array which contains all of its elements.
24+
\item \texttt{from\_shape(self, shape, value)}: returns an array filled with the same value.
25+
The first argument is a tuple which specifies the shape of the array, and the second argument specifies the value of the elements.
26+
This value must be 0 by default.
27+
\item \texttt{random(self, shape)}: returns an array filled with random values.
28+
It takes as an argument a tuple which specifies the shape of the array.
29+
\item \texttt{identity(self, n)}: returns an array representing the identity matrix of size n.
30+
\end{itemize}
31+
\textit{\textbf{BONUS:}} Add to these methods an optional argument which specifies the datatype (dtype) of the array (e.g. to represent its elements as integers, floats, ...)
32+
33+
\hint{Each of these methods can be implemented in one line. You only need to find the right Numpy functions.}
34+
35+
% ================================== %
36+
\section*{Examples}
37+
% ---------------------------------- %
38+
\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python}
39+
from NumpyCreator import NumpyCreator
40+
npc = NumpyCreator()
41+
42+
npc.from_list([[1,2,3],[6,3,4]])
43+
# Output :
44+
array([[1, 2, 3],
45+
[6, 3, 4]])
46+
47+
48+
npc.from_list([[1,2,3],[6,4]])
49+
# Output :
50+
None
51+
52+
53+
npc.from_list([[1,2,3],['a','b','c'],[6,4,7]])
54+
# Output :
55+
array([['1','2','3'],
56+
['a','b','c'],
57+
['6','4','7'], dtype='<U21'])
58+
59+
60+
npc.from_list(((1,2),(3,4)))
61+
# Output :
62+
None
63+
64+
65+
npc.from_tuple(("a", "b", "c"))
66+
# Output :
67+
array(['a', 'b', 'c'])
68+
69+
70+
npc.from_tuple(["a", "b", "c"])
71+
# Output :
72+
None
73+
74+
75+
npc.from_iterable(range(5))
76+
# Output :
77+
array([0, 1, 2, 3, 4])
78+
79+
80+
shape=(3,5)
81+
npc.from_shape(shape)
82+
# Output :
83+
array([[0, 0, 0, 0, 0],
84+
[0, 0, 0, 0, 0],
85+
[0, 0, 0, 0, 0]])
86+
87+
88+
npc.random(shape)
89+
# Output :
90+
array([[0.57055863, 0.23519999, 0.56209311, 0.79231567, 0.213768 ],
91+
[0.39608366, 0.18632147, 0.80054602, 0.44905766, 0.81313615],
92+
[0.79585328, 0.00660962, 0.92910958, 0.9905421 , 0.05244791]])
93+
94+
95+
npc.identity(4)
96+
# Output :
97+
array([[1., 0., 0., 0.],
98+
[0., 1., 0., 0.],
99+
[0., 0., 1., 0.],
100+
[0., 0., 0., 1.]])
101+
\end{minted}

module03/exercises/m03ex01.tex

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
\chapter{Exercise 01}
2+
\extitle{ImageProcessor}
3+
\turnindir{ex01}
4+
\exnumber{01}
5+
\exfiles{ImageProcessor.py}
6+
\exforbidden{None}
7+
\makeheaderfilesforbidden
8+
9+
10+
% ================================= %
11+
\section*{Objective}
12+
% --------------------------------- %
13+
Basic manipulation of images via the Matplotlib library.
14+
15+
16+
% ================================= %
17+
\section*{Instructions}
18+
% --------------------------------- %
19+
Build a tool that will be helpful to load and display images in the upcoming exercises.\\
20+
\\
21+
Write a class named \texttt{ImageProcessor} that implements the following methods:
22+
\begin{itemize}
23+
\item \texttt{load(path)}: opens the PNG file specified by the \texttt{path} argument and returns an array with the RGB values of the pixels in the image. It must display a message specifying the dimensions of the image (e.g. 340 x 500).
24+
25+
\item \texttt{display(array)}: takes a numpy array as an argument and displays the corresponding RGB image.
26+
\end{itemize}
27+
You must handle these errors: if the file passed as argument does not exist or if it can't be read as an image, with an appropriate message of your choice.
28+
29+
\hint{You can use the library of your choice for this exercise, but converting the image to a numpy array is mandatory.
30+
The goal of this exercise is to dispense with the technicality of loading and displaying images,
31+
so that you can focus on array manipulations in the upcoming exercises.
32+
}
33+
34+
% ================================= %
35+
\section*{Examples}
36+
% --------------------------------- %
37+
\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python}
38+
from ImageProcessor import ImageProcessor
39+
imp = ImageProcessor()
40+
arr = imp.load("non_existing_file.png")
41+
# Output :
42+
Exception: FileNotFoundError -- strerror: No such file or directory
43+
44+
print(arr)
45+
# Output :
46+
None
47+
48+
arr = imp.load("empty_file.png")
49+
# Output :
50+
Exception: OSError -- strerror: None
51+
52+
print(arr)
53+
# Output :
54+
None
55+
56+
arr = imp.load("../resources/42AI.png")
57+
# Output :
58+
Loading image of dimensions 200 x 200
59+
60+
arr
61+
# Output :
62+
array([[[0.03529412, 0.12156863, 0.3137255 ],
63+
[0.03921569, 0.1254902 , 0.31764707],
64+
[0.04313726, 0.12941177, 0.3254902 ],
65+
...,
66+
[0.02745098, 0.07450981, 0.22745098],
67+
[0.02745098, 0.07450981, 0.22745098],
68+
[0.02352941, 0.07058824, 0.22352941]],
69+
70+
[[0.03921569, 0.11764706, 0.30588236],
71+
[0.03529412, 0.11764706, 0.30980393],
72+
[0.03921569, 0.12156863, 0.30980393],
73+
...,
74+
[0.02352941, 0.07450981, 0.22745098],
75+
[0.02352941, 0.07450981, 0.22745098],
76+
[0.02352941, 0.07450981, 0.22745098]],
77+
78+
[[0.03137255, 0.10980392, 0.2901961 ],
79+
[0.03137255, 0.11372549, 0.29803923],
80+
[0.03529412, 0.11764706, 0.30588236],
81+
...,
82+
[0.02745098, 0.07450981, 0.23137255],
83+
[0.02352941, 0.07450981, 0.22745098],
84+
[0.02352941, 0.07450981, 0.22745098]],
85+
86+
...,
87+
88+
[[0.03137255, 0.07450981, 0.21960784],
89+
[0.03137255, 0.07058824, 0.21568628],
90+
[0.03137255, 0.07058824, 0.21960784],
91+
...,
92+
[0.03921569, 0.10980392, 0.2784314 ],
93+
[0.03921569, 0.10980392, 0.27450982],
94+
[0.03921569, 0.10980392, 0.27450982]],
95+
96+
[[0.03137255, 0.07058824, 0.21960784],
97+
[0.03137255, 0.07058824, 0.21568628],
98+
[0.03137255, 0.07058824, 0.21568628],
99+
...,
100+
[0.03921569, 0.10588235, 0.27058825],
101+
[0.03921569, 0.10588235, 0.27058825],
102+
[0.03921569, 0.10588235, 0.27058825]],
103+
104+
[[0.03137255, 0.07058824, 0.21960784],
105+
[0.03137255, 0.07058824, 0.21176471],
106+
[0.03137255, 0.07058824, 0.21568628],
107+
...,
108+
[0.03921569, 0.10588235, 0.26666668],
109+
[0.03921569, 0.10588235, 0.26666668],
110+
[0.03921569, 0.10588235, 0.26666668]]], dtype=float32)
111+
imp.display(arr)
112+
\end{minted}
113+
\begin{figure}[h!]
114+
\centering
115+
\includegraphics[scale=0.5]{assets/capture_display_img.png}
116+
\end{figure}
117+
118+
The image must be displayed in a separate window when running in the console.

module03/exercises/m03ex02.tex

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
\chapter{Exercise 02}
2+
\extitle{ScrapBooker}
3+
\turnindir{ex02}
4+
\exnumber{02}
5+
\exfiles{ScrapBooker.py}
6+
\exforbidden{None}
7+
\makeheaderfilesforbidden
8+
9+
% ================================= %
10+
\section*{Objective}
11+
% --------------------------------- %
12+
Manipulation and initiation to slicing on numpy arrays.
13+
14+
% ================================= %
15+
\section*{Instructions}
16+
% --------------------------------- %
17+
Implement a class named \texttt{ScrapBooker} with the following methods:
18+
\begin{itemize}
19+
\item \texttt{crop},
20+
\item \texttt{thin},
21+
\item \texttt{juxtapose},
22+
\item \texttt{mosaic}.
23+
\end{itemize}
24+
25+
\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python}
26+
# within the class
27+
def crop(self, array, dim, position=(0,0)):
28+
"""
29+
Crops the image as a rectangle via dim arguments (being the new height
30+
and width of the image) from the coordinates given by position arguments.
31+
32+
Args:
33+
-----
34+
array: numpy.ndarray
35+
dim: tuple of 2 integers.
36+
position: tuple of 2 integers.
37+
38+
Returns:
39+
-------
40+
new_arr: the cropped numpy.ndarray.
41+
None: (if the combination of parameters is not possible).
42+
43+
Raises:
44+
------
45+
This function should not raise any Exception.
46+
"""
47+
... your code ...
48+
49+
def thin(self, array, n, axis):
50+
"""
51+
Deletes every n-th line pixels along the specified axis (0: vertical, 1: horizontal)
52+
53+
Args:
54+
-----
55+
array: numpy.ndarray.
56+
n: non null positive integer lower than the number of row/column of the array
57+
(depending of axis value).
58+
axis: positive non null integer.
59+
60+
Returns:
61+
-------
62+
new_arr: thined numpy.ndarray.
63+
None: (if the combination of parameters is not possible).
64+
65+
Raises:
66+
------
67+
This function should not raise any Exception.
68+
"""
69+
... your code ...
70+
71+
def juxtapose(self, array, n, axis):
72+
"""
73+
Juxtaposes n copies of the image along the specified axis.
74+
75+
Args:
76+
-----
77+
array: numpy.ndarray.
78+
n: positive non null integer.
79+
axis: integer of value 0 or 1.
80+
81+
Returns:
82+
-------
83+
new_arr: juxtaposed numpy.ndarray.
84+
None: (if the combination of parameters is not possible).
85+
86+
Raises:
87+
-------
88+
This function should not raise any Exception.
89+
"""
90+
... your code ...
91+
\end{minted}
92+
\newpage
93+
\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python}
94+
def mosaic(self, array, dim):
95+
"""
96+
Makes a grid with multiple copies of the array. The dim argument specifies
97+
the number of repetition along each dimensions.
98+
99+
Args:
100+
-----
101+
array: numpy.ndarray.
102+
dim: tuple of 2 integers.
103+
104+
Return:
105+
-------
106+
new_arr: mosaic numpy.ndarray.
107+
None (combinaison of parameters not compatible).
108+
109+
Raises:
110+
-------
111+
This function should not raise any Exception.
112+
"""
113+
... your code ...
114+
\end{minted}
115+
\newline
116+
In this exercise, when specifying positions or dimensions, we will assume
117+
that the first coordinate is counted along the vertical axis starting from
118+
the top, and that the second coordinate is counted along the horizontal axis
119+
starting from the left. Indexing starts from 0.\\
120+
121+
\parbox{\textwidth}{
122+
e.g.:\\
123+
(1,3)\\
124+
.....\\
125+
...x.\\
126+
.....}
127+
128+
129+
% ================================= %
130+
\section*{Examples}
131+
% --------------------------------- %
132+
\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python}
133+
import numpy as np
134+
from ScrapBooker import ScrapBooker
135+
136+
spb = ScrapBooker()
137+
arr1 = np.arange(0,25).reshape(5,5)
138+
spb.crop(arr1, (3,1),(1,0))
139+
#Output :
140+
array([[ 5],
141+
[10],
142+
[15]])
143+
144+
145+
arr2 = np.array("A B C D E F G H I".split() * 6).reshape(-1,9)
146+
spb.thin(arr2,3,0)
147+
#Output :
148+
array([['A', 'B', 'D', 'E', 'G', 'H', 'J', 'K'],
149+
['A', 'B', 'D', 'E', 'G', 'H', 'J', 'K'],
150+
['A', 'B', 'D', 'E', 'G', 'H', 'J', 'K'],
151+
['A', 'B', 'D', 'E', 'G', 'H', 'J', 'K'],
152+
['A', 'B', 'D', 'E', 'G', 'H', 'J', 'K'],
153+
['A', 'B', 'D', 'E', 'G', 'H', 'J', 'K']], dtype='<U1')
154+
155+
156+
arr3 = np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]])
157+
spb.juxtapose(arr3, 3, 1)
158+
#Output :
159+
array([[1, 2, 3, 1, 2, 3, 1, 2, 3],
160+
[1, 2, 3, 1, 2, 3, 1, 2, 3],
161+
[1, 2, 3, 1, 2, 3, 1, 2, 3]])
162+
\end{minted}

0 commit comments

Comments
 (0)