Esempio n. 1
0
def plot_sea_onset_comparison(run_name,
                              var,
                              ax,
                              signature_types=signature_types):

    if run_name == 'obs':

        seadata['MPB'] = ['MPB ($nT^4$)', obs_mpb_v, obs_mpb_t]
        seadata['al'] = ['AL (nT)', sml, supermag_times]

        ylabel, data, times = seadata[var]

        artists, labels = plot_onset_sea(obs_signatures,
                                         obs_threshold,
                                         data,
                                         times,
                                         ylabel,
                                         ax,
                                         signature_types=signature_types)

    else:

        names = [runprops['name'] for runprops in run_properties]
        runprops = run_properties[names.index(run_name)]

        run_signatures = get_model_signature_lists(runprops, datadir=datadir)

        from spacepy import datamodel as dm
        auroral_inds = dm.fromHDF5(
            os.path.join(
                datadir, runprops['name'].replace('/', '').replace(' ', '_') +
                '_auroral_inds.h5'))
        al_time = [
            datetime(2005, 1, 1, tzinfo=UTC) + timedelta(seconds=60 * m)
            for m in range(0, 1440 * 31)
        ]

        #try:
        #    mpbdata=loadmat(os.path.join(datadir,'John Haiducek - '+runprops['name'].replace('/','').replace(' ','_')+'_mag_grid_lat=33_mpb.mat'))
        #except:
        #    raise

        #mpb_t=[datetime(2005,1,1,tzinfo=UTC)+timedelta(seconds=m*60) for m in range(0,31*24*60)]
        #mpb_v=mpbdata['mpb']
        mpb_t, mpb_v = parse_index(os.path.join(datadir, 'mpb_index.txt'))

        seadata['MPB'] = ['MPB ($nT^4$)', mpb_v, mpb_t]
        seadata['al'] = ['AL (nT)', auroral_inds['AL'], al_time]

        ylabel, data, times = seadata[var]
        artists, labels = plot_onset_sea(run_signatures,
                                         model_threshold,
                                         data,
                                         times,
                                         ylabel,
                                         ax,
                                         signature_types=signature_types)
    return artists, labels
def plot_skill_score_v_count_ratio(runprops,obs_thresholds=None,model_thresholds=None,skip_styles=0,show_circle=True):
    sweep_shapes=['o','s','v','d','^','>']

    run_signatures=get_model_signature_lists(runprops,datadir=datadir)
    obs_thresholds,model_thresholds,substorm_bins=get_sweep_substorm_bins(obs_signatures,run_signatures,obs_thresholds=obs_thresholds,model_thresholds=model_thresholds)
    true_positive,false_positive,false_negative,true_negative=get_counts(substorm_bins[:,:,0,:],substorm_bins[:,:,1,:],axis=2)



    hit_rates=hit_rate(true_positive,false_positive,false_negative,true_negative)
    false_alarm_rates=false_alarm_rate(true_positive,false_positive,false_negative,true_negative)

    total_model_substorms=(true_positive+false_positive).astype(int)
    total_obs_substorms=(true_positive+false_negative).astype(int)

    skillscores=heidke_skill(true_positive,false_positive,false_negative,true_negative)

    skillscore_ci_lower,skillscore_ci_upper=heidke_ci(substorm_bins[:,:,0,:],substorm_bins[:,:,1,:],axis=2)
    skillscore_err_upper=skillscore_ci_upper-skillscores
    skillscore_err_lower=-(skillscore_ci_lower-skillscores)

    fig=plt.figure(figsize=[5.5,3.5])
    lines=[]

    for i in range(skip_styles):
        plt.plot([],[])

    for i in range(total_obs_substorms.shape[0]):
        if obs_thresholds[i]==2.5:
            plot_kwargs={
                'linewidth':2.5,
                'markersize':8,
                'zorder':10
            }
        else:
            plot_kwargs={}
        line,caps,bars=plt.errorbar(total_model_substorms[i].astype(float)/total_obs_substorms[i],skillscores[i],[skillscore_err_upper[i],skillscore_err_lower[i]],linestyle='',marker=sweep_shapes[i+skip_styles],**plot_kwargs)
        lines.append(line)

    if show_circle:
        i=np.where(np.array(obs_thresholds)==2.5)
        j=np.where(np.array(model_thresholds)==2.5)
        plt.plot(total_model_substorms[i,j].astype(float)/total_obs_substorms[i,j],skillscores[i,j],marker='o',markersize=14,markerfacecolor='none',zorder=15,markeredgecolor='k',markeredgewidth=2)
        
    labels=['Obs. threshold={0:0.1f} ({1:d} events)'.format(obs_thresholds[i],total_obs_substorms[i,0]) for i in range(len(lines))]
    plt.legend(lines,labels,loc='upper left')
    plt.xscale('log')
    plt.xlabel('$n_{model}/n_{obs}$')
    plt.ylabel('Heidke skill score')
    plt.axvline(1,color='k',alpha=0.5,zorder=-1)
    plt.axhline(0,color='k',alpha=0.5,zorder=-1)
    plt.xlim(0.003,40)
    plt.ylim(-0.03,0.3)
    plt.tight_layout()
    return fig
Esempio n. 3
0
def plot_roc(runprops):
    run_signatures=get_model_signature_lists(runprops,datadir=datadir)
    obs_thresholds,model_thresholds,substorm_bins=get_sweep_substorm_bins(obs_signatures,run_signatures)
    true_positive,false_positive,false_negative,true_negative=get_counts(substorm_bins[:,:,0,:],substorm_bins[:,:,1,:],axis=2)

    hit_rates=hit_rate(true_positive,false_positive,false_negative,true_negative)
    false_alarm_rates=false_alarm_rate(true_positive,false_positive,false_negative,true_negative)

    total_model_substorms=(true_positive+false_positive).astype(int)
    total_obs_substorms=(true_positive+false_negative).astype(int)

    skillscores=heidke_skill(true_positive,false_positive,false_negative,true_negative)

    skillscore_ci_lower,skillscore_ci_upper=heidke_ci(substorm_bins[:,:,0,:],substorm_bins[:,:,1,:],axis=2)
    skillscore_err_upper=skillscore_ci_upper-skillscores
    skillscore_err_lower=-(skillscore_ci_lower-skillscores)

    fig=plt.figure(figsize=(4.5,3.5))
    plt.plot([0,1],color='k',marker='',alpha=0.6)
    lines=[]
    for i in range(false_alarm_rates.shape[0]):
        if obs_thresholds[i]==2.5:
            plot_kwargs={
                'linewidth':2.5,
                'markersize':8,
                'zorder':10
            }
        else:
            plot_kwargs={}

        lines.extend(plt.plot(false_alarm_rates[i],hit_rates[i],linestyle='-',clip_on=False,**plot_kwargs))

    i=np.where(obs_thresholds==2.5)
    j=np.where(model_thresholds==2.5)
    plt.plot(false_alarm_rates[i,j],hit_rates[i,j],marker='o',markersize=14,markerfacecolor='none',zorder=15,markeredgecolor='k',markeredgewidth=2)
    plt.xlabel('Probability of False Detection')
    plt.ylabel('Probability of Detection')
    labels=['Obs. threshold={0:0.1f} ({1:d})'.format(obs_thresholds[i],total_obs_substorms[i,0]) for i in range(len(lines))]
    plt.legend(lines,labels,loc='lower right')
    namestr=runprops['name'].replace('/','').replace(' ','_')
    plt.xlim(0,1)
    plt.ylim(0,1)
    plt.tight_layout()
    plt.savefig('roc_curves.svg')
    plt.close(fig)
Esempio n. 4
0
def isi_subplot(ax, run_names, onset_type, bin_max=15):
    fills = []
    lines = []
    labels = []

    for run_name in run_names:

        if run_name == 'obs':
            signatures = get_obs_signature_lists(datadir=datadir)
            threshold = obs_threshold
        else:
            runprops, = [
                runprops for runprops in run_properties
                if runprops['name'] == run_name
            ]

            signatures = get_model_signature_lists(runprops, datadir=datadir)
            threshold = model_threshold

        if onset_type == 'all':
            onsets = find_convolution_onsets(signatures,
                                             threshold,
                                             bandwidth=timedelta(0, 60 * 10))
            #onsets=[(onset-datetime(2005,1,1,tzinfo=UTC)).total_seconds() for onset in onsets]
            #print onsets
            #substorms,onsets=find_substorms(signatures,threshold=1,return_times=True,signature_filters=['AL'])
            #onsets=onsets.compressed()
            #print onsets
        else:
            onsets = signatures.get(onset_type, [])

        bins = np.linspace(0, bin_max, 100)
        bw = 0.2

        if len(onsets) > 3:
            if run_name == 'obs':
                color = 'LightSteelBlue'
            else:
                all_runnames = [
                    runprops['name'] for runprops in run_properties
                ]
                irun = all_runnames.index(run_name)
                import matplotlib
                run_colors = matplotlib.rcParams['axes.prop_cycle'].by_key(
                )['color']
                color = run_colors[irun]

            if run_name == 'obs' or onset_type == 'plasmoids':
                show_ci = True
            else:
                show_ci = False
            line, fill = plot_isi(ax, bins, onsets, color, bw, show_ci=show_ci)
            lines.append(line)
            fills.append(fill)
            if run_name == 'obs':
                labels.append('Observations')
            else:
                if len(run_properties) > 1:
                    labels.append(run_name)
                else:
                    labels.append('MHD')
    return zip(lines, fills), labels
Esempio n. 5
0
def get_table_data(table_signatures, nsamples=32000):

    obs_signatures = get_obs_signature_lists(datadir=datadir)
    obs_substorms = find_substorms_convolution(obs_signatures, obs_threshold)

    table_data = []

    for runprops in run_properties:
        table_data.append([])

        run_signatures = get_model_signature_lists(runprops, datadir=datadir)
        for signature in table_signatures:
            if signature == 'All':
                signature_run_substorms = find_substorms_convolution(
                    run_signatures, model_threshold)
                signature_obs_substorms = obs_substorms
            else:
                if signature in run_signatures:
                    signature_run_substorms, keys = make_grid(
                        {signature: run_signatures[signature]})
                    signature_run_substorms = signature_run_substorms.flatten()
                if signature in obs_signatures:
                    signature_obs_substorms, keys = make_grid(
                        {signature: obs_signatures[signature]})
                    signature_obs_substorms = signature_obs_substorms.flatten()

            if signature in obs_signatures and signature in run_signatures or signature == 'All':
                counts = get_counts(signature_run_substorms,
                                    signature_obs_substorms)
                true_positive, false_positive, false_negative, true_negative = counts
                obs_total = true_positive + false_negative
                run_total = true_positive + false_positive
                skill = heidke_skill(*counts)
                skill_ci = heidke_ci(signature_run_substorms,
                                     signature_obs_substorms, nsamples)
                signature_hit_rate = hit_rate(*counts)
                signature_hit_rate_ci = metric_ci(signature_run_substorms,
                                                  signature_obs_substorms,
                                                  hit_rate, nsamples)
                signature_false_alarm_rate = false_alarm_rate(*counts)
                signature_false_alarm_rate_ci = metric_ci(
                    signature_run_substorms, signature_obs_substorms,
                    false_alarm_rate, nsamples)
            else:
                if signature in run_signatures:
                    run_total = np.sum(signature_run_substorms)
                else:
                    run_total = None
                if signature in obs_signatures:
                    obs_total = np.sum(signature_obs_substorms)
                else:
                    obs_total = None
                skill = None
                skill_ci = (None, None)
                signature_hit_rate = None
                signature_hit_rate_ci = (None, None)
                signature_false_alarm_rate = None
                signature_false_alarm_rate_ci = (None, None)

            all_counts = get_counts(signature_run_substorms, obs_substorms)
            all_skill = heidke_skill(*all_counts)
            all_skill_ci = heidke_ci(signature_run_substorms, obs_substorms,
                                     nsamples)
            all_hit_rate = hit_rate(*all_counts)
            all_hit_rate_ci = metric_ci(signature_run_substorms, obs_substorms,
                                        hit_rate, nsamples)
            all_false_alarm_rate = false_alarm_rate(*all_counts)
            all_false_alarm_rate_ci = metric_ci(signature_run_substorms,
                                                obs_substorms,
                                                false_alarm_rate, nsamples)

            table_data[-1].append([
                signature, run_total, obs_total, skill, skill_ci,
                signature_hit_rate, signature_hit_rate_ci,
                signature_false_alarm_rate, signature_false_alarm_rate_ci,
                all_skill, all_skill_ci, all_hit_rate, all_hit_rate_ci,
                all_false_alarm_rate, all_false_alarm_rate_ci
            ])

    return table_data
    ax.axhline(0, color='k', linestyle=':')
    ax.axvline(0, color='k', linestyle=':')
    ax.set_ylabel(ylabel)
    ax.set_xlabel('Time since onset (h)')
    return zip(polycols,
               lines), [signature_type_labels[key] for key in signature_types]


obs_substorms, obs_onsets = find_substorms_convolution(obs_signatures,
                                                       obs_threshold,
                                                       tstep=tstep,
                                                       return_times=True)

run_onsets = {}

run_signatures = get_model_signature_lists(run_properties[0], datadir=datadir)

signature_type_labels = {
    'All': 'All',
    'AL': 'AL',
    'image': 'IMAGE/FUV',
    'plasmoids': 'Plasmoids',
    'dipolarizations': 'Dipolarizations',
    'epdata': 'LANL',
    'MPB': 'MPB'
}

signature_types = set(['All'] + obs_signatures.keys() + run_signatures.keys())
run_colors = {
    run: color
    for run, color in zip(