Ignore:
Timestamp:
Dec 24, 2017 10:42:41 AM (6 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
2109350
Parents:
378e808
Message:

Constraint validator - initial implementation + tests. SASVIEW-844

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Perspectives/Fitting/MultiConstraint.py

    • Property mode changed from 100755 to 100644
    r378e808 r6b0c2f6  
    22Widget for parameter constraints. 
    33""" 
     4from numpy import * 
     5 
    46from PyQt5 import QtCore 
    57from PyQt5 import QtGui 
     
    2325        self.setupTooltip() 
    2426 
     27        # Set param text control to the second parameter passed 
     28        self.txtConstraint.setText(self.params[1]) 
     29 
    2530        self.cmdOK.clicked.connect(self.accept) 
    2631        self.cmdRevert.clicked.connect(self.revert) 
     32        self.txtConstraint.editingFinished.connect(self.validateFormula) 
    2733 
    2834    def revert(self): 
     
    5258        self.txtConstraint.setToolTip(tooltip) 
    5359 
    54         pass 
     60    def validateFormula(self): 
     61        """ 
     62        Add visual cues when formula is incorrect 
     63        """ 
     64        formula_is_valid = False 
     65        formula_is_valid = self.validateConstraint(self.txtConstraint.text()) 
     66        if not formula_is_valid: 
     67            self.cmdOK.setEnabled(False) 
     68            self.txtConstraint.setStyleSheet("QLineEdit {background-color: red;}") 
     69        else: 
     70            self.cmdOK.setEnabled(True) 
     71            self.txtConstraint.setStyleSheet("QLineEdit {background-color: white;}") 
    5572 
     73    def validateConstraint(self, constraint_text): 
     74        """ 
     75        Ensure the constraint has proper form 
     76        """ 
     77        # 0. none or empty 
     78        if not constraint_text or not isinstance(constraint_text, str): 
     79            return False 
     80 
     81        param_str = str(self.params[1]) 
     82        constraint_text = constraint_text.strip() 
     83 
     84        # 1. just the parameter 
     85        if param_str == constraint_text: 
     86            return True 
     87 
     88        # 2. ensure the text contains parameter name 
     89        parameter_string_start = constraint_text.find(param_str) 
     90        has_parameter_name = (parameter_string_start > -1) 
     91        if not has_parameter_name: 
     92            return False 
     93        parameter_string_end = parameter_string_start + len(param_str) 
     94 
     95        # 3. parameter name should be a separate word, but can have "()[]*+-/" around 
     96        valid_neighbours = "()[]*+-/ " 
     97        has_only_parameter = False 
     98        start_loc = parameter_string_start -1 
     99        end_loc = parameter_string_end 
     100        if not any([constraint_text[start_loc] == char for char in valid_neighbours]): 
     101            return False 
     102        if end_loc < len(constraint_text): 
     103            if not any([constraint_text[end_loc] == char for char in valid_neighbours]): 
     104                return False 
     105 
     106        # 4. replace parameter name with "1" and try to evaluate the expression 
     107        try: 
     108            expression_to_evaluate = constraint_text.replace(param_str, "1.0") 
     109            eval(expression_to_evaluate) 
     110        except Exception: 
     111            # Too many cases to cover individually, just a blanket 
     112            # Exception should be sufficient 
     113            # Note that in current numpy things like sqrt(-1) don't 
     114            # raise but just return warnings 
     115            return False 
     116 
     117        return True 
     118 
     119 
     120 
Note: See TracChangeset for help on using the changeset viewer.