Exemplo n.º 1
0
def main():
    ps_path = '/home/tim/repos/py_solvers/applications/deconvolution_challenge/test.ini'
    ps_params = ParameterStruct(ps_path)
    dict_in = {}
    sec_input = sf.create_section(ps_params, 'Input1')
    sec_input.read(dict_in)
    #'x' now has the reconstruction, so move it to 'x_n'
    dict_in['x_n'] = dict_in['x']
    dict_in['x_n_f'] = fftn(dict_in['x_n'])
    sec_input2 = sf.create_section(ps_params, 'Input3')
    sec_input2.read(dict_in)
    sec_observe = sf.create_section(ps_params, 'Observe1')
    sec_observe.observe(dict_in)
    #now remove the extra padding from x
    results = sf.create_section(ps_params, 'Results1')
    results.update(dict_in)
Exemplo n.º 2
0
    def __init__(self, ps_parameters, str_section):
        """Class constructor for Operator.
        """

        super(Solver, self).__init__(ps_parameters, str_section)
        self.int_iterations = self.get_val('nitn', True)
        self.results = sf.create_section(ps_parameters,
                                         self.get_val('results', False))
Exemplo n.º 3
0
 def __init__(self, ps_parameters, str_section):
     """
     Class constructor for ISNR.
     """
     super(RER, self).__init__(ps_parameters, str_section)
     self.x_f = None  #ground truth dft
     self.fmetrics = sf.create_section(ps_parameters,
                                       self.get_val('fmetrics', False))
Exemplo n.º 4
0
 def __init__(self, ps_params, str_section):
     """
     Class constructor for Scattering
     """
     super(Scattering, self).__init__(ps_params, str_section)
     self.depth = self.get_val('depth', True)
     self.transform_sec = self.get_val('transform', False)
     self.W = sf.create_section(ps_params, self.transform_sec)
     self.max_transform_levels = self.W.nlevels
     if self.depth > self.max_transform_levels:
         raise ValueError(
             'cannot have more scattering transform layers than wavelet transform scales'
         )
     #create the versions of W we'll need in a list, increasing the number of scales at each index
     self.W = []
     for J in xrange(1, self.max_transform_levels + 1):
         ps_params.set_key_val_pairs(self.transform_sec, ['nlevels'], [J])
         self.W.append(sf.create_section(ps_params, self.transform_sec))
Exemplo n.º 5
0
 def __init__(self,ps_parameters,str_section):
     """Class constructor for SampledFT. 
     """
     super(SampledFT,self).__init__(ps_parameters,str_section)
     self.output_fourier = 1
     #setup the mask
     mask_sec_in = self.get_val('masksectioninput',False)
     self.mask = None
     if mask_sec_in:
         sec_mask = sf.create_section(self.get_params(), mask_sec_in)
         self.mask = sec_mask.read({}, True)
Exemplo n.º 6
0
 def load_csr_avg(self):
     sec_input=sf.create_section(self.get_params(),self.sparse_matrix_input)
     sec_input.filepath+=self.file_string
     self.file_path=sec_input.filepath
     if os.path.isfile(self.file_path):
         self.csr_avg_save=sec_input.read({},True)
         if self.csr_avg_save!=None:
             self.csr_avg=csr_matrix((1.0/self.csr_avg_save.data,
                                      self.csr_avg_save.indices,
                                      self.csr_avg_save.indptr),
                                      shape=self.csr_avg_save.shape)
             return True
     return False
Exemplo n.º 7
0
def main():
    #configuration specification, absolute path
    seed_flag = -1
    if len(sys.argv)>1:
        ps_path=sys.argv[1]
        if len(sys.argv)>2:
            seed_flag = sys.argv[2]
    else:    
        ps_path='/home/tim/repos/py_solvers/applications/deconvolution/config/downsampled_cameraman_vbmm.ini'
    ps_params = ParameterStruct(ps_path)
    seed_flag = int(seed_flag)
    if seed_flag!=-1 and seed_flag>0:
        #sweep the seed and run this a bunch of times
        seeds = np.arange(0,seed_flag)
    else:
        seeds = [None]
    for seed in seeds:        
        if len(seeds)>1:
            ps_params.set_key_val_pairs('Observe1', ['seed'], [seed])
        dict_in = {}
        sec_input = sf.create_section(ps_params,'Input1')
        sec_preprocess = sf.create_section(ps_params,'Preprocess1')
        sec_observe = sf.create_section(ps_params,'Observe1')
        so_solver = sf.create_section(ps_params,'Solver1')

        #read, observe, solve, report
        sec_input.read(dict_in)
        sec_preprocess.preprocess(dict_in)
        sec_observe.observe(dict_in)
        t0 = time.time()    
        so_solver.solve(dict_in)
        t1 = time.time()
        so_solver.results.display()
        so_solver.results.save()
        print "Solver finished in " + str(t1-t0) + " seconds"
        if 'profiling' in dict_in:
            pretty2(dict_in['profiling'])
Exemplo n.º 8
0
def setup():

    this_dir = os.path.dirname(__file__)

    global qbgn
    qbgn = np.load(pjoin(this_dir, 'qbgn.npz'))['qbgn']

    global verif
    verif = np.load(pjoin(this_dir, 'verification.npz'))

    global ps_params
    ps_params = ParameterStruct(pjoin(this_dir, 'testparameters.ini'))

    global cell_image
    cell_image = sf.create_section(ps_params, 'Input1').read({}, True)
Exemplo n.º 9
0
 def __init__(self, ps_params, str_section):
     """
     Class constructor for ISNR.
     """
     super(ISNR, self).__init__(ps_params, str_section)
     self.y = None  #observation
     self.x = None  #ground truth
     self.y_key = self.get_val('comparisony', False)
     if self.y_key == '':
         self.y_key = 'y'
     self.transform_name = self.get_val('transform', False)
     if self.transform_name != '':
         self.transform = sf.create_section(ps_params, self.transform_name)
     else:
         self.transform = None
Exemplo n.º 10
0
 def __init__(self, ps_params, str_section):
     """
     Class constructor for Operator.
     """
     super(OperatorComp, self).__init__(ps_params, str_section)
     self.ls_op_names = self.get_val('operators', False)
     if self.ls_op_names.__class__.__name__ == 'str':
         self.ls_op_names = [self.ls_op_names]
     #build the operator objects
     self.ls_ops = [
         sf.create_section(ps_params, self.ls_op_names[i])
         for i in arange(len(self.ls_op_names))
     ]
     self.str_eval = self.get_mult_eval(False)
     self.str_eval_adjoint = self.get_mult_eval(True)
     self.str_eval_f = self.get_mult_eval(False, '.get_spectrum()')
     self.str_eval_adjoint_f = self.get_mult_eval(True, '.get_spectrum()')
     self.str_eval_psd = self.get_mult_eval(False, '.get_spectrum_sq()')
Exemplo n.º 11
0
def main():
    strmainpath='/home/tim/GoogleDrive/[email protected]/PhD/Projects/Classification/Data/GalaxyClassification' #workstation
    strmodelpath=strmainpath+'/images_training_rev1_formatted/training_model_small' #workstation
    ps_path='/home/tim/repos/py_solvers/applications/classification/galaxy_classification.ini'
    ps_params = ParameterStruct(ps_path)
    exp_list=['exp_1','exp_2','exp_3','exp_4','exp_5','exp_6','exp_7','exp_8','exp_9','exp_10','exp_11']
    class_dict={}
    class_dict[exp_list[0]]=['Class1.1','Class1.2','Class1.3']
    class_dict[exp_list[1]]=['Class2.1','Class2.2']
    class_dict[exp_list[2]]=['Class3.1','Class3.2']
    class_dict[exp_list[3]]=['Class4.1','Class4.2']
    class_dict[exp_list[4]]=['Class5.1','Class5.2','Class5.3','Class5.4']
    class_dict[exp_list[5]]=['Class6.1','Class6.2']
    class_dict[exp_list[6]]=['Class7.1','Class7.2','Class7.3']
    class_dict[exp_list[7]]=['Class8.1','Class8.2','Class8.3','Class8.4','Class8.5','Class8.6','Class8.7']
    class_dict[exp_list[8]]=['Class9.1','Class9.2','Class9.3']
    class_dict[exp_list[9]]=['Class10.1','Class10.2','Class10.3']
    class_dict[exp_list[10]]=['Class11.1','Class11.2','Class11.3','Class11.4','Class11.5','Class11.6']
    class_dict = OrderedDict(sorted(class_dict.items(), key=lambda t: t[0]))
    #do classification on testing set with unknown labels
    sec_input = sf.create_section(ps_params,'Input2')
    dict_in={}
    dict_in['x_feature']=sec_input.read(dict_in,return_val=True)
    for exp in exp_list:
        with open(strmainpath + '/' + exp + '_predict.csv', 'wb') as csvfile:
            galaxywriter = csv.writer(csvfile)
            Xtest=np.zeros([len(dict_in['x_feature'].keys()),241])
            for idx,galaxy_id in enumerate(dict_in['x_feature'].keys()):
                #aggregate the test matrix
                Xtest[idx,:]=dict_in['x_feature'][galaxy_id]
            #load the model for this experiment
            scaler = preprocessing.StandardScaler().fit(Xtest)
            Xtest=scaler.fit_transform(Xtest)
            filehandler = open(strmodelpath + '/' + exp + '_x_model_params.pkl', 'r') 
            model = cPickle.load(filehandler)
            filehandler.close()
            #make prediction
            ypreds=model.predict_proba(Xtest)
            #write to the currently open csv file
            galaxywriter.writerow(['GalaxyID']+class_dict[exp]) #header
            for idx,galaxy_id in enumerate(dict_in['x_feature'].keys()):
                galaxywriter.writerow([galaxy_id]+list(ypreds[idx,:]))
Exemplo n.º 12
0
 def create_kernel(self):
     """Creates or reads from file the kernel weights and stores
     in the class kernel attribute.
     """
     if self.str_type != 'file':
         self.ary_sz = self.get_val('size', True)
         ary_kernel = np.zeros(self.ary_sz)
     if self.str_type == 'uniform':
         ary_kernel[:] = 1.0 / np.prod(self.ary_sz)
     elif self.str_type == 'separable':
         pyr_side = np.asfarray([1, 4, 6, 4, 1])  #hard-coded size
         ary_kernel = np.outer(pyr_side, pyr_side)
         self.ary_sz = np.asarray(ary_kernel.shape)
         ary_kernel /= np.sum(ary_kernel)
     elif self.str_type == 'rational':
         length = 7  #hard-coded size
         ary_kernel = np.zeros([2 * length + 1, 2 * length + 1])
         for x1 in np.arange(-7, 7 + 1):
             for x2 in np.arange(-7, 7 + 1):
                 ary_kernel[x1 + 7, x2 + 7] = 1.0 / (x1**2 + x2**2 + 1)
         self.ary_sz = np.asarray(ary_kernel.shape)
         ary_kernel /= np.sum(ary_kernel)
     elif self.str_type == 'gaussian':
         ary_kernel = gaussian(self.ary_sz, self.gaussian_sigma)
     elif self.str_type == 'hamming':
         ary_kernel = np.hamming(self.ary_sz)
         ary_kernel /= np.sum(ary_kernel)
     elif self.str_type == 'file':
         sec_input = sf.create_section(self.ps_parameters,
                                       self.get_val('filesection', False))
         ary_kernel = sec_input.read({}, True)
         self.ary_sz = array(ary_kernel.shape)
     else:
         raise ValueError("no such kernel " + self.str_type + " supported")
     if self.ary_sz.__class__.__name__ == 'int':
         self.ary_sz = np.array([self.ary_sz])
     self.kernel = ary_kernel
Exemplo n.º 13
0
    def preprocess(self, dict_in):
        """Loads observation model parameters into a dictionary, 
        performs the forward model and provides an initial solution.

        Args:
        dict_in (dict): Dictionary which must include the following members:
            'x' (ndarray): The 'ground truth' input signal to be modified.
        """
        #build the preprocessing parameters
        if (self.str_type == 'brainwebmri'):
            #need to pad/crop the input data for wavelet processing
            swap_axes = self.get_val('swapaxes', True)
            if swap_axes.__class__.__name__ == 'ndarray':
                dict_in['x'] = dict_in['x'].swapaxes(swap_axes[0],
                                                     swap_axes[1])
            input_shape = dict_in['x'].shape

            #cropping
            new_shape = self.get_val('newshape', True)
            if new_shape.__class__.__name__ == 'ndarray':
                new_shape = tuple(new_shape)
            #figure out what to crop, if anything
            if np.any(new_shape < input_shape):
                crop_shape = np.min(np.vstack((new_shape, input_shape)),
                                    axis=0)
                dict_in['x'] = crop_center(dict_in['x'], crop_shape)
            else:
                crop_shape = input_shape
            #padding
            if np.any(new_shape > crop_shape):
                pad_shape = np.max(np.vstack((new_shape, crop_shape)), axis=0)
                dict_in['x'] = pad_center(dict_in['x'], pad_shape)
        # elif (self.str_type == 'superresolution'):
        #     #need to crop edge of image to make results compatible with the literature

        elif (self.str_type == 'phasevelocity'):
            mask_sec_in = self.get_val('masksectioninput', False)
            bmask_sec_in = self.get_val('boundarymasksectioninput', False)
            ls_local_lim_sec_in = self.get_val('vcorrects', False)
            if ls_local_lim_sec_in.__class__.__name__ == 'str' and ls_local_lim_sec_in:
                ls_local_lim_sec_in = [ls_local_lim_sec_in]
            ls_local_lim_secs = []
            if ls_local_lim_sec_in:
                ls_local_lim_secs = [
                    sf.create_section(self.get_params(), local_lim_sec_in)
                    for local_lim_sec_in in ls_local_lim_sec_in
                ]
                ls_local_lim_secs = [{
                    'phaselowerlimit':
                    local_lim.get_val('phaselowerlimit', True),
                    'phaseupperlimit':
                    local_lim.get_val('phaseupperlimit', True),
                    'regionupperleft':
                    local_lim.get_val('regionupperleft', True),
                    'regionlowerright':
                    local_lim.get_val('regionlowerright', True)
                } for local_lim in ls_local_lim_secs]
            #load the mask
            if mask_sec_in != '':
                sec_mask_in = sf.create_section(self.get_params(), mask_sec_in)
                dict_in['mask'] = np.asarray(sec_mask_in.read(dict_in, True),
                                             dtype='bool')
            else:
                dict_in['mask'] = True

            if bmask_sec_in != '':
                sec_bmask_in = sf.create_section(self.get_params(),
                                                 bmask_sec_in)
                dict_in['boundarymask'] = np.asarray(sec_bmask_in.read(
                    dict_in, True),
                                                     dtype='bool')
            else:
                dict_in['boundarymask'] = np.asarray(np.zeros(
                    dict_in['x'][:, :, 0].shape),
                                                     dtype='bool')

            if self.get_val('nmracquisition',
                            True):  #compute phase from lab measurement
                #The frame ordering determines in which direction to compute the
                #phase differences to obtain positive velocities

                frame_order = [0, 1]
                if self.get_val('reverseframeorder'):
                    frame_order = [1, 0]
                #Fully sampled fourier transform in order to extract phase data
                for frame in xrange(2):
                    dict_in['x'][:, :, frame] = fftn(
                        fftshift(dict_in['x'][:, :, frame]))
                if self.get_val('extrafftshift', True):
                    for frame in xrange(2):
                        dict_in['x'][:, :,
                                     frame] = fftshift(dict_in['x'][:, :,
                                                                    frame])

                #Compute phase differences between the two frames
                diff_method = self.get_val('phasedifferencemethod')
                if diff_method == 'conjugateproduct':
                    new_x = (dict_in['x'][:, :, frame_order[1]] *
                             conj(dict_in['x'][:, :, frame_order[0]]))
                    theta = angle(new_x)
                    theta += np.max(np.abs(theta))
                    # theta /= np.max(np.abs(theta))
                    # theata *= np.pi*2
                    magnitude = sqrt(abs(new_x))

                elif diff_method == 'subtraction':
                    theta = (angle(dict_in['x'][:, :, frame_order[1]]) -
                             angle(dict_in['x'][:, :, frame_order[0]]))
                    magnitude = 0.5 * (
                        np.abs(dict_in['x'][:, :, frame_order[0]]) +
                        np.abs(dict_in['x'][:, :, frame_order[1]]))
                # if self.get_val('reverseframeorder'):
                #     theta = -theta
                #     theta+=np.abs(np.min(theta))
                new_x = magnitude * exp(1j * theta)

            else:  #synthetic data
                theta = angle(dict_in['x'])
                magnitude = nabs(dict_in['x'])

            #Do phase unwrapping. This works almost everywhere, except
            #in certain areas where the range of phases exceeds 2*pi.
            #These areas must also be unwrapped with special limits
            #which are determined from the data.
            dict_global_lims = {}
            dict_global_lims['lowerlimit'] = self.get_val(
                'phaselowerlimit', True)
            dict_global_lims['upperlimit'] = self.get_val(
                'phaseupperlimit', True)
            dict_global_lims['boundary_mask'] = dict_in['boundarymask']
            dict_global_lims['boundary_upperlimit'] = self.get_val(
                'boundaryphaseupperlimit', True)
            dict_global_lims['boundaryoverlapvcorrects'] = self.get_val(
                'boundaryoverlapvcorrects', True)

            theta = phase_unwrap(theta, dict_global_lims, ls_local_lim_secs)
            magnitude /= np.max(nabs(magnitude))
            dict_in['x'] = magnitude * exp(1j * theta)
            dict_in['theta'] = dict_in['mask'] * theta
            dict_in['magnitude'] = magnitude
            dict_in['dict_global_lims'] = dict_global_lims
            dict_in['ls_local_lim_secs'] = ls_local_lim_secs
Exemplo n.º 14
0
 def __init__(self, ps_parameters, str_section):
     """Class constructor for Results.
     """       
     super(Results,self).__init__(ps_parameters,str_section)
     self.ls_metric_names = self.get_val('metrics',False)
     if self.ls_metric_names.__class__.__name__ == 'str':
         self.ls_metric_names = [self.ls_metric_names]
     self.ls_metrics = [sf.create_section(ps_parameters, self.ls_metric_names[i]) \
                        for i in arange(len(self.ls_metric_names))]
     self.ls_metrics_csv = [self.ls_metrics[i] 
                            for i in arange(len(self.ls_metrics))
                            if self.ls_metrics[i].has_csv]                   
     self.ls_metrics_no_csv = [self.ls_metrics[i] 
                               for i in arange(len(self.ls_metrics))
                               if not self.ls_metrics[i].has_csv]                  
     self.grid_size = aa([self.get_val('figuregridwidth',True), \
                          self.get_val('figuregridheight',True)], dtype = np.int)
     self.desktop = self.get_val('desktop',True)
     self.row_offset = self.get_val('rowoffset',True)
     self.int_overlap = max(5,self.get_val('overlap',True))
     self.save_interval = self.get_val('saveinterval',True)
     self.output_directory = self.get_val('outputdirectory',False)
     #default to the configuration file's filename as a prefix
     self.output_filename = self.get_val('outputfilename',False,
                                         default_value=self.get_params_fname(False))
     self.overwrite_results = self.get_val('overwriteresults',True)
     self.zeros = self.get_val('zeros', True, DEFAULT_ZEROS)
     self.display_enabled = not self.get_val('disablefigures',True)
     self.monitor_count=0
     self.resolutions=tuple()
     
     #get screen info
     if self.display_enabled:
         #try to find a display
         screen = os.popen("xrandr -q -d :0").readlines()
         if len(screen)>0:
             ls_res=re.findall(' connected [0-9]*x[0-9]*', ''.join(screen))
             ls_res=re.findall('[0-9]*x[0-9]*', ''.join(ls_res))
             #the ordering of these assumes the screens are in the same order
             #if this is not the case, simply rearrange your screens ;-)
             self.resolutions=tuple([np.array(res.split('x'),'uint16') for res in ls_res])
             self.monitor_count = len(self.resolutions)
             self.desktop=np.mod(self.desktop,self.monitor_count)
             self.arrange_metric_windows() #figure out the coordinates
         else: #turn display off    
             self.monitor_count=0
             self.display_enabled = False
     #multiple viewport/desktop support not enabled, so wrapping the desktop to the range
     #of available monitors
     #create a folder in the output directory with the current minute's time stamp
     if self.output_directory=='':
         print ('Not writing results to file no output dir specified')
         return None
     st = '/' + self.output_filename
     self.output_directory += st + '/'
     if not os.path.exists(self.output_directory):
         os.makedirs(self.output_directory)
     if not self.overwrite_results:
         #old timestamping method, keep in case this is deemed better in the future
         # st += '/' + datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d_%H-%M-%S')
         #get the count of the current numbered directories
         files_enumerated = enumerate(os.walk(self.output_directory))
         files,dir_info=files_enumerated.next()
         results_count_string=str(len(dir_info[1])).zfill(self.zeros)
         self.output_directory += '/' + results_count_string + '/'
     if not os.path.exists(self.output_directory):
         os.makedirs(self.output_directory)
     #save the parameters to this folder as ini, and write as csv
     self.ps_parameters.write(self.output_directory + self.output_filename + '_config.ini')
     self.ps_parameters.write_csv(self.output_directory + self.output_filename + '_config.' + DEFAULT_CSV_EXT)
Exemplo n.º 15
0
def main():
    # strpath='/home/tim/GoogleDrive/[email protected]/PhD/Projects/Classification/Data/GalaxyClassification/' #workstation
    # strpath='/home/zelda/tr331/Projects/GalaxyChallenge/' #yoshi
    # training_dir='images_training_rev1/'
    # save_dir='images_training_rev1_formatted/'
    strpath = '/home/tim/GoogleDrive/[email protected]/PhD/Projects/Classification/Data/GalaxyClassification/'  #workstation
    # strpath='/home/zelda/tr331/Projects/GalaxyChallenge' #yoshi
    training_dir = 'images_training_rev1/'
    save_dir = 'images_training_rev1_formatted/'
    # training_dir='images_test_rev1/'
    # save_dir='images_test_rev1_formatted/'
    exp_list = [
        'exp1', 'exp2', 'exp3', 'exp4', 'exp5', 'exp6', 'exp7', 'exp8', 'exp9',
        'exp10', 'exp11'
    ]
    for index, exp in enumerate(exp_list):
        exp_list[index] = 'class_csv_' + exp
    class_dict = {}
    class_cols = {}
    class_dict[exp_list[0]] = ['Class1.1', 'Class1.2', 'Class1.3']
    class_dict[exp_list[1]] = ['Class2.1', 'Class2.2']
    class_dict[exp_list[2]] = ['Class3.1', 'Class3.2']
    class_dict[exp_list[3]] = ['Class4.1', 'Class4.2']
    class_dict[exp_list[4]] = ['Class5.1', 'Class5.2', 'Class5.3', 'Class5.4']
    class_dict[exp_list[5]] = ['Class6.1', 'Class6.2']
    class_dict[exp_list[6]] = ['Class7.1', 'Class7.2', 'Class7.3']
    class_dict[exp_list[7]] = [
        'Class8.1', 'Class8.2', 'Class8.3', 'Class8.4', 'Class8.5', 'Class8.6',
        'Class8.7'
    ]
    class_dict[exp_list[8]] = ['Class9.1', 'Class9.2', 'Class9.3']
    class_dict[exp_list[9]] = ['Class10.1', 'Class10.2', 'Class10.3']
    class_dict[exp_list[10]] = [
        'Class11.1', 'Class11.2', 'Class11.3', 'Class11.4', 'Class11.5',
        'Class11.6'
    ]
    tgt_size = [128, 128]
    feature_reduce = Scat().reduce  #function handle
    gen_csv = 1
    gen_bw_cropped_images = 0
    gen_feature_vector_files = 0
    if gen_feature_vector_files:
        feature_vector = {}  #dict, keys are galaxyids'
        ps_path = strpath + 'galaxy_params.ini'
        ps_params = ParameterStruct(ps_path)
        S = sf.create_section(ps_params, 'Transform2')
        #BEGINCOMMENTED SECTION(only for generating training data)
    with open(strpath + 'training_solutions_rev1.csv', 'rb') as csvfile:
        galaxyreader = csv.reader(csvfile)
        count = 0  #to skip the first row
        for row in galaxyreader:
            #ENDCOMMENTED SECTION(only for generating training data)
            #BEGINCOMMENTED SECTION(only for generating training data)
            if count == 0:  #skip
                header = row
                #get the column numbers (as lists) corresponding to each experiment
                for exp in exp_list:
                    class_cols[exp] = [
                        header.index(_class) for _class in class_dict[exp]
                    ]
            else:
                print 'processing galaxy ' + row[0]
                #decode the row data and separate into classes
                if gen_csv:
                    for exp in exp_list:
                        with open(strpath + exp + '.csv', 'ab') as csvfile2:
                            expwriter = csv.writer(csvfile2)
                            #pull out the probabilities corresponding to each class
                            class_probs = np.array([
                                np.float(row[column])
                                for column in class_cols[exp]
                            ])
                            #find the maximal class
                            _class = class_dict[exp][np.argmax(class_probs)]
                            #output the maximal class
                            expwriter.writerow([row[0]] + [_class])

                galaxyid = row[0]
                #now open the file, and do some adjustments
                traingalaxyfile = strpath + training_dir + galaxyid + '.jpg'
                savegalaxyfile = strpath + save_dir + galaxyid + '.png'
                #open the galaxies and resave them if specified
                if gen_bw_cropped_images:
                    galaxydata = mpimg.imread(traingalaxyfile)
                    newgalaxy = np.zeros(tgt_size)
                    #get the cropping region
                    st0 = (galaxydata.shape[0] - tgt_size[0]) / 2
                    st1 = (galaxydata.shape[1] - tgt_size[1]) / 2
                    SL0 = slice(st0, st0 + tgt_size[0], None)
                    SL1 = slice(st1, st1 + tgt_size[1], None)
                    #convert color-> rgb
                    galaxydata = rgb2gray(galaxydata)
                    newgalaxy = galaxydata[SL0, SL1]
                    f = open(savegalaxyfile, 'wb')
                    w = png.Writer(*(newgalaxy.shape[1], newgalaxy.shape[0]),
                                   greyscale=True)
                    newgalaxy = newgalaxy / np.max(newgalaxy) * 255
                    w.write(f, newgalaxy)
                    f.close()
                if gen_feature_vector_files:
                    x_data = misc.imread(savegalaxyfile)
                    feature_vector[galaxyid] = (feature_reduce(
                        (S * x_data).flatten(), method='average'))
            count += 1

    #now generate the features vector files for each experiment
    if gen_feature_vector_files:
        feature_file = strpath + save_dir + 'allfeatures.pkl'
        filehandler = open(feature_file, 'wb')
        cPickle.dump(feature_vector, filehandler)
        filehandler.close()
        #reopen
        filehandler = open(feature_file, 'r')
        feature_vector = cPickle.load(filehandler)
        filehandler.close()
        for exp in [exp_list[0]]:
            print 'creating feature vector file for ' + exp
            feature_vector_exp = {}
            for _class in class_dict[exp]:  #initialize with empty feature list
                feature_vector_exp[_class] = []
            feature_file_exp = exp + '_features.pkl'
            exp_csv_file = exp + '_small.csv'
            with open(strpath + save_dir + exp_csv_file, 'rb') as csvfile:
                csvreader = csv.reader(csvfile)
                for row in csvreader:
                    feature_vector_exp[row[1]].append(feature_vector[row[0]])
            filehandlerexp = open(strpath + save_dir + feature_file_exp, 'wb')
            cPickle.dump(feature_vector_exp, filehandlerexp)
            filehandlerexp.close()
Exemplo n.º 16
0
    def solve(self, dict_in):
        super(MSIST, self).solve()

        ##################################
        ### Transforms and Modalities ####
        ##################################
        H = self.H  #mapping from solution domain to observation domain
        dict_in['H'] = H
        W = self.W  #sparsifying transform
        dict_in['W'] = W
        # precision = 'float32'
        # if W.output_dtype!='':
        #     precision = W.output_dtype

        if self.alpha.__class__.__name__ != 'ndarray':
            self.alpha = su.spectral_radius(
                self.W, self.H, dict_in['x_0'].shape,
                self.get_val('alphamethod', False, 'spectrum'))
            # self.alpha = su.spectral_radius(self.W, self.H, (64,64,64),
            #                                 self.get_val('alphamethod', False, 'spectrum'))
        alpha = self.alpha  #Lambda_alpha main diagonal (B-sized vector of subband gains)
        dict_in['alpha'] = alpha
        ############
        #Input Data#
        ############

        if H.output_fourier:
            y_hat = dict_in['y']
        else:
            #do an extra FFT to do deconvolution in fourier domain
            y_hat = fftn(dict_in['y'])

        x_n = dict_in['x_0'].copy()  #seed current solution
        # The famous Joan Lasenby "residuals"
        dict_in['x_n'] = x_n
        x = dict_in['x'].copy()
        dict_in['resid_n'] = x - x_n
        x_max = np.max(x)
        x_min = np.min(x)
        # dict_in['resid_range'] = np.array([x_min - x_max, x_max + x_max])
        dict_in['resid_range'] = np.array([-255.0 / 2, 255.0 / 2])
        #######################
        #Common Initialization#
        #######################

        #determine whether/not we need double the wavelet transforms on
        #each iteration for a complex-valued input signal
        self.input_complex = np.iscomplexobj(x_n)

        #initialize current solution in sparse domain
        #g_i is the element group size (2 for CWT, 4 for CWT and input_complex)
        if self.input_complex:
            if self.input_phase_encoded:
                theta_n = su.phase_unwrap(angle(x_n),
                                          dict_in['dict_global_lims'],
                                          dict_in['ls_local_lim_secs'])
            else:
                theta_n = angle(x_n)
            dict_in['theta_n'] = theta_n
            dict_in['magnitude_n'] = nabs(x_n)

            w_n = [W * x_n.real, W * x_n.imag]
            g_i = 2 * (w_n[0].is_wavelet_complex() + 1)
        else:
            w_n = [W * x_n]
            g_i = (w_n[0].is_wavelet_complex() + 1)
        w_n_len = len(w_n)
        w_n_it = xrange(w_n_len)  #iterator for w_n
        dict_in['w_n'] = w_n

        #initialize the precision matrix with zeros
        S_n = w_n[0] * 0
        dict_in['S_n'] = S_n
        #initialize continuation parameters
        epsilon, nu = self.get_epsilon_nu()
        if self.ordepsilon:
            # self.ordepsilonpercstart = 8.0/9.0*(.55**2)
            epsilon = np.zeros(self.int_iterations + 1, )
            self.percentiles = np.arange(30, self.ordepsilonpercstop,
                                         -1.0 / self.int_iterations)
            epsilon[0] = self.get_ord_epsilon(w_n[0], np.inf,
                                              self.percentiles[0])
            dict_in['epsilon_sq'] = epsilon[0]**2
        else:
            dict_in['epsilon_sq'] = epsilon**2
        if self.convexnu:
            nu = np.zeros(self.int_iterations + 1, )
            nu[0] = self.get_convex_nu(w_n[0], epsilon[0]**2, np.min(alpha))
            dict_in['nu_sq'] = nu[0]**2
        else:
            dict_in['nu_sq'] = nu**2

        #wavelet domain variance used for poisson deblurring
        ary_p_var = 0

        ########################################
        #Sparse penalty-specific initialization#
        ########################################

        if self.str_sparse_pen == 'l0rl2_bivar':
            w_tilde = w_n[0] * 0
            sqrt3 = sqrt(3.0)
            sigsq_n = self.get_val('nustop', True)**2
            sig_n = sqrt(sigsq_n)

        if self.str_sparse_pen == 'l0rl2_group':
            tau = self.get_val('tau', True)
            tau_rate = self.get_val('taurate', True)
            tau_start = self.get_val('taustart', True)
            if np.all(tau_start != 0) and tau_rate != 0:
                tau_end = tau
                tau = tau_start
            A = sf.create_section(self.ps_parameters,
                                  self.get_val('clusteraverage',
                                               False))  #cluster
            G = sf.create_section(self.ps_parameters,
                                  self.get_val('groupaverage', False))  #group
            #initialize A and G with parameters of the master vector
            A.init_csr_avg(w_n[0])
            G.init_csr_avg(w_n[0])
            dup_it = xrange(
                A.duplicates)  # iterator for duplicate variable space

            #initialize non-overlapping space (list of ws objects ls_w_hat_n)
            ls_w_hat_n = [[w_n[ix_] * 1 for j in dup_it] for ix_ in w_n_it]

            #initialize non-overlapping space precision
            ls_S_hat_n = [
                ((sum([w_n[ix].energy()
                       for ix in w_n_it]) / g_i) + epsilon[0]**2).invert()
                for int_dup in dup_it
            ]
            w_bar_n = [w_n[ix_] * 1 for ix_ in w_n_it]

            #using the structure of A, initialize the support of Shat, what
            A_row_ix = np.nonzero(A.csr_avg)[0]
            A_col_ix = np.nonzero(A.csr_avg)[1]
            D = csr_matrix((np.ones(A_col_ix.size, ), (A_row_ix, A_col_ix)),
                           shape=A.csr_avg.shape)

            #compute the support of Shat
            ls_S_hat_sup = unflat_list(
                D.transpose() * ((w_n[0] * 0 + 1).flatten()), A.duplicates)

            #load this vector into each new wavelet subband object
            ls_S_hat_sup = [(w_n[0] * 0).unflatten(S_sup)
                            for S_sup in ls_S_hat_sup]
            ls_S_hat_sup = [
                S_hat_n_sup.nonzero() for S_hat_n_sup in ls_S_hat_sup
            ]
            del S_sup
            del S_hat_n_sup
            #precompute AtA (doesn't change from one iteration to the next)
            AtA = (A.csr_avg.transpose() * A.csr_avg).tocsr()

            #convert tau**2 to csr format to allow for subband-adaptive constraint
            if tau.__class__.__name__ != 'ndarray':
                tau_sq = np.ones(w_n[0].int_subbands) * tau**2
            else:
                tau_sq = tau**2
            tau_sq_dia = [((w_n[0] * 0 + 1).cast(A.dtype)) * tau_sq
                          for j in dup_it]
            # tau_sq_dia = [((w_n[0]*0+1))*tau_sq for j in dup_it]
            tau_sq_dia = su.flatten_list(tau_sq_dia)
            offsets = np.array([0])
            tau_sz = tau_sq_dia.size
            tau_sq_dia = dia_matrix((tau_sq_dia, offsets),
                                    shape=(tau_sz, tau_sz))

            #initialize S_hat_bar parameters for efficient matrix inverses
            Shatbar_p_filename = A.file_path.split('.pkl')[0] + 'Shatbar.pkl'
            if not os.path.isfile(Shatbar_p_filename):
                dict_in['col_offset'] = A.int_size
                S_hat_n_csr = su.flatten_list_to_csr(ls_S_hat_sup)
                su.inv_block_diag((tau_sq_dia) * AtA + S_hat_n_csr, dict_in)
                filehandler = open(Shatbar_p_filename, 'wb')
                cPickle.dump(dict_in['dict_bdiag'], filehandler, -1)
                del S_hat_n_csr
            else:
                filehandler = open(Shatbar_p_filename, 'rb')
                dict_in['dict_bdiag'] = cPickle.load(filehandler)
            filehandler.close()

            #store all of the l0rl2_group specific variables in the solver dict_in
            dict_in['ls_S_hat_n'] = ls_S_hat_n
            dict_in['ls_w_hat_n'] = ls_w_hat_n
            dict_in['w_bar_n'] = w_bar_n
            dict_in['G'] = G
            dict_in['A'] = A
            dict_in['W'] = W
            dict_in['AtA'] = AtA
            dict_in['ls_S_hat_sup'] = ls_S_hat_sup
            dict_in['w_n_it'] = w_n_it
            dict_in['dup_it'] = dup_it
            dict_in['ws_dummy'] = w_n[0] * 0
            dict_in['g_i'] = g_i

            # self.update_duplicates(dict_in,nu[0],epsilon[0],tau_sq, tau_sq_dia)

            w_bar_n = dict_in['w_bar_n']
            ls_w_hat_n = dict_in['ls_w_hat_n']
            ls_S_hat_n = dict_in['ls_S_hat_n']
            del D  #iterations need A and G only, not D

        if (self.str_sparse_pen == 'vbmm' or  #vbmm
                self.str_sparse_pen == 'vbmm_hmt'):
            p_a = self.get_val('p_a', True)
            p_b_0 = self.get_val('p_b_0', True)
            p_k = self.get_val('p_k', True)
            p_theta = self.get_val('p_theta', True)
            p_c = self.get_val('p_c', True)
            p_d = self.get_val('p_d', True)
            b_n = w_n[0] * 0
            sigma_n = 0
            if self.str_sparse_pen == 'vbmm_hmt':
                ary_a = self.get_gamma_shapes(W * dict_in['x_0'])
                b_n = w_n[0] * p_b_0

        #poisson + gaussiang noise,
        #using the scaling coefficients in the regularization (MSIST-P)
        if self.input_poisson_corrupted:
            #need a 0-padded y to get the right size for the scaling coefficients
            b = dict_in['b']
            if not H.output_fourier:
                y_hat = fftn(dict_in['y'] - b)
            else:
                y_hat = fftn(ifftn(dict_in['y']) - b)
            w_y = (W * dict_in['y_padded'])
            dict_in['x_n'] = su.crop_center(x_n, dict_in['y'].shape)
            w_y_scaling_coeffs = w_y.downsample_scaling()

        self.results.update(dict_in)
        print 'Finished itn: n=' + str(0)
        #begin iterations here for the MSIST(-X) algorithm, add some profiling info here
        if self.profile:
            dict_profile = {}
            dict_profile['twoft_time'] = []
            dict_profile['wht_time'] = []
            dict_profile['other_time'] = []
            dict_profile['reproj_time_inv'] = []
            dict_profile['reproj_time_for'] = []
            dict_in['profiling'] = dict_profile
            t0 = time.time()
        ####################
        ##Begin Iterations##
        ####################
        for n in np.arange(self.int_iterations):
            ####################
            ###Landweber Step###
            ####################
            twoft_0 = time.time()
            H.set_output_fourier(True)  #force Fourier output to reduce ffts
            if self.input_complex:
                f_resid = y_hat - H * x_n  #Landweber difference
            else:
                f_resid = ifftn(y_hat - H * x_n)
                H.set_output_fourier(False)
            twoft_1 = time.time()
            if self.input_complex:
                HtHf = (~H) * f_resid
                w_resid = [W * (HtHf).real, W * (HtHf).imag]
            else:
                w_resid = [W * ((~H) * f_resid)]
            wht = time.time()
            if self.profile:
                dict_profile['twoft_time'].append(twoft_1 - twoft_0)
                dict_profile['wht_time'].append(wht - twoft_1)
            ########################
            ######Convex Nu#########
            #####Ord/HMT Epsilon####
            ########################
            if self.ordepsilon:
                if n == 0:
                    prevepsilon = epsilon[0]
                else:
                    prevepsilon = epsilon[n - 1]
                epsilon[n] = self.get_ord_epsilon(w_n[0], prevepsilon,
                                                  self.percentiles[n])
                dict_in['epsilon_sq'] = epsilon[n]**2
            if self.convexnu:
                nu[n] = self.get_convex_nu(w_n[0], epsilon[n]**2,
                                           np.min(self.alpha))
                dict_in['nu_sq'] = nu[n]**2

            ###############################################
            ###Sparse Penalty-Specific Thresholding Step###
            ###############################################
            if self.str_sparse_pen == 'l0rl2_group':
                #S_hat_n, w_hat_n, and wb_bar (eqs 11, 19, and 13)
                self.update_duplicates(dict_in, nu[n], epsilon[n], tau_sq,
                                       tau_sq_dia)
                w_bar_n = dict_in['w_bar_n']
                ls_w_hat_n = dict_in['ls_w_hat_n']

            #####################################################
            #Subband-adaptive subband update of precision matrix#
            #####################################################
            if (self.str_sparse_pen[0:5] == 'l0rl2'
                    and self.str_sparse_pen[-5:] != 'bivar'):
                if self.str_sparse_pen == 'l0rl2_group':
                    S0_n = nsum(
                        [nabs(w_n[ix].ary_lowpass)**2 for ix in w_n_it],
                        axis=0) / g_i + epsilon[n]**2
                    S0_n = 1.0 / S0_n
                else:
                    if self.hmt:
                        S_n_prev = S_n * 1.0
                        S_n.set_subband(
                            0, (1.0 /
                                ((1.0 / g_i) * nabs(w_n[0].get_subband(0))**2 +
                                 (epsilon[n]**2))))

                        for s in xrange(w_n[0].int_subbands - 1, 0, -1):
                            sigma_sq_parent_us = nabs(
                                w_n[0].get_upsampled_parent(s))**2
                            s_parent_sq = 1.0 / (
                                (2.0**(-2.25)) *
                                (1.0 / g_i * sigma_sq_parent_us))
                            S_n.set_subband(s, s_parent_sq)

                    else:
                        S_n = (sum([w_n[ix_].energy()
                                    for ix_ in w_n_it]) / g_i +
                               epsilon[n]**2).invert()
            elif (self.str_sparse_pen[0:5] == 'vbmm'
                  and self.str_sparse_pen[-5:] != 'hmt'):
                cplx_norm = 1.0 + self.input_complex
                S_n = ((g_i + 2.0 * p_a) *
                       (sum([w_n[ix_].energy()
                             for ix_ in w_n_it]) / cplx_norm + sigma_n +
                        2.0 * b_n).invert())
                b_n = (p_k + p_a) * (S_n.get_subband(s) + p_theta).invert()
                sigma_n = (1.0 / nu[n]**2 * alpha[s] + S_n).invert()

            else:
                #iterating through subbands is necessary, coarse to fine
                for s in xrange(w_n[0].int_subbands - 1, -1, -1):
                    #Sendur Selesnick BSWLVE paper
                    if self.str_sparse_pen == 'l0rl2_bivar':
                        if s > 0:
                            s_parent_us = nabs(
                                w_n[0].get_upsampled_parent(s))**2
                            s_child = nabs(w_n[0].get_subband(s))**2
                            yi, yi_mask = su.get_neighborhoods(s_child,
                                                               1)  #eq 8
                            s_child_norm = sqrt(s_parent_us + s_child)
                            sigsq_y = np.sum(yi, axis=yi.ndim - 1) / np.sum(
                                yi_mask, axis=yi.ndim - 1)  #still eq 8...
                            sig = sqrt(np.maximum(sigsq_y - sigsq_n, 0))
                            w_tilde.set_subband(s, sqrt3 * sigsq_n /
                                                sig)  #the thresholding fn
                            thresh = np.maximum(
                                s_child_norm - w_tilde.get_subband(s),
                                0) / s_child_norm  #eq 5
                            if np.mod(
                                    n, 2
                            ) == 0:  #update with the bivariate thresholded coefficients on every other iteration
                                S_n.set_subband(
                                    s,
                                    (1.0 /
                                     ((1.0 / g_i) *
                                      nabs(thresh * w_n[0].get_subband(s))**2 +
                                      (epsilon[n]**2))))
                            else:
                                S_n.set_subband(
                                    s, (1.0 / ((1.0 / g_i) *
                                               nabs(w_n[0].get_subband(s))**2 +
                                               (epsilon[n]**2))))
                        else:
                            S_n.set_subband(s, (1.0 / (
                                (1.0 / g_i) * nabs(w_n[0].get_subband(s))**2 +
                                epsilon[n]**2)))

                    elif self.str_sparse_pen == 'vbmm_hmt':  #vbmm
                        if n == 0:
                            sigma_n = 0
                        else:
                            sigma_n = (1.0 / nu[n]**2 * alpha[s] +
                                       S_n.get_subband(s))**(-1)
                        if s > 0:
                            w_parent_us = w_n[0].get_upsampled_parent(s)
                            alpha_dec = 2.25
                            if s > S_n.int_orientations:
                                s_child = S_n.subband_group_sum(
                                    s - S_n.int_orientations, 'children')
                                b_child = b_n.subband_group_sum(
                                    s - S_n.int_orientations, 'children')
                            else:
                                s_child = 0
                                b_child = 0
                            if s < S_n.int_subbands - S_n.int_orientations:
                                ap = ary_a[s + S_n.int_orientations]
                            else:
                                ap = .5
                            w_en_avg = w_n[0].subband_group_sum(
                                s, 'parent_children')
                            S_n.set_subband(
                                s, (g_i + 2.0 * ary_a[s]) /
                                (nabs(w_n[0].get_subband(s))**2 + sigma_n +
                                 2.0 * b_n.get_subband(s)))
                            b_n.set_subband(s, ary_a[s] * w_en_avg)
                        else:  #no parents, so generate fixed-param gammas
                            S_n.set_subband(
                                s, (g_i + 2.0 * ary_a[s]) /
                                (nabs(w_n[0].get_subband(s))**2 + sigma_n +
                                 2.0 * b_n.get_subband(s)))
                            b_n.set_subband(s, (p_k + ary_a[s]) /
                                            (S_n.get_subband(s) + p_theta))
                    else:
                        raise ValueError('no such solver variant')
            #########################
            #Update current solution#
            #########################
            for s in xrange(w_n[0].int_subbands - 1, -1, -1):
                if self.input_poisson_corrupted:
                    if s == 0:
                        ary_p_var = w_y.ary_lowpass
                    else:
                        int_lev, int_ori = w_n[0].lev_ori_from_subband(s)
                        ary_p_var = w_y_scaling_coeffs[int_lev]
                        ary_p_var[ary_p_var <= 0] = 0
                if (self.str_sparse_pen == 'l0rl2_group'):
                    if s > 0:
                        for ix_ in w_n_it:
                            w_n[ix_].set_subband(
                                s,
                                (alpha[s] * w_n[ix_].get_subband(s) +
                                 w_resid[ix_].get_subband(s) +
                                 (tau_sq[s]) * w_bar_n[ix_].get_subband(s)) /
                                (alpha[s] + tau_sq[s]))
                    else:  #a standard msist update for the lowpass coeffs
                        for ix_ in w_n_it:
                            w_n[ix_].set_subband(s, \
                                                 (alpha[s] * w_n[ix_].get_subband(s) + w_resid[ix_].get_subband(s)) /
                                                 (alpha[s] + (nu[n]**2) * S0_n))
                else:
                    for ix_ in w_n_it:
                        w_n[ix_].set_subband(
                            s, (alpha[s] * w_n[ix_].get_subband(s) +
                                w_resid[ix_].get_subband(s)) /
                            (alpha[s] +
                             (nu[n]**2 + self.sc_factor * ary_p_var) *
                             S_n.get_subband(s)))
                #end updating subbands

            #############################################
            ##Solution Domain Projection and Operations##
            #############################################
            tother = time.time()
            if self.input_complex:
                x_n = np.asfarray(~W * w_n[0], 'complex128')
                x_n += 1j * np.asfarray(~W * w_n[1], 'complex128')
                m_n = nabs(x_n)
                theta_n = angle(x_n)
                if self.input_phase_encoded:  #need to apply boundary conditions for phase encoded velocity
                    #the following isn't part of the documented algorithm
                    #it only needs to be executed at the end to fix
                    #phase wrapping in very high dynamic-phase regions
                    theta_n = su.phase_unwrap(angle(x_n),
                                              dict_in['dict_global_lims'],
                                              dict_in['ls_local_lim_secs'])
                    if self.get_val(
                            'iterationmask', True
                    ):  #apply boundary conditions for phase encoded velocity
                        theta_n *= dict_in['mask']
                        if self.get_val('magnitudemask', True, 1):
                            m_n *= dict_in[
                                'mask']  #uncomment this for 'total' masking
                    x_n = m_n * exp(1j * theta_n)
                dict_in['theta_n'] = theta_n
                dict_in['magnitude_n'] = m_n
            else:
                x_n = ~W * w_n[0]
            tinvdwt = time.time()
            #implicit convolution operator is used, so crop and repad
            if H.str_object_name == 'Blur' and H.lgc_even_fft:
                x_n = su.crop_center(x_n, dict_in['y'].shape)
            if self.input_poisson_corrupted and self.spatial_threshold:
                x_n[x_n < self.spatial_threshold_val] = 0.0

            #finished spatial domain operations on this iteration, store
            dict_in['x_n'] = x_n
            # store "residuals"
            dict_in['resid_n'] = x - x_n
            # dict_in['resid_range'] = np.array([np.min(dict_in['resid_n']), np.max(dict_in['resid_n'])])
            print "resid min " + str(np.round(np.min(dict_in['resid_n']), 2))
            print "resid max " + str(np.round(np.max(dict_in['resid_n']), 2))

            if H.str_object_name == 'Blur' and H.lgc_even_fft:
                x_n = su.pad_center(x_n, dict_in['x_0'].shape)

            #############################
            #Wavelet Domain Reprojection#
            #############################
            if self.profile:
                dict_profile['other_time'].append(tother - wht)
            if self.input_complex:
                w_n = [W * x_n.real, W * x_n.imag]
            else:
                w_n = [W * x_n]
            tforwardwt = time.time()
            if self.profile:
                dict_profile['reproj_time_inv'].append(tinvdwt - tother)
                dict_profile['reproj_time_for'].append(tforwardwt - tinvdwt)
            if self.str_sparse_pen[:11] == 'l0rl2_group':
                ls_w_hat_n = [[
                    ls_w_hat_n[ix_][j] * ls_S_hat_sup[j] + w_bar_n[ix_] *
                    ((ls_S_hat_sup[j] + (-1)) * (-1)) for j in dup_it
                ] for ix_ in w_n_it]  #fill in the gaps with w_bar_n
                w_bar_n = [W * ((~W) * w_bar_n[ix_]) for ix_ in w_n_it]
                ls_w_hat_n = [[
                    W * ((~W) * w_hat_n) for w_hat_n in ls_w_hat_n[ix_]
                ] for ix_ in w_n_it]
                dict_in['w_bar_n'] = w_bar_n
                dict_in['ls_w_hat_n'] = ls_w_hat_n
                if tau_rate != 0 and not np.any(tau > tau_end):
                    tau_sq_dia = tau_rate * tau_sq_dia
                    tau = np.sqrt(tau_rate) * tau
            dict_in['w_n'] = w_n
            dict_in['S_n'] = S_n
            ################
            #Update Results#
            ################
            self.results.update(dict_in)
            print 'Finished itn: n=' + str(n + 1)
            # if self.str_sparse_pen[:11] == 'l0rl2_group' and n==150: #an interesting experiment for cs..
            #     self.str_sparse_pen = 'l0rl2'

        return dict_in
Exemplo n.º 17
0
    def solve(self,dict_in):
        super(Classify,self).solve()
        S = self.S
        feature_reduce = Scat().reduce #function handle to static method
        classes = sorted(dict_in['x'].keys())
        dict_in['class_labels']=classes
        if self.feature_sec_in != '': #load the feature vectors from disk
            sec_input = sf.create_section(self.get_params(), self.feature_sec_in)
            dict_in['x_feature'] = sec_input.read(dict_in, return_val = True)
            
            if self.class_sec_in != '': 
                #The class specification (csv), should already have 
                #been read in sec_input.
                #Now organize dict_in['x_feature'] into classes 
                #using dict_in['x'] as a reference
                #dict_in['x_feature'][exemplarid]->
                #dict_in['x_feature'][exemplarclass] 
                #where dict_in['x_feature']['exemplarclass'] is a list
                print 'organizing classes for this experiment...'
                for _class in classes:
                    dict_in['x_feature'][_class]=[]
                    for _exemplar in dict_in['x'][_class]:
                        exemplar_feature=dict_in['x_feature'].pop(_exemplar[0])
                        dict_in['x_feature'][_class].append(exemplar_feature)
        else:   #no feature vector file specified, so compute
            met_output_obj = sf.create_section(self.get_params(),
                                               self.feature_sec_out)
            #generate the features if not previously stored from a prior run
            #takes a while...
            for _class_index,_class in enumerate(classes):
                print 'generating scattering features for class ' + _class
                n_samples = len(dict_in['x'][_class])
                dict_in['x_feature'][_class]= (
                  [feature_reduce((S*dict_in['x'][_class][sample][1]).flatten(),
                                  method = self.feature_reduction)
                                  for sample in xrange(n_samples)])

        #assumes each feature vector is the same size for all exemplars...
        dict_in['feature_vec_sz'] = dict_in['x_feature'][classes[0]][-1].size
        Xtrain = np.zeros([dict_in['n_training_samples'],
                           dict_in['feature_vec_sz']],dtype='double')
        Xtest = np.zeros([dict_in['n_testing_samples'],
                          dict_in['feature_vec_sz']],dtype='double')
        
        #vector to hold the training labels
        ytrain = np.zeros(dict_in['n_training_samples'],dtype='int16')
        ytest  = np.zeros(dict_in['n_testing_samples'],dtype='int16')
        sample_index = 0
        print 'generating training/test data using pre-computed partitions...'
        #generate the training and test data matrix (X) and label vectors (y)
        for _class_index,_class in enumerate(classes):
            for train_ix in dict_in['x_train'][_class]:
                Xtrain[sample_index,:] = (dict_in['x_feature'][_class][train_ix])
                ytrain[sample_index] = dict_in['y_label'][_class]
                sample_index+=1
        sample_index = 0
        ls_testing_index = []
        ls_exemplar_id = []
        for _class_index,_class in enumerate(classes):
            for test_ix in dict_in['x_test'][_class]:
                Xtest[sample_index,:] = (dict_in['x_feature'][_class][test_ix])
                ytest[sample_index] = dict_in['y_label'][_class]
                ls_testing_index.append(test_ix)
                ls_exemplar_id.append(dict_in['x'][_class][test_ix][0])
                sample_index+=1
                
        dict_in['y_truth']=ytest
        dict_in['y_truth_sample_index']=np.array(ls_testing_index,dtype='int16')
        dict_in['exemplar_id']=ls_exemplar_id

        #rescaling X and y, since SVM is not scale invariant
        # Xtrain /= np.max(Xtrain)
        # Xtest /= np.max(Xtest)
        scaler = preprocessing.StandardScaler().fit(Xtrain)
        Xtrain=scaler.fit_transform(Xtrain)
        Xtest=scaler.transform(Xtest)
        dict_in['x_scaler_params']=scaler
        #train the model
        print 'fitting the model...'
        if self.classifier_method=='affinepca':
            dict_in['pca_train']={}
            #we must fit a model separately for each of the class subspaces
            for _cls_index,_cls in enumerate(classes):
                #xcls_feat is n_samples (C) X n_features (\barSx_c)
                xcls_feat=Xtrain[ytrain==dict_in['y_label'][_cls],:]
                xcls_feat_mean=np.mean(xcls_feat,axis=0)
                #subtract the mean from each sample scattering vector 
                #(rows in xcls_feat) and do pca
                self.clf.fit(xcls_feat-xcls_feat_mean)
                #store the components as a list in the pca_train dict
                #the first element being the mean (scattering class centroid), 
                #the second: the n_features x n_components array of PCs
                dict_in['pca_train'][_cls]=[xcls_feat_mean,self.clf.components_]
            #now fit the models by minimizing the error in the orthogonal linear 
            #space projection of classes
            #error matrix is (D+1) X ()
            #where D is the number of components in the pca decomposition
            pcaD=self.clf.components_.shape[0]+1
            err_mtx=np.zeros([pcaD,dict_in['n_testing_samples'],len(classes)])
            for _cls_index,_cls in enumerate(classes):      
                sx_minus_esxc=Xtest-dict_in['pca_train'][_cls][0]
                vc=dict_in['pca_train'][_cls][1]
                #calculate the orthoganal projection at each d in D
                #for the first row, sum energy along feature axis
                err_temp_1 = np.sum(np.abs(sx_minus_esxc)**2,axis=1)
                err_temp_rest = -(np.abs(vc.dot(sx_minus_esxc.T))**2)
                # pdb.set_trace()
                err_temp = np.cumsum(np.vstack([err_temp_1,err_temp_rest]),
                                     axis=0)**(.5)
                err_mtx[:,:,_cls_index]=err_temp
            #error matrix is now num_samplesXnum_classes
            err_mtx=err_mtx[-1,:,:]    
            #minimum D+1 error along class dimension
            error_mins=np.amin(err_mtx,axis=1)
            #now find the indices corresponding to these mins
            print 'making prediction...'
            dict_in['y_pred']=np.array([int(
                np.where(error_mins[j]==err_mtx[j,:])[0])
                for j in xrange(error_mins.shape[0])])
            dict_in['x_model_params']=dict_in['pca_train']
        else:#svm or some other method
            self.clf.fit(Xtrain,ytrain)
            #perform classification/prediction on the test set
            print 'making prediction...'
            if self.output=='probabilities':
                dict_in['y_pred']=self.clf.predict_proba(Xtest)
                for index,_class in enumerate(classes):
                    dict_in[_class]=dict_in['y_pred'][:,index]
            else:#non-probabilistic              
                dict_in['y_pred']=self.clf.predict(Xtest)
            dict_in['x_model_params']=self.clf
        self.results.update(dict_in)