-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor.py
executable file
·186 lines (167 loc) · 8.02 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/groups/scicompsoft/home/ackermand/miniconda3/envs/annotator-metrics/bin/python
import os
import re
import time
import smtplib
from email.mime.text import MIMEText
import datetime
import glob
import yaml
from yaml.loader import SafeLoader
import json
class Monitor:
def __init__(self):
self.base_dir = (
"/groups/cellmap/cellmap/annotation_and_analytics/analysis/submission"
)
self.previous_submission_time = int(
os.popen(f"tail -n 1 {self.base_dir}/submission_times.txt").read()
)
self.current_time = int(time.time())
self.groups_to_info_dict = {}
with open(
"/groups/scicompsoft/home/ackermand/Programming/annotator-metrics/email_info.json"
) as f:
email_info = json.load(f)
self.gmail_user = email_info["gmail_user"]
self.gmail_application_password = email_info["gmail_application_password"]
self.recipients = email_info["recipients"]
def get_modified_file_info(self):
seconds_since_submission = self.current_time - self.previous_submission_time
annotations_added_since_last_submission = []
for annotation_dir_to_check in [
"/groups/cellmap/cellmap/annotation_and_analytics/training/",
"/groups/cellmap/cellmap/annotation_and_analytics/supplemental_variability_crops/variability_crops_personal",
]:
current_annotations_added_since_last_submssion = os.popen(
f'find "{annotation_dir_to_check}" -type f -name "*.tif" -newermt -"{seconds_since_submission} seconds"'
).read()
annotations_added_since_last_submission += current_annotations_added_since_last_submssion.split(
"\n"
)
non_annotator_dirs = [
"paintera templates",
"conversion_scripts",
"textfile_templates",
]
for annotation in annotations_added_since_last_submission:
if (
all(
[
non_annotator_dir not in annotation
for non_annotator_dir in non_annotator_dirs
]
)
and annotation
):
if "supplemental_variability_crops" not in annotation:
user_and_group = (
annotation.split("-labels")[0].split("training/")[-1].split("/")
)
user = user_and_group[0]
group = user_and_group[1]
if re.search(
f"{group}-labels/{group}_.*/{group}_*_.*\.tif", annotation
): # then is likely an actual annotation
if group not in self.groups_to_info_dict:
self.groups_to_info_dict[group] = {user: [annotation]}
else:
if user not in self.groups_to_info_dict[group]:
self.groups_to_info_dict[group][user] = [annotation]
else:
self.groups_to_info_dict[group][user].append(annotation)
else:
user_and_group = (
annotation.split("group.*_crop")[0]
.split("variability_crops_personal/")[-1]
.split("/")
)
user = user_and_group[0]
group = user_and_group[1]
if re.search(
f"{group}/{group}_crop.*/{group}_*_.*\.tif", annotation
): # then is likely an actual annotation
if group not in self.groups_to_info_dict:
self.groups_to_info_dict[group] = {user: [annotation]}
else:
if user not in self.groups_to_info_dict[group]:
self.groups_to_info_dict[group][user] = [annotation]
else:
self.groups_to_info_dict[group][user].append(annotation)
def submit_jobs(self):
groups = self.groups_to_info_dict.keys()
if groups:
# Then we need to submit
groups = ",".join(groups)
os.system(
f"echo {self.current_time} >> {self.base_dir}/submission_times.txt"
)
self.current_time = int(time.time())
new_config = (
f"{self.base_dir}/submission_configs/lsf-config-{self.current_time}"
)
os.system(f"cp -r {self.base_dir}/lsf-config-template {new_config}")
os.system(
f"sed -i 's/template_group/\"{groups}\"/g' {new_config}/run-config.yaml"
)
os.system(f"bsub -n 2 -P cellmap annotator-metrics {new_config} -n 48")
monitor_new_annotations.send_email("submitted", new_config)
def monitor_job_status(self):
with open(f"{self.base_dir}/completed_submissions.txt") as f:
completed_submissions = f.read()
submission_configs = glob.glob(
f"{self.base_dir}/submission_configs/lsf-config-*-*"
)
for submission_config in submission_configs:
if submission_config not in completed_submissions:
with open(f"{submission_config}/output.log") as f:
log = f.read()
if "Calculations completed successfully!" in log:
self.send_email("completed", submission_config)
os.system(
f"echo {submission_config} >> {self.base_dir}/completed_submissions.txt"
)
elif "Calculations failed!" in log:
self.send_email("failed", submission_config)
os.system(
f"echo {submission_config} >> {self.base_dir}/completed_submissions.txt"
)
def send_email(self, status, config_path):
dt = datetime.datetime.fromtimestamp(self.previous_submission_time)
if status == "submitted":
dt = datetime.datetime.fromtimestamp(self.previous_submission_time)
body = f"You are receiving this email because the following annotations have been added/modified since {dt}:\n"
for group, users_dict in self.groups_to_info_dict.items():
body += f"\n{group}:\n"
for user, annotations in users_dict.items():
body += f"\t{user}:\n"
for annotation in annotations:
body += f"\t\t{annotation}\n"
body += "\n"
body += f"The job has been submitted to the cluster using the config located at {config_path}"
msg = MIMEText(body)
msg["Subject"] = "Annotator Metrics Analysis Submission"
else:
dt = datetime.datetime.fromtimestamp(int(config_path.split("-")[2]))
with open(f"{config_path}/run-config.yaml") as f:
groups = yaml.load(f, Loader=SafeLoader)["required_settings"][
"group"
].split(",")
msg = MIMEText(
f"""You are receiving this email because the following analysis submitted {dt} has {status} for {groups}: {config_path}"""
)
msg["Subject"] = f"Annotator Metrics Analysis {status.capitalize()}!"
sender = self.gmail_user
msg["From"] = sender
msg["To"] = ", ".join(self.recipients)
s = smtplib.SMTP_SSL("smtp.gmail.com", 465)
s.ehlo()
s.login(self.gmail_user, self.gmail_application_password)
s.sendmail(sender, self.recipients, msg.as_string())
if __name__ == "__main__":
monitor_new_annotations = Monitor()
monitor_new_annotations.get_modified_file_info()
monitor_new_annotations.submit_jobs()
monitor_new_annotations.monitor_job_status()
# submit
# copy lsf-config over...instead get datetime of submission directory in case submission might take some time