def on_calcSingle_requested(self): ''' Calculation of gradient for a single timestep has been requested ''' try: df = self.inputValues()['data'] #pd.DataFrame in the input terminal `data` info = self.ctrlWidget().selectedWellsInfo() if len(info.keys()) < 3: raise ValueError('Select at least 3 wells to calculate gradient') # first select the timestep datetimeColName = self.ctrlWidget().comboBox_Datetime.currentText() if not datetimeColName: return row = df.loc[df[datetimeColName] == self.ctrlWidget().selectedTimestep()] # select row whith user-specified datetime `timestep` if row.empty: raise IndexError('Invalid timestep. Selected timestep `{0}` not found in `data`s column {1}.'.format(self.ctrlWidget().selectedTimestep(), datetimeColName)) # then get the information dictForDf = {'well': [], 'x': [], 'y': [], 'z': []} # dictionary that will be used for a convinient creation of the DataFrame that is requered for calculations for wellName, wellInfo in info.iteritems(): if wellInfo['z'] not in row: raise IndexError('Z-value (head) for well `{2}` is missing. Cannot find column `{0}` at timestep {1} in DataFrame `data`'.format(wellInfo['z'], self.ctrlWidget().selectedTimestep(), wellName)) dictForDf['well'].append(wellName) dictForDf['x'].append(wellInfo['x']) dictForDf['y'].append(wellInfo['y']) dictForDf['z'].append(float(row[wellInfo['z']])) df_this = pd.DataFrame(dictForDf) gradient, direction = devlin2003pandas(df_this, 'x', 'y', 'z') single_magnitude_str = ' {0:.3e}'.format(gradient) single_direction_str = ' {0:.1f} degrees N'.format(direction) self.ctrlWidget().label_singleMagVal.setText(single_magnitude_str) self.ctrlWidget().label_singleDirVal.setText(single_direction_str) self.setOutput(this=df_this) self.sigSingleGradientChanged.emit(gradient, direction) self.clearException() except: self.setOutput(this=None) self.ctrlWidget().clear(clearTable=False) self.setException(sys.exc_info()) self.sigOutputChanged.emit(self) ## triggers flowchart to propagate new data
def on_calcSingle_requested(self): ''' Calculation of gradient for a single timestep has been requested ''' try: df = self.inputValues()[ 'data'] #pd.DataFrame in the input terminal `data` info = self.ctrlWidget().selectedWellsInfo() if len(info.keys()) < 3: raise ValueError( 'Select at least 3 wells to calculate gradient') # first select the timestep datetimeColName = self.ctrlWidget().comboBox_Datetime.currentText() if not datetimeColName: return row = df.loc[ df[datetimeColName] == self.ctrlWidget().selectedTimestep( )] # select row whith user-specified datetime `timestep` if row.empty: raise IndexError( 'Invalid timestep. Selected timestep `{0}` not found in `data`s column {1}.' .format(self.ctrlWidget().selectedTimestep(), datetimeColName)) # then get the information dictForDf = { 'well': [], 'x': [], 'y': [], 'z': [] } # dictionary that will be used for a convinient creation of the DataFrame that is requered for calculations for wellName, wellInfo in info.iteritems(): if wellInfo['z'] not in row: raise IndexError( 'Z-value (head) for well `{2}` is missing. Cannot find column `{0}` at timestep {1} in DataFrame `data`' .format(wellInfo['z'], self.ctrlWidget().selectedTimestep(), wellName)) dictForDf['well'].append(wellName) dictForDf['x'].append(wellInfo['x']) dictForDf['y'].append(wellInfo['y']) dictForDf['z'].append(float(row[wellInfo['z']])) df_this = pd.DataFrame(dictForDf) gradient, direction = devlin2003pandas(df_this, 'x', 'y', 'z') single_magnitude_str = ' {0:.3e}'.format(gradient) single_direction_str = ' {0:.1f} degrees N'.format(direction) self.ctrlWidget().label_singleMagVal.setText(single_magnitude_str) self.ctrlWidget().label_singleDirVal.setText(single_direction_str) self.setOutput(this=df_this) self.sigSingleGradientChanged.emit(gradient, direction) self.clearException() except: self.setOutput(this=None) self.ctrlWidget().clear(clearTable=False) self.setException(sys.exc_info()) self.sigOutputChanged.emit( self) ## triggers flowchart to propagate new data
def process(self, coord, data): if data is not None: colname = [col for col in data.columns if isNumpyDatetime(data[col].dtype)] self._ctrlWidget.param('Datetime').setLimits(colname) self.data = data else: self.data = None return dict(this=None, All=self.All_out) if coord is not None: colname = [col for col in coord.columns if isNumpyNumeric(coord[col].dtype)] self._ctrlWidget.param('coords_grp', 'x').setLimits(colname) self._ctrlWidget.param('coords_grp', 'y').setLimits(colname) self.CW().disconnect_valueChanged2upd(self.CW().param('coords_grp', 'x')) self.CW().disconnect_valueChanged2upd(self.CW().param('coords_grp', 'y')) self.CW().param('coords_grp', 'x').setValue(colname[0]) self.CW().param('coords_grp', 'y').setValue(colname[1]) self.CW().connect_valueChanged2upd(self.CW().param('coords_grp', 'x')) self.CW().connect_valueChanged2upd(self.CW().param('coords_grp', 'y')) else: return dict(this=None, All=self.All_out) # now make sure All well specified in `coord` dataframe are found in `data` well_names = coord.index.values for well_n in well_names: if well_n not in data.columns: raise ValueError('Well named `{0}` not found in `data` but is declared in `coords`'.format(well_n)) kwargs = self.ctrlWidget().prepareInputArguments() # select row whith user-specified datetime `timestep` row = data.loc[data[kwargs['datetime']] == kwargs['t']] if row.empty: raise IndexError('Selected timestep `{0}` not found in `data`s column {1}. Select correct one'.format(kwargs['t'], kwargs['datetime'])) # now prepare dataframe for devlin calculations df = coord.copy() df['z'] = np.zeros(len(df.index)) for well_n in well_names: df.loc[well_n, 'z'] = float(row[well_n]) gradient, direction = devlin2003pandas(df, kwargs['x'], kwargs['y'], 'z') self.CW().param('grad').setValue(gradient) self.CW().param('angle').setValue(direction) # here we will generate large dataset of all timesteps if self.CW().CALCULATE_ALL: # now generate long dataframe All = pd.DataFrame({kwargs['datetime']: data[kwargs['datetime']], 'gradient': np.zeros(len(data.index)), 'direction(degrees North)': np.zeros(len(data.index))} ) self.All_out = All # pointer with pg.ProgressDialog("Calculating gradient for All timesteps {0}".format(len(All.index)), 0, len(All.index)) as dlg: for row_i in data.index: row = data.loc[row_i] z = np.zeros(len(coord.index)) for i, well_n in enumerate(well_names): z[i] = float(row[well_n]) x = coord[kwargs['x']].values y = coord[kwargs['y']].values _, gradient, angle = devlin2003(np.matrix([x, y, z]).T) All.loc[row_i, 'gradient'] = gradient All.loc[row_i, 'direction(degrees North)'] = angle2bearing(angle, origin='N')[0] dlg += 1 del z if dlg.wasCanceled(): del All self.All_out = None break #return dict(df=df, All=self.All_out) dlg += 1 return dict(this=df, All=self.All_out)
def process(self, coord, data): if data is not None: colname = [ col for col in data.columns if isNumpyDatetime(data[col].dtype) ] self._ctrlWidget.param('Datetime').setLimits(colname) self.data = data else: self.data = None return dict(this=None, All=self.All_out) if coord is not None: colname = [ col for col in coord.columns if isNumpyNumeric(coord[col].dtype) ] self._ctrlWidget.param('coords_grp', 'x').setLimits(colname) self._ctrlWidget.param('coords_grp', 'y').setLimits(colname) self.CW().disconnect_valueChanged2upd(self.CW().param( 'coords_grp', 'x')) self.CW().disconnect_valueChanged2upd(self.CW().param( 'coords_grp', 'y')) self.CW().param('coords_grp', 'x').setValue(colname[0]) self.CW().param('coords_grp', 'y').setValue(colname[1]) self.CW().connect_valueChanged2upd(self.CW().param( 'coords_grp', 'x')) self.CW().connect_valueChanged2upd(self.CW().param( 'coords_grp', 'y')) else: return dict(this=None, All=self.All_out) # now make sure All well specified in `coord` dataframe are found in `data` well_names = coord.index.values for well_n in well_names: if well_n not in data.columns: raise ValueError( 'Well named `{0}` not found in `data` but is declared in `coords`' .format(well_n)) kwargs = self.ctrlWidget().prepareInputArguments() # select row whith user-specified datetime `timestep` row = data.loc[data[kwargs['datetime']] == kwargs['t']] if row.empty: raise IndexError( 'Selected timestep `{0}` not found in `data`s column {1}. Select correct one' .format(kwargs['t'], kwargs['datetime'])) # now prepare dataframe for devlin calculations df = coord.copy() df['z'] = np.zeros(len(df.index)) for well_n in well_names: df.loc[well_n, 'z'] = float(row[well_n]) gradient, direction = devlin2003pandas(df, kwargs['x'], kwargs['y'], 'z') self.CW().param('grad').setValue(gradient) self.CW().param('angle').setValue(direction) # here we will generate large dataset of all timesteps if self.CW().CALCULATE_ALL: # now generate long dataframe All = pd.DataFrame({ kwargs['datetime']: data[kwargs['datetime']], 'gradient': np.zeros(len(data.index)), 'direction(degrees North)': np.zeros(len(data.index)) }) self.All_out = All # pointer with pg.ProgressDialog( "Calculating gradient for All timesteps {0}".format( len(All.index)), 0, len(All.index)) as dlg: for row_i in data.index: row = data.loc[row_i] z = np.zeros(len(coord.index)) for i, well_n in enumerate(well_names): z[i] = float(row[well_n]) x = coord[kwargs['x']].values y = coord[kwargs['y']].values _, gradient, angle = devlin2003(np.matrix([x, y, z]).T) All.loc[row_i, 'gradient'] = gradient All.loc[row_i, 'direction(degrees North)'] = angle2bearing( angle, origin='N')[0] dlg += 1 del z if dlg.wasCanceled(): del All self.All_out = None break #return dict(df=df, All=self.All_out) dlg += 1 return dict(this=df, All=self.All_out)