Python Question

QUESTION

Implementing our Choose Unit Function / Printing Some Statistics

This is truly exciting. We’ve now got data loaded into our current_set object, and we are ready to display that data!

First, in order to make our program internationally useful and also recognized by the broad scientific community, we must implement our choose_unit() function, use our previously implemented convert_unit() function.

We need to create a global integer variable (not a constant, be sure to use the right naming convention) to hold the current unit selection and provide a default value. Name this variable current_unit. The variable, current_unit should have a default value that indicates units in Celsius.

We will use a global constant dictionary to contain valid unit. Because dictionaries are old news to us, I’m giving it to you. (What makes it is global constant?

UNITS = {
    0: ("Celsius", "C"),
    1: ("Fahrenheit", "F"),
    2: ("Kelvin", "K"),
    }

Next, we need to create a function to choose unit. It should provide the following functionality:

REPORT what the current unit is

GIVE A MENU of the available units

ASK the user to choose a new unit. Do not leave until the user chooses correctly. Be sure to filter for non-integer values.

CHANGE current_unit to the user’s selection.

Important: You should use the dictionary above to generate the menu options and validate the user selection.

When called, your function should generate something like this:

STEM Center Temperature Project
Mike Murphy

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

What is your choice? 2
Current unit in Celsius
Choose new unit:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
Which unit?
1

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

What is your choice? 2
Current unit is Fahrenheit
Choose new unit:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
Which unit?
2

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

What is your choice? 2
Current unit is Kelvin
Choose new unit:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
Which unit?
43
Please choose a unit from the list
Choose new unit:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
Which unit?
a
*** Please enter a number only ***
Choose new unit:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
Which unit?
0

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

What is your choice?

Now, let’s make sure you implemented this correctly. Add an item to the dictionary so it looks like this WITHOUT MAKING ANY OTHER CHANGES TO YOUR CODE:

UNITS = {
0: ("Celsius", "C"),
1: ("Fahrenheit", "F"),
2: ("Kelvin", "K"),
5: ("Rankine", "R")
}

Of course, this won’t work with our convert_unit() function, but it’s okay, it’ll only be there for a second. Now make sure you can reproduce this, including the error entry of 4:

STEM Center Temperature Project
Mike Murphy

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

What is your choice? 2
Current unit is Celsius
Choose new unit:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
5 - Rankine
Which unit?
4
Please choose a unit from the list
Choose new unit:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
5 - Rankine
Which unit?
5

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

What is your choice? 2
Current unit is Rankine
Choose new units:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
5 - Rankine
Which unit?

Now make sure you remove the Rankine line.

Alright, got that. You should test your convert_unit() function, and make sure it works

We now need to implement the method get_avg_temperature_day_time(self, filter_list, day, time). I listed the parameters to remind you. We still want it to return None if there is no dataset loaded. We also want it to return None if there are no sensors active in filter_list or if the active sensors have no readings (you can do both in one statement!). If there are readings available, we want it to find all the samples based on our filter_list at a particular day of week and time of day, and return the average as a float (also known as a floating-point number).

It’s your job to determine how to implement this. I used a list comprehension to create a list of just the temperature field of each matching line, then it was easy to calculate the average. I encourage you to use a list comprehension. They are elegant, especially when we are filtering a few values like this, and they are one of the best-known features of Python. Remember, this function must not print anything out.

Once you have implemented this function, let’s make sure that it is working. Add a line after your call to print_menu() like this:

 print(current_set.get_avg_temperature_day_time(filter_list, 5, 7))  # for testing

After loading the dataset, you should see the number 20.4554411…… print out. That’s the average of temperatures on day 5 (Friday) at hour 7 (7AM-8AM). Now go to edit room filter and remove “Out”. When you go back to the main menu you should see 20.86392….. No sense continuing until you get these numbers! Now go back and remove ALL the rooms from the filter. The average temp should report None.

Leave the test line in for your submission, please. Remove it before the next assignment.

We’ll use the get_avg_temperature_day_time() function for the next assignment, but as long as we’re on a roll with summary statistics let’s implement menu option 4. This should be a piece of cake, it’s very similar to what we just did.

First implement get_summary_statistics(self, filter_list) to return a tuple of floats with the minimum, maximum, and average temperature of the sensors that are active in filter_list. If there is no data, this method should return None. Remember, this function must not print anything out, that’s the “We don’t need an if statement like we did for the last assignment”. This time we’re being good programmers and using getter. If we don’t have a dataset loaded the accessor will return None, and the print function will dutifully print None as well! This is the job of our helper function, print_summary_statistics()…

Just take a look at the sample run and you’ll know what to do for print_summary_statistics(). Be sure print_summary_statistics() behaves well in these situations:

  • Normal operation (all sensors active, and some sensors filtered)
  • No data loaded
  • All sensors off

Notice in the sample run that temperatures are rounded to two decimal places. Notice also that the units are listed! Those should come from the units dictionary, of course, not from some kind of if statement. Hint, in this case, the units dictionary behaves “kind-of” like a multi-dimensional array, as described in the modules. That also means you need to use convert_units to get the right values. Finally, note that the name we gave the dataset is printed out with the summary statistics!

The sample run is really long, don’t be shy to make yours long as well! Put your program through its paces:

STEM Center Temperature Project
Mike Murphy
Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

None
What is your choice? 4
Please load data file and make sure at least one sensor is active

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

None
What is your choice? 1
Please enter the filename of the new dataset: Temperatures2022-03-07.csv
Loaded 11724 samples

Please provide a 3 to 20 character name for the dataset Test Week: aaa

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

20.45544117647059
What is your choice? 4
Summary statistics for Test Week
Minimum Temperature: 16.55 C
Maximum Temperature: 28.42 C
Average Temperature: 21.47 C

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

20.45544117647059
What is your choice? 2
Current unit is Celsius
Choose new units:
0 - Celsius
1 - Fahrenheit
2 - Kelvin
Which unit?
1

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

20.45544117647059
What is your choice? 4
Summary statistics for Test Week
Minimum Temperature: 61.79 F
Maximum Temperature: 83.16 F
Average Temperature: 70.64 F

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

20.45544117647059
What is your choice? 3

4201: Foundations Lab [ACTIVE]
4204: CS Lab [ACTIVE]
4205: Tiled Room [ACTIVE]
4213: STEM Center [ACTIVE]
4218: Workshop Room [ACTIVE]
Out: Outside [ACTIVE]

Type the sensor number to toggle (e.g. 4201) or x to end 4201

4201: Foundations Lab
4204: CS Lab [ACTIVE]
4205: Tiled Room [ACTIVE]
4213: STEM Center [ACTIVE]
4218: Workshop Room [ACTIVE]
Out: Outside [ACTIVE]

Type the sensor number to toggle (e.g. 4201) or x to end 4204

4201: Foundations Lab
4204: CS Lab
4205: Tiled Room [ACTIVE]
4213: STEM Center [ACTIVE]
4218: Workshop Room [ACTIVE]
Out: Outside [ACTIVE]

Type the sensor number to toggle (e.g. 4201) or x to end x

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

19.910638297872342
What is your choice? 4
Summary statistics for Test Week
Minimum Temperature: 61.79 F
Maximum Temperature: 83.16 F
Average Temperature: 70.13 F

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

19.910638297872342
What is your choice? 3

4201: Foundations Lab
4204: CS Lab
4205: Tiled Room [ACTIVE]
4213: STEM Center [ACTIVE]
4218: Workshop Room [ACTIVE]
Out: Outside [ACTIVE]

Type the sensor number to toggle (e.g. 4201) or x to end 4205

4201: Foundations Lab
4204: CS Lab
4205: Tiled Room
4213: STEM Center [ACTIVE]
4218: Workshop Room [ACTIVE]
Out: Outside [ACTIVE]

Type the sensor number to toggle (e.g. 4201) or x to end 4213

4201: Foundations Lab
4204: CS Lab
4205: Tiled Room
4213: STEM Center
4218: Workshop Room [ACTIVE]
Out: Outside [ACTIVE]

Type the sensor number to toggle (e.g. 4201) or x to end 4218

4201: Foundations Lab
4204: CS Lab
4205: Tiled Room
4213: STEM Center
4218: Workshop Room
Out: Outside [ACTIVE]

Type the sensor number to toggle (e.g. 4201) or x to end Out

4201: Foundations Lab
4204: CS Lab
4205: Tiled Room
4213: STEM Center
4218: Workshop Room
Out: Outside

Type the sensor number to toggle (e.g. 4201) or x to end x

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

None
What is your choice? 4
Please load data file and make sure at least one sensor is active

Main Menu
---------
1 - Process a new data file
2 - Choose unit
3 - Edit room filter
4 - Show summary statistics
5 - Show temperature by date and time
6 - Show histogram of temperatures
7 - Quit

None
What is your choice? 7
Thank you for using the STEM Center Temperature Project
>>>

Get your college paper done by experts

Do my question How much will it cost?

Place an order in 3 easy steps. Takes less than 5 mins.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *