def test_fence_from_vertical_polygon(): """Test fence from polygon which only has vertical samples, e.g. a vertical well""" pol = Polygons() mypoly = { pol.xname: [0.0, 0.0, 0.0], pol.yname: [100.0, 100.0, 100.0], pol.zname: [0, 50, 60], pol.pname: [1, 1, 1], } pol.dataframe = pd.DataFrame(mypoly) fence = pol.get_fence(distance=10, nextend=1, name="SOMENAME", asnumpy=False, atleast=3) logger.info(fence.dataframe.to_string()) assert len(fence.dataframe) == 161 assert fence.dataframe.H_DELTALEN.mean() == 0.125 assert fence.dataframe.H_DELTALEN.std() <= 0.001 assert fence.dataframe.H_CUMLEN.max() == 10 assert fence.dataframe.H_CUMLEN.min() == -10.0
def test_fence_from_almost_vertical_polygon(): """Test fence from polygon which only has close to vertical samples""" pol = Polygons() mypoly = { pol.xname: [0.1, 0.2, 0.3], pol.yname: [100.1, 100.2, 100.3], pol.zname: [0, 50, 60], pol.pname: [1, 1, 1], } pol.dataframe = pd.DataFrame(mypoly) fence = pol.get_fence(distance=10, nextend=1, name="SOMENAME", asnumpy=False, atleast=3) logger.info(fence.dataframe.to_string()) assert len(fence.dataframe) == 145 assert fence.dataframe.H_DELTALEN.mean() == pytest.approx(0.1414, abs=0.01) assert fence.dataframe.H_DELTALEN.std() <= 0.001 assert fence.dataframe.H_CUMLEN.max() == pytest.approx(10.0, abs=0.5) assert fence.dataframe.H_CUMLEN.min() == pytest.approx(-10.0, abs=0.5)
def test_fence_from_polygon(): """Test polygons get_fence method""" pol = Polygons(POLSET2) df = pol.dataframe[0:3] df.at[0, "X_UTME"] = 0.0 df.at[1, "X_UTME"] = 100.0 df.at[2, "X_UTME"] = 100.0 df.at[0, "Y_UTMN"] = 20.0 df.at[1, "Y_UTMN"] = 20.0 df.at[2, "Y_UTMN"] = 100.0 df.at[0, "Z_TVDSS"] = 0.0 df.at[1, "Z_TVDSS"] = 1000.0 df.at[2, "Z_TVDSS"] = 2000.0 pol.dataframe = df fence = pol.get_fence(distance=100, nextend=4, name="SOMENAME", asnumpy=False, atleast=10) logger.info(fence.dataframe) assert fence.dataframe.H_DELTALEN.mean() == pytest.approx(19.98, abs=0.01) assert fence.dataframe.H_DELTALEN.std() <= 0.05
def test_fence_from_polygon(): """Test polygons get_fence method""" pol = Polygons(POLSET2) df = pol.dataframe[0:3] df.at[0, "X_UTME"] = 0.0 df.at[1, "X_UTME"] = 100.0 df.at[2, "X_UTME"] = 100.0 df.at[0, "Y_UTMN"] = 20.0 df.at[1, "Y_UTMN"] = 20.0 df.at[2, "Y_UTMN"] = 100.0 df.at[0, "Z_TVDSS"] = 0.0 df.at[1, "Z_TVDSS"] = 1000.0 df.at[2, "Z_TVDSS"] = 2000.0 pol.dataframe = df fence = pol.get_fence( distance=100, nextend=4, name="SOMENAME", asnumpy=False, atleast=10 ) logger.info(fence.dataframe) tsetup.assert_almostequal(fence.dataframe.at[13, "X_UTME"], -202.3, 0.01)
def test_raise_non_existing_name(name_attribute): pol = Polygons() data = pd.DataFrame({"ANYTHING": [1, 2]}) pol.dataframe = data with pytest.raises(ValueError) as execinfo: setattr(pol, name_attribute, "NON_EXISTING") assert "does not exist" in str(execinfo.value)
def test_raise_incorrect_name_type(test_name, name_attribute): pol = Polygons() data = pd.DataFrame({"ANYTHING": [1, 2]}) pol.dataframe = data with pytest.raises(ValueError) as execinfo: setattr(pol, name_attribute, test_name) assert "Wrong type of input" in str(execinfo.value)
def test_check_multi_polygon_in_single_polygon(): inner_pols = Polygons(SMALL_POLY_INNER + SMALL_POLY_OVERLAP_INNER) outer_pol = Polygons(SMALL_POLY_OUTER) inner_pols.operation_polygons(outer_pol, value=1, opname="eli", inside=True) assert len(inner_pols.dataframe) == 0
def test_polygon_boundary(): """Import XYZ polygons from file and test boundary function.""" mypoly = Polygons() mypoly.from_file(PFILE, fformat="xyz") boundary = mypoly.get_boundary() tsetup.assert_almostequal(boundary[0], 460595.6036, 0.0001) tsetup.assert_almostequal(boundary[4], 2025.952637, 0.0001) tsetup.assert_almostequal(boundary[5], 2266.996338, 0.0001)
def test_get_polygons_one_well(): """Import a well and get the polygon segments.""" wlist = [] for w in glob.glob(str(WFILES1)): wlist.append(Well(w, zonelogname="Zonelog")) logger.info("Imported well {}".format(w)) mypoly = Polygons() nwell = mypoly.from_wells(wlist, 2) print(mypoly.dataframe) logger.info("Number of well made to tops: {}".format(nwell)) mypoly.to_file("TMP/poly_w1.irapasc")
def test_get_polygons_many_wells(): """Import some wells and get the polygon segments.""" wlist = [] for w in glob.glob(str(WFILES2)): wlist.append(Well(w, zonelogname="Zonelog")) print("Imported well {}".format(w)) mypoly = Polygons() nwell = mypoly.from_wells(wlist, 2, resample=10) print(mypoly.dataframe) print("Number of well made to tops: {}".format(nwell)) mypoly.to_file("TMP/poly_w1_many.irapasc")
def test_get_polygons_many_wells(): """Import some wells and get the polygon segments""" wlist = [] for w in glob.glob(wfiles2): wlist.append(Well(w, zonelogname='Zonelog')) print('Imported well {}'.format(w)) mypoly = Polygons() nwell = mypoly.from_wells(wlist, 2, resample=10) print(mypoly.dataframe) print('Number of well made to tops: {}'.format(nwell)) mypoly.to_file('TMP/poly_w1_many.irapasc')
def test_avg02(): """Make average map from Reek Eclipse.""" grd = Grid() grd.from_file(GFILE2, fformat="egrid") # get the poro po = GridProperty() po.from_file(IFILE2, fformat="init", name="PORO", grid=grd) # get the dz and the coordinates dz = grd.get_dz(mask=False) xc, yc, _zc = grd.get_xyz(mask=False) # get actnum actnum = grd.get_actnum() # convert from masked numpy to ordinary xcuse = np.copy(xc.values3d) ycuse = np.copy(yc.values3d) dzuse = np.copy(dz.values3d) pouse = np.copy(po.values3d) # dz must be zero for undef cells dzuse[actnum.values3d < 0.5] = 0.0 pouse[actnum.values3d < 0.5] = 0.0 # make a map... estimate from xc and yc zuse = np.ones((xcuse.shape)) avgmap = RegularSurface( nx=200, ny=250, xinc=50, yinc=50, xori=457000, yori=5927000, values=np.zeros((200, 250)), ) avgmap.avg_from_3dprop( xprop=xcuse, yprop=ycuse, zoneprop=zuse, zone_minmax=(1, 1), mprop=pouse, dzprop=dzuse, truncate_le=None, ) # add the faults in plot fau = Polygons(FFILE1, fformat="zmap") fspec = {"faults": fau} avgmap.quickplot(filename="TMP/tmp_poro2.png", xlabelrotation=30, faults=fspec) avgmap.to_file("TMP/tmp.poro.gri", fformat="irap_ascii") logger.info(avgmap.values.mean()) assert avgmap.values.mean() == pytest.approx(0.1653, abs=0.01)
def test_values_in_polygon(): """Test replace values in polygons""" xprop = GridProperty() logger.info("Import roff...") grid = Grid(TESTFILE5) xprop.from_file(TESTFILE1, fformat="roff", name="PORO", grid=grid) poly = Polygons(POLYFILE) xprop.geometry = grid xorig = xprop.copy() xprop.operation_polygons(poly, 99, inside=True) tsetup.assert_almostequal(xprop.values.mean(), 25.1788, 0.01) xp2 = xorig.copy() xp2.values *= 100 xp2.continuous_to_discrete() xp2.set_inside(poly, 44) xp2.dtype = np.uint8 xp2.set_inside(poly, 44) print(xp2.values) xp2.dtype = np.uint16 xp2.set_inside(poly, 44) print(xp2.values) xp3 = xorig.copy() xp3.values *= 100 print(xp3.values.mean()) xp3.dtype = np.float32 xp3.set_inside(poly, 44) print(xp3.values.mean()) tsetup.assert_almostequal(xp3.values.mean(), 23.40642788381048, 0.001)
def test_rename_columns(): """Renaming xname, yname, zname""" pol = Polygons(POLSET2) assert pol.xname == "X_UTME" pol.xname = "NEWX" assert pol.xname == "NEWX" assert "NEWX" in pol.dataframe pol.yname = "NEWY" assert pol.yname == "NEWY" assert pol.xname != "NEWY" assert "NEWY" in pol.dataframe
def test_get_polygons_one_well(): """Import a well and get the polygon segments""" wlist = [] for w in glob.glob(wfiles1): wlist.append(Well(w, zonelogname='Zonelog')) logger.info('Imported well {}'.format(w)) mypoly = Polygons() nwell = mypoly.from_wells(wlist, 2) print(mypoly.dataframe) logger.info('Number of well made to tops: {}'.format(nwell)) mypoly.to_file('TMP/poly_w1.irapasc')
def test_polygons_overlap(): pol = Polygons(SMALL_POLY_INNER + SMALL_POLY_OVERLAP_INNER) # The Four points are placed: within first poly, within the overlap, within the # second poly, outside both poly poi = Points([(3.5, 3.5, 0.0), (4.5, 4.5, 0.0), (5.5, 5.5, 0.0), (6.5, 6.5, 0.0)]) poi.operation_polygons(pol, value=1, opname="eli", inside=True) assert len(poi.dataframe) == 1
def test_check_multi_point_single_polygon_zdir(): pol = Polygons(SMALL_POLY_INNER) poi = Points([(4.0, 4.0, 0.0), (4.0, 4.0, 4.0), (4.0, 4.0, 8.0)]) assert len(poi.dataframe) == 3 # Note z-direction has no effect. All points are deleted. poi.operation_polygons(pol, value=1, opname="eli", inside=True) assert len(poi.dataframe) == 0
def test_check_single_point_in_polygon(): pol = Polygons(SMALL_POLY_INNER) poi = Points([(4.0, 4.0, 4.0)]) poi.operation_polygons(pol, value=1, opname="eli", inside=False) assert len(poi.dataframe) == 1 poi.operation_polygons(pol, value=1, opname="eli", inside=True) assert len(poi.dataframe) == 0
def test_polygon_tlen_hlen(testpath): """Test the tlen and hlen operations""" pol = Polygons(testpath / POLSET3) pol.tlen() pol.hlen() assert pol.dataframe[pol.hname].all() <= pol.dataframe[pol.tname].all() assert pol.dataframe[pol.hname].any() <= pol.dataframe[pol.tname].any() pol.filter_byid(0) hlen = pol.get_shapely_objects()[0].length # shapely length is 2D! assert (abs(pol.dataframe[pol.hname].iloc[-1] - hlen)) < 0.001 assert (abs(pol.dataframe[pol.dhname].iloc[0] - 1761.148)) < 0.01
def test_oper_points_inside_overlapping_polygon(oper, expected): pol = Polygons(SMALL_POLY_INNER + SMALL_POLY_OVERLAP_INNER) # The Four points are placed: within first poly, within the overlap, within the # second poly, outside both poly poi = Points([(3.5, 3.5, 10.0), (4.5, 4.5, 10.0), (5.5, 5.5, 10.0), (6.5, 6.5, 10.0)]) # Operations are performed n times, where n reflects the number of polygons a # point is outside poi.operation_polygons(pol, value=2, opname=oper, inside=False) assert list(poi.dataframe[poi.zname].values) == expected
def test_check_multi_point_multi_polyon_outside_op(): pol = Polygons(SMALL_POLY_INNER + LARGE_POLY_SHIFTED) # Two points in small cube, one in large cube, one outside poi = Points([(4.0, 4.0, 0.0), (4.5, 4.0, 0.0), (7.0, 7.0, 0.0), (20.0, 5.0, 0.0)]) assert len(poi.dataframe) == 4 # Note the operation will loop over the polygons, and hence remove the points # in the small polygon when considering the large polygon, and vice versa poi.operation_polygons(pol, value=1, opname="eli", inside=False) assert len(poi.dataframe) == 0
def test_operation_inclusive_polygon(): pol = Polygons(SMALL_POLY_INNER) # We place a point on the edge of a polygon poi = Points([(4.0, 4.0, 0.0)]) poi.operation_polygons(pol, value=1, opname="eli", inside=True) assert len(poi.dataframe) == 0 # We place a point on a corner of a polygon poi = Points([(3.0, 3.0, 0.0)]) poi.operation_polygons(pol, value=1, opname="eli", inside=True) assert len(poi.dataframe) == 0
def test_fence_from_more_slanted_polygon(): """Test fence from poly which is slanted; and total HLEN is > than distance""" pol = Polygons() mypoly = { pol.xname: [0.0, 7.0, 15.0], pol.yname: [100.0, 110.0, 120.0], pol.zname: [0, 50, 60], pol.pname: [1, 1, 1], } pol.dataframe = pd.DataFrame(mypoly) fence = pol.get_fence(distance=10, nextend=1, name="SOMENAME", asnumpy=False, atleast=3) assert len(fence.dataframe) == 5 assert fence.dataframe.H_DELTALEN.mean() == pytest.approx(12.49, abs=0.02) assert fence.dataframe.H_DELTALEN.std() <= 0.001
def test_values_in_polygon(): """Test replace values in polygons""" logger.info('Name is {}'.format(__name__)) xprop = GridProperty() logger.info("Import roff...") grid = Grid(testfile5) xprop.from_file(testfile1, fformat="roff", name='PORO', grid=grid) poly = Polygons(polyfile) xprop.geometry = grid xprop.operation_polygons(poly, 99, inside=True) tsetup.assert_almostequal(xprop.values.mean(), 25.1788, 0.01)
def test_fence_from_slanted_polygon(): """Test fence from polygon which is slanted; but total HLEN is less than distance""" pol = Polygons() mypoly = { pol.xname: [0.0, 3.0, 6.0], pol.yname: [100.0, 102.0, 104.0], pol.zname: [0, 50, 60], pol.pname: [1, 1, 1], } pol.dataframe = pd.DataFrame(mypoly) fence = pol.get_fence(distance=10, nextend=1, name="SOMENAME", asnumpy=False, atleast=3) assert len(fence.dataframe) == 9 assert fence.dataframe.H_DELTALEN.mean() == pytest.approx(3.6, abs=0.02) assert fence.dataframe.H_DELTALEN.std() <= 0.001
def test_get_polygons_one_well(testpath, tmp_path): """Import a well and get the polygon segments.""" wlist = [Well((testpath / WFILES1), zonelogname="Zonelog")] mypoly = Polygons() mypoly.from_wells(wlist, 2) mypoly.to_file(tmp_path / "poly_w1.irapasc")
def test_check_multi_point_multi_polyon_inside_op(): pol = Polygons(SMALL_POLY_INNER + LARGE_POLY_SHIFTED) # Two points in small cube, one in large cube, one outside poi = Points([(4.0, 4.0, 0.0), (4.5, 4.0, 0.0), (7.0, 7.0, 0.0), (20.0, 5.0, 0.0)]) assert len(poi.dataframe) == 4 poi.operation_polygons(pol, value=1, opname="eli", inside=True) assert len(poi.dataframe) == 1 poi = Points([(4.0, 4.0, 0.0), (4.5, 4.0, 0.0), (7.0, 7.0, 0.0), (20.0, 5.0, 0.0)]) poi.operation_polygons(pol, value=1, opname="eli", inside=False) assert len(poi.dataframe) == 0
def test_avg03(): """Make average map from Reek Eclipse, speed up by zone_avg.""" g = Grid() g.from_file(gfile2, fformat="egrid") # get the poro po = GridProperty() po.from_file(ifile2, fformat='init', name='PORO', grid=g) # get the dz and the coordinates dz = g.get_dz(mask=False) xc, yc, zc = g.get_xyz(mask=False) # get actnum actnum = g.get_actnum() actnum = actnum.get_npvalues3d() # convert from masked numpy to ordinary xcuse = xc.get_npvalues3d() ycuse = yc.get_npvalues3d() dzuse = dz.get_npvalues3d(fill_value=0.0) pouse = po.get_npvalues3d(fill_value=0.0) # dz must be zero for undef cells dzuse[actnum < 0.5] = 0.0 pouse[actnum < 0.5] = 0.0 # make a map... estimate from xc and yc zuse = np.ones((xcuse.shape)) avgmap = RegularSurface(nx=200, ny=250, xinc=50, yinc=50, xori=457000, yori=5927000, values=np.zeros((200, 250))) avgmap.avg_from_3dprop(xprop=xcuse, yprop=ycuse, zoneprop=zuse, zone_minmax=(1, 1), mprop=pouse, dzprop=dzuse, truncate_le=None, zone_avg=True) # add the faults in plot fau = Polygons(ffile1, fformat='zmap') fspec = {'faults': fau} avgmap.quickplot(filename='TMP/tmp_poro3.png', xlabelrotation=30, faults=fspec) avgmap.to_file('TMP/tmp.poro3.gri', fformat='irap_ascii') logger.info(avgmap.values.mean()) assert avgmap.values.mean() == pytest.approx(0.1653, abs=0.01)
def test_points_in_polygon(testpath): """Import XYZ points and do operations if inside or outside""" poi = Points(testpath / POINTSET2) pol = Polygons(testpath / POLSET2) assert poi.nrow == 30 # remove points in polygon poi.operation_polygons(pol, 0, opname="eli", where=True) assert poi.nrow == 19 poi = Points(testpath / POINTSET2) # remove points outside polygon poi.operation_polygons(pol, 0, opname="eli", inside=False, where=True) assert poi.nrow == 1
def test_get_polygons_many_wells(testpath, tmp_path): """Import some wells and get the polygon segments.""" wlist = [ Well(wpath, zonelogname="Zonelog") for wpath in glob.glob(str(testpath / WFILES2)) ] mypoly = Polygons() mypoly.from_wells(wlist, 2, resample=10) mypoly.to_file(tmp_path / "poly_w1_many.irapasc")