-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Module ramp rate to google sheet #14868
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
abr-testing/abr_testing/data_collection/module_ramp_rates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
"""Get ramp rates of modules.""" | ||
from abr_testing.automation import google_sheets_tool | ||
from abr_testing.data_collection import read_robot_logs | ||
import gspread # type: ignore[import] | ||
import argparse | ||
import os | ||
import sys | ||
import json | ||
from datetime import datetime | ||
from typing import Dict, Any | ||
import requests | ||
|
||
|
||
def ramp_rate(file_results: Dict[str, Any]) -> Dict[int, float]: | ||
"""Get ramp rates.""" | ||
i = 0 | ||
commands = file_results["commands"] | ||
for command in commands: | ||
commandType = command["commandType"] | ||
if ( | ||
commandType == "thermocycler/setTargetBlockTemperature" | ||
or commandType == "temperatureModule/setTargetTemperature" | ||
or commandType == "heaterShaker/setTargetTemperature" | ||
): | ||
temp = command["params"].get("celsius", 0.0) | ||
if ( | ||
commandType == "thermocycler/waitForBlockTemperature" | ||
or commandType == "temperatureModule/waitForTemperature" | ||
or commandType == "heaterShaker/waitForTemperature" | ||
): | ||
start_time = datetime.strptime( | ||
command.get("startedAt", ""), "%Y-%m-%dT%H:%M:%S.%f%z" | ||
) | ||
end_time = datetime.strptime( | ||
command.get("completedAt", ""), "%Y-%m-%dT%H:%M:%S.%f%z" | ||
) | ||
duration = (end_time - start_time).total_seconds() | ||
i += 1 | ||
temps_and_durations[duration] = temp | ||
ramp_rates = {} | ||
times = list(temps_and_durations.keys()) | ||
for i in range(len(times) - 1): | ||
time1 = times[i] | ||
time2 = times[i + 1] | ||
temp1 = temps_and_durations[time1] | ||
temp2 = temps_and_durations[time2] | ||
ramp_rate = (temp2 - temp1) / (time2) | ||
ramp_rates[i] = ramp_rate | ||
return ramp_rates | ||
|
||
|
||
if __name__ == "__main__": | ||
# SCRIPT ARGUMENTS | ||
parser = argparse.ArgumentParser(description="Read run logs on google drive.") | ||
parser.add_argument( | ||
"storage_directory", | ||
metavar="STORAGE_DIRECTORY", | ||
type=str, | ||
nargs=1, | ||
help="Path to long term storage directory for run logs.", | ||
) | ||
parser.add_argument( | ||
"google_sheet_name", | ||
metavar="GOOGLE_SHEET_NAME", | ||
type=str, | ||
nargs=1, | ||
help="Google sheet name.", | ||
) | ||
parser.add_argument( | ||
"email", metavar="EMAIL", type=str, nargs=1, help="opentrons gmail." | ||
) | ||
args = parser.parse_args() | ||
storage_directory = args.storage_directory[0] | ||
google_sheet_name = args.google_sheet_name[0] | ||
# FIND CREDENTIALS FILE | ||
try: | ||
credentials_path = os.path.join(storage_directory, "credentials.json") | ||
except FileNotFoundError: | ||
print(f"Add credentials.json file to: {storage_directory}.") | ||
sys.exit() | ||
# CONNECT TO GOOGLE SHEET | ||
try: | ||
google_sheet = google_sheets_tool.google_sheet( | ||
credentials_path, google_sheet_name, 1 | ||
) | ||
print(f"Connected to google sheet: {google_sheet_name}") | ||
except gspread.exceptions.APIError: | ||
print("ERROR: Check google sheet name. Check credentials file.") | ||
sys.exit() | ||
run_ids_on_sheet = google_sheet.get_column(2) | ||
runs_and_robots = {} | ||
for filename in os.listdir(storage_directory): | ||
file_path = os.path.join(storage_directory, filename) | ||
if file_path.endswith(".json"): | ||
with open(file_path) as file: | ||
file_results = json.load(file) | ||
else: | ||
continue | ||
# CHECK if file is ramp rate run | ||
run_id = file_results.get("run_id", None) | ||
temps_and_durations: Dict[float, float] = dict() | ||
if run_id is not None and run_id not in run_ids_on_sheet: | ||
|
||
ramp_rates = ramp_rate(file_results) | ||
protocol_name = file_results["protocol"]["metadata"].get("protocolName", "") | ||
if "Ramp Rate" in protocol_name: | ||
ip = filename.split("_")[0] | ||
if len(ramp_rates) > 1: | ||
cooling_ramp_rate = abs(min(ramp_rates.values())) | ||
heating_ramp_rate = abs(max(ramp_rates.values())) | ||
start_time = datetime.strptime( | ||
file_results.get("startedAt", ""), "%Y-%m-%dT%H:%M:%S.%f%z" | ||
) | ||
start_date = str(start_time.date()) | ||
module_serial_number = file_results["modules"][0].get( | ||
"serialNumber", "NaN" | ||
) | ||
try: | ||
response = requests.get( | ||
f"http://{ip}:31950/modules", | ||
headers={"opentrons-version": "3"}, | ||
) | ||
modules = response.json() | ||
for module in modules["data"]: | ||
if module["serialNumber"] == module_serial_number: | ||
firmwareVersion = module["firmwareVersion"] | ||
else: | ||
firmwareVersion = "NaN" | ||
except requests.exceptions.ConnectionError: | ||
firmwareVersion = "NaN" | ||
row = { | ||
"Robot": file_results.get("robot_name", ""), | ||
"Run_ID": run_id, | ||
"Protocol_Name": file_results["protocol"]["metadata"].get( | ||
"protocolName", "" | ||
), | ||
"Software Version": file_results.get("API_Version", ""), | ||
"Firmware Version": firmwareVersion, | ||
"Date": start_date, | ||
"Serial Number": module_serial_number, | ||
"Approx. Average Heating Ramp Rate (C/s)": heating_ramp_rate, | ||
"Approx. Average Cooling Ramp Rate (C/s)": cooling_ramp_rate, | ||
} | ||
headers = list(row.keys()) | ||
runs_and_robots[run_id] = row | ||
read_robot_logs.write_to_local_and_google_sheet( | ||
runs_and_robots, | ||
storage_directory, | ||
google_sheet_name, | ||
google_sheet, | ||
headers, | ||
) | ||
else: | ||
continue |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with something like this, it's usually a good idea to split out these checks and the code inside them by creating some boundary where
A common way to do it would be to have a function that takes a dict and only a dict - and doesn't check - and returns some data; and before calling the function you check that it's a dict.
Here though a good intermediate step would be to invert the logic to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha that makes sense.