def runFilter(self, objId, filterType, condition): import copy experiment = self.readExperiment(self.inputExperiment.get().fnPKPD) self.printSection("Filtering") if self.filterType.get() == 0: filterType = "exclude" elif self.filterType.get() == 1: filterType = "keep" elif self.filterType.get() == 2: filterType = "rmNA" elif self.filterType.get() == 3: filterType = "rmLL" elif self.filterType.get() == 4: filterType = "subsLL" elif self.filterType.get() == 5: filterType = "subsUL" filteredExperiment = PKPDExperiment() filteredExperiment.general = copy.copy(experiment.general) filteredExperiment.variables = copy.copy(experiment.variables) filteredExperiment.samples = {} filteredExperiment.doses = {} filteredExperiment.vias = {} usedDoses = [] for sampleKey, sample in experiment.samples.items(): candidateSample = PKPDSample() candidateSample.variableDictPtr = copy.copy(sample.variableDictPtr) candidateSample.doseDictPtr = copy.copy(sample.doseDictPtr) candidateSample.sampleName = copy.copy(sample.sampleName) candidateSample.doseList = copy.copy(sample.doseList) candidateSample.descriptors = copy.copy(sample.descriptors) candidateSample.measurementPattern = copy.copy( sample.measurementPattern) N = 0 # Number of initial measurements if len(sample.measurementPattern) > 0: aux = getattr(sample, "measurement_%s" % sample.measurementPattern[0]) N = len(aux) if N == 0: continue # Create empty output variables Nvar = len(sample.measurementPattern) convertToFloat = [] for i in range(0, Nvar): exec("candidateSample.measurement_%s = []" % sample.measurementPattern[i]) convertToFloat.append( sample.variableDictPtr[sample.measurementPattern[i]]. varType == PKPDVariable.TYPE_NUMERIC) for n in range(0, N): toAdd = [] okToAddTimePoint = True conditionPython = copy.copy(condition) for i in range(0, Nvar): ldict = {} exec( "aux=sample.measurement_%s[%d]" % (sample.measurementPattern[i], n), locals(), ldict) aux = ldict['aux'] if filterType == "rmNA": if aux == "NA" or aux == "None": okToAddTimePoint = False else: toAdd.append(aux) elif filterType == "rmLL": if aux == "LLOQ" or aux == "ULOQ": okToAddTimePoint = False else: toAdd.append(aux) elif filterType == "subsLL": okToAddTimePoint = True if aux == "LLOQ": toAdd.append(str(self.substitute.get())) else: toAdd.append(aux) elif filterType == "subsUL": okToAddTimePoint = True if aux == "ULOQ": toAdd.append(str(self.substitute.get())) else: toAdd.append(aux) else: # Keep or exclude toAdd.append(aux) varString = "$(%s)" % sample.measurementPattern[i] if varString in conditionPython: if aux == "NA": okToAddTimePoint = False else: if convertToFloat[i]: conditionPython = conditionPython.replace( varString, "%f" % float(aux)) else: conditionPython = conditionPython.replace( varString, "'%s'" % aux) if (filterType == "exclude" or filterType == "keep") and okToAddTimePoint: okToAddTimePoint = eval(conditionPython, {"__builtins__": None}, {}) if filterType == "exclude": okToAddTimePoint = not okToAddTimePoint if okToAddTimePoint: for i in range(0, Nvar): exec("candidateSample.measurement_%s.append('%s')" % (sample.measurementPattern[i], toAdd[i])) N = len( getattr(sample, "measurement_%s" % sample.measurementPattern[0] )) # Number of final measurements if N != 0: filteredExperiment.samples[ candidateSample.sampleName] = candidateSample for doseName in candidateSample.doseList: if not doseName in usedDoses: usedDoses.append(doseName) if len(usedDoses) > 0: for doseName in usedDoses: filteredExperiment.doses[doseName] = copy.copy( experiment.doses[doseName]) viaName = experiment.doses[doseName].via.viaName if not viaName in filteredExperiment.vias: filteredExperiment.vias[viaName] = copy.copy( experiment.vias[viaName]) self.writeExperiment(filteredExperiment, self._getPath("experiment.pkpd")) self.experiment = filteredExperiment
def runFilter(self, objId, filterType, condition): import copy experiment = self.readExperiment(self.inputExperiment.get().fnPKPD) self.printSection("Filtering") if self.filterType.get() == 0: filterType = "exclude" elif self.filterType.get() == 1: filterType = "keep" else: filterType = "rmNA" filteredExperiment = PKPDExperiment() filteredExperiment.general = copy.copy(experiment.general) filteredExperiment.variables = copy.copy(experiment.variables) filteredExperiment.vias = copy.copy(experiment.vias) filteredExperiment.groups = {} filteredExperiment.samples = {} filteredExperiment.doses = {} # http://stackoverflow.com/questions/701802/how-do-i-execute-a-string-containing-python-code-in-python safe_list = ['descriptors'] safe_dict = dict([(k, locals().get(k, None)) for k in safe_list]) usedDoses = [] for sampleKey, sample in experiment.samples.items(): ok = filterType == "rmNA" try: if filterType == "rmNA": conditionPython = "True" else: conditionPython = copy.copy(condition) conditionPython = conditionPython.replace( '$(sampleName)', '"%s"' % sample.sampleName) for key, variable in experiment.variables.items(): if key in sample.descriptors: value = sample.descriptors[key] if value == "NA": conditionPython = "False" elif value == "NS": conditionPython = "False" elif value == "": conditionPython = "False" else: if filterType != "rmNA": if variable.varType == PKPDVariable.TYPE_NUMERIC: conditionPython = conditionPython.replace( "$(%s)" % key, "%f" % float(sample.descriptors[key])) else: conditionPython = conditionPython.replace( "$(%s)" % key, "'%s'" % sample.descriptors[key]) ok = eval(conditionPython, {"__builtins__": { "True": True, "False": False }}, {}) except: print(sys.exc_info()[0]) pass if (ok and (filterType == "keep" or filterType == "rmNA")) or ( not ok and filterType == "exclude"): filteredExperiment.samples[sampleKey] = copy.copy(sample) for doseName in sample.doseList: usedDoses.append(doseName) for groupName in sample.groupList: if not groupName in filteredExperiment.groups: filteredExperiment.groups[groupName] = copy.copy( experiment.groups[groupName]) if len(usedDoses) > 0: for doseName in usedDoses: filteredExperiment.doses[doseName] = copy.copy( experiment.doses[doseName]) self.writeExperiment(filteredExperiment, self._getPath("experiment.pkpd")) self.experiment = filteredExperiment