-
Notifications
You must be signed in to change notification settings - Fork 13
/
jobtable.py
243 lines (211 loc) · 8.64 KB
/
jobtable.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
"""
The Jobtable module provides a set of top level functions to interact with the database.
"""
import numpy as np
from pyiron_base.database.filetable import FileTable
__author__ = "Jan Janssen"
__copyright__ = (
"Copyright 2020, Max-Planck-Institut für Eisenforschung GmbH - "
"Computational Materials Design (CM) Department"
)
__version__ = "1.0"
__maintainer__ = "Jan Janssen"
__email__ = "janssen@mpie.de"
__status__ = "production"
__date__ = "Sep 1, 2017"
def get_jobs(database, sql_query, user, project_path, recursive=True, columns=None):
"""
Internal function to return the jobs as dictionary rather than a pandas.Dataframe
Args:
database (DatabaseAccess): Database object
sql_query (str): SQL query to enter a more specific request
user (str): username of the user whoes user space should be searched
project_path (str): root_path - this is in contrast to the project_path in GenericPath
recursive (bool): search subprojects [True/False]
columns (list): by default only the columns ['id', 'project'] are selected, but the user can select a subset
of ['id', 'status', 'chemicalformula', 'job', 'subjob', 'project', 'projectpath', 'timestart',
'timestop', 'totalcputime', 'computer', 'hamilton', 'hamversion', 'parentid', 'masterid']
Returns:
dict: columns are used as keys and point to a list of the corresponding values
"""
if not isinstance(database, FileTable):
if columns is None:
columns = ["id", "project"]
df = database.job_table(
sql_query=sql_query,
user=user,
project_path=project_path,
recursive=recursive,
columns=columns,
)
if len(df) == 0:
return {key: list() for key in columns}
return df.to_dict(orient="list")
else:
return database.get_jobs(
project=project_path, recursive=recursive, columns=columns
)
def get_job_ids(database, sql_query, user, project_path, recursive=True):
"""
Return the job IDs matching a specific query
Args:
database (DatabaseAccess): Database object
sql_query (str): SQL query to enter a more specific request
user (str): username of the user whoes user space should be searched
project_path (str): root_path - this is in contrast to the project_path in GenericPath
recursive (bool): search subprojects [True/False]
Returns:
list: a list of job IDs
"""
if not isinstance(database, FileTable):
return get_jobs(
database=database,
sql_query=sql_query,
user=user,
project_path=project_path,
recursive=recursive,
columns=["id"],
)["id"]
else:
return database.get_job_ids(project=project_path, recursive=recursive)
def get_child_ids(database, sql_query, user, project_path, job_specifier, status=None):
"""
Get the childs for a specific job
Args:
database (DatabaseAccess): Database object
sql_query (str): SQL query to enter a more specific request
user (str): username of the user whoes user space should be searched
project_path (str): root_path - this is in contrast to the project_path in GenericPath
job_specifier (str): name of the master job or the master jobs job ID
status (str): filter childs which match a specific status - None by default
Returns:
list: list of child IDs
"""
if not isinstance(database, FileTable):
id_master = get_job_id(database, sql_query, user, project_path, job_specifier)
if id_master is None:
return []
else:
search_dict = {"masterid": str(id_master)}
if status is not None:
search_dict["status"] = status
return sorted(
[
job["id"]
for job in database.get_items_dict(
search_dict, return_all_columns=False
)
]
)
else:
return database.get_child_ids(job_specifier=job_specifier, project=project_path)
def get_job_id(database, sql_query, user, project_path, job_specifier):
"""
get the job_id for job named job_name in the local project path from database
Args:
database (DatabaseAccess): Database object
sql_query (str): SQL query to enter a more specific request
user (str): username of the user whoes user space should be searched
project_path (str): root_path - this is in contrast to the project_path in GenericPath
job_specifier (str): name of the job or job ID
Returns:
int: job ID of the job
"""
if not isinstance(database, FileTable):
if isinstance(job_specifier, (int, np.integer)):
return job_specifier # is id
job_dict = database._job_dict(
sql_query=sql_query,
user=user,
project_path=project_path,
recursive=False,
job=job_specifier,
)
if len(job_dict) == 0:
job_dict = database._job_dict(
sql_query=sql_query,
user=user,
project_path=project_path,
recursive=True,
job=job_specifier,
)
if len(job_dict) == 0:
return None
elif len(job_dict) == 1:
return job_dict[0]["id"]
else:
raise ValueError(
"job name '{0}' in this project '{1}' is not unique '{2}".format(
job_specifier, project_path, job_dict
)
)
else:
return database.get_job_id(job_specifier=job_specifier, project=project_path)
def set_job_status(database, sql_query, user, project_path, job_specifier, status):
"""
Set the status of a particular job
Args:
database (DatabaseAccess/ FileTable): Database object
sql_query (str): SQL query to enter a more specific request
user (str): username of the user whoes user space should be searched
project_path (str): root_path - this is in contrast to the project_path in GenericPath
job_specifier (str): name of the job or job ID
status (str): job status can be one of the following ['initialized', 'appended', 'created', 'submitted',
'running', 'aborted', 'collect', 'suspended', 'refresh', 'busy', 'finished']
"""
database.set_job_status(
job_id=get_job_id(
database=database,
sql_query=sql_query,
user=user,
project_path=project_path,
job_specifier=job_specifier,
),
status=status,
)
def get_job_status(database, sql_query, user, project_path, job_specifier):
"""
Get the status of a particular job
Args:
database (DatabaseAccess): Database object
sql_query (str): SQL query to enter a more specific request
user (str): username of the user whoes user space should be searched
project_path (str): root_path - this is in contrast to the project_path in GenericPath
job_specifier (str): name of the job or job ID
Returns:
str: job status can be one of the following ['initialized', 'appended', 'created', 'submitted', 'running',
'aborted', 'collect', 'suspended', 'refresh', 'busy', 'finished']
"""
return database.get_job_status(
job_id=get_job_id(
database=database,
sql_query=sql_query,
user=user,
project_path=project_path,
job_specifier=job_specifier,
)
)
def get_job_working_directory(database, sql_query, user, project_path, job_specifier):
"""
Get the working directory of a particular job
Args:
database (DatabaseAccess): Database object
sql_query (str): SQL query to enter a more specific request
user (str): username of the user whoes user space should be searched
project_path (str): root_path - this is in contrast to the project_path in GenericPath
job_specifier (str): name of the job or job ID
Returns:
str: working directory as absolute path
"""
return database.get_job_working_directory(
job_id=get_job_id(
database=database,
sql_query=sql_query,
user=user,
project_path=project_path,
job_specifier=job_specifier,
)
)