def setup(self): self.img = np.asanyarray(Image.open("moonmap_tiny.png").convert("L")) self.craters = mkin.ReadCombinedCraterCSV(dropfeatures=True) cx, cy = mkin.coord2pix(self.craters["Long"].as_matrix(), self.craters["Lat"].as_matrix(), [-180, 180, -90, 90], [self.img.shape[1], self.img.shape[0]]) self.craters["x"] = cx self.craters["y"] = cy self.craters["Diameter (pix)"] = \ self.craters["Diameter (km)"]*mkin.km2pix(self.img.shape[0], 180) self.craters.drop(np.where(self.craters["Diameter (pix)"] < 15.)[0], inplace=True) self.craters.reset_index(inplace=True) self.img2 = np.zeros([200, 200]) crat_x_list = np.array([100, 50, 2, 4, 167, 72, 198, 1]) crat_y_list = np.array([100, 50, 1, 191, 3, 199, 198, 199]) self.craters2 = pd.DataFrame([crat_x_list, crat_y_list]).T.rename(columns={ 0: "x", 1: "y" }) for i in range(len(crat_x_list)): self.img2[crat_y_list[i], crat_x_list[i]] += 1 # add one at crater location
def test_warpcraters(self): # Not the real image dimensions, but whatever imgdim = [250, 300] ilong = self.craters["Long"].as_matrix() ilat = self.craters["Lat"].as_matrix() res = self.oproj.transform_points(x=ilong, y=ilat, src_crs=self.geoproj)[:, :2] # Get output x, y = mkin.coord2pix(res[:, 0], res[:, 1], self.oextent, imgdim, origin="upper") ctr_sub = mkin.WarpCraterLoc(self.craters, self.geoproj, self.oproj, self.oextent, imgdim, llbd=None, origin="upper") xy_gt = np.r_[x, y] xy = np.r_[ctr_sub["x"].as_matrix(), ctr_sub["y"].as_matrix()] self.assertTrue(np.all(np.isclose(xy, xy_gt, rtol=1e-7, atol=1e-10)))
def test_pix2coord(self, origin): x, y = mkin.coord2pix(self.cx, self.cy, self.cdim, self.imgdim, origin=origin) cx, cy = mkin.pix2coord(x, y, self.cdim, self.imgdim, origin=origin) cxy = np.r_[cx, cy] cxy_gt = np.r_[self.cx, self.cy] assert np.all(np.isclose(cxy, cxy_gt, rtol=1e-7, atol=1e-10))
def test_pix2coord(self): for origin in ["lower", "upper"]: with self.subTest(origin=origin): x, y = mkin.coord2pix(self.cx, self.cy, self.cdim, self.imgdim, origin=origin) cx, cy = mkin.pix2coord(x, y, self.cdim, self.imgdim, origin=origin) cxy = np.r_[cx, cy] cxy_gt = np.r_[self.cx, self.cy] self.assertTrue( np.all(np.isclose(cxy, cxy_gt, rtol=1e-7, atol=1e-10)))
def test_coord2pix(self, origin): x_gt = (self.imgdim[0] * (self.cx - self.cdim[0]) / (self.cdim[1] - self.cdim[0])) y_gt = (self.imgdim[1] * (self.cy - self.cdim[2]) / (self.cdim[3] - self.cdim[2])) yi_gt = (self.imgdim[1] * (self.cdim[3] - self.cy) / (self.cdim[3] - self.cdim[2])) x, y = mkin.coord2pix(self.cx, self.cy, self.cdim, self.imgdim, origin=origin) if origin == "upper": y_gt_curr = yi_gt else: y_gt_curr = y_gt xy = np.r_[x, y] xy_gt = np.r_[x_gt, y_gt_curr] assert np.all(np.isclose(xy, xy_gt, rtol=1e-7, atol=1e-10))
def test_coord2pix(self): x_gt = self.imgdim[0] * \ (self.cx - self.cdim[0]) / (self.cdim[1] - self.cdim[0]) y_gt = self.imgdim[1] * \ (self.cy - self.cdim[2]) / (self.cdim[3] - self.cdim[2]) yi_gt = self.imgdim[1] * \ (self.cdim[3] - self.cy) / (self.cdim[3] - self.cdim[2]) for origin in ["lower", "upper"]: with self.subTest(origin=origin): x, y = mkin.coord2pix(self.cx, self.cy, self.cdim, self.imgdim, origin=origin) if origin == "upper": y_gt_curr = yi_gt else: y_gt_curr = y_gt xy = np.r_[x, y] xy_gt = np.r_[x_gt, y_gt_curr] self.assertTrue( np.all(np.isclose(xy, xy_gt, rtol=1e-7, atol=1e-10)))