neuroscout/populate/convert.py
def ingest_text_stimuli(filename, dataset_name, task_name, parent_ids=None,
                        transformer='FAVEAlign', params=None, onsets=None,
                        resample_ratio=1, complete_only=False, col_name='text'):
    """ Ingest converted text stimuli from file.
    Args:
        filename - aligned transcript, with onset, duration and text columns
        dataset_name - Name of dataset in debug
        task_name - Task name
        parent_ids - Parent stimulus db id(s)
        transformer - Transformer name
        params - Extra parameters to recordings
        onsets - onset of the parent stimulus relative to the transcript.
        resample_ratio - Ratio to multiply onsets (after cropping) to adjust
                         for variable play speeds.
        complete_only - Only add complete transcript ComplexTextStim
    """
    dataset_id = Dataset.query.filter_by(name=dataset_name).one().id

    # If no parent ids, find closest matching stimulus
    if parent_ids is None:
        stem = Path(filename).stem
        parent_ids = Stimulus.query.filter_by(
            dataset_id=dataset_id).filter(
                Stimulus.path.contains(stem + ".")).order_by(
                    Stimulus.id.asc()).first().id

    parent_ids = listify(parent_ids)
    onsets = listify(onsets)
    if params is None:
        params = {}

    df = pd.read_csv(filename, delimiter='\t').dropna()

    if onsets is None:
        onsets = [0] * len(parent_ids)
    if len(parent_ids) != len(onsets):
        raise ValueError("parent_ids must match number of onsets")

    for onset, parent_id in zip(onsets, parent_ids):
        # Trim and align trancript
        sub = df[df.onset > onset]
        sub = sub[sub.duration > 0]
        sub['onset'] = (sub['onset'] - onset) * resample_ratio

        # Calculate run duration
        duration = max(db.session.query(RunStimulus.duration).filter_by(
            stimulus_id=parent_id).distinct())[0]
        if onset < 0:
            duration = duration - onset
        sub = sub[sub.onset < duration]

        # Get associations with parent stimulus
        rs_orig = RunStimulus.query.filter_by(stimulus_id=parent_id).join(
            Run).join(Task).filter_by(name=task_name)

        if not rs_orig.count():
            raise Exception("No RunStimulus associations match.")

        # Create new stimuli
        new_stims = [
            TextStim(text=row['text'], onset=row['onset'],
                     duration=row.get('duration'))
            for ix, row in sub.iterrows()]

        # Complete transcript stimulus stand in
        transcript_stim = ComplexTextStim(
            elements=new_stims)
        transcript_stim.name = 'FULL_TRANSCRIPT'

        if complete_only:
            new_stims = [transcript_stim]
        else:
            new_stims.append(transcript_stim)

        params['onset'] = onset
        params['duration'] = duration
        params['resample_ratio'] = resample_ratio

        create_new_stimuli(
            dataset_id, task_name, new_stims, rs_orig,
            parent_id=parent_id, transformer=transformer, 
            transformer_params=params)
 Ingest converted text stimuli from file.
    Args:
        filename - aligned transcript, with onset, duration and text columns
        dataset_name - Name of dataset in debug
        task_name - Task name
        parent_ids - Parent stimulus db id(s)
        transformer - Transformer name
        params - Extra parameters to recordings
        onsets - onset of the parent stimulus relative to the transcript.
        resample_ratio - Ratio to multiply onsets (after cropping) to adjust
                         for variable play speeds.
        complete_only - Only add complete transcript ComplexTextStim
    
Ingest converted text stimuli from file.
Ingest converted text stimuli from file .
def ingest_text_stimuli(filename, dataset_name, task_name, parent_ids=None,
                        transformer='FAVEAlign', params=None, onsets=None,
                        resample_ratio=1, complete_only=False, col_name='text'):
    dataset_id = Dataset.query.filter_by(name=dataset_name).one().id
    if parent_ids is None:
        stem = Path(filename).stem
        parent_ids = Stimulus.query.filter_by(
            dataset_id=dataset_id).filter(
                Stimulus.path.contains(stem + ".")).order_by(
                    Stimulus.id.asc()).first().id
    parent_ids = listify(parent_ids)
    onsets = listify(onsets)
    if params is None:
        params = {}
    df = pd.read_csv(filename, delimiter='\t').dropna()
    if onsets is None:
        onsets = [0] * len(parent_ids)
    if len(parent_ids) != len(onsets):
        raise ValueError("parent_ids must match number of onsets")
    for onset, parent_id in zip(onsets, parent_ids):
        sub = df[df.onset > onset]
        sub = sub[sub.duration > 0]
        sub['onset'] = (sub['onset'] - onset) * resample_ratio
        duration = max(db.session.query(RunStimulus.duration).filter_by(
            stimulus_id=parent_id).distinct())[0]
        if onset < 0:
            duration = duration - onset
        sub = sub[sub.onset < duration]
        rs_orig = RunStimulus.query.filter_by(stimulus_id=parent_id).join(
            Run).join(Task).filter_by(name=task_name)
        if not rs_orig.count():
            raise Exception("No RunStimulus associations match.")
        new_stims = [
            TextStim(text=row['text'], onset=row['onset'],
                     duration=row.get('duration'))
            for ix, row in sub.iterrows()]
        transcript_stim = ComplexTextStim(
            elements=new_stims)
        transcript_stim.name = 'FULL_TRANSCRIPT'
        if complete_only:
            new_stims = [transcript_stim]
        else:
            new_stims.append(transcript_stim)
        params['onset'] = onset
        params['duration'] = duration
        params['resample_ratio'] = resample_ratio
        create_new_stimuli(
            dataset_id, task_name, new_stims, rs_orig,
            parent_id=parent_id, transformer=transformer, 
            transformer_params=params)
def ingest_text_stimuli ( filename , dataset_name , task_name , parent_ids = None , transformer = 'FAVEAlign' , params = None , onsets = None , resample_ratio = 1 , complete_only = False , col_name = 'text' ) : dataset_id = Dataset . query . filter_by ( name = dataset_name ) . one ( ) . id if parent_ids is None : stem = Path ( filename ) . stem parent_ids = Stimulus . query . filter_by ( dataset_id = dataset_id ) . filter ( Stimulus . path . contains ( stem + "." ) ) . order_by ( Stimulus . id . asc ( ) ) . first ( ) . id parent_ids = listify ( parent_ids ) onsets = listify ( onsets ) if params is None : params = { } df = pd . read_csv ( filename , delimiter = '\t' ) . dropna ( ) if onsets is None : onsets = [ 0 ] * len ( parent_ids ) if len ( parent_ids ) != len ( onsets ) : raise ValueError ( "parent_ids must match number of onsets" ) for onset , parent_id in zip ( onsets , parent_ids ) : sub = df [ df . onset > onset ] sub = sub [ sub . duration > 0 ] sub [ 'onset' ] = ( sub [ 'onset' ] - onset ) * resample_ratio duration = max ( db . session . query ( RunStimulus . duration ) . filter_by ( stimulus_id = parent_id ) . distinct ( ) ) [ 0 ] if onset < 0 : duration = duration - onset sub = sub [ sub . onset < duration ] rs_orig = RunStimulus . query . filter_by ( stimulus_id = parent_id ) . join ( Run ) . join ( Task ) . filter_by ( name = task_name ) if not rs_orig . count ( ) : raise Exception ( "No RunStimulus associations match." ) new_stims = [ TextStim ( text = row [ 'text' ] , onset = row [ 'onset' ] , duration = row . get ( 'duration' ) ) for ix , row in sub . iterrows ( ) ] transcript_stim = ComplexTextStim ( elements = new_stims ) transcript_stim . name = 'FULL_TRANSCRIPT' if complete_only : new_stims = [ transcript_stim ] else : new_stims . append ( transcript_stim ) params [ 'onset' ] = onset params [ 'duration' ] = duration params [ 'resample_ratio' ] = resample_ratio create_new_stimuli ( dataset_id , task_name , new_stims , rs_orig , parent_id = parent_id , transformer = transformer , transformer_params = params )
Ingest converted text stimuli from file.
Ingest converted text stimuli from file .
""" Ingest converted text stimuli from file.
    Args:
        filename - aligned transcript, with onset, duration and text columns
        dataset_name - Name of dataset in debug
        task_name - Task name
        parent_ids - Parent stimulus db id(s)
        transformer - Transformer name
        params - Extra parameters to recordings
        onsets - onset of the parent stimulus relative to the transcript.
        resample_ratio - Ratio to multiply onsets (after cropping) to adjust
                         for variable play speeds.
        complete_only - Only add complete transcript ComplexTextStim
    """ # If no parent ids, find closest matching stimulus # Trim and align trancript # Calculate run duration # Get associations with parent stimulus # Create new stimuli # Complete transcript stimulus stand in
filename
dataset_name
task_name
parent_ids
transformer
params
onsets
resample_ratio
complete_only
col_name