Skip to content

Commit

Permalink
Merge pull request #350 from Kvieta1990/master
Browse files Browse the repository at this point in the history
Limix max number of runs for MTS routine
  • Loading branch information
Kvieta1990 authored May 19, 2022
2 parents bc15636 + ad8c313 commit 86c63af
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
25 changes: 22 additions & 3 deletions addie/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from addie.utilities.job_monitor_thread import JobMonitorThread
from addie.utilities.job_status_handler import JobStatusHandler
from addie.utilities.job_status_handler import JobStatusHandlerMTS
from addie.utilities.logbook_thread import LogbookThread
from addie.utilities.logbook_handler import LogbookHandler

Expand Down Expand Up @@ -490,13 +491,31 @@ def launch_job_manager(
thread_index=thread_index)
job_handler.start()

# job utility for mantidtotalscattering
def launch_job_manager_mts(
self,
job_name='',
all_commands=None,
thread_index=-1):
job_handler = JobStatusHandlerMTS(parent=self, job_name=job_name,
all_commands=all_commands,
thread_index=thread_index)
job_handler.start()

def kill_job(self, row=-1):
job_row = self.job_list[row]
parent = psutil.Process(job_row['pid'])
for child in parent.children(recursive=True):
child.kill()
# child.wait()
parent.kill()
if child.status != psutil.STATUS_ZOMBIE:
for count, item in enumerate(self.job_list):
job_row_tmp = item
if job_row_tmp['pid'] == child.pid:
job_row_tmp['status'] = "killed"
job_row_tmp['pid'] = None
self.job_list[count] = job_row_tmp
child.kill()
if parent.name() != 'addie':
parent.kill()

table_widget = self.job_monitor_interface.ui.tableWidget
table_widget.removeCellWidget(row, 2)
Expand Down
34 changes: 31 additions & 3 deletions addie/processing/mantid/launch_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def run_mantid(parent):
full_reduction_filename = os.path.join(
os.path.expanduser('~'), '.mantid', 'addie.json')
print('writing out full table to "{}"'.format(full_reduction_filename))
all_commands = list()
all_files = list()
for row in range(num_rows):
dictionary, activate = exporter.retrieve_row_info(row)
if activate is True:
Expand Down Expand Up @@ -119,12 +121,36 @@ def run_mantid(parent):
"Container": container_type}

if final_validator(dict_out_tmp):
filename_to_run = os.path.join(os.path.expanduser('~'), '.mantid', 'JSON_output', 'running_tmp.json')
filename_to_run = os.path.join(os.path.expanduser('~'),
'.mantid',
'JSON_output',
'running_tmp_' + str(row) + '.json')
with open(filename_to_run, 'w') as outfile:
json.dump(dict_out_tmp, outfile, indent=2)
_script_to_run = "bash /SNS/NOM/shared/scripts/mantidtotalscattering/run_mts.sh " + filename_to_run
parent.launch_job_manager(job_name='MantidTotalScattering',
script_to_run=_script_to_run)
all_commands.append(_script_to_run.split())
all_files.append(filename_to_run)

limit = 4

if len(all_commands) > limit:
all_commands = list()
num_runs = len(all_files)
chunk_size = num_runs // limit
left_over = num_runs - (limit - 1) * chunk_size
for i in range(limit):
command_tmp = ""
if i < limit - 1:
for j in range(i * chunk_size, (i + 1) * chunk_size):
command_tmp += (" " + all_files[j])
else:
for j in range(left_over):
command_tmp += (" " + all_files[i * chunk_size + j])
command_tmp = "bash /SNS/NOM/shared/scripts/mantidtotalscattering/run_mts_all.sh" + command_tmp
all_commands.append(command_tmp.split())

parent.launch_job_manager_mts(job_name='MantidTotalScattering',
all_commands=all_commands)


def log_error(type_err, key):
Expand Down Expand Up @@ -373,3 +399,5 @@ def final_validator(final_dict):
else:
log_error(1, '["Merging"]["QBinning"]')
return False

return True
4 changes: 2 additions & 2 deletions addie/utilities/job_monitor_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _checking_status_of_jobs(self):
_item = QTableWidgetItem("Done!")
self.job_monitor_interafce.ui.tableWidget.setItem(_row, 2, _item)
else:
if not process.status() == 'sleeping':
if _job['status'] == 'killed':
self.job_monitor_interafce.ui.tableWidget.removeCellWidget(_row, 2)
_item = QTableWidgetItem("Done!")
_item = QTableWidgetItem("Killed!")
self.job_monitor_interafce.ui.tableWidget.setItem(_row, 2, _item)
51 changes: 51 additions & 0 deletions addie/utilities/job_status_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,54 @@ def get_launch_time(self):

def start(self):
pass


class JobStatusHandlerMTS(object):

def __init__(self, parent=None, job_name='', all_commands=None, thread_index=-1):
self.parent = parent

if self.parent.job_monitor_interface is None:
job_ui = JobMonitorInterface(parent=self.parent)
job_ui.show()
QApplication.processEvents()
self.parent.job_monitor_interface = job_ui
job_ui.launch_logbook_thread()
else:
self.parent.job_monitor_interface.activateWindow()
job_ui = self.parent.job_monitor_interface

if job_name == '':
return

job_list = self.parent.job_list

for cmd in all_commands:
p = subprocess.Popen(cmd)

new_job = {'job_name': job_name,
'time': self.get_launch_time(),
'status': 'processing',
'pid': p.pid,
'subprocess': p}
job_list.append(new_job)
self.parent.job_list = job_list

job_ui.refresh_table(job_list)

def update_logbook_text(self, text):
print(text)

def get_local_time(self):
local_hour_offset = time.timezone / 3600.
_gmt_time = time.gmtime()
[year, month, day, hour, minute, seconds, _wday, _yday, _isds] = _gmt_time
return [year, month, day, hour-local_hour_offset, minute, seconds]

def get_launch_time(self):
local_time = self.get_local_time()
return "%d %d %d %d:%d:%d" % (local_time[0], local_time[1], local_time[2],
local_time[3], local_time[4], local_time[5])

def start(self):
pass

0 comments on commit 86c63af

Please sign in to comment.