def create_image(R): ''' Create a image in which the maximum pixels form a circle having a radius R Parameters ---------- R : float Radius of the circle Returns ------- img_xy : RectBivariate object A narray whose values at non-integer coordinates are available thanks to interpolation imsh : ImShape object A object which helps to display the contour and the image ''' img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = circle(R, 0, 0) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x, y = img.arrf.sampling_vectors() img_xy = interpolate.RectBivariateSpline(x, y, np.flipud(img.arr)) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) return img_xy, imsh
def initialize(): # initialize the spline c = np.array([0, 0]) R = 100.0 nbr_init = 30 phi = np.linspace(0, 2.0 * np.pi, nbr_init) x_init = c[0] + R * np.cos(phi) y_init = c[1] + R * np.sin(phi) x_init[nbr_init - 1] = x_init[0] y_init[nbr_init - 1] = y_init[0] tck, _ = splprep([x_init, y_init], s=0, per=1) spl = BSpline_periodic(tck[0], np.transpose(tck[1]), tck[2], is_periodic=True) # create an image img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = circle(100, 0, 0) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} tmp = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x, y = tmp.arrf.sampling_vectors() img = RectBivariateSpline(x, y, np.flipud(tmp.arr)) return spl, img
def comparer(): ''' Create an image and compare the interpolation from different methods ''' img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = SGeom(box(-100, -50, 100, 50)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x_s, y_s = img.arrf.sampling_vectors() r, c = img.xy_to_rc(100, 50) interp = interp2d(x_s, y_s, np.flipud(img.arr)) bivar1 = RectBivariateSpline(x=x_s, y=y_s, z=np.flipud(img.arr)) bivar2 = RectBivariateSpline(x=y_s, y=x_s, z=np.flipud(img.arr)) print interp(100, 50) print img.arr[r, c] print bivar1(50, 100), bivar1(100, 50) print bivar2(50, 100), bivar2(100, 50) img.plot() plt.scatter(100, 50) plt.show()
def create_rect(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = SGeom(box(-100, -50, 100, 50)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) img.plot() plt.show()
def create_RGInterp(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = SGeom(box(-100, -50, 100, 50)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x, y = img.arrf.sampling_vectors() img_xy = interpolate.RegularGridInterpolator((x, y), np.flipud(img.arr)) return img_xy
def create_circle_1(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] R = 25 my_sgeom = circle(R, 0, 0) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) img.plot() plt.show()
def interp_array(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = SGeom(box(-100, -50, 100, 50)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x_s, y_s = img.arrf.sampling_vectors() bivar = RectBivariateSpline(x=x_s, y=y_s, z=np.flipud(img.arr)) x_n = np.linspace(0, 100, 10) y_n = np.linspace(100, 200, 10) print bivar(x_n, y_n, grid=False) print bivar(0, y_n)
def create_vague_local(R, threshold=50, per=np.pi / 3): ''' Create a image in which the maximum pixels form a circular wave having a radius R Parameters ---------- R : float Radius of the circle threshold: float, optional Every pixel of which the value is less than this threshold is set to 0 per: float, optional Periodicity of the wave Returns ------- img_xy : RectBivariate object A narray whose values at non-integer coordinates are available thanks to interpolation imsh : ImShape object A object which helps to display the contour and the image ''' img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] r_nb = 200 rough_dict = {'lwr': 10} my_sgeom = rand_circle(R, 0, 0, r_nb, rough_dict, per) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) img.arr[img.arr < threshold] = 0 x, y = img.arrf.sampling_vectors() img_xy = interpolate.RectBivariateSpline(x, y, np.flipud(img.arr)) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) return img_xy, imsh
def create_fluctuation(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] R = 25 r_nb = 150 rough_dict = {'lwr': 10} r_per = np.pi / 3 my_sgeom = rand_circle(R, 0, 0, r_nb, rough_dict, r_per) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) imsh.plot() plt.show()
def create_circle_2(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] R = 25 c = np.array([0, 0]) phi = np.linspace(0, 2 * np.pi, 1000) x = c[0] + R * np.cos(phi) y = c[1] + R * np.sin(phi) dat = np.array([x, y]).T my_sgeom = SGeom(Polygon(dat)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) img.plot() plt.show()
def create_2ob(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] R = 25 my_sgeom_1 = circle(R, -100, 0) my_sgeom_2 = SGeom(box(0, 0, 100, 50)) my_geom = MultiPolygon([my_sgeom_1.geom, my_sgeom_2.geom]) my_sgeom = SGeom(my_geom, label='circle_square') strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) imsh.plot() plt.show()
def regu_interp(): # Comparison between RegularGridInterpolator and RectBivariateSpline img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = SGeom(box(-100, -50, 100, 50)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x_s, y_s = img.arrf.sampling_vectors() reg = RegularGridInterpolator((x_s, y_s), np.flipud(img.arr)) bivar = RectBivariateSpline(x=x_s, y=y_s, z=np.flipud(img.arr)) spl = initialize(40) pts = spl(np.linspace(0, 1, 10)) print pts[0] print bivar(245, 244) print reg([245, 244])
def create_noisy_circle(R): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = circle(R, 0, 0) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) mu, sigma = 0, 10 noise = np.random.normal(mu, sigma, img.arr.shape).astype(float) noisy_img = img.arr + noise x, y = img.arrf.sampling_vectors() img_xy = interpolate.RectBivariateSpline(x, y, np.flipud(noisy_img)) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) imsh.plot() plt.show()
def create_rect(): ''' Create a image in which the maximum pixels form a rectangular ''' img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] my_sgeom = SGeom(box(-100, -50, 100, 50)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x, y = img.arrf.sampling_vectors() img_xy = interpolate.RectBivariateSpline(x, y, np.flipud(img.arr)) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) return img_xy, imsh
def plot_coll(): img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] R = 25 c = np.array([0, 0]) phi = np.linspace(0, 2 * np.pi, 1000) x = c[0] + R * np.cos(phi) y = c[1] + R * np.sin(phi) dat = np.array([x, y]).T my_sgeom = SGeom(Polygon(dat)) strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) imsh.plot() plt.show()
def create_2ob(): ''' Create a image in which the maximum pixels form 2 objects ''' img_shape = (512, 512) frame_bounds = [-256, 256, -256, 256] R = 25 my_sgeom_1 = circle(R, -100, 0) my_sgeom_2 = SGeom(box(0, 0, 100, 50)) my_geom = MultiPolygon([my_sgeom_1.geom, my_sgeom_2.geom]) my_sgeom = SGeom(my_geom, label='circle_square') strat = lscad.MountainStrategy() param_dict = {'hmax': 100., 'sigma': (0, 0)} img = fk_img.img_from_sgeom(img_shape, frame_bounds, my_sgeom, z_map_strategy=strat, param_dict=param_dict) x, y = img.arrf.sampling_vectors() img_xy = interpolate.RectBivariateSpline(x, y, np.flipud(img.arr)) gcoll = GeomCollection() gcoll.set_geom(0, my_sgeom) imsh = ImShape(image=img, geom_coll=gcoll) return img_xy, imsh