def test06_prepareForVesperKrig(self): csv_desc = CsvDescribe(fileTrimmed) df_csv = csv_desc.open_pandas_dataframe() global file_control sub_file = os.path.splitext(os.path.basename(file_csv))[0] file_control = sub_file + '_control_' + data_col + '.txt' vc = VesperControl() vc.update(xside=30, yside=30) if not os.path.exists(file_control): bat_file, file_control = prepare_for_vesper_krige(df_csv, data_col, file_block_txt, TmpDir, control_textfile=file_control, coord_columns=[], epsg=epsg, control_options=vc) self.assertTrue(os.path.exists(bat_file)) self.assertTrue(os.path.exists(file_control)) self.assertTrue(os.path.exists(os.path.join(TmpDir, r'Vesper', sub_file + '_vesperdata_' + data_col + '.csv'))) df_csv = pd.read_csv(os.path.join(TmpDir, r'Vesper', sub_file + '_vesperdata_' + data_col + '.csv')) x_column, y_column = predictCoordinateColumnNames(df_csv.columns) self.assertEqual(x_column.upper(), 'EASTING') self.assertEqual(y_column.upper(), 'NORTHING') print('Running Vesper, Please wait....') run_vesper(file_control)
def test1_CreateControlHighDensity_VesperControlClass(self): # check using VesperControl class file_csv = os.path.realpath(this_dir + "/area2_high_trimmed.csv") grid_file = os.path.realpath(this_dir + "/rasters/area2_5m_blockgrid_v.txt") data_col = r'Yld Mass(Dry)(tonne/ha)' csv_desc = CsvDescribe(file_csv) df_csv = csv_desc.open_pandas_dataframe() vc = VesperControl() vc.update(xside=30, yside=30) global g_ctrl_file file_bat, g_ctrl_file = prepare_for_vesper_krige( df_csv, data_col, grid_file, TEMPDIR, control_textfile='test_high_5m_control.txt', coord_columns=[], epsg=28354, control_options=vc) if os.path.exists(kriging_ops.vesper_exe): self.assertTrue( os.path.exists(os.path.join(TEMPDIR, 'Vesper/Do_Vesper.bat'))) else: self.assertEqual('', file_bat) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_control.txt'))) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_vesperdata.csv'))) src_df = pd.read_csv( os.path.realpath(this_dir + '/VESPER/high_5m_vesperdata.csv')) test_df = pd.read_csv( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_vesperdata.csv')) pd.testing.assert_frame_equal(src_df, test_df) with open(os.path.realpath(this_dir + '/VESPER/high_5m_control.txt')) as src_file,\ open(g_ctrl_file) as test_file: self.assertEqual(src_file.readlines()[11:], test_file.readlines()[11:])
def test_key_errors(self): vc = VesperControl(title='This is a test tile') # test the ValueErrors ------------------------------------------- # test invalid option on the value with self.assertRaises(AttributeError) as msg: vc.update(modtype=4) self.assertEqual("VesperControl has no attribute modtype", str(msg.exception)) vc.update(title='this is a test') vc.update(modtyp=4)
def test_write_to_file(self): vc = VesperControl(datfil="MyDataFile.txt", outdir='c:/data/temp', modtyp='Exponential', minpts=150, maxpts=200) with self.assertRaises(ValueError) as msg: vc.write_to_file( os.path.join('c:/nonexistent/path', 'test_control.txt')) self.assertEqual('Output folder c:/nonexistent/path does not exist', str(msg.exception)) out_file = os.path.join(tempfile.gettempdir(), 'test_control.txt') vc.write_to_file(out_file) self.assertTrue(out_file) with open(out_file) as r_file: data = r_file.read() self.assertIn("datfil='MyDataFile.txt'", data) self.assertIn("outdir='c:/data/temp'", data) self.assertIn("modtyp=2", data) self.assertIn("minpts=150", data)
def accept(self, *args, **kwargs): if not self.validate(): return False try: self.cleanMessageBars(True) message = '- and run VESPER' if self.gbRunVesper.isChecked() else '' LOGGER.info('{st}\nProcessing {} {}'.format( self.windowTitle(), message, st='*' * 50)) # Add settings to log settingsStr = 'Parameters:---------------------------------------' settingsStr += '\n {:30}\t{}'.format('Data File:', self.lneInCSVFile.text()) settingsStr += '\n {:30}\t{}'.format('Krige Column:', self.cboKrigColumn.currentText()) settingsStr += '\n {:30}\t{}'.format('Grid File:', self.lneInGridFile.text()) settingsStr += '\n {:30}\t{}'.format('Output Vesper Folder:', self.lneVesperFold.text()) settingsStr += '\n {:30}\t{}'.format(self.cboMethod.currentText(), '') if self.cboMethod.currentText() == 'High Density Kriging': settingsStr += '\n {:30}\t{}'.format('Block Kriging Size:', int(self.dsbBlockKrigSize.value())) else: settingsStr += '\n {:30}\t{}'.format('Variogram File:', self.lneVariogramFile.text()) settingsStr += '\n {:30}\t{}'.format('Min Point Number:', self.lneMinPoint.text()) settingsStr += '\n {:30}\t{}'.format( 'Display Vesper Graphics:', self.chkDisplayGraphics.isChecked()) settingsStr += '\n {:30}\t{}'.format( 'Run Vesper Now:', self.gbRunVesper.isChecked()) if self.gbRunVesper.isChecked(): settingsStr += '\n {:30}\t{}'.format('Import Vesper Files to Rasters:', self.chkVesper2Raster.isChecked()) if self.chkVesper2Raster.isChecked(): settingsStr += '\n {:30}\t{}'.format( 'Vesper Files Coordinate System:', self.lblInCRS.text()) LOGGER.info(settingsStr) # get a fresh dataframe for the input csv file self.dfCSV = pd.read_csv(self.lneInCSVFile.text()) vc = VesperControl() if self.cboMethod.currentText() == 'High Density Kriging': vc.update(xside=int(self.dsbBlockKrigSize.value()), yside=int(self.dsbBlockKrigSize.value())) else: # from the variogram text file find and update the control file keys vario = self.parse_variogram_file() vesp_keys = {key: val for key, val in vario.items() if key in vc} vc.update(vesp_keys) # apply the other keys. vc.update({'jpntkrg': 1, 'jlockrg': 0, 'minpts': int(self.lneMinPoint.text()), 'maxpts': len(self.dfCSV), 'jcomvar': 0, }) epsg = int(self.in_qgscrs.authid().replace('EPSG:', '')) bat_file, ctrl_file = prepare_for_vesper_krige(self.dfCSV, self.cboKrigColumn.currentText(), self.lneInGridFile.text(), self.lneVesperFold.text(), control_textfile=self.lneCtrlFile.text(), coord_columns=[], epsg=epsg, display_graphics=self.chkDisplayGraphics.isChecked(), control_options=vc) epsg = 0 if self.in_qgscrs is not None and self.chkVesper2Raster.isChecked(): epsg = int(self.in_qgscrs.authid().replace('EPSG:', '')) if self.gbRunVesper.isChecked(): # Add to vesper queue self.vesp_dict = {'control_file': ctrl_file, 'epsg': epsg} else: message = 'Successfully created files for Vesper kriging. ' \ 'The control file is {}'.format(ctrl_file) self.send_to_messagebar(message, level=QgsMessageBar.SUCCESS, duration=0, addToLog=True, core_QGIS=True) LOGGER.info('Successfully created files for Vesper kriging') QtGui.qApp.restoreOverrideCursor() return super(PreVesperDialog, self).accept(*args, **kwargs) except Exception as err: QtGui.qApp.restoreOverrideCursor() self.cleanMessageBars(True) self.send_to_messagebar(str(err), level=QgsMessageBar.CRITICAL, duration=0, addToLog=True, showLogPanel=True, exc_info=sys.exc_info()) return False
def test_value_errors(self): vc = VesperControl(title='This is a test tile') # test the ValueErrors ------------------------------------------- # test invalid option on the value with self.assertRaises(ValueError) as msg: vc.update(jpntkrg=4) self.assertEqual( "4 is an invalid option for jpntkrg. Options are {'Punctual': 1, " "'Block': 0, 'Point': 1}", str(msg.exception)) # test in valid value string with self.assertRaises(ValueError) as msg: vc.update(jpntkrg='Pnt') self.assertEqual( "Pnt is an invalid option for jpntkrg. Options are {'Punctual': 1, " "'Block': 0, 'Point': 1}", str(msg.exception)) # test string instead of number with self.assertRaises(ValueError) as msg: vc.update(icol_x='1') self.assertEqual("value for key icol_x should be a int - Got str", str(msg.exception)) # make sure lookups work vc.update(modtyp='Matern') self.assertEqual(7, vc['modtyp']) vc.update({ 'datfil': 'MyDataFile.txt', 'outdir': "'c:/data/temp'", 'modtyp': 'Exponential', 'minpts': 150, 'maxpts': 200 })
def test_updated_keys(self): vc = VesperControl(title='This is a test tile') self.assertEqual(1, len(vc.updated_keys())) vc.update({'datfil': 'MyDataFile.txt', 'outdir': 'c:/data/temp'}) self.assertEqual(3, len(vc.updated_keys()))
def test2_prepareForVesperKrig_LowDensity(self): file_csv = os.path.realpath(this_dir + '/area2_lowdensity_points.csv') grid_file = os.path.realpath(this_dir + "/rasters/area2_5m_blockgrid_v.txt") data_col = 'Hand_Sample' csv_desc = CsvDescribe(file_csv) ctrl_para = VesperControl() ctrl_para.update({ 'jpntkrg': 1, 'jlockrg': 0, 'minpts': csv_desc.row_count - 2, 'maxpts': csv_desc.row_count, 'jcomvar': 0, 'modtyp': 'Spherical', 'iwei': 'no_pairs/variance', 'CO': 92.71, 'C1': 277.9, 'A1': 116.0 }) file_bat, file_ctrl = prepare_for_vesper_krige( csv_desc.open_pandas_dataframe(), data_col, grid_file, TEMPDIR, control_textfile='test_low_control.txt', control_options=ctrl_para, coord_columns=[], epsg=28354) if os.path.exists(kriging_ops.vesper_exe): self.assertTrue( os.path.exists(os.path.join(TEMPDIR, 'Vesper/Do_Vesper.bat'))) else: self.assertEqual('', file_bat) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_low_control.txt'))) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_low_vesperdata.csv'))) df_csv = pd.read_csv( os.path.join(TEMPDIR, 'Vesper', 'test_low_vesperdata.csv')) x_column, y_column = predictCoordinateColumnNames(df_csv.columns) self.assertEqual('EASTING', x_column.upper()) self.assertEqual('NORTHING', y_column.upper()) with open(file_ctrl) as r_file: data = r_file.read() self.assertIn("outfil='test_low_kriged.txt'", data) self.assertIn("outdir=''", data) self.assertIn("jpntkrg=1", data) self.assertIn("jlockrg=0", data) self.assertIn("minpts=198", data) self.assertIn("maxpts=200", data) self.assertIn("modtyp=1", data) self.assertIn("jcomvar=0", data) self.assertIn("iwei=3", data) self.assertIn("CO=92.71", data) del data src_df = pd.read_csv( os.path.realpath(this_dir + '/VESPER/low_vesperdata.csv')) test_df = pd.read_csv( os.path.join(TEMPDIR, 'Vesper', 'test_low_vesperdata.csv')) pd.testing.assert_frame_equal(src_df, test_df) with open(os.path.realpath(this_dir + '/VESPER/low_control.txt')) as src_file,\ open(os.path.join(TEMPDIR, 'Vesper', 'test_low_control.txt')) as test_file: self.assertEqual(src_file.readlines()[11:], test_file.readlines()[11:])