def setOptions(self, table: Dict[int, Dict[str, Union[List[float], str]]], suffix: Tuple[bool, Optional[str]]) -> None: # Validate options errors = list() if not table: errors.append(('noAttr', 'Error: at least one attribute should be chosen')) if suffix[0] and not suffix[1]: errors.append(('suffix', 'Error: new attribute suffix must be specified')) if errors: raise exp.OptionValidationError(errors) options: Dict[int, Tuple[List[float], List[str]]] = dict() for row, opts in table.items(): if not opts.get('bins', None) or not opts.get('labels', None): errors.append(('notSet', 'Error: options at row {:d} are not set'.format(row))) continue # Edges are already parsed by delegate edges: List[float] = opts['bins'] # Labels must be parsed labels: List[str] = splitString(opts['labels'], sep=' ') labNum = len(edges) - 1 if len(labels) != labNum: errors.append(('invalidLabels', 'Error: Labels at row {:d} is not equal to the number of intervals ' 'defined ({:d})'.format(row, labNum))) options[row] = (edges, labels) if len(errors) > 8: break if errors: raise exp.OptionValidationError(errors) # If everything went well set options self.__attributes = options self.__attributeSuffix = suffix[1] if suffix[0] else None
def setOptions( self, attributes: Dict[int, Dict[str, Tuple[float, float]]]) -> None: # { attr_index: (min, max) } selectedAttributes: Dict[int, Tuple[float, float]] = dict() errors = list() if not attributes: raise exp.OptionValidationError([ ('noOptions', 'Error: no attributes are selected') ]) if not all(map(lambda v: bool(v), attributes.values())): errors.append( ('notAllOptions', 'Error: some attributes have no options')) for k, opts in attributes.items(): vRange = opts.get('range', None) if not vRange: raise exp.OptionValidationError([ ('nr', 'Error: no range is set at row {:d}'.format(k)) ]) if not (len(vRange) == 2 and vRange[0] < vRange[1]): raise exp.OptionValidationError([ ('ir', 'Error: range is invalid at row {:d}'.format(k)) ]) selectedAttributes[k] = vRange # Set options self.__attributes = selectedAttributes
def setOptions(self, table: Dict[int, Dict[str, str]], inverted: bool) -> None: if not table: raise exp.OptionValidationError([ ('nooptions', 'Error: no attributes are selected') ]) options: Dict[int, Tuple[List[List], List]] = dict() for c, opt in table.items(): type_c = self.shapes[0].colTypes[c] values: str = opt.get('values', None) replace: str = opt.get('replace', None) if not values or not replace: raise exp.OptionValidationError([ ('incompleteoptions', 'Error: options are not set at row {:d}'.format(c)) ]) lists: List[str] = splitString(values, ';') replace: List[str] = splitString(replace, ';') if not lists or not replace: raise exp.OptionValidationError([ ('incompleteoptions', 'Error: options are not set at row ' '{:d}'.format(c)) ]) if len(lists) != len(replace): raise exp.OptionValidationError([ ('wrongnum', 'Error: number of intervals is not equal to ' 'the number of values to replace at row {:d}'.format(c)) ]) parsedValues: List[List[str]] = [ splitString(s, ' ') for s in lists ] if type_c == Types.Numeric: if not all(map(isFloat, replace)): err = ('numericinvalid', 'Error: list of values to replace contains non ' 'numeric value at row {:d}, but selected attribute ' 'is numeric'.format(c)) raise exp.OptionValidationError([err]) replace: List[float] = [float(x) for x in replace] parsedValues: List[List[float]] = [ floatList(l, invalid='drop') for l in parsedValues ] else: # If type is not numeric everything will be treated as string replace: List = parseNan(replace) parsedValues: List[List] = [parseNan(a) for a in parsedValues] # Save parsed options for this column options[c] = (parsedValues, replace) self.__attributes = options self.__invertedReplace = inverted
def setOptions(self, table: Dict[int, Dict[str, str]]) -> None: errors = list() if not table: errors.append( ('no', 'Error: at least one column should be selected')) elif not table.values(): errors.append( ('values', 'Error: selected columns target names are not set')) else: names: Dict[int, str] = { i: d.get('rename', ' ').strip() for i, d in table.items() } nameList = set(names.values()) if not all(nameList): errors.append(('set', 'Error: some names are not set')) if len(nameList) != len(list(names.values())): errors.append( ('set', 'Error: some column names are duplicate')) if nameList & set(self.shapes[0].colNames): errors.append( ('overwrite', 'Error: some new columns names are already present')) if errors: raise exp.OptionValidationError(errors) self.__attributes = names
def setOptions(self, series: Dict[str, List[Tuple[str, int, int]]], time: List[str], outName: str) -> None: # series = { series: [ (frameName, attributeIndex, timeLabelIndex) ] } # outName = new workbench entry name # time = [ 'wave1', 'wave2', ... ] errors = list() if not outName: errors.append(('noname', 'Error: an output name must be set')) if not time: errors.append( ('notimelabels', 'Error: the time labels are not set')) if not series: errors.append(('noseries', 'Error: no series are defined')) else: lengthOk = all( map(lambda s: s and len(s) == len(time), series.values())) noDuplicates = all( map(lambda s: s and len(set(map(lambda t: t[2], s))) == len(s), series.values())) if not lengthOk: errors.append(( 'length', 'Error: you set {:d} time labels, but some series is made up ' 'of a different number of attributes'.format(len(time)))) if not noDuplicates: errors.append( ('duplicates', 'Error: some series contain duplicated time labels')) if errors: raise exp.OptionValidationError(errors) self.__series = copy.deepcopy(series) self.__outputName = outName self.__timeLabels = copy.deepcopy(time)
def setOptions(self, ls: str, rs: str, onindex: bool, onleft: int, onright: int, joinType: JoinType) -> None: errors = list() ls = ls.strip() rs = rs.strip() if not ls or not rs: errors.append(('suffix', 'Error: both suffixes are required')) elif ls == rs: errors.append(('suffixequals', 'Error: suffixes must be different')) if not joinType: errors.append(('jointype', 'Error: join type is not set')) # if not all(self.shapes): # errors.append(('shape', 'Error: some input is missing. Connect all source inputs before ' # 'setting options')) if all(self.shapes): if onindex: # Check index types if not self.shapes[0].index or not self.shapes[1].index: errors.append(('index', 'Error: join on indices require both indices to be set')) else: llen = self.shapes[0].nIndexLevels rlen = self.shapes[1].nIndexLevels if llen == rlen == 1: # Single index join tl = self.shapes[0].indexTypes[0] tr = self.shapes[1].indexTypes[0] if not self._checkColumnTypes(tl, tr): errors.append(('type', 'Error: type "{}" and "{}" are not ' 'compatible'.format(tl.name, tr.name))) else: # Multiindex join, check that name intersection in compatible indexNamesL = set(self.shapes[0].index) indexNamesR = set(self.shapes[1].index) indexDictL = self.shapes[0].indexDict indexDictR = self.shapes[1].indexDict for name in indexNamesL: if name in indexNamesR: tl = indexDictL[name] tr = indexDictR[name] if not self._checkColumnTypes(tl, tr): errors.append(('type', 'Error: common index level "{}" has ' 'different types "{}" and "{}" which are not ' 'compatible'.format(name, tl.name, tr.name))) break else: # Merge on columns: check types tl: Type = self.shapes[0].colTypes[onleft] tr: Type = self.shapes[1].colTypes[onright] if not self._checkColumnTypes(tl, tr): errors.append(('type', 'Error: column types "{}" and "{}" are not compatible' .format(tl.name, tr.name))) if errors: raise exp.OptionValidationError(errors) self.__lSuffix = ls self.__rSuffix = rs self.__onIndex = onindex self.__leftOn = onleft self.__rightOn = onright self.__type = joinType
def setOptions(self, attributes: Dict[int, Dict[str, Any]]) -> None: selection = list(attributes.keys()) if not selection: raise exp.OptionValidationError([ ('noOptions', 'Error: no attributes are selected') ]) self.__selected = selection
def setOptions(self, attributes: Dict[int, Dict]) -> None: # { attr_index: dict() } if not attributes: raise exp.OptionValidationError([ ('noOptions', 'Error: no attributes are selected') ]) # Set options self.__attributes = list(attributes.keys())
def setOptions(self, attributes: Dict[int, Dict[str, str]], errors: str) -> None: err = list() if not attributes: err.append(('nooptions', 'Error: select at least one attribute')) if not errors: err.append(('Noerror', 'Error: error mode must be specified')) if err: raise exp.OptionValidationError(err) self.__attributes = list(attributes.keys()) self.__errorMode = errors
def setOptions(self, names: Dict[int, str]) -> None: # Compute the output shape s = self._shapes[0] if s: s = s.clone() for index, name in self.__names.items(): s.colNames[index] = name if len(set(s.colNames)) < s.nColumns: raise exp.OptionValidationError([ ('dup', 'Error: new names contain duplicates') ]) self.__names = copy.deepcopy(names)
def setOptions(self, attributes: Dict[int, Dict[str, str]], errors: str) -> None: valErrors = list() if not attributes: valErrors.append(('noSelected', 'Error: no attributes are selected')) if not errors: valErrors.append(('noMode', 'Error: error modality must be selected')) if valErrors: raise exp.OptionValidationError(valErrors) # Set options for row, opts in attributes.items(): f = opts.get('format', None) self.__attributes[row] = f if f else None self.__errorMode = errors
def setOptions(self, attributes: Dict[int, Dict[str, Any]]) -> None: if not attributes: raise exp.OptionValidationError([('nooptions', 'Error: select at least one attribute')]) options: Dict[int, Tuple[Optional[List[str]], bool]] = dict() for c, opt in attributes.items(): catString: Optional[str] = opt.get('cat', None) categories: Optional[List[str]] = None orderCategories: Optional[bool] = opt.get('ordered', None) if catString: categories = splitString(catString, sep=' ') if not categories: categories = None options[c] = (categories, orderCategories) # Options are correctly set self.__attributes = options
def setOptions(self, file: str, separator: str, name: str, splitByRow: int, selectedCols: Set[int]) -> None: errors = list() if not name: errors.append( ('nameError', 'Error: a valid name must be specified')) if not selectedCols: errors.append(('noSelection', 'Error: at least 1 attribute must be selected')) if errors: raise exp.OptionValidationError(errors) self.__file = file self.__separator = separator self.__wName = name self.__splitByRowN = splitByRow if (splitByRow and splitByRow > 0) else None self.__selectedColumns = selectedCols
def setOptions(self, frame: str, file: str, sele: Dict[int, None], sep: str, nan: str, ffloat: str, decimal: str, header: bool, index: bool, date: str) -> None: errors = list() if frame not in self.workbench.names: errors.append(('e1', 'Error: frame name is not valid')) if not sele: errors.append(('e2', 'Error: no attribute to write are selected')) nan = parseUnicodeStr(nan) sep = parseUnicodeStr(sep) if not sep: errors.append(('es1', 'Error: a separator char is required')) elif len(sep) > 1: errors.append( ('es2', 'Error: separator must be a single character')) if not decimal: errors.append(('na1', 'Error: a decimal separator is required')) elif len(decimal) > 1: errors.append( ('na2', 'Error: decimal separator must be a single character')) if not date: errors.append(('d1', 'Error: datetime format must be specified')) if not file: errors.append(('f1', 'Error: no output file specified')) if errors: raise exp.OptionValidationError(errors) # Save selected column names columns: List[str] = self.workbench.getDataframeModelByName( frame).frame.colnames self.__frame_name = frame self.__path = file self.__selected_columns = [columns[i] for i in sele.keys()] self.__date_format = date self.__sep = sep self.__nan_rep = nan self.__float_format = ffloat self.__decimal = decimal self.__index = index self.__header = header
def setOptions(self, selected: Dict[int, Dict[str, Union[List, Tuple]]], suffix: Tuple[bool, Optional[str]]) -> None: errors = list() if not selected: errors.append(('attribute', 'Error: no attribute is selected')) if suffix[0] and not suffix[1]: errors.append(('suff', 'Error: suffix is unspecified')) selection: Dict[int, Tuple[List[pd.Timestamp], List[str], bool, bool]] = dict() for k, opts in selected.items(): optionsTuple: Tuple[List[pd.Timestamp], bool, bool] = opts.get('ranges', None) labels: Optional[List[str]] = opts.get('labels', None) if not optionsTuple or not optionsTuple[0]: errors.append( ('bins', 'Error: bins are not specified at row {:d}'.format(k))) if not labels: errors.append( ('lab', 'Error: no labels are specified at row {:d}'.format(k))) if labels and len(set(labels)) != len(labels): errors.append(('unique', 'Error: labels are not unique')) if optionsTuple: bins, byDate, byTime = optionsTuple # unpack if labels and bins and len(labels) != len(bins) - 1: errors.append( ('len', 'Error: interval number ({:d}) ' 'does not match labels number ({:d}) at row {:d}'. format(len(bins) - 1, len(labels), k))) # byTimeOnly = byTime and not byDate selection[k] = bins, labels, byDate, byTime if len(errors) > 8: # Don't stack to many errors break # Not necessary to check for overlapping dates, since that is ensured by the delegate if errors: raise exp.OptionValidationError(errors) self.__attributesSuffix = suffix[1] if suffix[0] else None self.__attributes = selection
def setOptions(self, attributes: Dict[int, Dict[str, str]], strategy: BinStrategy, suffix: Tuple[bool, Optional[str]]) -> None: # Validate options def isPositiveInteger(x): try: y = int(x) except ValueError: return False else: if y > 1: return True return False errors = list() if not attributes: errors.append(('nosel', 'Error: At least one attribute should be selected')) for r, options in attributes.items(): bins = options.get('bins', None) if not bins: errors.append(('nooption', 'Error: Number of bins must be set at row {:d}'.format(r))) elif not isPositiveInteger(bins): errors.append(('binsNotInt', 'Error: Number of bins must be > 1 at row {:d}'.format(r))) if strategy is None: errors.append(('missingStrategy', 'Error: Strategy must be set')) if suffix[0] and not suffix[1]: errors.append(('suffix', 'Error: suffix for new attribute must be specified')) if errors: raise exp.OptionValidationError(errors) # Clear attributes self.__attributes = dict() # Set options for r, options in attributes.items(): k = int(options['bins']) self.__attributes[r] = k self.__strategy = strategy self.__attributeSuffix = suffix[1] if suffix[0] else None
def onAcceptEditor(self) -> None: try: if self.operation.needsOptions(): # Validate and set standard options options = self.editor.getOptions() if isinstance(options, dict): self.operation.setOptions(**options) else: self.operation.setOptions(*options) if self._hasInputOutputOptions: # Validate and set input and output options name: str = self._outputNameBox.getData() if name is not None: name = name.strip() selected: str = self._inputComboBox.currentText() if not name or not selected or self._inputComboBox.currentIndex( ) < 0: raise exp.OptionValidationError([ ('wrapper', 'Error: output name or input data has not ' 'been selected') ]) self.operation.outName = name self.operation.inputName = selected except exp.OptionValidationError as e: self.editor.handleErrors(e.invalid) else: self.wrapperStateChanged.emit(self.uid, 'start') # Prepare worker self.__worker = threads.Worker(self.operation, args=self._inputs) # Connect self.__worker.signals.result.connect(self._onSuccess) self.__worker.signals.error.connect(self._onError) self.__worker.signals.finished.connect(self._onFinish) # Start worker QThreadPool.globalInstance().start(self.__worker) self.editor.hide()
def setOptions(self, attributes: Dict[int, None]) -> None: if not attributes: raise exp.OptionValidationError([('noopt', 'Error: select at least one attribute')]) self.__attributes = list(attributes.keys())