def test_func(self): """Create object """ center = (2, 0, 0) abc = (1.2, 1.1, 1.1) surfaces = [Ellipse(center, abc, n1=1.0, n2=1.3)] points = ((3, -1, 1), (3, 1, -1), (1, 1, -1)) intensity = 0.3 rays = Generator.generate_rays_3d(*points, intensity) return [[rays], surfaces]
def generator_test(): print(type(np.pi)) angles = [np.pi, np.pi, 0] shifts = [2, 1, 0] mat = Generator.rot_shift_mat(angles, shifts) print(mat) vector3D = [1, 1, 0] print("vector = " + str(vector3D)) vector3D.append(0) print("new vector " + str(np.dot(mat, vector3D)))
def get_ray_surface(logfile=None): jones_vec = (1, 1j) ss = 2 # square side (init 1.5) # 0.3 points = [[ss, ss, dist_ray], [-ss, ss, dist_ray], [ss, -ss, dist_ray]] intensity = np.sqrt(count) / (2 * ss) # = 2 ray_dir = np.asarray((0, 0, 1)) rot_mat = Generator.get_rot_mat_x_3d(init_fall_angle) ray_dir = np.dot(rot_mat, ray_dir) ray_pool = Generator.generate_rays_3d(*points, intensity=intensity, direction=ray_dir) ray_pool.set_one_to_all(jones_vec, ray_pool.compon_index_class.Jo_OFFSET) direction_vec = ray_pool.e(0) p, s = coord_tr.get_initial_ortho_basis_3d(direction_vec) ray_pool.set_one_to_all(p, ray_pool.compon_index_class.P_VEC_OFFSET) ray_pool.set_one_to_all(s, ray_pool.compon_index_class.S_VEC_OFFSET) print(f"initial \np = {p}\ns = {s}\n", file=logfile) print(ray_pool) # %% n1, n2 = 1, reft_coef # 1.9968943799848582 # 1.9968943799848582 # 1.69 half_angle = 18 half_angle = np.deg2rad(half_angle) if is_bruster_angle: half_angle = np.pi / 2 - np.arctan(n1 / n2) print("half angle is ", half_angle, np.rad2deg(half_angle)) height = -20 apex = (0, 0, -height) axicon = Axicon3D(apex, half_angle, height + dist_ray + 2, type_of_surface=Axicon3D.types.REFRACTING, n1=n1, n2=n2, polarisation_matrix_refract=PolarMat(), polarisation_matrix_reflect=PolarMat()) return ray_pool, axicon
def main2(): center = [1, 2, 1] abc = [1, 1, 1] ellipse1 = Ellipse(center=center, ellipse_coefficients=abc, type_surface=Ellipse.types.REFRACTING, n1=1, n2=1.33) points = ((0.1, 1.1, 0.1), (0.1, 2.9, 1.9), (1.9, 2.9, 0.1)) intensity = 0.1 rays = Generator.generate_rays_3d(*points, intensity) a = RaySurfaceStorage([rays], [ellipse1]) a.trace()
from tools.generators import Generator from view.matlab.matlab_ray_view2D import * from view.matlab.matlab_surface_view2D import * from surfaces.analitic.plane import Plane from surfaces.analitic.ellipse import Ellipse points = ([-1, -1], [-1, 0.5]) intensity = 5 pool = Generator.generate_rays_2d(points[0], points[1], intensity) print(pool) # %% norm = (-1, -1) radius_vectors = ( [2, 1], # [2, 0] # for ellipses [1.3, 1], [2.8, 1]) axis_coeff = ([1.8, 2.5], [0.6, 0.3], [0.6, 0.28]) refr_indexes = ((1, 1.2), (1.2, 1.3), (1.2, 1.3)) surfaces = [] # for i in range(len(radius_vectors)): # surf = Ellipse(radius_vectors[i], axis_coeff[i], # n1=refr_indexes[i][0], n2=refr_indexes[i][1], # type_surface=Plane.types.REFRACTING) # surfaces.append(surf) # print(surf) for i in range(len(radius_vectors)): surf = Plane(radius_vectors[i], normal_vector=norm,
def main(): logfile = open("focus_set.txt", mode="a") maxiter = 260 # set 2 iter and launch ab_1_1 = (0.8, 3) ab_1_2 = (1, 3) ab_2_1 = (0.8, 3) ab_2_2 = (1, 3) ab_s = [ab_1_1, ab_1_2, ab_2_1, ab_2_2] # ab_s = np.asarray([0.84669971, 2.93134167, 1.04711517, 2.67583118, 0.83392378, 2.89171014, 1.04025161, 2.98363328]) # ab_s = np.asarray([0.87053379, 2.92960797, 1.05378818, 2.65834648, 0.83555828, 2.90351797, 1.05212543, 2.97412832]) # ab_s = ab_s.reshape((4, 2)) # distance between centers r0_1 = 0.5 r0_2 = 0.5 dis_bet_lens = 1 # distance between lens # illustration # / \ / \ # \---r0_1---/---dis_bet_lens---\---r0_2---/ # 1 2 3 4 # 0 1 2 3 - indexs # ----------------------------------------> X axis center2 = (0, 0) center1 = (center2[0] + r0_1, 0) center4 = (center2[0] + ab_s[1][0] + dis_bet_lens + ab_s[3][0], 0) # center4 = (center1[0] + ab_s[1][0] + dis_bet_lens + ab_s[3][0], 0)# Think about it center3 = (center4[0] + r0_2, 0) center_s = (center1, center2, center3, center4) print("ell centers ", center_s) # n1 - outside of len, n2 - inside of len, n3 - inside of len. n1 = 1 n2 = 1.33 n3 = 1.33 n_len = (n2, n3) ellipsis = [ Ellipse(center_s[i], ab_s[i], Ellipse.types.REFRACTING, n1=n1, n2=n_len[i // 2]) for i in range(4) ] y_lim_1 = 2.8 # coordinate Y where 2 ellipse intersect y_lim_2 = 2.8 limits1 = ((center1[0] - ab_s[0][0], center1[0]), (-y_lim_1, y_lim_1)) limits2 = ((center2[0], center2[0] + ab_s[1][0]), (-y_lim_1, y_lim_1)) limits3 = ((center3[0] - ab_s[2][0], center3[0]), (-y_lim_2, y_lim_2)) limits4 = ((center4[0], center4[0] + ab_s[3][0]), (-y_lim_2, y_lim_2)) limits = [limits1, limits2, limits3, limits4] limits = DELTA_2 + limits # make borders more a little. VERY IMPORTANT lim_ell = [LimitedSurface(ellipsis[i], limits[i]) for i in range(4)] # ___________________________________________________________ # raysPool preparation y_abs = 1 gen_ray_points = [[-1.1, -y_abs], [-1.1, y_abs]] intensity = 5.5 pool = Generator.generate_rays_2d(gen_ray_points[0], gen_ray_points[1], intensity) # ___________________________________________________________ # set the needed focus point needed_focus_point = np.asarray([9, 0]) # nelder-mead optimization functions = get_functional(needed_focus_point, pool, lim_ell, center_s, logfile=logfile) functional = lambda x: functions(x)[0] beg_point = np.asarray(ab_s).ravel() file_name = f"maxiter{maxiter}.txt" if os.path.exists(file_name): print(f"File '{file_name} exist'. Read data") with open(file_name, mode="rb") as f: res = pickle.load(f) print("Data is loaded") else: res = minimize(functional, beg_point, method='nelder-mead', options={ "maxiter": maxiter, 'disp': True }, tol=2e-10) with open(file_name, mode="wb") as f: pickle.dump(res, f) print(res.x, "\n") print(res) print("ell centers ", center_s) # ======================================== dif_bet_focus, pools, sco_f, r1, h, sco, focus_point = functions(res.x) points = r1(h) # ============== PLOTTING =================== def draw(limits, points, focus_point): global fig_num # drawing pylab.figure(fig_num, figsize=(6, 5)) fig_num = fig_num + 1 # focus point xy = [[points[j][i] for j in range(len(points))] for i in range(2)] pylab.scatter(xy[0], xy[1], color="red", marker='.', alpha=0.5) pylab.scatter(focus_point[0], focus_point[1], color="purple", marker="*") # surfaces for lim_surface in lim_ell: msv.draw_exist_surface(lim_surface, "purple", 1) for ellipse in ellipsis: msv.draw_exist_surface(ellipse) # rays pools for ray_pool in pools: mvray.draw_ray_pool(ray_pool, ray_const_length=18, alpha=0.2) # config the view pylab.grid() pylab.xlim(*limits[0]) pylab.ylim(*limits[1]) # first figure borders distanse_from_border = 0.1 b_max = np.max([ab_s[i][1] for i in range(4)]) a_max = np.max([ab_s[i][0] for i in range(4)]) y_inf = np.min([center_s[i][1] for i in range(4)]) - b_max - distanse_from_border y_sup = np.max([center_s[i][1] for i in range(4)]) + b_max + distanse_from_border x_inf = np.min([center_s[i][0] for i in range(4)]) - a_max - distanse_from_border x_sup = np.max([center_s[i][0] for i in range(4)]) + a_max + distanse_from_border # second figure borders is_scale_with_sco = True if is_scale_with_sco: x_sc, y_sc = 3, 3 lim_x_sco_min = focus_point[0] - x_sc * sco lim_x_sco_max = focus_point[0] + x_sc * sco lim_y_sco_min = focus_point[1] - y_sc * sco lim_y_sco_max = focus_point[1] + y_sc * sco else: x_border, y_border = 2, 1 lim_x_sco_min = focus_point[0] - x_border lim_x_sco_max = focus_point[0] + x_border lim_y_sco_min = focus_point[1] - y_border lim_y_sco_max = focus_point[1] + y_border draw(((x_inf, x_sup + 6.2), (y_inf, y_sup)), points, focus_point) pylab.title("Ray density: %.1f, focus at (%.3f,%3.f)" % (intensity, *focus_point)) draw(((lim_x_sco_min, lim_x_sco_max), (lim_y_sco_min, lim_y_sco_max)), points, focus_point) pylab.title(f"Count of ray is {len(points)}, sco is {sco}") pylab.show()
def run_model(batch_size, num_features, train_y, dev_y, skip_training=False): # Loads generators ands runs the model train = np.memmap('vectors/train.mm', dtype='float32', mode='r', shape=(50000, num_features)) dev = np.memmap('vectors/dev.mm', dtype='float32', mode='r', shape=(25000, num_features)) test = np.memmap('vectors/test.mm', dtype='float32', mode='r', shape=(25000, num_features)) train = Generator(train, train_y, batch_size) dev_p = GeneratorX(dev, batch_size) dev = Generator(dev, dev_y, batch_size) test = GeneratorX(test, batch_size) callback_list = [ EarlyStopping(monitor='val_acc', patience=10), ModelCheckpoint(filepath='ngram_model.h5', monitor='val_acc', save_best_only=True), ReduceLROnPlateau(monitor='val_acc', factor=0.1, patience=5) ] #################################################################################################################### # The model. model = Sequential() model.add(Dense(3000, activation='relu', input_shape=(num_features, ))) model.add(Dropout(0.5)) model.add(Dense(2000, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1000, activation='softmax')) opt = RMSprop(learning_rate=0.0001) model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['acc']) model.summary() #################################################################################################################### if not skip_training: history = model.fit(train, epochs=1000, validation_data=dev, callbacks=callback_list, shuffle=True) else: history = None model.load_weights('ngram_model.h5') score = model.evaluate(dev) print(score) # Generates outputs for ensemble. predict_vec = np.memmap('vectors/dev_ngram.mm', dtype='float32', mode='w+', shape=(25000, 1000)) predict_vec[:] = model.predict(dev_p)[:] del predict_vec predict_vec2 = np.memmap('vectors/test_ngram.mm', dtype='float32', mode='w+', shape=(25000, 1000)) predict_vec2[:] = model.predict(test)[:] del predict_vec2 predictions_dev = model.predict(dev_p).argmax(axis=-1) predictions_test = model.predict(test).argmax(axis=-1) return predictions_dev, predictions_test, history
def transform_to_one_sys_coord( arr_dir: Union[List[List[Union[float, int]]], Tuple[Tuple[Union[float, int]]], np.ndarray], arr_p_b_v: Union[List[List[Union[float, int]]], Tuple[Tuple[Union[float, int]]], np.ndarray], arr_s_b_v: Union[List[List[Union[float, int]]], Tuple[Tuple[Union[float, int]]], np.ndarray], jo_vecs: Union[List[List[complex]], Tuple[Tuple[complex]], np.ndarray], p_vec=(1, 0, 0), s_vec=(0, 1, 0), log_file=None, is_debug=False): """ Rotate the directions of the rays so that each direction coincides with [0,0,1] dir and recalculate Jones vector into new system of coordinate New basis will be {p_vec, s_vec, [0,0,1]} :param arr_dir array of ray direction. Each vector must be normalized :param arr_p_b_v array of p polar basis vector :param arr_s_b_v array of s polar basis vector :param jo_vecs Jones vectors in (p,s,e) basis. e - direction of ray. :param p_vec one of global basis vector. default is [1,0,0] :param s_vec one of global basis vector. default is [0,1,0] :param log_file opening log file for middle variable values :param is_debug if it is True. The function print middle variable values into console. """ # А вот сейчас на русском потому что что-то формулы муторные # нужно повернуть вектор направления e, чтобы он совпадал с осью z # углы поворота находим исходя из тангенсов углов, которые находим через отношения сторон тетраедра # (трехмерный треугольник) с прямоугольным прямоугольником в основании, который паралелен плоскости XY и # образован через проецирование вектора направления на плоскость XZ(Beta) и плоскость YZ(Alpha). if len(arr_dir) != len(arr_s_b_v) != len(arr_p_b_v) != len(jo_vecs): raise ValueError( f"None comparable length of arrays: arr_dir({len(arr_dir)}),arr_p_b_v({len(arr_p_b_v)})" + f" arr_s_b_v({len(arr_s_b_v)}),jo_vecs{len(jo_vecs)}") arr_dir = np.asarray(arr_dir) arr_p_b_v = np.asarray(arr_p_b_v) arr_s_b_v = np.asarray(arr_s_b_v) jo_vecs = np.asarray(jo_vecs) transformed_jo_vecs = [] arr_dir_tran = arr_dir.transpose() alpha = np.arctan2(arr_dir_tran[1], arr_dir_tran[2]) # arctan(e_y/e_z) rot_mat_list = [] if log_file: print("Rotation matrix to Jones vector basis\n") # get rot matrix for i in range(len(arr_dir)): rot_x = Generator.get_rot_mat_x_3d(alpha[i]) # ccw rotated_x_dir_vec = np.dot(rot_x, arr_dir[i]) # beta = np.arctan2(arr_dir_tran[i][0], arr_dir_tran[i][2]) # rotation angle of dir vector beta = np.arctan2( rotated_x_dir_vec[0], rotated_x_dir_vec[2]) # rotation angle of rotated dir vector rot_y = Generator.get_rot_mat_y_3d(-beta) # cw rot_mat_list.append(np.dot(rot_y, rot_x)) del arr_dir_tran del alpha # rot p and s vector and recalculate Jones vector for i in range(len(arr_dir)): # rotate s and p and recalculate Jones vector s_vec_rot = np.dot(rot_mat_list[i], arr_s_b_v[i]) p_vec_rot = np.dot(rot_mat_list[i], arr_p_b_v[i]) if log_file or is_debug: print(f"{i}:\nP: {arr_p_b_v[i]} S: {arr_s_b_v[i]}\n ", file=log_file) print(f"{rot_mat_list[i]}") print(f"P_new: {p_vec_rot} S_new: {s_vec_rot}\n\n") # recalculate basis vector with 3d rotation transform # trasformed_jo_vec = transform_jo_vec(arr_s_b_v[0], arr_p_b_v[0], s_vec_rot, p_vec_rot, jo_vecs[i]) # recalculate basis vector with 2d rotation transform # trasformed_jo_vec = transform_jo_vec(s_vec_rot, p_vec_rot, s_vec, p_vec, trasformed_jo_vec) trasformed_jo_vec = transform_jo_vec(s_vec_rot, p_vec_rot, s_vec, p_vec, jo_vecs[i]) transformed_jo_vecs.append(trasformed_jo_vec) # transformed_jo_vecs.append(jo_vecs[i]) if log_file or is_debug: print("\n\n\n\n") return transformed_jo_vecs
pylab.xlim(x_inf, x_sup) pylab.ylim(y_inf, y_sup) pylab.show() # ___________________________________________________________ # raysPool preparation y_abs = 1 gen_ray_points = [ [-1.1, -y_abs], [-1.1, y_abs] ] intensity = 5.5 pool = Generator.generate_rays_2d(gen_ray_points[0], gen_ray_points[1], intensity) # modeling and calculating optical path # pools = rpmc.tracing_rayspool_ordered_surface(pool, lim_ell) pools = rpmc.tracing_rayspool_ordered_surface(pool, lim_ell, is_set_optical_path=True) # find the refraction coefficient(refractive_indexes) there goes the last RaysPool last_RaysPool: RaysPool = pools[len(pools) - 1] # index 0 and length DELTA = 0.1 are random T_val = 0.1 point = last_RaysPool.calc_point_of_ray(0, T_val) refr_coef = lim_ell[len(lim_ell) - 1].get_refractive_indexes(point)[0] print("refr_coef(%s) = %s" % (str(point), str(refr_coef))) sco_f, r1 = get_sco_func(pools, refr_coef) # minimize the sco
def main(args): print('Settings:') print(str(args)[10:-1]) train_x, dev_x, test_x = load_asts(args.data) # Load asts and labels train_y, dev_y, test_y = load_all_labels(args.data) if not args.no_recalc: # Recalculate the vectors for each ast vectorizer = ASTVectorizer(args.ngram, args.features, head_only=args.head) print('Training vectorizer') vectorizer.fit(train_x) # Train vectoriser on training set print('Fitting') train_x = vectorizer.transform( train_x) # Convert train, dev and test and save them to memmaps. fp = np.memmap('vectors/train.mm', dtype='float32', mode='w+', shape=(50000, args.features)) fp[:] = train_x[:] del fp # Flushes memmap to file dev_x = vectorizer.transform(dev_x) fp = np.memmap('vectors/dev.mm', dtype='float32', mode='w+', shape=(25000, args.features)) fp[:] = dev_x[:] del fp test_x = vectorizer.transform(test_x) fp = np.memmap('vectors/test.mm', dtype='float32', mode='w+', shape=(25000, args.features)) fp[:] = test_x[:] del fp if args.mode == 'c': # Use minmax scaler for raw counts rescale(args.features) train = np.memmap('vectors/train.mm', dtype='float32', mode='r', shape=(50000, args.features)) dev = np.memmap('vectors/dev.mm', dtype='float32', mode='r', shape=(25000, args.features)) test = np.memmap('vectors/test.mm', dtype='float32', mode='r', shape=(25000, args.features)) features = args.features train = Generator(train, train_y, args.batch) # Use generator to train from memmap dev = Generator(dev, dev_y, args.batch) callback_list = [ EarlyStopping(monitor='val_acc', patience=10), ModelCheckpoint(filepath='ast_model.h5', monitor='val_acc', save_best_only=True), ReduceLROnPlateau(monitor='val_acc', factor=0.1, patience=3) ] model = Sequential() model.add(Dense(1000, activation='relu', input_shape=(features, ))) model.add(Dropout(0.5)) model.add(Dense(1000, activation='tanh')) model.add(Dropout(0.5)) model.add(Dense(1000, activation='softmax')) model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['acc']) if not args.skip: model.fit(train, epochs=1000, validation_data=dev, shuffle=True, callbacks=callback_list) model.load_weights('ast_model.h5') model.evaluate(dev) predict_veca = np.memmap('vectors/dev_ast.mm', dtype='float32', mode='w+', shape=(25000, 1000)) predict_veca[:] = model.predict( dev)[:] # Save dev and test predictions to file for ensemble. predict_vecb = np.memmap('vectors/test_ast.mm', dtype='float32', mode='w+', shape=(25000, 1000)) predict_vecb[:] = model.predict(test)[:] del predict_veca, predict_vecb