Skip to content

Commit

Permalink
Update TestForestModel.py
Browse files Browse the repository at this point in the history
Improving the test file by changing the way previpous predictions are stored.
  • Loading branch information
humbleOldSage committed Dec 13, 2023
1 parent f5fec64 commit 585cc90
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions emission/tests/modellingTests/TestForestModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,38 @@ def testRandomForestRegression(self):

## predictions take the form like :
#
#{'labels': {'mode_confirm': 'ebike', 'replaced_mode': 'walk', 'purpose_confirm': 'dog-park'}, 'p': 1.0}
# we can store these predictions in a json and then for every run other than the first we
# can load the predictions and compare

try:
if os.path.exists(file_path) and os.path.getsize(file_path)>0:
with open(file_path, 'r') as f:
prev_predictions_list = json.load(f)
logging.debug()
self.assertEqual(prev_predictions_list,curr_predictions_list," previous predictions should match current predictions")
else:
with open(file_path,'w') as file:
json.dump(curr_predictions_list,file,indent=4)
logging.debug("Previous predicitons stored for future matching" )
except json.JSONDecodeError:
logging.debug("jsonDecodeErrorError")
return " decoding JSON."
#{'labels': {'mode_confirm': 'ebike', 'replaced_mode': 'walk', 'purpose_confirm': 'dog-park'}, 'p': 1.0}

#Below are two ways we can store prev. predictions list . Whichever way we finalise, I'll delete the other one.
#
#Method 1 : Run predictions for the first time and hardcode them into
#prev_prdictions_list. For every iteration, simply compare them
#
# for the current data that we read from json, the predictions we get is an empty list. If
# we use a different file with more data, this'll take the for as mentioned above
#
prev_predictions_list= [
(
[],
-1
)
]

self.assertEqual(prev_predictions_list,curr_predictions_list," previous predictions should match current predictions")


#Method 2 ( which was failing): Store these predictions into a json and read from
#that json
#
# try:
# if os.path.exists(file_path) and os.path.getsize(file_path)>0:
# with open(file_path, 'r') as f:
# prev_predictions_list = json.load(f)
# logging.debug()
# self.assertEqual(prev_predictions_list,curr_predictions_list," previous predictions should match current predictions")
# else:
# with open(file_path,'w') as file:
# json.dump(curr_predictions_list,file,indent=4)
# logging.debug("Previous predicitons stored for future matching" )
# except json.JSONDecodeError:
# logging.debug("jsonDecodeErrorError")

0 comments on commit 585cc90

Please sign in to comment.