Sparkle expects meta data to have a certain heirarchial organization,
    reformat batlab experiment data to fit.

def batlab2sparkle(experiment_data):
    """Sparkle expects meta data to have a certain heirarchial organization,
    reformat batlab experiment data to fit. 
    """
    # This is mostly for convention.. attribute that matters most is samplerate, 
    # since it is used in the GUI to calculate things like duration
    nsdata = {}
    for attr in ['computername', 'pst_filename', 'title', 'who', 'date', 'program_date']:
        nsdata[attr] = experiment_data[attr]
    for itest, test in enumerate(experiment_data['test']):
        setname = 'test_{}'.format(itest+1)
        nsdata[setname] = {}
        nsdata[setname]['samplerate_ad'] = test['trace'][0]['samplerate_ad']
        nsdata[setname]['comment'] = test['comment']
        nsdata[setname]['start'] = test['time']
        nsdata[setname]['mode'] = 'finite'
        nsdata[setname]['user_tag'] = ''

        if test['full_testtype'] == 'General Auto Test' and test['testtype'] == 'tone':
            nsdata[setname]['testtype'] = 'Tuning Curve'
        else:
            nsdata[setname]['testtype'] = test['full_testtype']

        stims = []
        for itrace, trace in enumerate(test['trace']):
            try:
                stim = {'samplerate_da': trace['samplerate_da'],
                        'overloaded_attenuation': 0,}
                components = []
                for icomp, component in enumerate(trace['stimulus']):
                    # always add in silence component to match batlab's delay parameter
                    delay_comp = {'index': [icomp, 0], 'stim_type': 'silence', 
                            'intensity': 0, 'duration': component['delay']/1000., 
                            'start_s': 0, 'risefall': 0}
                    components.append(delay_comp)
                    # FIXME need to pull in speaker calibration to get real intensity
                    comp = {'risefall' : component['rise_fall']/1000., 
                            'index': [icomp, 1], 
                            'duration': component['duration']/1000.,
                            'start_s': component['delay']/1000.,
                            'intensity': 100 - component['attenuation']}
                    if component['soundtype_name'] == 'vocalization':
                        # print component
                        comp['stim_type'] = 'Vocalization'
                        comp['filename'] = component['vocal_call_file']
                        comp['browsedir'] = ''
                    elif component['soundtype_name'] == 'fmsweep':
                        comp['stim_type'] = 'FM Sweep'
                        usweep = 1 if component['usweep'] else -1
                        comp['start_f'] = component['frequency'] - (component['bandwidth']/2)*usweep
                        comp['stop_f'] = component['frequency'] + (component['bandwidth']/2)*usweep
                    elif component['soundtype_name'] == 'tone':
                        comp['stim_type'] = 'Pure Tone'
                        comp['frequency'] = component['frequency']
                    else:
                        # print 'FOUND UNKNOWN STIM', component['soundtype_name']
                        # raise ValueError
                        comp['stim_type'] = component['soundtype_name']

                    components.append(comp)
                stim['components'] = components
                stims.append(stim)
            except TypeError:
                print 'PROBLEM with', itest, itrace
                print 'component', component
                continue

        nsdata[setname]['stim'] = stims

    return nsdata