Example #1
0
    def _execute_expression(self):
        """Execute expression and store results.

        Check existence of all tables used in the expression and
        result.

        """
        statements = self.statements_edit.toPlainText()
        expression = self.expression_edit.toPlainText()
        identifier_strings = extract_identifiers(expression)
        identifiers = [i[1:] for i in identifier_strings]
        current_group = get_current_group()
        identifier_node_dict = build_identifier_node_dict(
            identifiers, current_group)
        if not self._all_identifiers_found(identifiers, identifier_node_dict):
            return False
        eval_globals, expression = self._create_eval_globals_and_epsression(
            expression, identifier_node_dict)
        results = self._get_result_group_and_name()
        if results is None:
            return False
        result_group, result_name = results
        try:
            result = vtce.evaluate(statements, expression, eval_globals)
        except Exception as e:
            self._logger.error(str(e))
            QtWidgets.QMessageBox.critical(
                self, translate('Calculator', 'Evaluation error'),
                translate(
                    'Calculator', 'An exception was raised during '
                    'evaluation, see log for details.'))
            return False
        if not isinstance(result, np.ndarray):
            if isinstance(result, (list, tuple)):
                result = np.array(result)
            else:
                result = np.array([result])
        try:
            result_group._v_file.create_array(
                result_group,
                result_name,
                obj=result,
                title='Expression: ' + self.expression_edit.toPlainText())
        except Exception as e:
            self._logger.error(str(e))
            QtWidgets.QMessageBox.critical(
                self, translate('Calculator', 'Result save error'),
                translate(
                    'Calculator', 'An exception was raised while '
                    'trying to store results, see log for details.'))
            return False
        vtu.getModel().updateTreeFromData()
        return True
Example #2
0
    def _execute_expression(self):
        """Execute expression and store results.

        Check existence of all tables used in the expression and
        result.

        """
        statements = self.statements_edit.toPlainText()
        expression = self.expression_edit.toPlainText()
        identifier_strings = extract_identifiers(expression)
        identifiers = [i[1:] for i in identifier_strings]
        current_group = get_current_group()
        identifier_node_dict = build_identifier_node_dict(identifiers,
                                                          current_group)
        if not self._all_identifiers_found(identifiers, identifier_node_dict):
            return False
        eval_globals, expression = self._create_eval_globals_and_epsression(
            expression, identifier_node_dict)
        results = self._get_result_group_and_name()
        if results is None:
            return False
        result_group, result_name = results
        try:
            result = vtce.evaluate(statements, expression, eval_globals)
        except Exception as e:
            log.error(str(e))
            QtWidgets.QMessageBox.critical(
                self, translate('Calculator', 'Evaluation error'),
                translate('Calculator', 'An exception was raised during '
                          'evaluation, see log for details.'))
            return False
        if not isinstance(result, np.ndarray):
            if isinstance(result, (list, tuple)):
                result = np.array(result)
            else:
                result = np.array([result])
        try:
            result_group._v_file.create_array(
                result_group, result_name, obj=result,
                title='Expression: ' + self.expression_edit.toPlainText())
        except Exception as e:
            log.error(str(e))
            QtWidgets.QMessageBox.critical(
                self, translate('Calculator', 'Result save error'),
                translate('Calculator', 'An exception was raised while '
                          'trying to store results, see log for details.'))
            return False
        vtu.getModel().updateTreeFromData()
        return True
Example #3
0
 def _get_result_group_and_name(self):
     """Find or create a group for result based on provided name."""
     result_identifier = self.result_edit.text()
     if not result_identifier:
         QtWidgets.QMessageBox.critical(
             self, translate('Calculator', 'Result name'),
             translate('Calculator',
                       'The location to store results is not specified'))
         return None
     model = vtu.getModel()
     result_ancestor, relative_path = find_identifier_root(
         model, result_identifier)
     if result_ancestor is None:
         result_ancestor = get_current_group()
         relative_path = result_identifier
     relative_path = relative_path.split('.')
     result_group = find_node(result_ancestor, relative_path[:-1])
     if result_group is None:
         answer = QtWidgets.QMessageBox.question(
             self,
             translate('Calculator', 'Create group'),
             translate(
                 'Calculator', 'There is no group "{group}" in '
                 '"{ancestor}". File "{filename}". Create it?'.format(
                     group='/'.join(relative_path[:-1]),
                     ancestor=result_ancestor._v_pathname,
                     filename=result_ancestor._v_file.filename)),
             buttons=QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
         if answer != QtWidgets.QMessageBox.Yes:
             return None
         result_group = create_group(result_ancestor, relative_path[:-1])
         if result_group is None:
             QtWidgets.QMessageBox.critical(
                 self, translate('Calculator', 'Result name'),
                 translate(
                     'Calculator',
                     'Failed to create group "{group}" inside '
                     '{ancestor} to hold results. File "{filename}".'
                     ''.format(group='/'.join(relative_path[:-1]),
                               ancestor=result_ancestor._v_pathname,
                               filename=result_ancestor._v_file.filename)))
             return None
     result_name = relative_path[-1]
     if result_name in result_group._v_children:
         QtWidgets.QMessageBox.critical(
             self, translate('Calculator', 'Result name'),
             translate(
                 'Calculator',
                 'Node "{node}" already exists in group "{group}". '
                 'File "{filename}". Choose another place to store '
                 'results.'.format(node=result_name,
                                   group=result_group._v_pathname,
                                   filename=result_group._v_file.filename)))
         return None, None
     return result_group, result_name
Example #4
0
 def _get_result_group_and_name(self):
     """Find or create a group for result based on provided name."""
     result_identifier = self.result_edit.text()
     if not result_identifier:
         QtWidgets.QMessageBox.critical(
             self, translate('Calculator', 'Result name'),
             translate('Calculator',
                       'The location to store results is not specified'))
         return None
     model = vtu.getModel()
     result_ancestor, relative_path = find_identifier_root(
         model, result_identifier)
     if result_ancestor is None:
         result_ancestor = get_current_group()
         relative_path = result_identifier
     relative_path = relative_path.split('.')
     result_group = find_node(result_ancestor, relative_path[:-1])
     if result_group is None:
         answer = QtWidgets.QMessageBox.question(
             self, translate('Calculator', 'Create group'),
             translate('Calculator', 'There is no group "{group}" in '
                       '"{ancestor}". File "{filename}". Create it?'.format(
                           group='/'.join(relative_path[:-1]),
                           ancestor=result_ancestor._v_pathname,
                           filename=result_ancestor._v_file.filename)),
             buttons=QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
         if answer != QtWidgets.QMessageBox.Yes:
             return None
         result_group = create_group(result_ancestor, relative_path[:-1])
         if result_group is None:
             QtWidgets.QMessageBox.critical(
                 self, translate('Calculator', 'Result name'),
                 translate('Calculator',
                           'Failed to create group "{group}" inside '
                           '{ancestor} to hold results. File "{filename}".'
                           ''.format(
                               group='/'.join(relative_path[:-1]),
                               ancestor=result_ancestor._v_pathname,
                               filename=result_ancestor._v_file.filename)))
             return None
     result_name = relative_path[-1]
     if result_name in result_group._v_children:
         QtWidgets.QMessageBox.critical(
             self, translate('Calculator', 'Result name'),
             translate('Calculator',
                       'Node "{node}" already exists in group "{group}". '
                       'File "{filename}". Choose another place to store '
                       'results.'.format(
                           node=result_name,
                           group=result_group._v_pathname,
                           filename=result_group._v_file.filename)))
         return None, None
     return result_group, result_name
Example #5
0
def build_identifier_node_dict(identifiers, current_group):
    """Map identifiers to pytables nodes."""
    model = vtu.getModel()
    identifier_node_dict = {}
    for identifier in identifiers:
        identifier_ancestor, relative_path = find_identifier_root(
            model, identifier)
        if identifier_ancestor is None:
            identifier_ancestor = current_group
            relative_path = identifier
        identifier_node = find_node(identifier_ancestor,
                                    relative_path.split('.'))
        if identifier_node:
            identifier_node_dict[identifier] = identifier_node
    return identifier_node_dict
Example #6
0
def build_identifier_node_dict(identifiers, current_group):
    """Map identifiers to pytables nodes."""
    model = vtu.getModel()
    identifier_node_dict = {}
    for identifier in identifiers:
        identifier_ancestor, relative_path = find_identifier_root(model,
                                                                  identifier)
        if identifier_ancestor is None:
            identifier_ancestor = current_group
            relative_path = identifier
        identifier_node = find_node(identifier_ancestor,
                                    relative_path.split('.'))
        if identifier_node:
            identifier_node_dict[identifier] = identifier_node
    return identifier_node_dict
Example #7
0
 def test_getModel(self):
     model = utils.getModel()
     assert model.objectName() == 'dbs_tree_model'