Commit 3532e829 authored by Elvira Clement's avatar Elvira Clement
Browse files

Update experiments

parent ca0fd1fd
python viz_final.py --noshow --save
\ No newline at end of file
# -*- coding: utf-8 -*-
import argparse
import numpy as np
import matplotlib.pyplot as plt
# Algorithm import
from src import __version__
from src.dictionaries import generate_dic
from src.utils import get_lambda_max, gamma_sequence_generator
# Screening
from src.screening.singletest import GapSphereSingleTest
from src.screening.gap_ptest import GAP_Ptest
from src.screening.gap_rqtest import GAP_RQtest
from src.screening.kappa_test import Kappa_test
# XP import
from xps.SIAM.slopepb import SlopePb
from xps.SIAM.setup import Setup
from xps.SIAM.xp_1_balls import xpparams
parser=argparse.ArgumentParser()
parser.add_argument('--id', help='setup id', type=str, default="1a")
args=parser.parse_args()
# -------------------------
# Load Setup
# -------------------------
setup = Setup(args.id)
solutions_filename = f"results/xp_setup{args.id}.npz"
try:
# Try to load existing results
load_results = np.load(solutions_filename, allow_pickle=True)
mat_seed = load_results["mat_seed"]
mat_pvopt = load_results["mat_pvopt"]
mat_dvopt = load_results["mat_dvopt"]
except FileNotFoundError:
print("No result found")
sys.exit(1)
# -------------------------
# Screening Xp
# -------------------------
# percentage of detected zero
n_rep = 5
mat_nb_zero_detected = np.full(
(3, xpparams.nb_point, setup.nb_sequence, n_rep),
np.nan,
dtype=int
)
poc_filename = f"results/poc{args.id}_screening.npz"
i_xp = 0
i_dic = 2
i_ratio = 1
ratio = .5
i_seq = 1
seq = "OSCAR-1"
# seq_r = 1+np.arange(setup.n, dtype=int)
seq_r = 1 + np.arange(setup.n, dtype=int) // 5
#1 + (1 + np.arange(setup.n, dtype=int)) // 2
# for rep in range(setup.n_rep):
for rep in range(n_rep):
np.random.seed(mat_seed[i_dic, i_seq, rep])
# ---- 1. Gen data ----
matA = generate_dic(
setup.list_dic[i_dic],
setup.m,
setup.n,
setup.normalize
)
vecy = np.random.randn(setup.m)
vecy /= np.linalg.norm(vecy)
# ---- 2. Compute parameters ----
vec_gammas = gamma_sequence_generator(
setup.m,
setup.n,
setup.list_sequence[i_seq][0],
setup.list_sequence[i_seq][1:]
)
lbd_max = get_lambda_max(vecy, matA, vec_gammas)
# ---- 3. XP ----
i_xp += 1
print(f"xp {i_xp}")
slopePb = SlopePb(matA, vecy, vec_gammas, ratio, lbdmax=lbd_max)
vecx_hat = mat_pvopt[i_dic, i_seq, i_ratio, rep, :]
vecu_hat = mat_dvopt[i_dic, i_seq, i_ratio, rep, :]
gap = slopePb.eval_gap(vecx_hat, vecu_hat)
Atu = matA.T @ vecu_hat
# ---- 3b. Build thin safe ball ----
rgap = np.sqrt(2 * gap)
# ---- 3c. Testing sphere ----
list_tests = [
GapSphereSingleTest(),
Kappa_test(vec_gammas, np.arange(setup.n, dtype=np.double)),
GAP_RQtest(vec_gammas, seq_r),
# GAP_Ptest(vec_gammas),
]
for i_offset, offset in enumerate(xpparams.vec_offsets):
for i_test, test in enumerate(list_tests):
out = test.apply_test(np.abs(Atu), gap, ratio * lbd_max, vec_gammas, offset_radius=offset)
mat_nb_zero_detected[i_test, i_offset, i_seq, rep] = np.sum(out)
f, ax = plt.subplots(1, 1)
for i_test in range(3):
ax.plot(np.mean(mat_nb_zero_detected[i_test, :, i_seq, :n_rep], axis=1))
plt.show()
# # Save
# np.savez(
# poc_filename,
# mat_nb_zero_detected=mat_nb_zero_detected,
# list_test = [test.get_name() for test in list_tests],
# version = __version__,
# allow_pickle=True
# )
\ No newline at end of file
# -*- coding: utf-8 -*-
import argparse
import numpy as np
import matplotlib.pyplot as plt
# Algorithm import
from src import __version__
from src.dictionaries import generate_dic
from src.utils import get_lambda_max, gamma_sequence_generator
# Screening
from src.screening.singletest import GapSphereSingleTest
from src.screening.gap_ptest import GAP_Ptest
from src.screening.gap_rqtest import GAP_RQtest
from src.screening.kappa_test import Kappa_test
# XP import
from xps.SIAM.slopepb import SlopePb
from xps.SIAM.setup import Setup
from xps.SIAM.xp_1_balls import xpparams
parser=argparse.ArgumentParser()
parser.add_argument('--id', help='setup id', type=str, default="1a")
args=parser.parse_args()
# -------------------------
# Load Setup
# -------------------------
setup = Setup(args.id)
solutions_filename = f"results/xp_setup{args.id}.npz"
try:
# Try to load existing results
load_results = np.load(solutions_filename, allow_pickle=True)
mat_seed = load_results["mat_seed"]
mat_pvopt = load_results["mat_pvopt"]
mat_dvopt = load_results["mat_dvopt"]
except FileNotFoundError:
print("No result found")
sys.exit(1)
# -------------------------
# Screening Xp
# -------------------------
# percentage of detected zero
mat_nb_zero_detected = np.full(
(4, xpparams.nb_point, setup.nb_sequence, setup.n_rep),
np.nan,
dtype=int
)
poc_filename = f"results/poc{args.id}_screening.npz"
i_xp = 0
nb_xp = setup.nb_dic * setup.n_rep * setup.nb_sequence * setup.nb_ratio_lbd
i_dic = 2
i_ratio = 1
ratio = .5
rep = 0
f, ax = plt.subplots(1, 3)
for i_seq, seq in enumerate(setup.list_sequence):
# for rep in range(setup.n_rep):
np.random.seed(mat_seed[i_dic, i_seq, rep])
# ---- 1. Gen data ----
matA = generate_dic(
setup.list_dic[i_dic],
setup.m,
setup.n,
setup.normalize
)
vecy = np.random.randn(setup.m)
vecy /= np.linalg.norm(vecy)
# ---- 2. Compute parameters ----
vec_gammas = gamma_sequence_generator(
setup.m,
setup.n,
setup.list_sequence[i_seq][0],
setup.list_sequence[i_seq][1:]
)
lbd_max = get_lambda_max(vecy, matA, vec_gammas)
# ---- 3. XP ----
i_xp += 1
print(f"xp {i_xp} / {nb_xp}")
slopePb = SlopePb(matA, vecy, vec_gammas, ratio, lbdmax=lbd_max)
vecx_hat = mat_pvopt[i_dic, i_seq, i_ratio, rep, :]
vecu_hat = mat_dvopt[i_dic, i_seq, i_ratio, rep, :]
gap = slopePb.eval_gap(vecx_hat, vecu_hat)
Atu = matA.T @ vecu_hat
# ---- 3b. Build thin safe ball ----
rgap = np.sqrt(2 * gap)
# ---- 3c. Testing sphere ----
list_tests = [
# --- Lasso like test --
GapSphereSingleTest(),
GAP_RQtest(vec_gammas, 1+np.arange(setup.n, dtype=int)),
# # --- ideal like test ---
Kappa_test(vec_gammas, np.arange(setup.n, dtype=np.double)),
GAP_RQtest(vec_gammas, np.ones(setup.n, dtype=int)),
# --- doing all tets ---
# GAP_Ptest(vec_gammas),
]
for i_offset, offset in enumerate(xpparams.vec_offsets):
for i_test, test in enumerate(list_tests):
out = test.apply_test(np.abs(Atu), gap, ratio * lbd_max, vec_gammas, offset_radius=offset)
mat_nb_zero_detected[i_test, i_offset, i_seq, rep] = np.sum(out)
ax[i_seq].plot(mat_nb_zero_detected[0, :, i_seq, rep], linewidth=4)
ax[i_seq].plot(mat_nb_zero_detected[1, :, i_seq, rep])
ax[i_seq].plot(mat_nb_zero_detected[2, :, i_seq, rep], linewidth=4)
ax[i_seq].plot(mat_nb_zero_detected[3, :, i_seq, rep])
plt.show()
\ No newline at end of file
# -*- coding: utf-8 -*-
import numpy as np
from xps.SIAM.setup import Setup
from xps.SIAM.xp_1_balls import xpparams
def process(setup, log=True):
# ---- load ----
solutions_filename = f"results/xp_setup{setup.setup_id}.npz"
solutions = np.load(solutions_filename, allow_pickle=True)
screenings_filename = f"results/xp_setup{setup.setup_id}_screening.npz"
screenings = np.load(screenings_filename, allow_pickle=True)
# mat_results_gap[1, i_dic, i_seq, rep, i_ratio]
mat_pvopt = solutions["mat_pvopt"]
mat_nb_zero_detected = screenings['mat_nb_zero_detected']
nb_test = mat_nb_zero_detected.shape[0]
list_tests = screenings["list_test"]
# ---- log ----
if log:
print("Experiment info")
# print(f"- run with version {solutions["version"]}")
print(f"- setup{setup.setup_id}")
print("")
# ---- processing ----
mat_pc_detected =np.zeros(
(nb_test, xpparams.nb_point, setup.nb_dic, setup.nb_sequence, setup.nb_ratio_lbd, setup.n_rep)
)
for i_dic in range(setup.nb_dic):
for i_seq in range(setup.nb_sequence):
for i_ratio in range(setup.nb_ratio_lbd):
for rep in range(setup.n_rep):
nb_0 = np.sum(mat_pvopt[i_dic, i_seq, i_ratio, rep, :] == 0)
if nb_0 > 0:
mat_detected = mat_nb_zero_detected[:, :, i_dic, i_seq, i_ratio, rep]
mat_pc_detected[:, :, i_dic, i_seq, i_ratio, rep] = mat_detected / float(nb_0)
# ---- return ----
return {
"mat_pc_detected": np.mean(mat_pc_detected, axis=5),
"list_tests": list_tests,
}
# {
# "results_mat_nb": results_mat_nb,
# "results_mat_nbz": results_mat_nbz,
# }
\ No newline at end of file
# -*- coding: utf-8 -*-
from decimal import Decimal
import argparse
import numpy as np
import matplotlib.pyplot as plt
# Slope import
from src.utils import gamma_sequence_generator
# XP import
from xps.SIAM.slopepb import SlopePb
from xps.SIAM.setup import Setup
from xps.SIAM.xp_1_balls import xpparams
from xps.SIAM.xp_1_balls.process_data import process
parser=argparse.ArgumentParser()
parser.add_argument('--noshow', help='do not display figures', action="store_true")
parser.add_argument('--save', help='save figure', action="store_true")
parser.add_argument('--id', help='setup id', type=str, default=1)
args=parser.parse_args()
# -------------------------
# Load Results
# -------------------------
setup = Setup(args.id)
dic_process = process(setup)
mat_pc_detected = dic_process["mat_pc_detected"]
list_tests = dic_process["list_tests"]
list_legends = ["$r_q=1 \\forall q$", "Testing all $q$", "$r_q=q \\forall q$"]
# -------------------------
# Plot Results
# -------------------------
for i_dic in range(setup.nb_dic):
if not np.any(mat_pc_detected[:, :, i_dic, :, :] > 0):
continue
f, ax = plt.subplots(setup.nb_sequence, setup.nb_ratio_lbd+1)
f.suptitle(f"{setup.list_dic[i_dic]}")
for i_seq, str_seq in enumerate(setup.list_sequence):
for i_lbd in range(len(setup.list_ratio_lbd)):
if i_seq ==0:
ax[i_seq, i_lbd].set_title(f"$\\lambda={setup.list_ratio_lbd[i_lbd]}$")
for i_test in [0, 2, 1]:
ax[i_seq, i_lbd].plot(
xpparams.vec_offsets,
100 * mat_pc_detected[i_test, :, i_dic, i_seq, i_lbd],
# '-',
# color="blue",
label= list_legends[i_test],
# ,
# linestyle=style
)
# if j == 0:
# ax[j, i_seq].set_title(f"{setup.list_sequence[i_seq]} -- lbd={setup.list_ratio_lbd[j]}")
# else:
# ax[j, i_seq].set_title(f"lbd={setup.list_ratio_lbd[j]}")
if i_lbd == 0 and i_seq == 0:
ax[i_seq, i_lbd].legend()
ax[i_seq, i_lbd].set_xscale('log')
# if i_seq >= 2:
# ax[i_seq, i_lbd].set_xlim([0, .5])
# if i_seq >= 3:
# ax[i_seq, i_lbd].set_xlim([0, .05])
vec_gammas = gamma_sequence_generator(
setup.m,
setup.n,
setup.list_sequence[i_seq][0],
setup.list_sequence[i_seq][1:],
log=True
)
ax[i_seq, len(setup.list_ratio_lbd)].plot(vec_gammas)
if args.save:
filename = f"figs/setup{args.id}_{setup.list_dic[i_dic]}.pdf"
plt.savefig(filename, bbox_inches='tight')
if not args.noshow:
plt.show()
\ No newline at end of file
# -*- coding: utf-8 -*-
from decimal import Decimal
import json, argparse
from prettytable import PrettyTable
import numpy as np
# XP import
from xps.SIAM.slopepb import SlopePb
from xps.SIAM.setup import Setup
from xps.SIAM.xp_1_balls import xpparams
from xps.SIAM.xp_1_balls.process_data import process
parser=argparse.ArgumentParser()
parser.add_argument('--noshow', help='do not display figures', action="store_true")
parser.add_argument('--save', help='save figure', action="store_true")
# parser.add_argument('--id', help='setup id', type=str, default=1)
args=parser.parse_args()
import matplotlib
if args.noshow:
matplotlib.use('PS')
else:
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerBase
import matplotlib.font_manager as font_manager
# -------------------------
# Font stuff
# -------------------------
fs = 20
font_math = font_manager.FontProperties(
fname='../fonts/cmunrm.ttf',
# weight='normal',
# style='normal',
math_fontfamily="cm",
size=fs+2
)
font_text = font_manager.FontProperties(
fname='../fonts/cmunrm.ttf',
# weight='normal',
# style='normal',
math_fontfamily="cm",
size=fs+2
)
font_ttt = font_manager.FontProperties(
# fname='../fonts/ectt1000.ttf',
fname='../fonts/computer-modern/cmuntt.ttf',
weight='bold',
style='normal',
size=fs
)
# -------------------------
# Load Results
# -------------------------
# OSCAR
setup_oscar = Setup("1a")
dic_process_oscar = process(setup_oscar)
mat_pc_detected_oscar = dic_process_oscar["mat_pc_detected"]
list_tests_oscar = dic_process_oscar["list_tests"]
# EXP
setup_exp = Setup("1b")
dic_process_exp = process(setup_exp)
mat_pc_detected_exp = dic_process_exp["mat_pc_detected"]
list_tests_exp = dic_process_exp["list_tests"]
# -------------------------
# Plot Results
# -------------------------
i_dic = 2
i_lbd = 1
fs=22
fs_ylabels = 20
list_colors = ["tab:blue", "tab:orange", "tab:green"]
list_legends = ["$r_q=q\\;\\forall q$", "best performance", "$r_q=1\\;\\forall q$"]
# "best $r_q \\;\\forall q$ "
print("printing xp_0_ball parameters with")
print(" - OSCAR sequence")
print(f"- {setup_oscar.list_dic[i_dic]} dictionary")
print(f"- lbd / lbd_max = {setup_oscar.list_ratio_lbd[i_lbd]}")
for i_seq in range(2):
f, ax = plt.subplots(1, 1, figsize=(.7*16, .6*9), sharex=True, sharey=True)
# ax.set_title(f"OSCAR-{i_seq+1}", fontsize=fs+2)
ax.set_xlabel(
r"$R$",
fontsize=fs+2,
fontproperties=font_math,
)
ax.set_ylabel(
"% of zero entries detected",
fontsize=fs+2,
fontproperties=font_text
)
for tick in ax.xaxis.get_major_ticks():
tick.label.set_fontproperties(font_math)
tick.label.set_fontsize(20)
for tick in ax.yaxis.get_major_ticks():
tick.label.set_fontproperties(font_math)
tick.label.set_fontsize(20)
for i_test in [2, 0, 1]:
ax.plot(
xpparams.vec_offsets,
100 * mat_pc_detected_exp[i_test, :, i_dic, i_seq, i_lbd],
label = list_legends[i_test],
linewidth=4.,
alpha=.9,
color=list_colors[i_test]
)
if i_seq == 0:
ax.legend(
fontsize=fs-2,
prop=font_math
)
ax.set_xscale("log")
ax.set_xlim([1e-6, 1e0])
ax.set_ylim([-2, 102])
if args.save:
filename = f"figs/xp_illustration_screening{i_seq}.eps"
plt.savefig(filename, bbox_inches='tight')
if not args.noshow:
plt.show()
\ No newline at end of file
# -*- coding: utf-8 -*-
import time, argparse, sys
import numpy as np
# Algorithm import
from src import __version__
from src.parameters import SlopeParameters, EnumLipchitzOptions
from src.slope import primal_func, dual_func, slope_gp
# Generative models
from src.utils import get_lambda_max, gamma_sequence_generator
from src.dictionaries import generate_dic
# Screening
from src.screening.singletest import GapSphereSingleTest
from src.screening.gap_ptest import GAP_Ptest
# XP import
from xps.SIAM.slopepb import SlopePb