Activity 3: Python - JSON handling
Extending file handling concepts from Activity 2, this activity introduces manipulation of JSON files. We also introduce the concept of Python Classes. Classes encapsulate data (attributes) and behavior (methods), inheritance into a single unit, making it easier to organize and manage code
<aside>
💡 Each method defined in the template is a member of a class called PQM_Metrics
. To call this method use the following syntax
</aside>
# This is just sample code and do not represent actual class or memeber variable
#names for the Activity.
class_var = TestClass() #This is a class instantiation
class_var.variable_1 = 1 #Accessing class variables and updating it
class_var.method_1() #Calling a method defined in the class
The JSON should be read, sanitized and stored using the following logic
pqm_metric_instance = PQM_Metrics() #Instantiate the class variable
pqm_metric_instance.store_and_sanitize_json('file.json') # You need to define the logic
print(pqm_metric_instance.src_json_dict) #Prints sanitized JSON of type <class 'dict'>
<aside> 💡 The above method is provided in the template. Populate it with the correct logic. This method will be used in the auto-grading as well
</aside>
Tasks | Expected Output | Grade |
---|---|---|
1.1 Read the string path of the JSON file | ||
1.2 Sanitize the JSON file where each entry of the cell contains 8 metric values. Of these, some values will be in a string format. Convert it into float. | ||
1.3 Store the cleaned JSON dict in the class variable | ||
src_json_dict |
This task is a pre-requisite for 4 and 5 | When a JSON file path is sent to the method, it is cleaned and stored in the class variable
NOTE: Method does not return anything | 40 |
| 2.1 The JSON contains an outer dictionary of elements. Use the get_json_keys
method to return a python list of keys of the outermost dictionary | The method should return the list of the outermost elements of the JSON | 10 |
| 3.1 The JSON contains an element called metrics which is another dictionary. Return the total number of cells present in this dictionary using the method get_cell_count
| Return the total number of cells in the metrics element as an integer | 10 |
| 4.1 Compute the average completeness, artifacts, resolution and accuracy of all the cells in the metrics element using get_avg_metrics
method
4.2 This method should update the respective class variables.
This method does not return anything | The average values of 4 metrics is stored in the class variables.
Note that since the metrics lie in the range of [0,1], the average must not exceed 1 or lie below 0
NOTE: Method does not return anything | 20 |
| 5.1 Populate the compute_sums
method to compute the sum of the chamfer, hausdorff and normalized chamfer values of all cells of the metrics element and store it in the respective class variables | Total sum of each of the defined metric is stored in the class variables
Note that this value can be greater than 1.
NOTE: Method does not return anything | 20 |
Reference for JSON level notation
{
"level_1_1":{
"level_2_1":0,
"level_2_2":{
"level_3_1":0,
"level_3_2":0,
}
},
"level_1_2":0,
}