def __init__(self, shared_state, computations_thread): self._shared_state = shared_state self._protocol = Protocol() self._opened_file = self._shared_state.base_dir self._tab_content = None self._system_columns = OrderedDict([['#', self._create_volume_number_column]])
def run(self, args, extra_args): if args.output_file is not None: output_file = os.path.realpath(args.output_file) else: output_file = os.path.realpath(args.input_protocol) additional_files = [] if args.additional_files: for file in args.additional_files: additional_files.append(np.genfromtxt(file)) protocol = mdt.load_protocol(os.path.realpath(args.input_protocol)) context_dict = { name: protocol.get_column(name) for name in protocol.column_names } exec(args.expr, {'np': np, 'files': additional_files}, context_dict) for key in context_dict: if is_scalar(context_dict[key]): context_dict[key] = np.ones( protocol.length) * context_dict[key] protocol = Protocol(context_dict) mdt.write_protocol(protocol, output_file)
def _clear_table(self): self._protocol = Protocol() self._update_views()
class GenerateProtocolTab(MainTab, Ui_GenerateProtocolTabContent): def __init__(self, shared_state, computations_thread): self._shared_state = shared_state self._protocol = Protocol() self._opened_file = self._shared_state.base_dir self._tab_content = None self._system_columns = OrderedDict([['#', self._create_volume_number_column]]) def setupUi(self, tab_content): super(GenerateProtocolTab, self).setupUi(tab_content) self._tab_content = tab_content self.loadProtocolButton.clicked.connect(lambda: self._select_protocol()) self.saveButton.clicked.connect(lambda: self._save_protocol()) self.loadColumnButton.clicked.connect(lambda: self._load_column_action()) self.loadGB.clicked.connect(lambda: self._load_g_and_b()) self.clearButton.clicked.connect(self._clear_table) self.protocol_table.setSortingEnabled(True) headers = self.protocol_table.horizontalHeader() headers.setContextMenuPolicy(Qt.CustomContextMenu) headers.customContextMenuRequested.connect(self.show_header_context_menu) headers.setSelectionMode(QAbstractItemView.SingleSelection) def _select_protocol(self): open_file, used_filter = QFileDialog().getOpenFileName( caption='Select the protocol', directory=self._shared_state.base_dir, filter=';;'.join(protocol_files_filters)) if open_file: self._shared_state.base_dir = os.path.dirname(open_file) self.load_protocol(open_file) def _save_protocol(self): output_file_name, used_filter = QFileDialog().getSaveFileName( caption='Save the protocol as', directory=self._opened_file, filter=';;'.join(protocol_files_filters)) if os.path.isdir(os.path.dirname(output_file_name)) and self._protocol.length: mdt.write_protocol(self._protocol, output_file_name) print('Saved protocol as: {}'.format(output_file_name)) def _clear_table(self): self._protocol = Protocol() self._update_views() def load_protocol(self, file_name): self._protocol = mdt.protocols.load_protocol(file_name) self._update_views() self._opened_file = file_name print('Loaded protocol: {}'.format(file_name)) def _update_views(self): self._update_protocol_info() self._update_table_view() def _update_protocol_info(self): self.nmrRows.setText(str(self._protocol.length)) try: self.nmrUnweighted.setText(str(len(self._protocol.get_unweighted_indices()))) except KeyError: self.nmrUnweighted.setText('0') try: self.nmrWeighted.setText(str(len(self._protocol.get_weighted_indices()))) except KeyError: self.nmrWeighted.setText('0') try: self.nmrShells.setText(str(len(self._protocol.get_b_values_shells()))) except KeyError: self.nmrShells.setText('0') self.nmrColumns.setText(str(self._protocol.number_of_columns)) try: shells = self._protocol.get_b_values_shells() shells_text = [] for shell in shells: occurrences = self._protocol.count_occurences('b', shell) shells_text.append('{0:0=.3f}e9 ({1})'.format(shell/1e9, occurrences)) self.differentShells.setText(', '.join(shells_text)) except KeyError: self.differentShells.setText('-') def _update_table_view(self): all_column_names, real_column_names, estimated_column_names, system_column_names = self._get_column_names() self.protocol_table.clear() self.protocol_table.setRowCount(self._protocol.length) self.protocol_table.setColumnCount(len(all_column_names)) for index, column_name in enumerate(all_column_names): header_cell = QTableWidgetItem(column_name) if column_name in estimated_column_names: header_cell.setToolTip('This column is estimated from the other columns in the protocol.') self.protocol_table.setHorizontalHeaderItem(index, header_cell) for column_ind, column_name in enumerate(all_column_names): if column_name in system_column_names: generate_function = self._system_columns[column_name] cells = generate_function() for row, cell in enumerate(cells): self.protocol_table.setItem(row, column_ind, cell) else: try: values = self._protocol.get_column(column_name) for row in range(self._protocol.length): cell = NumericalSortedTableItem('{:e}'.format(values[row, 0])) cell.setFlags(QtCore.Qt.ItemIsEnabled) if column_name in estimated_column_names: cell.setBackground(QBrush(Qt.lightGray)) self.protocol_table.setItem(row, column_ind, cell) except KeyError: for row in range(self._protocol.length): cell = QTableWidgetItem('?') cell.setFlags(QtCore.Qt.ItemIsEnabled) cell.setBackground(QBrush(Qt.lightGray)) self.protocol_table.setItem(row, column_ind, cell) self.protocol_table.resizeColumnsToContents() def _get_column_names(self): real_column_names = self._protocol.column_names if len(real_column_names): estimated_column_names = self._protocol.estimated_column_names else: estimated_column_names = [] system_column_names = list(self._system_columns.keys()) all_column_names = system_column_names + real_column_names + estimated_column_names return [all_column_names, real_column_names, estimated_column_names, system_column_names] def _create_volume_number_column(self): """Callback function to generate the volume number column cells. This should return a list of cells in the correct order. """ cells = [] for volume_nmr in range(self._protocol.length): cell = NumericalSortedTableItem(str(volume_nmr)) cell.setFlags(QtCore.Qt.ItemIsEnabled) cell.setBackground(QBrush(Qt.lightGray)) cells.append(cell) return cells def show_header_context_menu(self, position): all_column_names, real_column_names, estimated_column_names, system_column_names = self._get_column_names() column_index = self.protocol_table.horizontalHeader().logicalIndexAt(position) column_name = all_column_names[column_index] if column_name in real_column_names: menu = QMenu() remove_action = menu.addAction("&Remove column") ac = menu.exec_(self.protocol_table.horizontalHeader().mapToGlobal(position)) if ac == remove_action: quit_msg = "Are you sure you want to remove the " \ "column '{}' from the protocol".format(column_name) reply = QMessageBox.question(self._tab_content, 'Delete confirmation', quit_msg, QMessageBox.Yes, QMessageBox.No) if reply == QMessageBox.Yes: self._protocol.remove_column(column_name) self._update_views() def _load_column_action(self): dialog = LoadColumnDialog(self._shared_state, self._tab_content) return_value = dialog.exec_() if return_value: dialog.update_protocol(self._protocol) self._update_views() def _load_g_and_b(self): dialog = LoadGBDialog(self._shared_state, self._tab_content) return_value = dialog.exec_() if return_value: self._protocol = dialog.get_protocol() self._update_views()