Python basics

English translation

Learning objectives

Sensor records include missing and boundary values. Try handling normal and exceptional data with the same function.

Represent sensor records with data structures

Starting conditions

The record is [(0,21.0),(1,21.5),(2,None),(3,22.0),(4,20.5)], where each pair is time [s] and temperature [°C].

Procedure

  1. Convert each list item to a dictionary with time and temperature.
  2. Calculate the count, mean, minimum, and maximum of samples excluding None.
  3. Write a summarize function that leaves the original input unchanged.

Results to retain

records=[(0,21.),(1,21.5),(2,None),(3,22.),(4,20.5)]
valid=[value for _,value in records if value is not None]
print('valid count',len(valid),'mean',sum(valid)/len(valid))
# TODO: Add dict conversion and the summarize function.

Record results and self-check

t [s]:

Temperature [°C]:

Validity:

Points to check

Boundary values and empty input

Starting conditions

Convert each of the three inputs in cases in the code to the same list of dictionaries as in Activity 1, then pass them to summarize. Leave the empty list unchanged, and map the first value of each pair to time and the second to temperature.

Procedure

  1. Define the behavior to return None for the mean when there are 0 valid samples.
  2. Check that the returned fields are the same for one sample and multiple samples.
  3. Use asserts to check empty input, input with missing values, and single-sample input separately.

Results to retain

cases=[[],[(0,None)],[(0,25.)]]
# TODO: Add summarize results and assert checks.

Record results and self-check

Input type:

Valid count:

Mean:

Reason for handling:

Points to check

Organize repeated measurements in a function

Starting conditions

Compare the two devices A=[212223]A=\begin{bmatrix}21&22&23\end{bmatrix} and B=[19,None,20,21]. The temperature unit is the same as in the original data.

Procedure

  1. Convert each device's temperature list to a list of dictionaries with time and temperature, and pass it to the same summarize function. Since these data have no times, use time=None and summarize only the temperatures.
  2. Record each device's valid sample count, mean, and range in one table.
  3. Briefly summarize what to check when comparing data with different sample counts and missing-value patterns.

Results to retain

devices={'A':[21,22,23],'B':[19,None,20,21]}
# TODO: Use the same function to summarize each device.

Record results and self-check

Device:

Valid count:

Mean [°C]:

Minimum and maximum [°C]:

Missing count:

Points to check