def plot_learning_curves(classifier_pickle_filename, target_weight=1000, n_simulations=100, test_size=0.33): classifier_loaded = joblib.load(CLASSIFIER_PICKLE_FOLDER + classifier_pickle_filename) annotations_loaded, labels_loaded = load_ambiguous_annotations_labeled(ANNOTATIONS_LABELED_FILENAME) pool, _, _, _ = train_test_split(annotations_loaded, labels_loaded, test_size = test_size) n_iterations = len(pool) + 1 passive_accuracy = np.zeros((n_simulations, n_iterations)) active_accuracy = np.zeros((n_simulations, n_iterations)) counter = CountPrinter(n_simulations) for run_number in range(n_simulations): # securing statelessness classifier = deepcopy(classifier_loaded) annotations= deepcopy(annotations_loaded) labels = deepcopy(labels_loaded) train_test_set = train_test_split(annotations, labels, test_size = test_size) passive_accuracy[run_number] = get_accuracy_progression( train_test_set, classifier, annotations, labels, target_weight, PassiveLearner) active_accuracy[run_number] = get_accuracy_progression( train_test_set, classifier, annotations, labels, target_weight, UncertaintySamplingLeastConfidenceActiveLearner) counter.count() passive_avg_accuracy_progression = np.mean(passive_accuracy, axis=0) active_avg_accuracy_progression = np.mean(active_accuracy, axis=0) plot_filename = PLOT_FOLDER + classifier_pickle_filename + '_weight' + str(target_weight) plot_curves.plot_curves(plot_filename, title="Average iteration accuracy for %s simulations" % n_simulations, PassiveLearner=passive_avg_accuracy_progression, ActiveLearner=active_avg_accuracy_progression)
def plot_learning_curves(classifier_pickle_filename, target_weight=1000, n_simulations=100, test_size=0.33): classifier_loaded = joblib.load(CLASSIFIER_PICKLE_FOLDER + classifier_pickle_filename) annotations_loaded, labels_loaded = load_ambiguous_annotations_labeled( ANNOTATIONS_LABELED_FILENAME) pool, _, _, _ = train_test_split(annotations_loaded, labels_loaded, test_size=test_size) n_iterations = len(pool) + 1 passive_accuracy = np.zeros((n_simulations, n_iterations)) active_accuracy = np.zeros((n_simulations, n_iterations)) counter = CountPrinter(n_simulations) for run_number in range(n_simulations): # securing statelessness classifier = deepcopy(classifier_loaded) annotations = deepcopy(annotations_loaded) labels = deepcopy(labels_loaded) train_test_set = train_test_split(annotations, labels, test_size=test_size) passive_accuracy[run_number] = get_accuracy_progression( train_test_set, classifier, annotations, labels, target_weight, PassiveLearner) active_accuracy[run_number] = get_accuracy_progression( train_test_set, classifier, annotations, labels, target_weight, UncertaintySamplingLeastConfidenceActiveLearner) counter.count() passive_avg_accuracy_progression = np.mean(passive_accuracy, axis=0) active_avg_accuracy_progression = np.mean(active_accuracy, axis=0) plot_filename = PLOT_FOLDER + classifier_pickle_filename + '_weight' + str( target_weight) plot_curves.plot_curves( plot_filename, title="Average iteration accuracy for %s simulations" % n_simulations, PassiveLearner=passive_avg_accuracy_progression, ActiveLearner=active_avg_accuracy_progression)
def generate_plots(input_path, output_path): files = glob.glob(os.path.join(input_path, '*.json')) print(files) for filename in files: print(filename) try: with open(filename ) as f: # No need to specify 'r': this is the default. plot_json = json.loads(f.read()) show_legend = int(filename[-6]) == 1 plot_curves(plot_json, output_path, show_legend=show_legend) except IOError as exc: if exc.errno != errno.EISDIR: # Do not fail if a directory is found, just ignore it. raise # Propagate other kinds of IOError.
def added_mass_pot_damping(plota=0, dof_plot=[1, 2, 3, 4, 5, 6], multi_fig=False, T_lim=[0, 25], param_out=[]): if not param_out: param_out = output_params() ULEN = param_out[0][1] NBODY = param_out[0][5] arq1 = np.loadtxt('force.1') # print('added_mass_pot_damping: ULEN = {:.1f}'.format(ULEN)) # Unique with no sort _, idx = np.unique(arq1[:, 0], return_index=True) per = np.array([arq1[index, 0] for index in sorted(idx)]) # searching the evaluated dof's dof = arq1[arq1[:, 0] == per[0], 1:3] dof_aux = [1, 2, 3] aux1 = [] for n in range(NBODY - 1): #print(n) for z in dof_aux: aux1.append(z + 6 * (n + 1)) [dof_aux.append(n) for n in aux1] #dim = np.ones(arq1.shape) dim = [] for ii in arq1: if ii[1] == ii[2]: if ii[1] in dof_aux: k = 3 else: k = 4 else: k = 5 dim.append([ 1, 1, 1, 1.025 * (ULEN**k), (2 * np.pi / ii[0]) * 1.025 * (ULEN**k) ]) arq1d = arq1 * dim added_mass = [] pot_damp = [] dof1 = np.unique(arq1[:, 1:3], axis=0) #dof1 = [[1,1],[1,3],[1,5],[2,2],[2,4],[2,6],[3,1],[3,3],[3,5],[4,2],[4,4],[4,6],[5,1],[5,3],[5,5],[6,2],[6,4],[6,6]] # Added Mass # # Plane motion -> lower frequency # - At least 1 dof is surge, sway or yaw. Ex: [1,1],[1,3] # Out-plane motion -> mean between the mean and the max added mass # - Both dof are out-plane. Ex: [3,3], [3,4] aux_mad_matrix = [] aux_pdamp_matrix = [] for x in dof1: aux = [] #massa adicional aux2 = [] #amorteciment for jj in per: aux.append(arq1d[(arq1d[:, 1] == x[0]) & (arq1d[:, 2] == x[1]) & (arq1d[:, 0] == jj), 3]) aux2.append(arq1d[(arq1d[:, 1] == x[0]) & (arq1d[:, 2] == x[1]) & (arq1d[:, 0] == jj), 4]) added_mass.append(aux) pot_damp.append(aux2) # print(x) if (x == [3, 3]).all() or (x == [3, 5]).all() or (x == [ 4, 4 ]).all() or (x == [5, 3]).all() or (x == [5, 5]).all(): aux_mad_matrix.append(np.mean([np.mean(aux), np.max(aux)])) aux_pdamp_matrix.append(np.mean([np.mean(aux2), np.max(aux2)])) else: pos_min_freq = np.argmax(per) aux_mad_matrix.append(np.array(aux[pos_min_freq])) aux_pdamp_matrix.append(np.array(aux2[pos_min_freq])) aux_mad_matrix = np.array(aux_mad_matrix) aux_pdamp_matrix = np.array(aux_pdamp_matrix) added_mass_matrix = np.zeros((6 * NBODY, 6 * NBODY)) pot_damp_matrix = np.zeros((6 * NBODY, 6 * NBODY)) cont = 0 for x in dof1: pos1 = int(x[0] - 1) pos2 = int(x[1] - 1) added_mass_matrix[pos1, pos2] = aux_mad_matrix[cont] pot_damp_matrix[pos1, pos2] = aux_pdamp_matrix[cont] cont += 1 added_mass = np.transpose(added_mass) added_mass = added_mass[0] pot_damp = np.transpose(pot_damp) pot_damp = pot_damp[0] # plots if plota == 1: plot_curves('a', arq1d, per, dof_plot, [], NBODY=NBODY, multi_fig=multi_fig, T_lim=T_lim) plot_curves('b', arq1d, per, dof_plot, [], NBODY=NBODY, multi_fig=multi_fig, T_lim=T_lim) print('') print(' * Added Mass and Potential Damping') return [ added_mass, pot_damp, dof1, arq1d, added_mass_matrix, pot_damp_matrix ]
def drift_forces(plota=0, drift_analysis_type='m', dof_plot=[1, 2, 6], inc_plot=[0, 45, 90, 135, 180], multi_fig=False, T_lim=[0, 25], param_out=[]): dt_arq_name = {'m': 'force.8', 'p': 'force.9', 'c': 'force.7'} dt_message = {'m': 'Momentum', 'p': 'Pressure', 'c': 'Control Surface'} arq_name = dt_arq_name[drift_analysis_type] print('') print(' * Drift Analysis: ' + dt_message[drift_analysis_type]) if not param_out: param_out = output_params() arq8 = np.loadtxt(arq_name) # read the selected drift file ULEN = param_out[0][1] # read ULEN NBODY = param_out[0][5] # Unique with no sort _, idx = np.unique(arq8[:, 0], return_index=True) per = np.array([arq8[index, 0] for index in sorted(idx)]) # unique period vector inc = np.unique(arq8[:, 1]) # dof = np.unique(arq8[:, 3]) dof = dof[dof > 0] dim = np.ones(arq8.shape) # degrees of freedom to dimensioning dof_aux = np.arange(1, dof.max() + 1) dof_aux = dof_aux.reshape((-1, 6)) dof_aux = dof_aux[:, [3, 4, 5]] dof_aux = dof_aux.reshape((-1, 1)) pos = [] # positions for dimensioning for ii in dof_aux: pos.append(arq8[:, 3] == ii) pos = np.sum(pos, axis=0) == 1 dim[np.ix_(pos, [4, 6, 7])] = 1.025 * 9.80665 * (ULEN**2) dof_aux = np.arange(1, dof.max() + 1) dof_aux = dof_aux.reshape((-1, 6)) dof_aux = dof_aux[:, [0, 1, 2]] dof_aux = dof_aux.reshape((-1, 1)) pos = [] # positions for dimensioning for ii in dof_aux: pos.append(arq8[:, 3] == ii) pos = np.sum(pos, axis=0) == 1 dim[np.ix_(pos, [4, 6, 7])] = 1.025 * 9.80665 * (ULEN**1) arq8d = arq8 * dim wdforce = [] wdforce_phase = [] new_dof = np.arange(1, dof.max() + 1) for ii in new_dof: aux = [] aux2 = [] for jj in per: if np.isin(ii, dof): aux.append(arq8d[(arq8d[:, 3] == ii) & (arq8d[:, 0] == jj), 4]) aux2.append(arq8d[(arq8d[:, 3] == ii) & (arq8d[:, 0] == jj), 5]) else: aux.append(np.zeros(len(inc))) aux2.append(np.zeros(len(inc))) wdforce.append(aux) wdforce_phase.append(aux2) # plots if plota == 1: plot_curves(tipo='mdf', arq_n_d=arq8d, per=per, dof_plot=dof_plot, inc_plot=inc_plot, NBODY=NBODY, multi_fig=multi_fig, dt=drift_analysis_type, T_lim=T_lim) return [wdforce, wdforce_phase, arq8d]
def wave_forces(plota=0, dof_plot=[1, 2, 3, 4, 5, 6], inc_plot=[0, 45, 90, 135, 180], multi_fig=False, T_lim=[0, 25], param_out=[]): if not param_out: param_out = output_params() arq2 = np.loadtxt('force.2') ULEN = param_out[0][1] NBODY = param_out[0][5] # Unique with no sort _, idx = np.unique(arq2[:, 0], return_index=True) per = np.array([arq2[index, 0] for index in sorted(idx)]) inc = np.unique(arq2[:, 1]) dof = np.unique(arq2[:, 2]) dim = np.ones(arq2.shape) dof_aux = np.arange(1, dof.max() + 1) dof_aux = dof_aux.reshape((-1, 6)) dof_aux = dof_aux[:, [3, 4, 5]] dof_aux = dof_aux.reshape((-1, 1)) pos = [] # positions for dimensioning for ii in dof_aux: pos.append(arq2[:, 2] == ii) pos = np.sum(pos, axis=0) == 1 dim[np.ix_(pos, [3, 5, 6])] = 1.025 * 9.80665 * (ULEN**3) dof_aux = np.arange(1, dof.max() + 1) dof_aux = dof_aux.reshape((-1, 6)) dof_aux = dof_aux[:, [0, 1, 2]] dof_aux = dof_aux.reshape((-1, 1)) pos = [] # positions for dimensioning for ii in dof_aux: pos.append(arq2[:, 2] == ii) pos = np.sum(pos, axis=0) == 1 dim[np.ix_(pos, [3, 5, 6])] = 1.025 * 9.80665 * (ULEN**2) arq2d = arq2 * dim wforce = [] wforce_phase = [] for ii in dof: aux = [] aux2 = [] for jj in per: aux.append(arq2d[(arq2d[:, 2] == ii) & (arq2d[:, 0] == jj), 3]) aux2.append(arq2d[(arq2d[:, 2] == ii) & (arq2d[:, 0] == jj), 4]) wforce.append(aux) wforce_phase.append(aux2) # plots if plota == 1: plot_curves(tipo='wf', arq_n_d=arq2d, per=per, dof_plot=dof_plot, inc_plot=inc_plot, NBODY=NBODY, multi_fig=multi_fig, T_lim=T_lim) print('') print(' * Wave forces') return [wforce, wforce_phase, arq2d]
def raos(plota=0, dof_plot=[1, 2, 3, 4, 5, 6], inc_plot=[0, 45, 90, 135, 180], multi_fig=False, T_lim=[0, 25], param_out=[]): # from matplotlib.ticker import FormatStrFormatter # from scipy import interpolate if not param_out: param_out = output_params() # Inputs (after must be imported from a configuration file) arq4 = np.loadtxt('force.4') ULEN = param_out[0][1] NBODY = param_out[0][5] # Column: 0-Period, 1-Incidence angle, 2-DOF, 3-Amp, 4-Phase, 5-Real, 6-Imag. # OPTN.4: PER BETA I Mod(ξi) Pha(ξi) Re(ξi) Im(ξi) # Unique with no sort _, idx = np.unique(arq4[:, 0], return_index=True) per = np.array([arq4[index, 0] for index in sorted(idx)]) # # identify period to remove from analysis # pos_per=[] # if np.array(remove_per).size > 0: # print('Removendo períodos:') # print(remove_per) # delta_p = .1 # for p in remove_per: # pos_per.append(np.logical_not(np.logical_and(per > (p-delta_p), per < (p+delta_p)))) # pos_per = np.prod(np.array(pos_per),0)==1 # per = per[pos_per] inc = np.unique(arq4[:, 1]) dof = np.unique(arq4[:, 2]) dof_aux = np.arange(1, dof.max() + 1) dof_aux = dof_aux.reshape((-1, 6)) dof_aux = dof_aux[:, [3, 4, 5]] dof_aux = dof_aux.reshape((-1, 1)) dim = np.ones(arq4.shape) # dimensionalization of arq4 file # arq4: non-dimensional file # arq4d: dimensional file pos = [] # positions for dimensioning for ii in dof_aux: pos.append(arq4[:, 2] == ii) pos = np.sum(pos, axis=0) == 1 dim[np.ix_(pos, [3, 5, 6])] = 1 / (ULEN**1) arq4d = arq4 * dim # RAO in complex form rao_c_aux = arq4d[:, 5] + arq4d[:, 6] * 1j # Function to interpolate the RAO in the given incidences rao = [] rao_phase = [] rao_c = [] for ii in dof: aux = [] aux2 = [] aux3 = [] for jj in per: aux.append(arq4d[(arq4d[:, 2] == ii) & (arq4d[:, 0] == jj), 3]) aux2.append(arq4d[(arq4d[:, 2] == ii) & (arq4d[:, 0] == jj), 4]) aux3.append(rao_c_aux[(arq4d[:, 2] == ii) & (arq4d[:, 0] == jj)]) rao.append(aux) rao_phase.append(aux2) rao_c.append(np.array(aux3)) # plots if plota == 1: plot_curves('rao', arq_n_d=arq4d, per=per, dof_plot=dof_plot, inc_plot=inc_plot, NBODY=NBODY, multi_fig=multi_fig, T_lim=T_lim) print('') print(' * Response Amplitude Operator') return [rao, rao_phase, per, inc, dof, arq4d, rao_c]