Beispiel #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
Beispiel #2
0
def classify_test(test_name,
                  matrix,
                  user_input,
                  test_dir='test_samples/',
                  csamples=True):
    """
    Classify one photometric supernova using a trained KernelPCA matrix.

    input: test_name, str
           name of mean GP fit file

           matrix, snclass.matrix.DataMatrix object
           trained KernelPCA matrix

           user_input, dict
           output from snclass.util.read_user_input

           test_dir, str, optional
           name of directory to store samples from test light curve
           Default is 'test_samples/'

           csamples, bool, optional
           If True, fit GP object and generate sample file as output
           otherwise reads samples from file
           Default is True

    return: new_lc, snclass.treat_lc.LC object
            updated with test projections and probability of being Ia
    """
    # update path to raw light curve
    user_input['path_to_lc'] = [translate_snid(test_name, 'FLUXCAL')[0]]

    # store number of samples for latter tests
    nsamples = user_input['n_samples'][0]

    # reset the number of samples for preliminary tests
    user_input['n_samples'] = ['0']

    # read raw data
    raw = read_snana_lc(user_input)

    # load GP fit and test epoch cuts
    new_lc = LC(raw, user_input)
    new_lc.load_fit_GP(user_input['samples_dir'][0] + test_name)
    new_lc.normalize()
    new_lc.mjd_shift()
    new_lc.check_epoch()

    if new_lc.epoch_cuts:
        # update test sample directory
        user_input['samples_dir'] = [test_dir]

        # update user choices
        new_lc.user_choices = user_input

        # update number of samples
        new_lc.user_choices['n_samples'] = [nsamples]

        # fit GP or normalize/shift fitted mean
        test_matrix = test_samples(new_lc, calc_samples=bool(csamples))

        # project test
        new_lc.test_proj = matrix.transf_test.transform(test_matrix)

        # classify
        new_lc.new_label = nneighbor(new_lc.test_proj, matrix.low_dim_matrix,
                                     matrix.sntype, matrix.user_choices)

        if csamples:
            new_lc.prob_Ia = sum([1 for item in new_label if item == '0'
                                  ]) / float(nsamples)

        return new_lc

    else:
        return None