def plot_mvmm(mvmm, inches=8, save_dir=None): """ Plots loss history and estimated Pi matrix. """ if save_dir is not None: os.makedirs(save_dir, exist_ok=True) # TODO: maybe add Pi start loss_vals = mvmm.opt_data_['history']['loss_val'] ################ # Loss history # ################ plot_loss_history(loss_vals, loss_name='Observed data negative log-likelihood') if save_dir is not None: fpath = join(save_dir, 'loss_history.png') save_fig(fpath) ############### # Pi estimate ################ plt.figure(figsize=(inches, inches)) plot_Pi(mvmm.weights_mat_) plt.title("Estimated Pi") if save_dir is not None: fpath = join(save_dir, 'Pi_est.png') save_fig(fpath)
def plot_loss_history_from_idx(idx): estimator = sp_mvmm.estimators_[idx] if idx == 0: loss_vals = estimator.opt_data_['history']['loss_val'] # title = 'MVMM' else: loss_vals = estimator.opt_data_['adpt_opt_data']['history'][ 'loss_val'] # title = 'Penalty = {:1.2f}'.format(estimator.eval_pen_base) plot_loss_history(loss_vals)
def plot_sp_opt_history(sp_mvmm_gs, save_dir): for idx, est in enumerate(sp_mvmm_gs.estimators_): n_blocks = sp_mvmm_gs.est_n_blocks_[idx] if type(est) == MVMM: loss_vals = est.opt_data_['history']['obs_nll'] loss_name = 'Obs NLL' else: loss_vals = est.opt_data_['adpt_opt_data']['history']['loss_val'] loss_name = "eval pen loss" plot_loss_history(loss_vals, loss_name=loss_name) plt.title('{} steps'.format(len(loss_vals))) os.makedirs(save_dir, exist_ok=True) save_fig( os.path.join(save_dir, 'opt_history__n_blocks_{}.png'.format(n_blocks)))
def plot_log_pen_mvmm(mvmm, inches=8, save_dir=None): if save_dir is not None: os.makedirs(save_dir, exist_ok=True) # info = get_log_pen_mvmm_info(mvmm) # if save_dir is not None: # # TODO: save this # save_dir # else: # print(info) Pi = mvmm.weights_mat_ zero_thresh = 1e-10 # not sure if we need this summary, Pi_comm = community_summary(Pi, zero_thresh=zero_thresh) Pi_symlap_spec = eigh_Lsym_bp(Pi)[0] if 'init_params' in mvmm.opt_data_['history']: Pi_init = mvmm.opt_data_['history']['weights'].reshape( Pi.shape) # TODO: check this Pi_init_symlap_spec = eigh_Lsym_bp(Pi_init)[0] else: Pi_init = None obs_nll = mvmm.opt_data_['history']['obs_nll'] loss_vals = mvmm.opt_data_['history']['loss_val'] #################### # Initial weights # ################### if Pi_init is not None: plt.figure(figsize=(inches, inches)) plot_Pi(Pi_init) plt.title('weights initial value') if save_dir is not None: fpath = join(save_dir, 'weights_init.png') save_fig(fpath) ###################### # Estimated weights # ###################### plt.figure(figsize=(2 * inches, inches)) plt.subplot(1, 2, 1) plot_Pi(Pi) plt.title('weights estimate, n_blocks={}'.format(summary['n_communities'])) plt.subplot(1, 2, 2) plot_Pi(Pi, mask=Pi_comm < zero_thresh) plt.title('weights estimate, block diagonal perm') if save_dir is not None: fpath = join(save_dir, 'weights_est.png') save_fig(fpath) ########################## # Spectrum of BD weights # ########################## plt.figure(figsize=(inches, inches)) idxs = np.arange(1, len(Pi_symlap_spec) + 1) plt.plot(idxs, Pi_symlap_spec, marker='.', label='Estimate') if Pi_init is not None: plt.plot(idxs, Pi_init_symlap_spec, marker='.', label="Initial") plt.title('weights estimate spectrum') plt.ylim(0) plt.legend() if save_dir is not None: fpath = join(save_dir, 'weights_spectrum.png') save_fig(fpath) ########################### # Obs NLL for entire path # ########################### plt.figure(figsize=[inches, inches]) plot_loss_history(obs_nll, loss_name="Obs NLL") if save_dir is not None: fpath = join(save_dir, 'obs_nll.png') save_fig(fpath) plt.figure(figsize=[inches, inches]) plot_loss_history(loss_vals, loss_name="log penalized obs nll") if save_dir is not None: fpath = join(save_dir, 'loss_vals.png') save_fig(fpath)
def plot_bd_mvmm(mvmm, inches=8, save_dir=None): """ Initial BD weights, Estimated BD weights, spectrums of both Number of steps in each adaptive stage Evals of entire path Loss history for each segment """ if save_dir is not None: os.makedirs(save_dir, exist_ok=True) info = get_bd_mvmm_info(mvmm) if save_dir is not None: # TODO: save this save_dir else: print(info) # BD weight estimate bd_weights = mvmm.bd_weights_ zero_thresh = mvmm.zero_thresh summary, Pi_comm = community_summary(bd_weights, zero_thresh=zero_thresh) bd_weights_symlap_spec = eigh_Lsym_bp(bd_weights)[0] # initial BD weights bd_weights_init = mvmm.opt_data_['adpt_opt_data']['adapt_pen_history'][ 'opt_data'][0]['history']['init_params']['bd_weights'] bd_weights_init_symlap_spec = eigh_Lsym_bp(bd_weights_init)[0] # optimization history adpt_history = mvmm.opt_data_['adpt_opt_data']['adapt_pen_history'][ 'opt_data'] if 'raw_eval_sum' in adpt_history[0]['history']: n_steps = [ len(adpt_history[i]['history']['raw_eval_sum']) for i in range(len(adpt_history)) ] n_steps_cumsum = np.cumsum(n_steps) raw_eval_sum = \ np.concatenate([adpt_history[i]['history']['raw_eval_sum'] for i in range(len(adpt_history))]) else: raw_eval_sum = None n_steps = None n_steps_cumsum = None obs_nll = np.concatenate([ adpt_history[i]['history']['obs_nll'] for i in range(len(adpt_history)) ]) if mvmm.opt_data_['ft_opt_data'] is not None: fine_tune_obs_nll = mvmm.opt_data_['ft_opt_data']['history']['obs_nll'] else: fine_tune_obs_nll = None ###################### # Initial BD weights # ###################### plt.figure(figsize=(inches, inches)) plot_Pi(bd_weights_init) plt.title('BD weights initial value') if save_dir is not None: fpath = join(save_dir, 'BD_weights_init.png') save_fig(fpath) ######################## # Estimated BD weights # ######################## plt.figure(figsize=(2 * inches, inches)) plt.subplot(1, 2, 1) plot_Pi(bd_weights) plt.title('BD weights estimate, n_blocks={}'.format( summary['n_communities'])) plt.subplot(1, 2, 2) plot_Pi(bd_weights, mask=Pi_comm < zero_thresh) plt.title('BD weights estimate, block diagonal perm') if save_dir is not None: fpath = join(save_dir, 'BD_weights_est.png') save_fig(fpath) ########################## # Spectrum of BD weights # ########################## plt.figure(figsize=(inches, inches)) idxs = np.arange(1, len(bd_weights_symlap_spec) + 1) plt.plot(idxs, bd_weights_symlap_spec, marker='.', label='Estimate') plt.plot(idxs, bd_weights_init_symlap_spec, marker='.', label="Initial") plt.title('BD weights estimate spectrum') plt.ylim(0) plt.legend() if save_dir is not None: fpath = join(save_dir, 'BD_weights_spectrum.png') save_fig(fpath) ################################## # Number of steps for each stage # ################################## if n_steps is not None: plt.figure(figsize=(inches, inches)) idxs = np.arange(1, len(n_steps) + 1) plt.plot(idxs, n_steps, marker='.') plt.ylim(0) plt.ylabel("Number of steps") plt.xlabel("Adaptive stage") if save_dir is not None: fpath = join(save_dir, 'n_steps.png') save_fig(fpath) ########################### # Obs NLL for entire path # ########################### plt.figure(figsize=[inches, inches]) plot_loss_history(obs_nll, loss_name="Obs NLL (entire path)") if save_dir is not None: fpath = join(save_dir, 'path_obs_nll.png') save_fig(fpath) ######################### # Evals for entire path # ######################### if raw_eval_sum is not None: plt.figure(figsize=[inches, inches]) plt.plot(np.log10(raw_eval_sum), marker='.') plt.ylabel('log10(sum smallest evals)') plt.xlabel('step') plt.title('Eigenvalue history (entire path)') for s in n_steps_cumsum: plt.axvline(s - 1, color='grey') if save_dir is not None: fpath = join(save_dir, 'path_evals.png') save_fig(fpath) ########################### # Losses for each segment # ########################### if save_dir is not None: segment_dir = join(save_dir, 'segments') os.makedirs(segment_dir, exist_ok=True) for i in range(len(adpt_history)): loss_vals = adpt_history[i]['history']['loss_val'] plot_loss_history(loss_vals, 'loss val, adapt segment {}'.format(i + 1)) if save_dir is not None: fpath = join(segment_dir, 'loss_history_{}.png'.format(i + 1)) save_fig(fpath) ########################## # fine tune loss history # ########################## if fine_tune_obs_nll is not None: plot_loss_history(fine_tune_obs_nll, 'fine tune obs NLL') if save_dir is not None: fpath = join(segment_dir, 'fine_tune_loss_history.png') save_fig(fpath)
save_fig( os.path.join(save_dir, 'log_pen_mvmm', '{}_vs_{}.png'.format(param_name, measure))) ################ # loss history # ################ log_pen_mvmm_gs = models['log_pen_mvmm'] # mvmm = log_pen_mvmm_gs.estimators_[idx] mvmm = log_pen_mvmm_gs.best_estimator_ start_loss_val = mvmm.start_.opt_data_['history']['obs_nll'] final_loss_val = mvmm.final_.opt_data_['history']['loss_val'] plot_loss_history(start_loss_val) save_fig(os.path.join(save_dir, 'log_pen_mvmm', 'loss_history_start.png')) plot_loss_history(final_loss_val) save_fig(os.path.join(save_dir, 'log_pen_mvmm', 'loss_history_final.png')) ########### # SP MVMM # ########### if 'sp_mvmm' not in to_exclude: os.makedirs(os.path.join(save_dir, 'sp_mvmm'), exist_ok=True) models['sp_mvmm'].init_fit_data_.\ to_csv(os.path.join(save_dir, 'sp_mvmm', 'init_fit_data_.csv'))
# model selection est_n_comp = estimator.n_components res_writer.write("Cat data GMM estimated number of components: {}". format(est_n_comp)) # _model_sel_dir = make_and_get_dir(model_sel_dir, 'cat_gmm') # plt.figure(figsize=(inches, inches)) plot_model_selection(cat_model, save_dir=model_sel_dir, name_stub='cat_gmm', title='GMM on concatenated data', inches=inches) # optimization history loss_history = estimator.opt_data_['history']['obs_nll'] plot_loss_history(loss_history, loss_name='Observed data negative log-likelihood') save_fig(os.path.join(opt_diag_dir, 'cat_best_model_opt_history.png')) else: print("cat isnt fit...") ######################## # view marginal models # ######################## n_views = len(view_models) for v in range(n_views): estimator = view_models[v].best_estimator_ sel_models['view_' + str(v)] = estimator