Exemple #1
0
def classify_1obj(din):
    """
    Perform classification of 1 supernova.

    input: din, dict - keywords, value type: 
                     user_input, dict -> output from read_user_input
                     name, str -> name of raw light curve file
                     type_number, dict -> translate between str and numerical
                                          classes identification
                     do_plot, bool -> if True produce plots, default is False

                     p1, dict ->  keywords, value type:
                         fname_photo_list, str: list of all photometric 
                                                sample objects
                         photo_dir, str: directory of GP fitted results
                                         for photo sample
                         range_pcs, list: [min_number_PCs, max_number_PCs]
                                          to be tested through cross-validation
                         SNR_dir, str: directory to store all results from 
                                       this SNR cut
                         out_dir, str: directory to store classification 
                                       results
                         plot_proj_dir, str: directory to store 
                                             projection plots
                         data_matrix, str: file holding spec data matrix

    output: class_results:
               list -> [snid, true_type, prob_Ia] 
    """
    from snclass.functions import screen, nneighbor
    from snclass.util import translate_snid, read_snana_lc
    from snclass.treat_lc import LC

    # update supernova name
    din['user_input']['path_to_lc'] = [translate_snid(din['name'])[0]]

    # read raw data
    raw = read_snana_lc(din['user_input'])

    # set true type
    for names in din['type_number'].keys():
        if raw[din['user_input']['type_flag']
               [0]][0] in din['type_number'][names]:
            true_type = names

    # load GP fit and test epoch cuts
    new_lc = LC(raw, din['user_input'])
    new_lc.user_choices['samples_dir'] = [din['p1']['photo_dir']]
    new_lc.load_fit_GP(din['p1']['photo_dir'] + din['name'])

    l1 = [
        1 if len(new_lc.fitted['GP_fit'][fil]) > 0 else 0
        for fil in din['user_input']['filters']
    ]

    fil_choice = din['user_input']['ref_filter'][0]
    if fil_choice == 'None':
        fil_choice = None

    if sum(l1) == len(din['user_input']['filters']):
        new_lc.normalize(samples=True, ref_filter=fil_choice)
        new_lc.mjd_shift()
        new_lc.check_epoch()

        if new_lc.epoch_cuts:

            screen(new_lc.raw['SNID:'][0], din['user_input'])

            # build matrix lines
            new_lc.build_steps(samples=True)

            # transform samples
            small_matrix = new_lc.samples_for_matrix
            data_test = din['p1']['obj_kpca'].transform(small_matrix)

            #classify samples
            new_label = nneighbor(data_test, din['p1']['spec_matrix'],
                                  din['p1']['binary_types'], din['user_input'])

            # calculate final probability
            ntypes = [1 for item in new_label if item == '0']
            new_lc.prob_Ia = sum(ntypes) / \
                             float(din['user_input']['n_samples'][0])

            if din['do_plot']:
                plot_proj(din['p1']['spec_matrix'], data_test,
                          din['p1']['labels'], new_lc, din['p1']['plot_dir'],
                          [0, 1], true_type)

            # print result to screen
            screen('SN' + new_lc.raw['SNID:'][0] + \
                   ',   True type: ' + true_type + ', prob_Ia = ' + \
                    str(new_lc.prob_Ia), din['user_input'])

            class_results = [new_lc.raw['SNID:'][0], true_type, new_lc.prob_Ia]
            return class_results
Exemple #2
0
    def check_file(self, filename, epoch=True, ref_filter=None):
        """
        Construct one line of the data matrix.

        input:   filename, str
                 file of raw data for 1 supernova

                 epoch, bool - optional
                 If true, check if SN satisfies epoch cuts
                 Default is True

                 ref_filter, str - optional
                 Reference filter for peak MJD calculation
                 Default is None
        """
        screen('Fitting ' + filename, self.user_choices)

        # translate identifier
        self.user_choices['path_to_lc'] = [
            translate_snid(filename, self.user_choices['photon_flag'][0])[0]
        ]

        # read light curve raw data
        raw = read_snana_lc(self.user_choices)

        # initiate light curve object
        lc_obj = LC(raw, self.user_choices)

        # load GP fit
        lc_obj.load_fit_GP(self.user_choices['samples_dir'][0] + filename)

        # normalize
        lc_obj.normalize(ref_filter=ref_filter)

        # shift to peak mjd
        lc_obj.mjd_shift()

        if epoch:
            # check epoch requirements
            lc_obj.check_epoch()
        else:
            lc_obj.epoch_cuts = True

        if lc_obj.epoch_cuts:
            # build data matrix lines
            lc_obj.build_steps()

            # store
            obj_line = []
            for fil in self.user_choices['filters']:
                for item in lc_obj.flux_for_matrix[fil]:
                    obj_line.append(item)

            rflag = self.user_choices['redshift_flag'][0]
            redshift = raw[rflag][0]

            obj_class = raw[self.user_choices['type_flag'][0]][0]

            self.snid.append(raw['SNID:'][0])

            return obj_line, redshift, obj_class

        else:
            screen('... Failed to pass epoch cuts!', self.user_choices)
            screen('\n', self.user_choices)
            return None