Skip to content

Commit

Permalink
Merge pull request #344 from Kvieta1990/master
Browse files Browse the repository at this point in the history
Major update to Mantid interface
  • Loading branch information
Kvieta1990 authored Feb 23, 2022
2 parents 071890b + e2ccfca commit 49f3314
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 277 deletions.
1 change: 0 additions & 1 deletion addie/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"max": 40,
"delta": 0.02
},
"reduction_configuration_file": "",
"characterization_file": ""
},
"bragg": {
Expand Down
3 changes: 3 additions & 0 deletions addie/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class MainWindow(QMainWindow):
intermediate_grouping = copy.deepcopy(grouping_dict)
output_grouping = copy.deepcopy(grouping_dict)

advanced_dict = {'push_bkg': False,
'ele_size': "1.0"}

statusbar_display_time = 5000 # 5s

# external ui (use to make sure there is only one open at a time
Expand Down
137 changes: 54 additions & 83 deletions addie/processing/mantid/launch_reduction.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,65 +1,8 @@
import os
import traceback
from mantidqt.utils.asynchronous import AsyncTask
import json

from addie.processing.mantid.master_table.master_table_exporter import TableFileExporter as MantidTableExporter

# Mantid Total Scattering integration
# (https://github.com/neutrons/mantid_total_scattering)
try:
import total_scattering
print("Mantid Total Scattering Version: ", total_scattering.__version__)
from total_scattering.reduction.total_scattering_reduction import TotalScatteringReduction
MANTID_TS_ENABLED = True
except ImportError:
print('total_scattering module not found. Functionality disabled')
MANTID_TS_ENABLED = False


class JobPool(object):
task_output = None,
running = None
task_exc_type, task_exc, task_exc_stack = None, None, None

def __init__(self, configurations):
self.jobs = []
for config in configurations:
print("CONFIG:", config)
self.jobs.append(AsyncTask(TotalScatteringReduction, args=(config,),
success_cb=self.on_success, error_cb=self.on_error,
finished_cb=self.on_finished))

def _start_next(self):
if self.jobs:
self.running = self.jobs.pop(0)
self.running.start()
else:
self.running = None

def start(self):
if not self.jobs:
raise RuntimeError('Cannot start empty job list')
self._start_next()

def on_success(self, task_result):
# TODO should emit a signal
self.task_output = task_result.output
print('SUCCESS!!! {}'.format(self.task_output))

def on_error(self, task_result):
# TODO should emit a signal
print('ERROR!!!')
self.task_exc_type = task_result.exc_type
self.task_exc = task_result.exc_value
self.task_exc_stack = traceback.extract_tb(task_result.stack)
traceback.print_tb(task_result.stack)
print(task_result)

def on_finished(self):
'''Both success and failure call this method afterwards'''
# TODO should emit a signal
self._start_next() # kick off the next one in the pool


def run_mantid(parent):
num_rows = parent.processing_ui.h3_table.rowCount()
Expand Down Expand Up @@ -87,28 +30,56 @@ def run_mantid(parent):
filename = os.path.join(os.path.expanduser('~'),'.mantid' ,'JSON_output',dictionary['Title'] +'_'+ str(row) + '.json')
exporter.export(filename,row)
print("Row",row,"Successfully output to",filename)

# append the individual rows to input list (reduction_inputs)
reduction_inputs = []
for row in range(num_rows):
if not exporter.isActive(row):
print('skipping row {} - inactive'.format(row + 1)) # REMOVE?
continue
print('Will be running row {} for reduction'.format(row + 1)) # TODO should be debug logging
json_input = exporter.retrieve_row_info(row)[0]
reduction_input = exporter.convert_from_row_to_reduction(json_input)
if not reduction_input:
return
reduction_inputs.append(reduction_input)
if len(reduction_inputs) == 0:
print('None of the rows were activated')
return

# locate total scattering script
if MANTID_TS_ENABLED:
pool = JobPool(reduction_inputs)
pool.start()
else:
# TODO should be on the status bar
print('total_scattering module not found. Functionality disabled')
return
with open(filename) as json_file:
data_tmp = json.load(json_file)
dict_out_tmp = {}
container_type=""
for key, item in data_tmp.items():
if "Sample" in key:
sample_tmp = {}
for key_s, item_s in item.items():
if "Density" not in key_s:
if "Material" in key_s:
string_tmp = item_s.replace("(", "").replace(")", "")
sample_tmp[key_s] = string_tmp
else:
sample_tmp[key_s] = item_s
if "Geometry" in key_s:
known_shape = ["PAC03", "PAC06", "PAC08",
"PAC10", "QuartzTube03"]
if item_s["Shape"] in known_shape:
shape_tmp = "Cylinder"
container_type = item_s["Shape"]
else:
shape_tmp = item_s["Shape"]
geo_dict_tmp = {}
for key_tmp in item_s:
geo_dict_tmp[key_tmp] = item_s[key_tmp]
geo_dict_tmp["Shape"] = shape_tmp
sample_tmp[key_s] = geo_dict_tmp
else:
sample_tmp["MassDensity"] = float(item_s["MassDensity"])
dict_out_tmp[key] = sample_tmp
elif "Normalization" in key or "Normalisation" in key:
van_tmp = {}
for key_v, item_v in item.items():
if "Density" not in key_v:
if "Material" in key_v:
string_tmp = item_v.replace("(", "").replace(")", "")
van_tmp[key_v] = string_tmp
else:
van_tmp[key_v] = item_v
else:
van_tmp["MassDensity"] = float(item_v["MassDensity"])
dict_out_tmp[key] = van_tmp
else:
dict_out_tmp[key] = item
if container_type:
dict_out_tmp["Environment"] = { "Name": "InAir",
"Container": container_type}
filename_to_run = os.path.join(os.path.expanduser('~'),'.mantid' ,'JSON_output', 'running_tmp.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)
113 changes: 34 additions & 79 deletions addie/processing/mantid/make_calibration_handler/make_calibration.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from qtpy.QtWidgets import (QMainWindow, QComboBox, QFileDialog, QHBoxLayout, QLabel, QDateEdit, QLineEdit, QPushButton,
QTableWidgetItem, QVBoxLayout, QWidget)
from addie.utilities import load_ui
from addie.widgets.filedialog import get_save_file
from qtpy import QtCore

import datetime
Expand Down Expand Up @@ -34,7 +33,7 @@ def __init__(self, parent=None):

class MakeCalibrationWindow(QMainWindow):

table_column_width = [60, 250, 350, 350, 90, 300]
table_column_width = [60, 250, 600, 90, 300]
table_row_height = 85
entry_level = 0

Expand All @@ -49,9 +48,6 @@ class MakeCalibrationWindow(QMainWindow):
"calibration_value",
"calibration_browser",
"calibration_browser_value",
"vanadium_value",
"vanadium_browser",
"vanadium_browser_value",
"date",
"output_dir_browser",
"output_dir_value",
Expand Down Expand Up @@ -175,17 +171,6 @@ def get_run_number_from_nexus_file_name(self, base_nexus_name=''):
return result.group(1)
return None

def vanadium_browser_clicked(self, entry=""):
sample_type = "Vanadium"
value_ui = self.master_list_ui[entry].vanadium_browser_value

[_file, run_number] = self._general_browser_clicked(sample_type=sample_type,
value_ui=value_ui)
if _file:
self.master_list_value[entry]["vanadium_browser"] = _file
self.master_list_ui[entry].vanadium_value.setText(str(run_number))
self.check_run_calibration_status()

def calibration_browser_clicked(self, entry=""):
sample_type = "Calibration"
value_ui = self.master_list_ui[entry].calibration_browser_value
Expand Down Expand Up @@ -273,41 +258,14 @@ def __insert_new_row(self, row=-1):
col1_widget.setLayout(verti_layout)
self.ui.tableWidget.setCellWidget(row, col, col1_widget)

# new column - Vanadium
col = 3
# first row
# first row
label = QLabel("Run #:")
vana_value = QLineEdit("")
vana_value.returnPressed.connect(lambda entry=_name: self.run_entered(entry))
vana_browser_button = QPushButton("Browse...")
vana_browser_button.setMinimumWidth(button_width)
vana_browser_button.setMaximumWidth(button_width)
vana_browser_button.clicked.connect(lambda state, entry=_name: self.vanadium_browser_clicked(entry))
first_row = QHBoxLayout()
first_row.addWidget(label)
first_row.addWidget(vana_value)
first_row.addWidget(vana_browser_button)
first_row_widget = QWidget()
first_row_widget.setLayout(first_row)
# second row
vana_browser_button_value = QLabel("N/A")

verti_layout = QVBoxLayout()
verti_layout.addWidget(first_row_widget)
verti_layout.addWidget(vana_browser_button_value)
col1_widget = QWidget()
col1_widget.setLayout(verti_layout)
self.ui.tableWidget.setCellWidget(row, col, col1_widget)

# new column - date
col = 4
col = 3
date = QDateEdit()
date.setDate(self.master_date_value)
self.ui.tableWidget.setCellWidget(row, col, date)

# new column - output dir
col = 5
col = 4
browser_button = QPushButton("Browse...")
browser_button.setMinimumWidth(button_width)
browser_button.setMaximumWidth(button_width)
Expand All @@ -329,18 +287,13 @@ def __insert_new_row(self, row=-1):
calibration_value=cali_value,
calibration_browser=cali_browser_button,
calibration_browser_value=cali_browser_button_value,
vanadium_value=vana_value,
vanadium_browser=vana_browser_button,
vanadium_browser_value=vana_browser_button_value,
date=date,
output_dir_browser=browser_button,
output_dir_value=browser_value,
output_reset=reset)
self.master_list_ui[_name] = list_local_ui

list_local_name = dict(vanadium_run_number="",
vanadium_browser="",
calibration_run_number="",
list_local_name = dict(calibration_run_number="",
calibration_browser="")
self.master_list_value[_name] = list_local_name

Expand All @@ -359,10 +312,6 @@ def _check_status_of_row(self, row=-1):
if not self._check_local_column(run_value = local_list_ui.calibration_value):
return False

# Vanadium column
if not self._check_local_column(run_value=local_list_ui.vanadium_value):
return False

# output dir
browse_label = local_list_ui.output_dir_value
if browse_label.text() == 'N/A':
Expand Down Expand Up @@ -395,15 +344,37 @@ def check_run_calibration_status(self):
self.ui.run_calibration_button.setEnabled(_status)

def run_calibration_button_clicked(self):
# make dictionary of all infos
instr_dict = {"NOM": "NOMAD",
"PG3": "PG3}"}
o_dict = MakeCalibrationDictionary(parent=self)
_file, _ = get_save_file(parent=self,
caption="Select where and name of json file to create...",
# directory = '/SNS/users/ntm/',
filter={'json (*.json)':'json'})
if _file:
with open(_file, 'w') as fp:
simplejson.dump(o_dict.dictionary, fp, indent=2, ignore_nan=True)
for calibrant in o_dict.dictionary['Calibrants'].keys():
calib_tmp_dict = o_dict.dictionary['Calibrants'][calibrant]
calib_file = calib_tmp_dict['Filename']
calib_date = calib_tmp_dict['Date'].replace("_", "-")
calib_senv = calib_tmp_dict['SampleEnvironment']
calib_outd = calib_tmp_dict['CalDirectory']
if "/" in calib_file:
instrument_name = calib_file.split("/")[2]
else:
instrument_name = calib_file.split("_")[0]
calib_control_file = os.path.join('/SNS/', instrument_name,
'shared/CALIBRATION/Group_calib_scripts',
'control_' + calibrant + ".dat")
with open(calib_control_file, "w") as calib_f:
calib_f.write("{0:<25s}:: {1:s}\n".format("diamond file", calib_file))
calib_f.write("{0:<25s}:: {1:s}\n".format("instrument",
instr_dict[instrument_name]))
calib_f.write("{0:<25s}:: {1:s}\n".format("date", calib_date))
calib_f.write("{0:<25s}:: {1:s}\n".format("sample environment",
calib_senv))
calib_f.write("{0:<25s}:: {1:s}\n".format("output directory",
calib_outd))
running_script = os.path.join('/SNS/', instrument_name,
'shared/CALIBRATION/Group_calib_scripts',
'running')
running_script += (" " + calib_control_file)
self.parent.launch_job_manager(job_name='MakeCalibration',
script_to_run=running_script)

def closeEvent(self, c):
self.parent.make_calibration_ui = None
Expand Down Expand Up @@ -452,15 +423,6 @@ def built_dict(self):
else:
cali_filename = None

# vanadium run number
vana_run_number = str(local_list_ui.vanadium_value.text())

# vanadium full file name (if any)
if str(local_list_ui.vanadium_browser.text()) != "N/A":
vana_filename = str(local_list_value["vanadium_browser"])
else:
vana_filename = None

# local date
_date = local_list_ui.date.date()
[year, month, day] = _date.getDate()
Expand All @@ -472,14 +434,7 @@ def built_dict(self):
# local output dir
local_output_dir = str(local_list_ui.output_dir_value.text())

# save data in vanadium dict
vanadium_dict = {}
vanadium_dict["RunNumber"] = vana_run_number
if vana_filename:
vanadium_dict["Filename"] = vana_filename

cali_dict = {}
cali_dict["Vanadium"] = vanadium_dict
if cali_filename:
cali_dict["Filename"] = cali_filename

Expand Down
Loading

0 comments on commit 49f3314

Please sign in to comment.