<s> import os <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> from expjobs . job import Job <EOL> from multimodal . pairing import organize_by_values <EOL> from multimodal . experiment import ThreeModalitiesExperiment <EOL> from multimodal . lib . logger import Logger <EOL> from multimodal . lib . plot import pcolormesh <EOL> from multimodal . lib . metrics import mutual_information <EOL> from multimodal . lib . munkres import min_weight_perm <EOL> from multimodal . db . choreo2 import Choreo2Loader <EOL> from multimodal . db . acorns import Year1Loader as AcornsLoader <EOL> from multimodal . db . objects import ObjectsLoader <EOL> WORKDIR = os . path . expanduser ( '<STR_LIT>' ) <EOL> SCRIPT3 = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> DEFAULT_FIG_DEST = os . path . expanduser ( <EOL> '<STR_LIT>' ) <EOL> DEFAULT_PLOT_PARAMS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:True>' , <EOL> '<STR_LIT>' : '<STR_LIT:True>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:True>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT:6> ) , <EOL> } <EOL> CMAP = plt . cm . Blues <EOL> K = <NUM_LIT:15> <EOL> N_RUN = <NUM_LIT:20> <EOL> DEFAULT_PARAMS = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> exps = [ ( "<STR_LIT>" . format ( K , i ) , <EOL> ThreeModalitiesExperiment ( <EOL> { '<STR_LIT:image>' : ObjectsLoader ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : Choreo2Loader ( ) , <EOL> '<STR_LIT>' : AcornsLoader ( <NUM_LIT:1> ) } , <EOL> K , <NUM_LIT:50> , <NUM_LIT:50> , ** DEFAULT_PARAMS ) <EOL> ) for i in range ( N_RUN ) ] <EOL> jobs = [ Job ( WORKDIR , n , SCRIPT3 ) for n , e in exps ] <EOL> MOD_PAIRS = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:image>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:image>' , '<STR_LIT>' ) ] <EOL> EXPS_BY_NAME = dict ( exps ) <EOL> JOBS_BY_NAME = dict ( { j . name : j for j in jobs } ) <EOL> def internal_histograms_by_label ( internal_values , by_labels_idx ) : <EOL> """<STR_LIT>""" <EOL> value_range = ( internal_values . min ( ) , internal_values . max ( ) ) <EOL> return [ np . histogram ( internal_values [ idx ] , bins = <NUM_LIT:10> , range = value_range ) [ <NUM_LIT:0> ] <EOL> for idx in by_labels_idx ] <EOL> def _sum_all_but_i ( a , i ) : <EOL> a = np . array ( a ) <EOL> mask = np . ones ( a . shape , dtype = np . bool ) <EOL> mask [ i , : ] = False <EOL> return ( a * mask ) . sum ( axis = <NUM_LIT:0> ) <EOL> def mutual_information_by_label ( internal_values , by_labels_idx ) : <EOL> """<STR_LIT>""" <EOL> hists = internal_histograms_by_label ( internal_values , by_label ) <EOL> joint_hists = [ np . vstack ( [ hists [ i ] , _sum_all_but_i ( hists , i ) ] ) <EOL> for i in range ( len ( hists ) ) ] <EOL> return [ mutual_information ( <NUM_LIT:1.> * h / h . sum ( ) ) for h in joint_hists ] <EOL> def plot_mutual_info ( m , labels ) : <EOL> perm = min_weight_perm ( - m ) <EOL> not_in_perm = [ i for i in range ( K ) if i not in perm ] <EOL> permuted_m = np . hstack ( [ m [ : , perm ] , m [ : , not_in_perm ] ] ) <EOL> p = pcolormesh ( permuted_m , xticklabels = perm + not_in_perm , <EOL> yticklabels = labels , cmap = CMAP ) <EOL> cb = plt . colorbar ( p , drawedges = False ) <EOL> cb . outline . set_visible ( False ) <EOL> mutual_information_matrices = [ ] <EOL> plots = [ ] <EOL> for exp_idx , ( name , exp ) in enumerate ( exps ) : <EOL> exp . logger . filename = None <EOL> job = JOBS_BY_NAME [ name ] <EOL> logger = Logger . load ( os . path . join ( job . path , job . name ) ) <EOL> raw_labels = exp . _get_raw_labels ( ) <EOL> label_assoc = logger . get_value ( '<STR_LIT>' ) <EOL> sample_assoc = logger . get_value ( '<STR_LIT>' ) <EOL> labels = [ label_assoc [ <NUM_LIT:0> ] . index ( raw_labels [ <NUM_LIT:0> ] [ a [ <NUM_LIT:0> ] ] ) for a in sample_assoc ] <EOL> exp . _set_labels_and_prepare_data ( label_assoc , labels , sample_assoc ) <EOL> exp . _set_data_and_labels_for_examples ( logger . get_value ( '<STR_LIT>' ) ) <EOL> labels = exp . labels <EOL> learner = exp . _get_new_learner ( ) <EOL> learner . dico = logger . get_last_value ( '<STR_LIT>' ) <EOL> data = exp . data <EOL> print ( "<STR_LIT>" ) <EOL> internal = learner . reconstruct_internal_multi ( exp . modalities , <EOL> data , <EOL> exp . iter_test ) <EOL> n_labels = len ( label_assoc [ <NUM_LIT:0> ] ) <EOL> by_label = organize_by_values ( labels , nb_values = n_labels ) <EOL> mutual_information_matrices . append ( np . array ( [ <EOL> mutual_information_by_label ( internal [ : , i ] , by_label ) <EOL> for i in range ( K ) <EOL> ] ) . T ) <EOL> with plt . rc_context ( rc = DEFAULT_PLOT_PARAMS ) : <EOL> plt . interactive ( False ) <EOL> fig = plt . figure ( ) <EOL> sound_labels = label_assoc [ exp . modalities . index ( "<STR_LIT>" ) ] <EOL> plot_mutual_info ( mutual_information_matrices [ - <NUM_LIT:1> ] , sound_labels ) <EOL> for ext in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> path = os . path . join ( DEFAULT_FIG_DEST , <EOL> '<STR_LIT>' . format ( exp_idx , ext ) ) <EOL> fig . savefig ( path , transparent = True ) <EOL> print ( '<STR_LIT>' . format ( path ) ) </s>
