Week 3-4: Probability as a Frequency#
Laboratory 2
Last updated April 24, 2023
00. Content #
Mathematics
Sample spaces and events
Conditional probability
Binary numbers
Programming Skills
Functions
Data visualization
Embedded Systems
Thonny and MicroPython
0. Required Hardware #
Raspberry Pi Pico
Breadboard
USB connector
Camera (Arducam HM01B0)
8 Wires
Write your name and email below:
Name:
Email:
import numpy as np
import matplotlib.pyplot as plt
Week 3#
This week’s portion of the lab can be done individually, but you may also collaborate with your peers.
1. Probability as a Frequency #
The sample space of an experiment is the set of all possible outcomes, and an event is any subset of the sample space. An event occurs if the outcome of an experiment is contained within the subset defining the event. When probability is viewed as a frequency, the probability of an event is the relative frequency at which the event occurs over many repetitions of the experiment.
The Raspberry Pi Pico has its own internal temperature sensor that returns an integer between 0 and 65535. In this example, our experiment involves measuring the internal temperature of the Pico, and the sample space is represented by the set \(\{0,1,2,\dots, 65535\}\). We can easily calculate the temperature in Celsius from the sensor reading \(r\) using the formula:
Exercise 1 #
Let \(E\) be the event that the Pico’s internal temperature is between 70-72 degrees Fahrenheit. Which subset of outcomes from the sample space \(\{0,1,2,\dots, 65535\}\) define event \(E\)?
Write Answer for Exercise 1 Below
Collecting Temperature Data#
The Raspberry Pi Pico is equipped with 2MB of onboard memory or flash storage, enabling us to store small files of sensor data directly on the Pico. To begin, please download the pico_temperature.py
script from GitHub into your Downloads folder. The purpose of this script is to capture a total of 12 temperature readings, with measurements taken at intervals of 0.25 seconds, and save them to a file named temp.txt
on the Pico.
To perform the task, place the Pico on the breadboard and connect it to your computer using the USB cable. This is how you place the Pico:
Open Thonny and follow the directions here to install the Micropython firmware on the Pico. The following page at that link has directions for testing to make sure it’s working. Once the firmware is installed, you may need to press the STOP/RESTART
button multiple times until the Micropython shell appears on the bottom tab of Thonny. Once you see the Micropython shell, the Pico is connected to Thonny.
After completing the firmware installation, open Thonny and go to the top bar. Under “View”, select “Files” to bring up a side bar that allows you to transfer files onto your Pico. In the side bar, navigate to your “Downloads” folder. If you’re unsure, you can find it at /home/shay/a/"your username"/Downloads
. Right-click the pico_temperature.py
file and choose “Upload to /” to copy the file onto your Pico.
In Thonny, execute the pico_temperature.py
script on the Pico by pressing the RUN
button, it should look like a green play button. Once the script finishes running, transfer the temp.txt
file stored on the Pico to the folder you are currently working in, and then delete temp.txt
from the Pico’s storage. Feel free to modify the pico_temperature.py
script as necessary to adjust the number of measurements or the measurement interval.
Exercise 2 #
Collect 1,000 temperature measurements from the Pico, ensuring a minimum time interval of 0.25 seconds between each measurement. Display a bar plot to visualize the values stored in the temp.txt
file. Include clear and descriptive axis labels, as well as an informative title.
Write Answer for Exercise 2 Below
data = np.loadtxt("temp.txt", dtype=int) # make sure temp.txt is in the same directory as this notebook file
# dtype stands for data type, every line in the txt file will be read as an integer
Exercise 3 #
What value occurs the most frequently in temp.txt
? What is the relative frequency of that value?
Write Answers for Exercise 3 Below
NOTE
Press the STOP/RESTART button on Thonny to view the new
temp.txt
file.If you plan on running the program multiple times or overwriting the existing
temp.txt
file, make sure to keep the file closed.
Exercise 4#
Find the mode \(m\) (i.e., the value that occurs the most) among the first 11 temperature measurements. Define event \(E\) as the occurrence of a temperature measurement that is equal to \(m\). The complement of event \(E\) is denoted as \(E'\), representing the cases where the measurement differs from \(m\).
After obtaining the first 11 readings, calculate the frequency at which event \(E\) occurs. Similarly, determine the frequency of event \(E'\) in the given set of measurements.
Write Answers for Exercise 4 Below
Exercise 5#
Let’s focus solely on the first digit from the left of every temperature measurement in temp.txt
. Plot a bar graph that displays the frequency at which each digit from 0 to 9 appears.
Write Answer for Exercise 5 Below
Exercise 6#
Part 1: Repeat the previous exercise, but this time, consider the second digit instead of the first. Additionally, repeat the exercise separately for the third, fourth, and fifth digits. This means you will create a total of four bar charts, each showing the frequency of digits 0 to 9 in their respective digit places.
Write Answers for Exercise 6 Part 1 Below
Part 2: Which digit place exhibits the highest variability in your set of measurements?
Write Answer for Exercise 6 Part 2 Below
2. Conditional Probability #
In the context of two events, denoted as \(E\) and \(F\), the probability of event \(E\) occurring given that event \(F\) has occurred is referred to as a conditional probability. When we view probability as a frequency, this implies that the probability of event \(E\) given event \(F\) is calculated as the relative frequency of event \(E\) occurring within the sample space defined by event \(F\). Mathematically, this can be expressed as:
Binary Numbers#
For reasons that will be clear in the next section, let’s take a detour into binary numbers.
We are accustomed to the base-10 number system, where numbers such as 9234 can be broken down as follows:
Similarly, in the base-2 system (or binary), the binary number 1101 in base-10 is represented as:
In python, the prefix 0b
indicates to the computer that the sequence should be interpreted as a base-2 number. To perform the reverse conversion, you can use the bin
function, as demonstrated below.
x = 0b1101
print(x)
y = 13
print(bin(13))
# you can use underscores to make numbers more readable without affecting the value
z = 0b_100_001_111_111_001_100_111_001_110_001_0101
w = 9_123_456_789
print(z == w)
Exercise 7#
What are the following binary numbers in base-10?
110
010
101011
110000
Write Answers for Exercise 7 Below
Exercise 8 #
Part 1: What range of integers can be represented using 16 bits?
Write Answer for Exercise 8 Part 1 Below
Part 2: What is the minimum length of a random binary sequence required to obtain a random number between 0 and 100?
Write Answer for Exercise 8 Part 2 Below
NOTE
This is a 2-week lab. Turn in the exercises above. Pick up from here during the next lab session.
Week 4#
This week’s portion of the lab should be completed in groups of 2-3; however, everyone must submit their lab individually.
Identifying textures in images is an important task in many fields, such as medical imaging, remote sensing, and facial detection. A simple yet powerful method called local binary patterns (LBP) is commonly used to classify textures when employed in conjunction with certain machine learning algorithms.
We will be working with the grayscale images in this folder. Download the folder of images from GitHub.
For an \(m \times n\) grayscale image, there are \(m\) rows of pixels and \(n\) columns of pixels. Each pixel describes the intensity of the image with an integer ranging from 0 and 255 (0 representing black and 255 representing white).
Please refer to the cell below for instructions on how to read and display grayscale images in Python.
from PIL import Image # needed for reading images
img = np.array(
Image.open("textures/texture_1.jpg")
) # read in the image and store it as a numpy array
print(f"image size is {img.shape}")
fig, ax = plt.subplots(figsize=(12, 6)) # create figure and set figure size
ax.imshow(
img, cmap="gray", vmin=0, vmax=255
) # display the image in grayscale between 0 and 255
# ax.axis('off') # uncomment this line to hide the axes
plt.show()
Exercise 9#
Perform the following steps of LBP for each image in the textures folder:
1. For each pixel, examine the values of its 8 neighboring pixels.
Ex:
10
15
12
19
12
11
20
16
12
2. Starting from the pixel directly above the center pixel, proceed clockwise around the center to generate an 8-bit binary sequence. Assign '0'
if the neighbor’s value is greater than the center pixel’s value, and '1'
otherwise.
Using the given example, we begin at the center value of 12 and then move to the top neighbor, which is 15. Therefore, the binary sequence starts with
'0'
. Continuing clockwise, we arrive at the top-right neighbor of 12. Hence, the binary sequence becomes'01'
. If we continue this process, the final binary number will be'01110001'
.
3. Convert the 8-bit binary sequence to a base-10 integer.
In our example, the resulting LBP value is 113.
4. Create a plot illustrating the frequency of each LBP value, ranging from 0 to 255.
Write Answers for Exercise 9 Below
Exercise 10#
The maximum possible LBP value is 255, and it occurs frequently. If we only consider pixels that result in an LBP value of 255, which grayscales values (i.e., the value of the center pixel) are the most common?
Please use a bar plot to summarize your answer. Remember to label the axes and title the plot.
Write Answer for Exercise 10 Below
3. Connecting the Camera #
This time, we will record our own videos using the Arducam HM01B0, which is a small camera that can be connected to the Pico.
Wiring Instructions#
Please ensure that your microcontroller is not connected to the computer while you are wiring components together. If you are unsure about your wiring, please consult the instructor. Use your jumper wires to establish the following connections:
HM01B0 |
Pico |
---|---|
VCC |
3V3 |
SCL |
GP5 |
SDA |
GP4 |
VSYNC |
GP16 |
HREF |
GP15 |
PCLK |
GP14 |
DO |
GP6 |
GND |
GND |
Here is an image of the completed breadboard:
To find the names of the pins on the Raspberry Pi Pico, you can refer to its pinout diagram located here or in the Extra Materials section. The HM01B0, on the other hand, should have its pins labeled.
After confirming that the wiring is correct, press and hold the BOOTSEL button on the Pico while plugging it in. Download the arducam.uf2 file and copy it onto the Pico’s drive using your computer’s file manager (it should be listed as an external drive: "RPI-RP2"
) and not with Thonny. Once the file transfer is complete, the Pico will automatically disconnect, and its LED will start blinking rapidly.
Once the Pico has been successfully connected, please execute the following cell to ensure that we have successfully detected the Pico.
import time
import serial
from serial.tools import list_ports
PICO_HWID = "2E8A"
def get_pico_port():
pico_ports = list(list_ports.grep(PICO_HWID))
if len(pico_ports) == 0:
raise Exception(
"No Raspberry Pi Pico was detected. Check to make sure it is plugged in, and that no other programs are accessing it"
)
return pico_ports[0].device
print("Here are all the serial devices detected:")
for port in list_ports.comports():
print(port.device, port.hwid)
port = get_pico_port()
print(f"\nselected port {port} as most likely to have a raspberry pi pico")
Capturing a Still Image#
Now that the Pico and camera have been connected, execute the following cell to capture a still image.
buffer = bytearray(96 * 96)
img = np.zeros(shape=(96, 96), dtype="uint8")
with serial.Serial(port, timeout=1) as s:
s.read_until(b"\x55\xAA")
s.readinto(buffer)
img.flat[::-1] = buffer
plt.imshow(img, cmap="gray")
plt.show()
Exercise 11#
Part 1: Connect the camera to the Pico and capture at least 3 images of different surfaces, such as the desk, floor, your phone case, your hand, etc.
Apply LBP to the captured images.
Write Answers for Exercise 11 Below
Exercise 12#
Do you observe significant variations in the frequency of LBP values across different images?
Can you propose a mathematical rule to differentiate between two distinct textures based on LBP?
Write Answers for Exercise 12 Below
Reflection #
How did working with others help you complete this lab?
Which part of the lab did you find the most challenging?
Which part of the lab was the easiest?