def compute_pca_scores(recording, sorting, n_comp=3, grouping_property=None, compute_property_from_recording=False,
                     by_electrode=False, max_num_waveforms=np.inf, save_as_features=True, verbose=False):
    '''

    Parameters
    ----------
    recording
    sorting
    n_comp
    grouping_property
    by_electrode
    max_num_waveforms
    save_as_features

    Returns
    -------

    '''
    # concatenate all waveforms
    all_waveforms = np.array([])
    nspikes = []
    idx_not_none = None
    if 'waveforms' in sorting.get_unit_spike_feature_names():
        if verbose:
            print("Using 'waveforms' features")
        waveforms = []
        for unit_id in sorting.get_unit_ids():
            wf = sorting.get_unit_spike_features(unit_id, 'waveforms')
            idx_not_none = np.array([i for i in range(len(wf)) if wf[i] is not None])
            if len(idx_not_none) != len(wf):
                if verbose:
                    print("Using ", len(idx_not_none), " waveforms for unit ", unit_id)
                wf = np.array([wf[i] for i in idx_not_none])
            waveforms.append(wf)
    else:
        if verbose:
            print("Computing waveforms")
        waveforms = get_unit_waveforms(recording, sorting)

    for i_w, wf in enumerate(waveforms):
        if wf is None:
            wf = get_unit_waveforms(recording, sorting, [sorting.get_unit_ids()[i_w]],
                                  max_num_waveforms=max_num_waveforms,
                                  compute_property_from_recording=compute_property_from_recording,
                                  verbose=verbose)
        if by_electrode:
            wf_reshaped = wf.reshape((wf.shape[0] * wf.shape[1], wf.shape[2]))
            nspikes.append(len(wf) * recording.get_num_channels())
        else:
            wf_reshaped = wf.reshape((wf.shape[0], wf.shape[1] * wf.shape[2]))
            nspikes.append(len(wf))
        if i_w == 0:
            all_waveforms = wf_reshaped
        else:
            all_waveforms = np.concatenate((all_waveforms, wf_reshaped))
    print("Fitting PCA of %d dimensions on %d waveforms" % (n_comp, len(all_waveforms)))

    pca = PCA(n_components=n_comp, whiten=True)
    pca.fit_transform(all_waveforms)
    scores = pca.transform(all_waveforms)

    init = 0
    pca_scores = []
    for i_n, nsp in enumerate(nspikes):
        pcascores = scores[init: init + nsp, :]
        init = nsp + 1
        if by_electrode:
            pca_scores.append(pcascores.reshape(nsp // recording.get_num_channels(),
                                                recording.get_num_channels(), n_comp))
        else:
            pca_scores.append(pcascores)

    if save_as_features:
        for i, unit_id in enumerate(sorting.get_unit_ids()):
            if len(pca_scores[i]) < len(sorting.get_unit_spike_train(unit_id)):
                assert idx_not_none is not None
                if 'pca_scores' not in sorting.get_unit_spike_feature_names(unit_id):
                    features = np.array([None] * len(sorting.get_unit_spike_train(unit_id)))
                else:
                    features = np.array(sorting.get_unit_spike_features(unit_id, 'pca_scores'))
                for i, ind in enumerate(idx_not_none):
                    features[ind] = pca_scores[i]
            else:
                features = pca_scores[i]
            sorting.set_unit_spike_features(unit_id, 'pca_scores', features)

    return pca_scores
def compute_pca_scores ( recording , sorting , n_comp = 3 , grouping_property = None , compute_property_from_recording = False , by_electrode = False , max_num_waveforms = np . inf , save_as_features = True , verbose = False ) : all_waveforms = np . array ( [ ] ) nspikes = [ ] idx_not_none = None if 'waveforms' in sorting . get_unit_spike_feature_names ( ) : if verbose : print ( "Using 'waveforms' features" ) waveforms = [ ] for unit_id in sorting . get_unit_ids ( ) : wf = sorting . get_unit_spike_features ( unit_id , 'waveforms' ) idx_not_none = np . array ( [ i for i in range ( len ( wf ) ) if wf [ i ] is not None ] ) if len ( idx_not_none ) != len ( wf ) : if verbose : print ( "Using " , len ( idx_not_none ) , " waveforms for unit " , unit_id ) wf = np . array ( [ wf [ i ] for i in idx_not_none ] ) waveforms . append ( wf ) else : if verbose : print ( "Computing waveforms" ) waveforms = get_unit_waveforms ( recording , sorting ) for i_w , wf in enumerate ( waveforms ) : if wf is None : wf = get_unit_waveforms ( recording , sorting , [ sorting . get_unit_ids ( ) [ i_w ] ] , max_num_waveforms = max_num_waveforms , compute_property_from_recording = compute_property_from_recording , verbose = verbose ) if by_electrode : wf_reshaped = wf . reshape ( ( wf . shape [ 0 ] * wf . shape [ 1 ] , wf . shape [ 2 ] ) ) nspikes . append ( len ( wf ) * recording . get_num_channels ( ) ) else : wf_reshaped = wf . reshape ( ( wf . shape [ 0 ] , wf . shape [ 1 ] * wf . shape [ 2 ] ) ) nspikes . append ( len ( wf ) ) if i_w == 0 : all_waveforms = wf_reshaped else : all_waveforms = np . concatenate ( ( all_waveforms , wf_reshaped ) ) print ( "Fitting PCA of %d dimensions on %d waveforms" % ( n_comp , len ( all_waveforms ) ) ) pca = PCA ( n_components = n_comp , whiten = True ) pca . fit_transform ( all_waveforms ) scores = pca . transform ( all_waveforms ) init = 0 pca_scores = [ ] for i_n , nsp in enumerate ( nspikes ) : pcascores = scores [ init : init + nsp , : ] init = nsp + 1 if by_electrode : pca_scores . append ( pcascores . reshape ( nsp // recording . get_num_channels ( ) , recording . get_num_channels ( ) , n_comp ) ) else : pca_scores . append ( pcascores ) if save_as_features : for i , unit_id in enumerate ( sorting . get_unit_ids ( ) ) : if len ( pca_scores [ i ] ) < len ( sorting . get_unit_spike_train ( unit_id ) ) : assert idx_not_none is not None if 'pca_scores' not in sorting . get_unit_spike_feature_names ( unit_id ) : features = np . array ( [ None ] * len ( sorting . get_unit_spike_train ( unit_id ) ) ) else : features = np . array ( sorting . get_unit_spike_features ( unit_id , 'pca_scores' ) ) for i , ind in enumerate ( idx_not_none ) : features [ ind ] = pca_scores [ i ] else : features = pca_scores [ i ] sorting . set_unit_spike_features ( unit_id , 'pca_scores' , features ) return pca_scores
Parameters
    ----------
    recording
    sorting
    n_comp
    grouping_property
    by_electrode
    max_num_waveforms
    save_as_features

    Returns
    -------