def featurize_data(pbmap, pbnames, lcdata, metadata, nobjects, featurefile):
    """
    ***Feature extractor  for PLaSTiCC***

    Extracts features from data by some Cesium library functions.
    Builds a timeseries dictionary and for each time series extracts
    features. Features described in file: feature_sets.
    
    Created on Mon Apr 29 19:30:52 2019
    
    @author: luisarribas
    
    """
    print("")
    print("EXTRACTING FEATURES")
    print("===================")
    print("")
    print("Building Timeseries....wait")
    print("===========================")
    #**********************BUILD TIME SERIES**********************************
    tsdict = OrderedDict()
    for i in range(nobjects):
        row = metadata[i]
        thisid = row['object_id']
        target = row['target']

        meta = {'zBand':row['zBand'],\
                'z':row['hostgal_photoz'],\
                'zerr':row['hostgal_photoz_err'],\
                'mag':row['magnitude'],\
                'u-b':row['u-b'],\
                'b-v':row['b-v']
                }

        ind = (lcdata['object_id'] == thisid)
        thislc = lcdata[ind]

        pbind = [(thislc['passband'] == pb) for pb in pbmap]
        t = [thislc['mjd'][mask].data for mask in pbind]
        m = [thislc['flux'][mask].data for mask in pbind]
        e = [thislc['flux_err'][mask].data for mask in pbind]

        tsdict[thisid] = TimeSeries(t=t, m=m, e=e,\
                            label=target, name=thisid, meta_features=meta,\
                            channel_names=pbnames )

    print("")
    print("OK!")
    print(" ")

    #***********************FEATURE EXTRACTION WITH CESIUM********************
    warnings.simplefilter('ignore')
    if os.path.exists(featurefile):
        print("")
        print("Loading features from file....wait")
        print("==================================")
        featuretable, _ = featurize.load_featureset(featurefile)
        print("")
        print("OK!")
        print(" ")
    else:
        features_list = []
        print("")
        print("Computing features....wait")
        print("==========================")

        with schwimmbad.MultiPool() as pool:
            results = pool.imap(worker, list(tsdict.values()))
            for res in results:
                features_list.append(res)

        featuretable = featurize.assemble_featureset(features_list=features_list,\
                                  time_series=tsdict.values())
        featurize.impute_featureset(fset=featuretable,
                                    strategy='constant',
                                    value=0,
                                    max_value=18446744073709551000,
                                    inplace=True)
        featurize.save_featureset(fset=featuretable, path=featurefile)
        print("")
        print("OK!")
        print(" ")

    #*******Build Pandas dataframe output*************************************
    old_names = featuretable.columns.values
    new_names = ['{}_{}'.format(x, pbmap.get(y, 'meta')) for x, y in old_names]
    cols = [featuretable[col] for col in old_names]
    allfeats = Table(cols, names=new_names, masked=False)
    allfeats['target'] = metadata['target']
    allfeats = allfeats.to_pandas()
    allfeats = np.nan_to_num(allfeats)
    new_names.append('target')
    allfeats = Table(allfeats, names=new_names, masked=False)
    allfeats = allfeats.to_pandas()
    print("")
    print("Extracted features = ", len(allfeats.columns))
    print("==========================")
    print("")
    print("Nan Values detected = ", sum(len(allfeats) - allfeats.count()))
    print("==========================")

    return allfeats