Example #1
0
def fig_ew(outfil=None):

    # Init
    wvmnx = (1214.5, 1216.8) * u.AA
    wave = np.linspace(wvmnx[0].value, wvmnx[1].value, 100)
    wave = wave * u.AA

    # Single line (Lya)
    zabs = 0.0
    line = AbsLine(1215.6701 * u.AA)
    line.z = zabs
    line.attrib['N'] = 16.0
    line.attrib['b'] = 30.0

    vmodel = xsp.voigt.voigt_model(wave, line, Npix=None)  # No smoothing
    line.spec = vmodel

    # EW
    line.analy['WVMNX'] = wvmnx
    EW, sigEW = line.ew()

    # Plot boundaries
    ymnx = (-0.05, 1.1)

    if outfil == None:
        outfil = 'fig_EW.pdf'

    # Start the plot
    if outfil != None:
        pp = PdfPages(outfil)

    # Make the plots
    for ss in range(2):

        plt.figure(figsize=(8, 5))
        plt.clf()
        gs = gridspec.GridSpec(1, 1)

        # Axes
        ax = plt.subplot(gs[0, 0])
        ax.xaxis.set_minor_locator(plt.MultipleLocator(0.5))
        ax.xaxis.set_major_locator(plt.MultipleLocator(1.))
        ax.get_xaxis().get_major_formatter().set_useOffset(False)
        ax.set_xlim(wvmnx.value)
        ax.set_ylim(ymnx)
        ax.set_ylabel('Normalized Flux')
        ax.set_xlabel(r'Wavelength ($\AA$)')

        # Zero line
        ax.plot(wvmnx.value, (0., 0.), 'g--')

        # Data
        lines = ax.step(vmodel.dispersion,
                        vmodel.flux,
                        'k',
                        where='mid',
                        linewidth=1.5)
        #lines = ax.step(vmodel.dispersion, vmodel.flux, 'k', where='mid')#, linewidth=1.5)

        # Fill?
        if ss > 0:
            #ax.fill_between( vmodel.dispersion, vmodel.flux, [1.]*len(wave),
            #                        color='blue', alpha=0.3)
            ax.fill_between(lines[0].get_xdata(orig=False),
                            1,
                            lines[0].get_ydata(orig=False),
                            alpha=0.4)
            csz = 20.
            ax.text(1215.670, 0.5, 'EW', color='black', ha='center', size=csz)
            ax.text(1214.8,
                    0.2,
                    r'$W_\lambda = \, $' + '{:.2f}'.format(EW.value) +
                    r' $\AA$',
                    color='blue',
                    ha='left',
                    size=csz)

        # Fonts
        xputils.set_fontsize(ax, 15.)

        # Write
        plt.tight_layout(pad=0.2, h_pad=0., w_pad=0.1)
        pp.savefig()
        plt.close()

    # Finish
    print('tlk_spectra.fig_ew: Wrote {:s}'.format(outfil))
    pp.close()
# plot cluster boundaries
FeH_centers = 0.5 * (FeH_bins[1:] + FeH_bins[:-1])
alphFe_centers = 0.5 * (alphFe_bins[1:] + alphFe_bins[:-1])

Xgrid = np.meshgrid(FeH_centers, alphFe_centers)
Xgrid = np.array(Xgrid).reshape((2, 50 * 50)).T

H = clf.predict(scaler.transform(Xgrid)).reshape((50, 50))

for i in range(n_clusters):
    Hcp = H.copy()
    flag = (Hcp == i)
    Hcp[flag] = 1
    Hcp[~flag] = 0

    ax.contour(FeH_centers,
               alphFe_centers,
               Hcp, [-0.5, 0.5],
               linewidths=1,
               colors='k')

ax.xaxis.set_major_locator(plt.MultipleLocator(0.3))
ax.set_xlim(-1.101, 0.101)
ax.set_ylim(alphFe_bins[0], alphFe_bins[-1])

ax.set_xlabel(r'$\rm [Fe/H]$')
ax.set_ylabel(r'$\rm [\alpha/Fe]$')

plt.show()
print "Twinned"
print "GaAs GaSb AlSb CdTe Ge ZnTe InP GaP"
print np.degrees(np.arcsin(np.sqrt(3)*lattice_constants_all/(2*5.43095))) - 74.2068

print "Test"
print np.degrees(np.arcsin(1.2 * np.sin(np.radians(19.4712)))) - 19.4712

plt.hold(True)
plt.axis([0,4.10695300643,0,20])
#plt.pcolormesh(x,y2,strain_epi, cmap=cmap_map(lambda x: x/2.0+0.4, plt.cm.RdBu))
plt.pcolormesh(x,y2,strain_epi, cmap=plt.cm.bwr, vmin=-0.32, vmax=+0.32, rasterized=True)
#plt.colorbar(use_gridspec=True, format="%+1.2f", ticks=[-0.6,-0.45,-0.3,-0.15,0,0.15,0.3,0.45,0.6] )
cb = plt.colorbar(use_gridspec=True, format="%+1.2f")
cb.solids.set_rasterized(True) 
ax.xaxis.set_minor_locator(plt.MultipleLocator(0.5))
ax.yaxis.set_major_locator(plt.MultipleLocator(2.5))
ax.yaxis.set_minor_locator(plt.MultipleLocator(1.25))

plt.xlabel('Tilt (Degrees)')
plt.ylabel('Intrinsic Lattice Mismatch (\%)')
#plt.title('Effective Mismatch of Tilted Strained Films')

CS = plt.contour(x,y2,strain_epi,levels=[0], colors='black', linestyles='dashdot', zorder=100)
#Change dash-dot pattern, on, off, on, off
for c in CS.collections:
    c.set_dashes([(0, (4.0, 3.0, 1.0, 3.0))])
    
ax.annotate('Zero Projected Strain', xy=(1.55, 7.45), xytext=(1.75, 5.5),
            arrowprops=dict(facecolor='black', shrink=0.1,width=0.05,headwidth=2),
            )
Example #4
0
        yerr=error_FM1,
        error_kw=dict(elinewidth=1, ecolor='black'),
        label=r'$ FM1-virus $')
plt.bar(gT_lab_fresh - bar_width / 2,
        gX31_lab_fresh,
        bar_width,
        alpha=0.1,
        color='black',
        yerr=error_lab_fresh,
        error_kw=dict(elinewidth=1, ecolor='black'),
        label=r'$ (X31-virus) $')
plt.grid(True, which='both')
plt.title(r'$ Original \ Antigenic \ Sin \ (sequential-infection)$',
          fontsize=AlvaFontSize)
plt.xlabel(r'$time \ (%s)$' % (timeUnit), fontsize=AlvaFontSize)
plt.ylabel(r'$ Neutralization \ \ titer $', fontsize=AlvaFontSize)
plt.xticks(fontsize=AlvaFontSize * 0.7)
plt.yticks(fontsize=AlvaFontSize * 0.7)
plt.xlim([minT, 2 * 30 * day])
plt.ylim([2**5, 2**14])
plt.yscale('log', basey=2)
# gca()---GetCurrentAxis and Format the ticklabel to be 2**x
plt.gca().yaxis.set_major_formatter(
    FuncFormatter(lambda x, pos: int(2**(np.log(x) / np.log(2)))))
plt.gca().xaxis.set_major_locator(plt.MultipleLocator(7))
plt.legend(loc=(1, 0), fontsize=AlvaFontSize)
plt.savefig(save_figure, dpi=100, bbox_inches='tight')
plt.show()

# In[ ]:
Example #5
0
fig = plt.figure(figsize=(10, 8))
fig.subplots_adjust(left=0.05,
                    right=0.95,
                    wspace=0.05,
                    bottom=0.1,
                    top=0.95,
                    hspace=0.05)

titles = ['PCA components', 'ICA components', 'NMF components']

for i, comp in enumerate(decompositions):
    for j in range(n_components):
        ax = fig.add_subplot(n_components, 3, 3 * j + 1 + i)

        ax.yaxis.set_major_formatter(plt.NullFormatter())
        ax.xaxis.set_major_locator(plt.MultipleLocator(1000))
        if j < n_components - 1:
            ax.xaxis.set_major_formatter(plt.NullFormatter())
        else:
            ax.set_xlabel(r'wavelength $(\AA)$')

        ax.plot(wavelengths, comp[j], '-k', lw=1)

        # plot zero line
        xlim = [3000, 7999]
        ax.plot(xlim, [0, 0], '-', c='gray', lw=1)
        ax.set_xlim(xlim)

        if j == 0:
            ax.set_title(titles[i], fontsize='medium')
Example #6
0
def frame_fiberflat(outfil, qaframe, frame, fiberflat):
    """ QA plots for fiber flat

    Args:
        outfil:
        qaframe:
        frame:
        fiberflat:

    Returns:
        Stuff?
    """
    # Setup
    fibermap = frame.fibermap
    gdp = fiberflat.mask == 0
    nfiber = len(frame.fibers)
    xfiber = np.zeros(nfiber)
    yfiber = np.zeros(nfiber)
    for ii, fiber in enumerate(frame.fibers):
        mt = np.where(fiber == fibermap['FIBER'])[0]
        xfiber[ii] = fibermap['X_TARGET'][mt]
        yfiber[ii] = fibermap['Y_TARGET'][mt]

    jet = cm = plt.get_cmap('jet')

    # Tile plot(s)
    fig = plt.figure(figsize=(8, 5.0))
    gs = gridspec.GridSpec(2, 2)

    # Mean Flatfield flux in each fiber
    ax = plt.subplot(gs[0, 0])
    ax.xaxis.set_major_locator(plt.MultipleLocator(100.))

    mean_flux = np.mean(frame.flux * gdp, axis=1)
    rms_mean = np.std(mean_flux)
    med_mean = np.median(mean_flux)
    #from xastropy.xutils import xdebug as xdb
    #pdb.set_trace()
    mplt = ax.scatter(xfiber, yfiber, marker='o', s=9., c=mean_flux, cmap=jet)
    mplt.set_clim(vmin=med_mean - 2 * rms_mean, vmax=med_mean + 2 * rms_mean)
    cb = fig.colorbar(mplt)
    cb.set_label('Mean Flux')

    # Mean
    ax = plt.subplot(gs[0, 1])
    ax.xaxis.set_major_locator(plt.MultipleLocator(100.))
    mean_norm = np.mean(fiberflat.fiberflat * gdp, axis=1)
    m2plt = ax.scatter(xfiber, yfiber, marker='o', s=9., c=mean_norm, cmap=jet)
    m2plt.set_clim(vmin=0.98, vmax=1.02)
    cb = fig.colorbar(m2plt)
    cb.set_label('Mean of Fiberflat')

    # RMS
    ax = plt.subplot(gs[1, 0])
    ax.xaxis.set_major_locator(plt.MultipleLocator(100.))
    rms = np.std(
        gdp *
        (fiberflat.fiberflat - np.outer(mean_norm, np.ones(fiberflat.nwave))),
        axis=1)
    rplt = ax.scatter(xfiber, yfiber, marker='o', s=9., c=rms, cmap=jet)
    #rplt.set_clim(vmin=0.98, vmax=1.02)
    cb = fig.colorbar(rplt)
    cb.set_label('RMS in Fiberflat')

    # Meta text
    ax2 = plt.subplot(gs[1, 1])
    ax2.set_axis_off()
    show_meta(ax2, qaframe, 'FIBERFLAT', outfil)
    """
    xlbl = 0.05
    ylbl = 0.85
    i0 = outfil.rfind('/')
    ax2.text(xlbl, ylbl, outfil[i0+1:], color='black', transform=ax2.transAxes, ha='left')
    yoff=0.10
    for key in sorted(qaframe.data['FIBERFLAT']['METRICS'].keys()):
        if key in ['QA_FIG']:
            continue
        # Show
        ylbl -= yoff
        ax2.text(xlbl+0.05, ylbl, key+': '+str(qaframe.data['FIBERFLAT']['METRICS'][key]),
            transform=ax2.transAxes, ha='left', fontsize='x-small')
    """

    # Finish
    plt.tight_layout(pad=0.1, h_pad=0.0, w_pad=0.0)
    plt.savefig(outfil)
    plt.close()
    print('Wrote QA SkyRes file: {:s}'.format(outfil))
Example #7
0
for (offset, color, error, linewidth) in zip(offsets, colors,
                                             errors, linewidths):
    # compute the PSD
    err = np.random.normal(0, error, size=hj.shape)
    hj_N = hj + err + offset
    fk, PSD = PSD_continuous(tj, hj_N)

    # plot the data and PSD
    ax1.scatter(tj, hj_N, s=4, c=color, lw=0)
    ax1.plot(tj, 0 * tj + offset, '-', c=color, lw=1)
    ax2.plot(fk, PSD, '-', c=color, lw=linewidth)

# vertical line marking the expected peak location
ax2.plot([0.5 / np.pi, 0.5 / np.pi], [-0.1, 1], ':k', lw=1)

ax1.set_xlim(-25, 25)
ax1.set_ylim(-0.1, 0.3001)

ax1.set_xlabel('$t$')
ax1.set_ylabel('$h(t)$')

ax1.yaxis.set_major_locator(plt.MultipleLocator(0.1))

ax2.set_xlim(0, 0.8)
ax2.set_ylim(-0.101, 0.801)

ax2.set_xlabel('$f$')
ax2.set_ylabel('$PSD(f)$')

plt.show()
Example #8
0
def makeChartsByAquifer(options, DBKeyList):
    ThisDate = dt.date.today()
    currentTime = dt.datetime.now().time()
    timeStr = currentTime.strftime("%H:%M:%S")
    timeStamp = ThisDate.strftime("%m/%d/%Y") + " " + timeStr
    print timeStamp
    myFmt = mdates.DateFormatter('%b %Y')

    try:
        print 'Establishing Database connection'
        cnxn = pyodbc.connect('DSN=WREP; UID=pub; PWD=pub')
    except Exception as err:
        print err
    cursor = cnxn.cursor()

    # Define SQL for selecting date ranges by DBKEY
    sqlQ = setSQLstring('DBKeyDates', keys=DBKeyList)

    for frequency in ('DA', 'RI'):
        print 'Selecting date ranges for ' + frequency + ' DBkeys'
        DBKeys2WorkWith = fetchSQL(cursor, sqlQ, frequency)
        recKnt = len(DBKeys2WorkWith)

        # Display Execution progress on screen
        steps = recKnt / 10
        if steps > 1:
            print 'Progress  interval: ' + str(
                steps) + ' hydrographs created for each 10%'
            print 'Total hydrographs: ' + str(recKnt)
            i = 0
            print 'Building Hydrographs'
            print 'Progress for ' + frequency
            print '                                    1'
            print '                  1 2 3 4 5 6 7 8 9 0'
            print '                  0 0 0 0 0 0 0 0 0 0 '
            print '                [',
        else:
            steps = 1
            print 'Total hydrographs being created for ' + frequency + ': ' + str(
                recKnt)
            i = 0
            print 'Building Hydrographs'
            print '                [',
        sys.stdout.flush()

        for row in DBKeys2WorkWith:
            # Define chart title and figure name variables from each row
            station = row.STATION.strip(' ')
            key = row.DBKEY.strip(' ')
            stationDB = station + ' (dbkey[' + key + '])'
            dataType = row.DATA_TYPE.strip(' ')
            dataSource = row.AGENCY.strip(' ')

            i = i + 1
            if i % steps == 0:
                print 'X',
                sys.stdout.flush()

    # Frequency codes for Daily and Random require separate queries as the data is pulled from separate tables in DBHydro.
    #   DMDBASE.DAILY_DATA and DMDBASE.RANDOM_DATA

    # Daily query:
            if frequency == 'DA':
                sql = setSQLstring('DailyData', rowData=row)
                sdateDT = row.SDATE
                oneStartDate = row.START_DATE.strip(' ')

                #   Hydrographs for Daily data need an additional query for each set of qualifier codes.
                #   Currently E for Estimated in one query, all other codes <> M in another.

                sqlwithCodes = setSQLstring('DailyCodes', rowData=row)
                sqlQualifiers = setSQLstring('DailyQualifiers', rowData=row)
                sqlAnnotations = setSQLstring('DailyAnnotations', rowData=row)
                sqlDCVPcodes = setSQLstring('DCVPcodes', rowData=row)
                sqlDCVPcodesV = setSQLstring('DCVPcodeV', rowData=row)
                sqlwithEs = setSQLstring('DailyEcodes', rowData=row)

            else:

                # Random Interval query:
                #   anyDailyDBKey= '01081'    <--- This DBKey was picked to provide daily dates to random data.
                sql = setSQLstring('RandomIntervalData', rowData=row)

    #   Fetch records for either Daily or Random Interval with sql String
            records = fetchSQL(cursor, sql, 'none')

            # If query returned 0 records skip the chart processing.
            if len(records):
                # Process Random Interval and Daily data in a similar fashion
                #   when retrieving data values and defining the x and y axis
                dbhydDF = pd.DataFrame(records, columns=['dateread', 'value'])
                dmax = dbhydDF.dateread.max()
                ymax = float(dbhydDF.value.max())
                ymin = float(dbhydDF.value.min())
                yrange = ymax - ymin
                ymin = ymin - round((yrange * .05), 2)
                ymax = ymax + round((yrange * .05), 2)
                yinterval = round((yrange / 8.0), 1)
                ys = dbhydDF['value']

                # Hydrographs for Daily data need additional query processing for each set of qualifier codes.
                if frequency == 'DA':
                    dmin = sdateDT
                    xs = pd.date_range(dmin, dmax - dt.timedelta(days=1))

                    qualiferRecord = fetchSQL(cursor, sqlQualifiers, 'none')
                    cols = [t[0] for t in cursor.description]
                    for col, value in zip(cols, qualiferRecord):
                        qualiferStr = str(value).strip('( )')[:-1]

                    annotationRecord = fetchSQL(cursor, sqlAnnotations, 'none')
                    cols = [t[0] for t in cursor.description]
                    for col, value in zip(cols, annotationRecord):
                        annotationStr = str(value).strip('( )')[:-1]

    # Create dataframe from Qualifier Code cursor
                    Qcodes = fetchSQL(cursor, sqlwithCodes, 'none')
                    codesDF = pd.DataFrame(
                        Qcodes, columns=['dateread', 'value', 'code'])
                    ycodes = codesDF['value']
                    dmax2 = codesDF.dateread.max()
                    xs2 = pd.date_range(dmin, dmax2 - dt.timedelta(days=1))

                    # Create dataframe from Estimated Code cursor
                    Ecodes = fetchSQL(cursor, sqlwithEs, 'none')
                    EcodesDF = pd.DataFrame(
                        Ecodes, columns=['dateread', 'value', 'code'])
                    ecodes = EcodesDF['value']
                    dmax3 = EcodesDF.dateread.max()
                    xs3 = pd.date_range(dmin, dmax3 - dt.timedelta(days=1))

                    # Create dataframes from Annoatation and Verification cursors
                    Acodes = fetchSQL(cursor, sqlDCVPcodes, 'none')
                    if len(Acodes) > 0:
                        annoDF = pd.DataFrame(
                            Acodes, columns=['dateread', 'value', 'code'])
                        dmax4 = annoDF.dateread.max()
                        xs4 = pd.date_range(dmin, dmax4 - dt.timedelta(days=1))
                        CannoCodes = annoDF['value']

                    Vcodes = fetchSQL(cursor, sqlDCVPcodesV, 'none')
                    if len(Vcodes) > 0:
                        annoVDF = pd.DataFrame(
                            Vcodes, columns=['dateread', 'value', 'code'])
                        dmax5 = annoVDF.dateread.max()
                        xs5 = pd.date_range(dmin, dmax5 - dt.timedelta(days=1))
                        VannoCodes = annoVDF['value']

                else:
                    #   Random Interval needs to create a mask for null values with isfinite
                    dmin = dbhydDF.dateread.min()
                    xs = pd.date_range(dmin, dmax)
                    ts2 = np.array(ys.values).astype(np.double)
                    ts2mask = np.isfinite(ts2)

    # Define general chart characteristics, titles and descriptive text:
                fig, chart = plt.subplots()
                fontP = FontProperties()
                fontP.set_size('xx-small')
                chart.set_autoscale_on(True)
                minorlocator = plt.MultipleLocator(10)

                chart.text(0.99, 0.01, 'Plots created: '+ timeStamp + \
                        '\nReviewed by the Hydrogeology Unit of the Water Supply Bureau',
                        verticalalignment='bottom', horizontalalignment='right',
                        transform=chart.transAxes,
                        color='green', fontsize=7)

                if frequency == 'DA':
                    title= station +'\n' + dataSource +' ' + Aquifer2Process +' Daily Water Level\n' + \
                           sdateDT.strftime("%m/%d/%Y") + ' thru ' + ThisDate.strftime("%m/%d/%Y")
                else:
                    title= station +'\n' + dataSource +' '+ Aquifer2Process + ' Random Interval Water Level\n' + \
                           sdateDT.strftime("%m/%d/%Y") + ' thru ' + ThisDate.strftime("%m/%d/%Y")

                plt.title(str(title), fontsize=8)

                # Create X axis with full date range
                chart.set_xlim(sdateDT, ThisDate)

                # Plot the Data:
                markers = []
                for m in Line2D.markers:
                    try:
                        if len(m) == 1 and m != ' ':
                            markers.append(m)
                    except TypeError:
                        pass
                style6 = markers[6]
                style9 = markers[9]

                if frequency == 'DA':
                    chart.plot_date(xs,
                                    ys,
                                    ydate=False,
                                    linestyle='-',
                                    marker='.',
                                    markersize=1,
                                    linewidth=1,
                                    color='blue',
                                    label=stationDB)
                    Qstr = 'Qualifier List:[' + qualiferStr + ']'
                    chart.plot_date(xs3,
                                    ecodes,
                                    ydate=False,
                                    linestyle='-',
                                    marker='.',
                                    markersize=1,
                                    linewidth=1,
                                    color='green',
                                    label='Estimated')
                    chart.plot_date(xs2,
                                    ycodes,
                                    ydate=False,
                                    linestyle='b-',
                                    marker='o',
                                    markersize=4,
                                    linewidth=1,
                                    color='red',
                                    label=Qstr)
                    if annotationStr <> 'None':
                        if 'C' in annotationStr:
                            chart.plot_date(xs4,
                                            CannoCodes,
                                            ydate=False,
                                            linestyle='None',
                                            marker=style9,
                                            markersize=6,
                                            linewidth=1,
                                            color='magenta',
                                            label='Calibration')
                        if 'V' in annotationStr:
                            chart.plot_date(xs5,
                                            VannoCodes,
                                            ydate=False,
                                            linestyle='None',
                                            marker=style6,
                                            markersize=4,
                                            linewidth=1,
                                            color='yellow',
                                            label='Verification')
                else:
                    chart.plot(xs[ts2mask],
                               ys[ts2mask],
                               linestyle='-',
                               marker='o',
                               markersize=2,
                               linewidth=1,
                               color='red',
                               label=stationDB)

    # Set xaxis specifics:
                if len(xs) < 1095:
                    majorlocator = plt.MultipleLocator(30.4)
                else:
                    majorlocator = plt.MultipleLocator(365)

                chart.xaxis.set_major_formatter(myFmt)
                chart.xaxis.set_major_locator(majorlocator)

                for label in chart.xaxis.get_ticklabels():
                    label.set_rotation(300)
                    label.set_fontsize(7)

    # Set yaxis specifics:
                major_yloc = plt.MultipleLocator(yinterval)
                chart.yaxis.set_major_locator(major_yloc)

                if dataType == 'UNHD':
                    chart.set_ylabel('Uncorrected Head (ft)')
                else:
                    chart.set_ylabel('Head (ft)')

                for ylabel in chart.yaxis.get_ticklabels():
                    ylabel.set_fontsize(7)

                chart.legend(loc=3,
                             bbox_to_anchor=(.01, .01),
                             prop=fontP,
                             fancybox=True,
                             shadow=False)

                # Create figure filenames and output chart .png's to the hydrograph directory.
                stationKey = station + '_' + key
                fname = str(stationKey)
                fig = str('{0}.png'.format(fname))
                figout = fig.strip()
                out = basepath + Aquifer2Process + "\\" + str(figout)
                plt.savefig(out)
                plt.close()

        print ' ]  Done!'
Example #9
0
    def plot(self, rec:str, data:Optional[np.ndarray]=None, ticks_granularity:int=0, leads:Optional[Union[str, List[str]]]=None, same_range:bool=False, waves:Optional[ECGWaveForm]=None, **kwargs) -> NoReturn:
        """ finished, checked, to improve,

        plot the signals of a record or external signals (units in μV),
        with metadata (fs, labels, tranche, etc.),
        possibly also along with wave delineations

        Parameters
        ----------
        rec: str,
            name of the record
        data: ndarray, optional,
            12-lead ecg signal to plot,
            if given, data of `rec` will not be used,
            this is useful when plotting filtered data
        ticks_granularity: int, default 0,
            the granularity to plot axis ticks, the higher the more,
            0 (no ticks) --> 1 (major ticks) --> 2 (major + minor ticks)
        leads: str or list of str, optional,
            the leads to plot
        same_range: bool, default False,
            if True, forces all leads to have the same y range
        waves: ECGWaveForm, optional,
            the waves (p waves, t waves, qrs complexes)
        kwargs: dict,

        TODO:
        -----
        1. slice too long records, and plot separately for each segment
        2. plot waves using `axvspan`

        NOTE:
        -----
        `Locator` of `plt` has default `MAXTICKS` equal to 1000,
        if not modifying this number, at most 40 seconds of signal could be plotted once

        Contributors: Jeethan, and WEN Hao
        """
        if "plt" not in dir():
            import matplotlib.pyplot as plt
            plt.MultipleLocator.MAXTICKS = 3000
        _leads = self._normalize_leads(leads, standard_ordering=True, lower_cases=False)

        # lead_list = self.load_ann(rec)["df_leads"]["lead_name"].tolist()
        # _lead_indices = [lead_list.index(l) for l in leads]
        _lead_indices = [self.all_leads.index(l) for l in _leads]
        if data is None:
            _data = self.load_data(rec, data_format="channel_first", units="μV")[_lead_indices]
        else:
            units = self._auto_infer_units(data)
            print(f"input data is auto detected to have units in {units}")
            if units.lower() == "mv":
                _data = 1000 * data
            else:
                _data = data
        
        if same_range:
            y_ranges = np.ones((_data.shape[0],)) * np.max(np.abs(_data)) + 100
        else:
            y_ranges = np.max(np.abs(_data), axis=1) + 100

        if data is None and waves is None:
            waves = self.load_ann(rec, leads=_leads)["waves"]

        if waves is not None:
            pwaves = {l:[] for l in _leads}
            qrs = {l:[] for l in _leads}
            twaves = {l:[] for l in _leads}
            for l, l_w in waves.items():
                for w in l_w:
                    itv = [w.onset, w.offset]
                    if w.name == self._symbol_to_wavename["p"]:
                        pwaves[l].append(itv)
                    elif w.name == self._symbol_to_wavename["N"]:
                        qrs[l].append(itv)
                    elif w.name == self._symbol_to_wavename["t"]:
                        twaves[l].append(itv)
        
        palette = {"pwaves": "green", "qrs": "red", "twaves": "yellow",}
        plot_alpha = 0.4

        diagnoses = self.load_diagnoses(rec)

        nb_leads = len(_leads)

        seg_len = self.fs * 25  # 25 seconds
        nb_segs = _data.shape[1] // seg_len

        t = np.arange(_data.shape[1]) / self.fs
        duration = len(t) / self.fs
        fig_sz_w = int(round(4.8 * duration))
        fig_sz_h = 6 * y_ranges / 1500
        fig, axes = plt.subplots(nb_leads, 1, sharex=True, figsize=(fig_sz_w, np.sum(fig_sz_h)))
        for idx in range(nb_leads):
            lead_name = self.all_leads[_lead_indices[idx]]
            axes[idx].plot(t, _data[idx], label=f"lead - {lead_name}")
            axes[idx].axhline(y=0, linestyle="-", linewidth="1.0", color="red")
            # NOTE that `Locator` has default `MAXTICKS` equal to 1000
            if ticks_granularity >= 1:
                axes[idx].xaxis.set_major_locator(plt.MultipleLocator(0.2))
                axes[idx].yaxis.set_major_locator(plt.MultipleLocator(500))
                axes[idx].grid(which="major", linestyle="-", linewidth="0.5", color="red")
            if ticks_granularity >= 2:
                axes[idx].xaxis.set_minor_locator(plt.MultipleLocator(0.04))
                axes[idx].yaxis.set_minor_locator(plt.MultipleLocator(100))
                axes[idx].grid(which="minor", linestyle=":", linewidth="0.5", color="black")
            # add extra info. to legend
            # https://stackoverflow.com/questions/16826711/is-it-possible-to-add-a-string-as-a-legend-item-in-matplotlib
            for d in diagnoses:
                axes[idx].plot([], [], " ", label=d)
            for w in ["pwaves", "qrs", "twaves"]:
                for itv in eval(f"{w}["{lead_name}"]"):
                    axes[idx].axvspan(
                        itv[0]/self.fs, itv[1]/self.fs,
                        color=palette[w], alpha=plot_alpha,
                    )
            axes[idx].legend(loc="upper left")
            axes[idx].set_xlim(t[0], t[-1])
            axes[idx].set_ylim(-y_ranges[idx], y_ranges[idx])
            axes[idx].set_xlabel("Time [s]")
            axes[idx].set_ylabel("Voltage [μV]")
Example #10
0
    def plot(self,
             min_freq,
             output='VEL',
             show=True,
             label='',
             axes=None,
             sampling_rate=None,
             sym='b-'):
        """
        Plot PAZ object's Response
        
        :type min_freq: float
        :param min_freq: Lowest frequency to plot.
        :type output: str
        :param output: Output units. One of:

                ``"DISP"``
                    displacement
                ``"VEL"``
                    velocity
                ``"ACC"``
                    acceleration
        :type axes: list of 2 :class:`matplotlib.axes.Axes`
        :param axes: List/tuple of two axes instances to plot the
            amplitude/phase spectrum into. If not specified, a new figure is
            opened.
        :type label: str
        :param label: Label string for legend.
        :type sampling_rate: float
        :param sampling_rate: Manually specify sampling rate of time series.
            If not given it is attempted to determine it from the information
            in the individual response stages.  Does not influence the spectra
            calculation, if it is not known, just provide the highest frequency
            that should be plotted times two.
        :type show: bool
        :param show: Whether to show the figure after plotting or not. Can be
            used to do further customization of the plot before showing it.
        :type sym: str
        :param sym: symbol to use on plot
        """
        paz = self.copy()
        if output == 'DISP':
            paz.input_units = 'm'
        elif output == 'VEL':
            paz.input_units = 'm/s'
        elif output == 'ACC':
            paz.input_units = 'm/s^2'
        else:
            print(f'Unknown output type: {output}, using VEL')
            paz.input_units = 'm/s'
        if sampling_rate is None:
            sampling_rate = paz.sampling_rate
        if sampling_rate is None:
            if min_freq < 1:
                sampling_rate = 100.
            else:
                sampling_rate = min_freq * 100.
        else:
            assert sampling_rate > 2 * min_freq
        if axes is not None:
            axs = axes
            fig = axs[0].figure
            npts = 50
        else:
            fig, axs = plt.subplots(2, 1, sharex=True)
            npts = 100
        f = np.power(
            10.,
            np.linspace(np.log10(min_freq), np.log10(sampling_rate / 2), npts))
        resp = paz.get_response(f)
        axs[0].loglog(f, np.absolute(resp), sym, label=label)
        axs[0].set_ylabel(
            f'Amplitude ({self.output_units.lower()}/{self.input_units.lower()})'
        )
        axs[0].grid(True)
        axs[0].legend()
        axs[1].semilogx(f, np.angle(resp), sym)
        axs[1].grid(True)
        axs[1].set_ylabel('Phase')
        axs[1].yaxis.set_major_locator(plt.MultipleLocator(np.pi / 2))
        axs[1].yaxis.set_major_formatter(
            plt.FuncFormatter(multiple_formatter()))
        axs[1].set_xlabel('Frequency (Hz)')
        if show is True:
            plt.show()
#----------------------------------------------------------------------
# plot results
fig = plt.figure(figsize=(5, 2.5))
fig.subplots_adjust(left=0.1, right=0.95, wspace=0.25, bottom=0.17, top=0.95)

ax = fig.add_subplot(121)
ax.plot(mGrid, sigGrid, '-k', label='scatter')
ax.plot(mGrid, medGrid, '--k', label='bias')
ax.plot(mGrid, 0 * mGrid, ':k', lw=1)
ax.legend(loc=2)

ax.set_xlabel(r'$m_{\rm obs}$')
ax.set_ylabel('bias/scatter (mag)')

ax.set_ylim(-0.04, 0.21)
ax.xaxis.set_major_locator(plt.MultipleLocator(1.0))
ax.yaxis.set_major_locator(plt.MultipleLocator(0.1))
ax.yaxis.set_minor_locator(plt.MultipleLocator(0.01))

for l in ax.yaxis.get_minorticklines():
    l.set_markersize(3)

ax = fig.add_subplot(122)
ax.plot(pErrGrid, med2, '-k', label='$p=2$')
ax.plot(pErrGrid, med4, '--k', label='$p=4$')
ax.legend(loc=2)
ax.set_xlabel(r'$\sigma_\pi / \pi$')
ax.set_ylabel('absolute magnitude bias')
ax.xaxis.set_major_locator(plt.MultipleLocator(0.1))
ax.set_xlim(0.02, 0.301)
ax.set_ylim(0, 0.701)
Example #12
0
def plotlines3(left_arrays, right_arrays, oops_arrays, durata, partic, integ,
               scale):

    # dB_sum_mono = mono_arrays[0]
    dB_sum_left = left_arrays[0]
    dB_sum_righ = right_arrays[0]
    dB_sum_oops = oops_arrays[0]

    sample_frames = left_arrays[1]

    mk_col = 'k'
    mk_col2 = 'g'
    mk_coll = 'b'
    mk_colr = 'r'

    plt.figure(figsize=(15, 10))

    fig, ax = plt.subplots(figsize=(15, 5))

    # plt.annotate(str(round(np.percentile(dB_sum_mono, 90),1)), (0,np.percentile(dB_sum_mono, 90)))
    # plt.annotate(str(round(np.percentile(dB_sum_mono, 50),1)), (0,np.percentile(dB_sum_mono, 50)))
    # plt.annotate(str(round(np.percentile(dB_sum_mono, 10),1)), (0,np.percentile(dB_sum_mono, 10)))

    # plt.axhline(y=np.percentile(dB_sum_mono, 90),  linewidth= .5, linestyle= ':',color=mk_col, label='LA10' )
    # plt.axhline(y=np.percentile(dB_sum_mono, 10), linewidth= .5,linestyle= '--',color=mk_col, label='LA90')
    # plt.axhline(y=np.percentile(dB_sum_mono, 50), linewidth= .5,linestyle= '-',color=mk_col, label='LA50')

    # la10m=str(round(np.percentile(dB_sum_mono, 90),1))
    # la50m=str(round(np.percentile(dB_sum_mono, 50),1))
    # la90m=str(round(np.percentile(dB_sum_mono, 10),1))

    la10l = str(round(np.percentile(dB_sum_left, 90), 1))
    la50l = str(round(np.percentile(dB_sum_left, 50), 1))
    la90l = str(round(np.percentile(dB_sum_left, 10), 1))

    la10r = str(round(np.percentile(dB_sum_righ, 90), 1))
    la50r = str(round(np.percentile(dB_sum_righ, 50), 1))
    la90r = str(round(np.percentile(dB_sum_righ, 10), 1))

    la10o = str(round(np.percentile(dB_sum_oops, 90), 1))
    la50o = str(round(np.percentile(dB_sum_oops, 50), 1))
    la90o = str(round(np.percentile(dB_sum_oops, 10), 1))

    plt.axhline(y=np.percentile(dB_sum_left, 90),
                linewidth=.7,
                linestyle=':',
                color=mk_coll,
                label='LA10L = ' + la10l)
    plt.axhline(y=np.percentile(dB_sum_left, 50),
                linewidth=.7,
                linestyle='-',
                color=mk_coll,
                label='LA50L = ' + la50l)
    plt.axhline(y=np.percentile(dB_sum_left, 10),
                linewidth=.7,
                linestyle='--',
                color=mk_coll,
                label='LA90L = ' + la90l)

    plt.axhline(y=np.percentile(dB_sum_righ, 90),
                linewidth=.7,
                linestyle=':',
                color=mk_colr,
                label='LA10R = ' + la10r)
    plt.axhline(y=np.percentile(dB_sum_righ, 50),
                linewidth=.7,
                linestyle='-',
                color=mk_colr,
                label='LA50R = ' + la50r)
    plt.axhline(y=np.percentile(dB_sum_righ, 10),
                linewidth=.7,
                linestyle='--',
                color=mk_colr,
                label='LA90R = ' + la90r)

    print('la10l = ', la10l)
    print('la50l = ', la50l)
    print('la90l = ', la90l)
    print('seconds = ', durata / fs)

    print('la10r = ', la10r)
    print('la50r = ', la50r)
    print('la90r = ', la90r)
    print('seconds = ', durata / fs)

    # rowm = [partic, integ, round(durata/fs,3), la10m, la50m, la90m, durata]
    rowl = [
        partic[:-6] + 'left_A', integ,
        round(durata / fs, 3), la10l, la50l, la90l, durata
    ]
    rowr = [
        partic[:-6] + 'righ_A', integ,
        round(durata / fs, 3), la10r, la50r, la90r, durata
    ]
    rowo = [
        partic[:-6] + 'oops_A', integ,
        round(durata / fs, 3), la10o, la50o, la90o, durata
    ]

    with open('results.csv', 'a') as csvFile:
        writer = csv.writer(csvFile)
        # writer.writerow(rowm)
        writer.writerow(rowl)
        writer.writerow(rowr)
        writer.writerow(rowo)

    csvFile.close()

    plt.xticks(sample_frames, fontsize=14)

    plt.plot(sample_frames,
             dB_sum_oops,
             color='k',
             linestyle='-.',
             linewidth=.5,
             label=' LA oops')
    # plt.plot(sample_frames, dB_sum_mono, color=mk_col, linestyle= '-.', linewidth= .5, label=' LA mono')
    plt.plot(sample_frames,
             dB_sum_left,
             color='b',
             linewidth=.9,
             label=' LA left')
    plt.plot(sample_frames,
             dB_sum_righ,
             color='r',
             linewidth=.9,
             label=' LA right')

    plt.legend(loc="lower left", bbox_to_anchor=[0, 0], ncol=3)

    if scale == 's':

        #     seconds
        ax.xaxis.set_major_locator(plt.MultipleLocator(44100 * 30))

        def format_func(value, tick_number):

            return int(value / 44100)

        ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))
        plt.xlabel('Seconds')
#     min
    elif scale == 'm':
        ax.xaxis.set_major_locator(plt.MultipleLocator(44100 * 60))

        def format_func(value, tick_number):

            return int(value / (44100 * 60))

        ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))
        plt.xlabel('Minutes')

    # ax.xaxis.set_major_locator(plt.MultipleLocator(44100 *5))
    #
    # def format_func(value, tick_number):
    #
    #     return int(value / 44100)

    # ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))

    plt.title(partic + '_tri_' + '_LA ' + integ)
    # plt.xlabel('Seconds')
    plt.ylabel('LA_' + integ)
    plt.grid(True, which='major', axis='both')
    plt.xlim(0, durata)
    plt.ylim((35, 80))

    fig.tight_layout()
    plt.savefig(partic + "_tri_" + integ + '_' + str(durata) + '.eps', dpi=150)
Example #13
0
# 0.2 Formatter for inserting commas in y axis labels with magnitudes in the thousands


def func(x, pos):  # formatter function takes tick label and tick position
    s = '{:0,d}'.format(int(x))
    return s


y_format = plt.FuncFormatter(func)  # make formatter

# 0.3 format the x axis ticksticks
years2, years4, years5, years10, years15 = dts.YearLocator(2), dts.YearLocator(
    4), dts.YearLocator(5), dts.YearLocator(10), dts.YearLocator(15)

# 0.4 y label locator for vertical axes plotting gdp
majorLocator_y = plt.MultipleLocator(3)
majorLocator_shares = plt.MultipleLocator(0.2)

# In[3]:

# 1. Import data
pwt = pd.read_excel('../xslx/pwt90.xlsx', sheet_name='Data')

# In[4]:

# 2. lists of countries, codes, and years
year0 = 1960

country_codes = []
countries = []
years = []
Example #14
0
X = fetch_LIGO_bigdog()
t = X['t']
h = X['Hanford']

dt = t[1] - t[0]

Q = np.sqrt(22)
f0 = 1

f0 = 2**np.linspace(5, 8, 50)

f, H = FT_continuous(t, h)
W = np.conj(wavelet_FT(f, 0, f0[:, None], Q))

t, HW = IFT_continuous(f, H * W)

t = t[::100]
HW = HW[:, ::100]

fig = plt.figure(figsize=(5, 3.75))
plt.imshow(abs(HW),
           origin='lower',
           aspect='auto',
           extent=[t[0], t[-1], np.log2(f0[0]),
                   np.log2(f0[-1])])
plt.colorbar()
plt.gca().yaxis.set_major_locator(plt.MultipleLocator(1))
plt.gca().yaxis.set_major_formatter(
    plt.FuncFormatter(lambda x, *args: "%i" % (2**x)))
plt.show()
Example #15
0
for key in sorted(reds.keys()):
    blue_keys.append(key)
    blue_values.append(blues[key].seconds / day_to_seconds[dt.datetime.strptime(key, "%Y-%m-%d").weekday()])

yellow_keys = []
yellow_values = []
for key in sorted(reds.keys()):
    yellow_keys.append(key)
    yellow_values.append(yellows[key].seconds / day_to_seconds[dt.datetime.strptime(key, "%Y-%m-%d").weekday()])

green_keys = []
green_values = []
for key in sorted(reds.keys()):
    green_keys.append(key)
    green_values.append(greens[key].seconds / day_to_seconds[dt.datetime.strptime(key, "%Y-%m-%d").weekday()])


data = range(0, max(len(red_values), len(blue_values), len(yellow_values), len(green_values)))
plt.plot(data, red_values, 'r')
plt.plot(data, yellow_values, 'y')
plt.plot(data, blue_values, 'b')
plt.plot(data, green_values, 'g')
ax = plt.axes()
ax.grid()
ax.xaxis.set_major_locator(plt.MultipleLocator(base=7.0))
ax.xaxis.set_minor_locator(plt.MultipleLocator(base=1.0))
ax.set_xticklabels(["Day: ", weekday_string[starting_date.weekday()]])

plt.show()

Example #16
0
plt.rc('xtick', labelsize=24)  # fontsize of the tick labels
plt.rc('ytick', labelsize=24)

N = len(x)
print N
ind = np.arange(N)  # the x locations for the groups
width = 0.15  # the width of the bars

ax = fig.add_subplot(111)
font = {'size': '24'}

rects1 = ax.bar(ind - width, y1, width, color='g', label="B")
rects2 = ax.bar(ind, y2, width, color='r', label="L_Init")
rects3 = ax.bar(ind + width, y3, width, color='b', label="L_Z-ordering")
rects4 = ax.bar(ind + 2 * width, y4, width, color='y', label="L_Building")
rects5 = ax.bar(ind + 3 * width, y5, width, color='pink', label="L_Traversing")
ax.set_ylabel('Time for 1GB data (s)', font)
ax.set_ylim([0, 4])
y_major_locator = plt.MultipleLocator(1)
ax.yaxis.set_major_locator(y_major_locator)

#ax.set_title("RMSE comparison",axis_font)
ax.set_xticks(ind + width)
ax.set_xticklabels(namelist)
ax.set_xlim([-0.3, 7.8])
ax.set_xlabel('Number of AMR levels', font)
ax.legend(loc=0, ncol=5, prop=font)

plt.savefig(name_hat + 'Time_level.pdf', format='pdf', bbox_inches="tight")
plt.show()
Example #17
0
    fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(20, 24))

    ax1.plot(win['time'][int(windowStart):int(windowEnd)],
             btot_tw[:-1, iEv],
             label='B$_{tot}$')
    ax1.plot(win['time'][int(windowStart):int(windowEnd)],
             bz_tw[:-1, iEv],
             label='B$_{z}$')
    ax1.axvline(x=win['time'][int(mostart)], color='r', linestyle='--')
    ax1.axvline(x=win['time'][int(moend)], color='r', linestyle='--')
    # # ax1.set_xlabel('Start Time (' + start_month + '/' + start_day + '/' + start_year + ' ' + start_hr + ':' + start_min + ')')
    ax1.set_ylabel('B [nT]')
    ax1.set_xlim([win['time'][int(windowStart)], win['time'][int(windowEnd)]])

    ax1.xaxis.set_major_locator(
        plt.MultipleLocator(0.1))  # want to have ticks every 1/10th day
    # define formatter function for tick-labels
    fmtr = plt.FuncFormatter(hourMin)
    _ = ax1.xaxis.set_major_formatter(fmtr)

    ax1.legend(numpoints=1, ncol=2, loc='best')

    ax2.plot(win['time'][int(windowStart):int(windowEnd)],
             vtot_tw[:-1, iEv],
             label='v$_{tot}$')
    ax2.axvline(x=win['time'][int(mostart)], color='r', linestyle='--')
    ax2.axvline(x=win['time'][int(moend)], color='r', linestyle='--')
    # # ax2.set_xlabel('Start Time (' + start_month + '/' + start_day + '/' + start_year + ' ' + start_hr + ':' + start_min + ')')
    ax2.set_ylabel('v [km/s]')
    ax2.set_xlim([win['time'][int(windowStart)], win['time'][int(windowEnd)]])
Example #18
0
def chartTimeSeriesDistributions(tables,
                                 output_dir,
                                 output_base_name,
                                 n_bins=40,
                                 min_pctl=0.05,
                                 max_pctl=99.95,
                                 background_color='#D6D1CA',
                                 font_color='#1B1716',
                                 overwrite=False,
                                 howManyHarmonics=3,
                                 showChart=False,
                                 annotate_harmonic_peaks=True):
    # print(tables)
    check_dir(output_dir)

    bands = []
    for table in tables:
        band = os.path.splitext(os.path.basename(table))[0].split('_')[-1]
        if band not in bands: bands.append(band)
    for band in bands:
        output_chart_name = os.path.join(
            output_dir, output_base_name + '_' + band + '.png')
        if not os.path.exists(output_chart_name) or overwrite:
            title = ' '.join(output_base_name.split(
                '_')) + ' ' + band + ' Distrubution Time Series'
            tablesT = [
                table for table in tables
                if os.path.basename(table).find(band) > -1
            ]
            #Find the name of the band/index
            index_name = band

            print('Creating time series distribution chart for:', band)
            values = []
            data = pd.concat([pd.read_csv(t) for t in tablesT], axis=1)
            columns = data.columns
            values = data.to_numpy()

            #Get min and max
            flat = values.flatten()
            flat = flat[~(np.isnan(flat))]
            min = np.percentile(flat, min_pctl)  #flat.min()
            max = np.percentile(flat, max_pctl)  #flat.max()
            min_2 = np.percentile(flat, 10)
            max_2 = np.percentile(flat, 90)
            values = values.clip(min, max)
            #Get dates (yyyy-mm-dd)
            dates = [i.split('_')[0] for i in columns]
            years = np.unique([i.split('-')[0] for i in dates])
            print('years', years)
            # dates = ['2000-{}-{}'.format(i.split('-')[1],i.split('-')[2]) for i in dates]

            #Set up bins for histogram
            bin_step = (max - min) / n_bins
            bins = np.arange(min, max + bin_step, bin_step)

            #Get histograms for each date and clip out outlier frequency values
            hist = np.array([
                np.histogram(data[column], bins=bins, density=True)[0]
                for column in columns
            ]).transpose()
            hist = np.nan_to_num(hist, nan=0)
            hist = hist.clip(np.percentile(hist, 10), np.percentile(hist, 99))

            #Harmonic regression fitting
            #Get table of all xs and ys
            table_xs = np.array([])
            table_ys = np.array([])
            table_all_xs = np.array([])
            d0 = date(1969, 12, 31)
            percentiles = []
            #Set up tables for harmonic regression
            for i, column in enumerate(columns):
                d = dates[i]
                d1 = date(int(d.split('-')[0]), int(d.split('-')[1]),
                          int(d.split('-')[2]))
                delta = d1 - d0
                delta_fraction = math.modf(delta.days / 365.25)[0]
                decimal_date = int(d.split('-')[0]) + delta_fraction

                ys = values[:, i]
                ys = ys[~(np.isnan(ys))]
                if (len(ys) > 3):
                    percentiles.append(
                        np.percentile(ys, [0, 5, 25, 50, 75, 95, 100]))
                else:
                    percentiles.append([
                        np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan
                    ])
                xs = np.repeat(decimal_date, len(ys))
                table_ys = np.append(table_ys, ys)
                table_xs = np.append(table_xs, xs)
                table_all_xs = np.append(table_all_xs, decimal_date)
            percentiles = np.array(percentiles)

            table_all_xs = np.array(table_all_xs).flatten()
            table_xs = np.array(table_xs).flatten()
            table_ys = np.array(table_ys).flatten()

            #Harmonic regression fit model
            peak_year_i = 2
            if peak_year_i > len(years) - 1: peak_year_i = 0
            xs = np.array([table_xs]).T
            sin1Term = np.sin(xs * 2 * math.pi)
            cos1Term = np.cos(xs * 2 * math.pi)
            sin2Term = np.sin(xs * 4 * math.pi)
            cos2Term = np.cos(xs * 4 * math.pi)
            sin3Term = np.sin(xs * 6 * math.pi)
            cos3Term = np.cos(xs * 6 * math.pi)
            intTerm = np.ones(xs.shape[0])
            harm_1 = np.c_[sin1Term, cos1Term, xs, intTerm]
            harm_1_2 = np.c_[sin1Term, cos1Term, sin2Term, cos2Term, xs,
                             intTerm]
            harm_1_2_3 = np.c_[sin1Term, cos1Term, sin2Term, cos2Term,
                               sin3Term, cos3Term, xs, intTerm]

            harm_1_model = np.linalg.lstsq(harm_1, table_ys, rcond=None)
            harm_1_2_model = np.linalg.lstsq(harm_1_2, table_ys, rcond=None)
            harm_1_2_3_model = np.linalg.lstsq(harm_1_2_3,
                                               table_ys,
                                               rcond=None)

            # print(harm_1_model)
            peak_1_fraction = math.atan(
                harm_1_model[0][0] / harm_1_model[0][1]) / (2 * math.pi)
            peak_2_fraction = peak_1_fraction + 0.5
            if peak_1_fraction < 0:
                peak_1_fraction = 1 + peak_1_fraction
            if peak_2_fraction > 1:
                peak_2_fraction = peak_2_fraction - 1

            peak_1_yr = int(years[peak_year_i]) + peak_1_fraction
            peak_2_yr = int(years[peak_year_i]) + peak_2_fraction
            peak_1_pred = np.dot([
                np.sin(peak_1_yr * 2 * math.pi),
                np.cos(peak_1_yr * 2 * math.pi), peak_1_yr, 1
            ], harm_1_model[0])
            peak_2_pred = np.dot([
                np.sin(peak_2_yr * 2 * math.pi),
                np.cos(peak_2_yr * 2 * math.pi), peak_2_yr, 1
            ], harm_1_model[0])
            peak_1_y = min_2
            peak_2_y = max_2
            if peak_1_pred > peak_2_pred:
                peak_1_y = max_2
                peak_2_y = min_2

            peak_date = int(peak_1_fraction * 365) + 1
            peak_date2 = int(peak_2_fraction * 365) + 1

            print(peak_date, peak_date2, peak_1_pred, peak_2_pred,
                  years[peak_year_i][2:] + f'{peak_date:03}',
                  years[peak_year_i][2:] + f'{peak_date2:03}')
            # print(datetime.datetime.strptime('19'+str(int(peak_date*365)), '%y%j').strftime("%d-%m-%Y"))
            peak_date = datetime.datetime.strptime(
                years[peak_year_i][2:] + f'{peak_date:03}',
                '%y%j').strftime("%m-%d")
            peak_date2 = datetime.datetime.strptime(
                years[peak_year_i][2:] + f'{peak_date2:03}',
                '%y%j').strftime("%m-%d")
            # print(peak_date)
            # print(peak_date2)
            #Apply harm model
            xs = np.array([table_all_xs]).T
            sin1Term = np.sin(xs * 2 * math.pi)
            cos1Term = np.cos(xs * 2 * math.pi)
            sin2Term = np.sin(xs * 4 * math.pi)
            cos2Term = np.cos(xs * 4 * math.pi)
            sin3Term = np.sin(xs * 6 * math.pi)
            cos3Term = np.cos(xs * 6 * math.pi)
            intTerm = np.ones(xs.shape[0])
            harm_1 = np.c_[sin1Term, cos1Term, xs, intTerm]
            harm_1_2 = np.c_[sin1Term, cos1Term, sin2Term, cos2Term, xs,
                             intTerm]
            harm_1_2_3 = np.c_[sin1Term, cos1Term, sin2Term, cos2Term,
                               sin3Term, cos3Term, xs, intTerm]
            # print('beta hat:',beta_hat[0],xs)
            pred_1 = np.dot(harm_1, harm_1_model[0])
            pred_1_2 = np.dot(harm_1_2, harm_1_2_model[0])
            pred_1_2_3 = np.dot(harm_1_2_3, harm_1_2_3_model[0])

            pred_dict = {'1': pred_1, '2': pred_1_2, '3': pred_1_2_3}

            pred = pred_dict[str(howManyHarmonics)]

            #Plot
            xTickFreq = 8  #int(len(columns)/20)
            yTickFreq = (max - min) / 10  #0.1
            width = (len(columns) / 25) + 2
            if width < 8:
                width = 12
                xTickFreq = 3
            # fig = plt.figure(figsize=(width,6),frameon=True,facecolor='w')
            fig, ax = plt.subplots(figsize=(width, 7),
                                   frameon=True,
                                   facecolor='w')
            fig.patch.set_facecolor(background_color)

            params = {
                "ytick.color": font_color,
                "xtick.color": font_color,
                "axes.labelcolor": font_color,
                "axes.edgecolor": font_color,
                'legend.fontsize': 7,
                'legend.handlelength': 1.2
            }
            plt.rcParams.update(params)

            # ax = fig.add_axes([0.06, 0.15, 0.84, 0.80])
            ax.set_title(title)

            # fig.annotate('local max', xy=(0.5, 0.5))
            cmap = plt.get_cmap('viridis')
            cf = plt.pcolormesh(dates, bins, hist, cmap=cmap)  #, vmin = 500)
            degrees = 45
            plt.xticks(rotation=degrees, fontsize=7, ha='right')

            # print(list(zip(dates,pred,table_all_xs)))
            harm_line = plt.plot(
                dates,
                pred,
                linestyle='-',
                color=background_color,
                linewidth=2,
                label='Harmonic Fit ({})'.format(howManyHarmonics))
            # median_line = plt.plot(dates, percentiles[:,3], linestyle = '--', color = font_color, linewidth = 1.5,label = 'Median')

            ax.set_ylim([min, max])
            ax.set_ylabel('{} Value'.format(index_name), fontsize=10)
            ax.set_xlabel('Date', fontsize=10)

            #Set up the x and y axis tick frequencies
            ax.xaxis.set_major_locator(plt.MultipleLocator(xTickFreq))
            ax.xaxis.set_minor_locator(plt.MultipleLocator(1))
            ax.yaxis.set_major_locator(plt.MultipleLocator(yTickFreq))
            ax.yaxis.set_minor_locator(plt.MultipleLocator(1))

            ax.grid(True,
                    which='major',
                    axis='y',
                    linestyle='--',
                    color=font_color)
            ax.grid(True,
                    which='major',
                    axis='x',
                    linestyle='--',
                    color=font_color)

            cbax = fig.add_axes([0.93, 0.11, 0.01, 0.71])
            legend = plt.legend(handles=harm_line,
                                bbox_to_anchor=(-2.5, 1.08),
                                loc='upper left')

            cb = plt.colorbar(cf, cax=cbax, orientation='vertical')
            cb.ax.tick_params(labelsize=10)
            cb.set_label('Percent of Samples (%)',
                         rotation=270,
                         labelpad=15,
                         fontsize=10,
                         color=font_color)

            if peak_1_pred > max:
                peak_1_pred = max
            elif peak_1_pred < min:
                peak_1_pred = min
            if peak_2_pred > max:
                peak_2_pred = max
            elif peak_2_pred < min:
                peak_2_pred = min
            if (annotate_harmonic_peaks):
                print('Annotating peak dates of harmonics')
                try:
                    yr_dates = [
                        i for i in dates
                        if i.split('-')[0] == years[peak_year_i]
                    ]
                    m_dates = [
                        i for i in yr_dates
                        if i.split('-')[1] == peak_date.split('-')[0]
                    ]
                    if len(m_dates) == 0:
                        m_dates = [
                            i for i in yr_dates if int(i.split('-')[1]) ==
                            int(peak_date.split('-')[0]) - 1
                        ]
                        print(m_dates)
                    m_dates = m_dates[0]
                    ax.annotate(
                        '{} ({})'.format(peak_date, round(peak_1_pred, 3)),
                        xy=(m_dates, peak_1_pred),
                        xycoords='data',
                        # xytext=(m_dates, peak_1_y),    # fraction, fraction
                        # textcoords='data',
                        # arrowprops=dict(facecolor='black', shrink=0.05),
                        color=background_color,
                        fontsize='10',
                        path_effects=[
                            pe.withStroke(linewidth=2.5, foreground=font_color)
                        ])
                    yr_dates = [
                        i for i in dates
                        if i.split('-')[0] == years[peak_year_i]
                    ]
                    m_dates = [
                        i for i in yr_dates
                        if i.split('-')[1] == peak_date2.split('-')[0]
                    ][0]
                    ax.annotate(
                        '{} ({})'.format(peak_date2, round(peak_2_pred, 3)),
                        xy=(m_dates, peak_2_pred),
                        xycoords='data',
                        # xytext=(m_dates, peak_2_y),    # fraction, fraction
                        # textcoords='data',
                        # arrowprops=dict(facecolor='black', shrink=0.05),
                        color=background_color,
                        fontsize='10',
                        path_effects=[
                            pe.withStroke(linewidth=2.5, foreground=font_color)
                        ])
                except Exception as e:
                    print(e)
            # ax.annotate('Peak Date 2',
            #       xy=('2019-{}'.format(peak_date2), 0.3), xycoords='data'
            #       )
            # plt.text(0.5, 1.1, 'Test', color=font_color,
            # bbox=dict(facecolor='none', edgecolor=font_color))
            fig.savefig(output_chart_name)
            if showChart:
                plt.show()
            plt.close()
        else:
            print('Already produced:', output_chart_name)
Example #19
0
 def locator(self):
     return plt.MultipleLocator(self.number / self.denominator)
                     ax=ax4,
                     scales=[2],
                     ec='k',
                     fc='gray',
                     alpha=0.2)

    titles = [
        'True Distribution', 'Noisy Distribution',
        'Extreme Deconvolution\n  resampling',
        'Extreme Deconvolution\n  cluster locations'
    ]

    ax = [ax1, ax2, ax3, ax4]

    for i in range(4):
        ax[i].xaxis.set_major_locator(plt.MultipleLocator(4))
        ax[i].yaxis.set_major_locator(plt.MultipleLocator(5))

        ax[i].text(0.05,
                   0.95,
                   titles[i],
                   ha='left',
                   va='top',
                   transform=ax[i].transAxes)

        if i in (0, 1):
            ax[i].xaxis.set_major_formatter(plt.NullFormatter())

        else:
            ax[i].set_xlabel('x')
def SolveProblem(LoadCase, ConstitutiveModel, BCsType, FinalRelativeStretch, RelativeStepSize, Dimensions, NumberElements, Mesh, V, u, du, v, Ic, J, F, Psi, Plot = False, Paraview = False):
    
    if LoadCase == 'Compression':
                
        # Load case
        [u_0, u_1, InitialState, Direction, Normal, NumberSteps, DeltaStretch] = LoadCaseDefinition(LoadCase, FinalRelativeStretch, RelativeStepSize, Dimensions, BCsType)
        
        
    elif LoadCase == 'Tension':
                
        # Load case
        [u_0, u_1, InitialState, Direction, Normal, NumberSteps, DeltaStretch] = LoadCaseDefinition(LoadCase, FinalRelativeStretch, RelativeStepSize, Dimensions, BCsType)

        
    elif LoadCase == 'SimpleShear':
                
        # Load case
        [u_0, u_1, InitialState, Direction, Normal, NumberSteps, DeltaStretch] = LoadCaseDefinition(LoadCase, FinalRelativeStretch*2, RelativeStepSize*2, Dimensions, BCsType)

    # Boundary conditions
    [BoundaryConditions, ds] = BCsDefinition(Dimensions, Mesh, V, u_0, u_1, LoadCase, BCsType)
    
    # Estimation of the displacement field using Neo-Hookean model (necessary for Ogden)
    u = Estimate(Ic, J, u, v, du, BoundaryConditions, InitialState, u_1)
    
    # Reformulate the problem with the correct constitutive model
    Pi = Psi * fe.dx

    # First directional derivative of the potential energy
    Fpi = fe.derivative(Pi,u,v)

    # Jacobian of Fpi
    Jac = fe.derivative(Fpi,u,du)
    
    # Define option for the compiler (optional)
    ffc_options = {"optimize": True, \
                   "eliminate_zeros": True, \
                   "precompute_basis_const": True, \
                   "precompute_ip_const": True, \
                   "quadrature_degree": 2, \
                   "representation" : "uflacs" }

    # Define the problem
    Problem = fe.NonlinearVariationalProblem(Fpi, u, BoundaryConditions, Jac, form_compiler_parameters=ffc_options)

    # Define the solver
    Solver = fe.NonlinearVariationalSolver(Problem)

    # Set solver parameters (optional)
    Prm = Solver.parameters
    Prm['nonlinear_solver'] = 'newton'
    Prm['newton_solver']['linear_solver'] = 'cg'             # Conjugate gradient
    Prm['newton_solver']['preconditioner'] = 'icc'           # Incomplete Choleski
    Prm['newton_solver']['krylov_solver']['nonzero_initial_guess'] = True
    
    # Data frame to store values
    cols = ['Stretches','P']
    df = pd.DataFrame(columns=cols, index=range(int(NumberSteps)+1), dtype='float64')
    
    if Paraview == True:
        # Results File
        Output_Path = os.path.join('OptimizationResults', BCsType, ConstitutiveModel)
        ResultsFile = xdmffile = fe.XDMFFile(os.path.join(Output_Path, str(NumberElements) + 'Elements_' + LoadCase + '.xdmf'))
        ResultsFile.parameters["flush_output"] = True
        ResultsFile.parameters["functions_share_mesh"] = True
    
    if Plot == True:
        plt.rc('figure', figsize=[12,7])
        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)
    
    # Set the stretch state to initial state
    StretchState = InitialState
    
    # Loop to solve for each step
    for Step in range(int(NumberSteps+1)):

        # Update current state
        u_1.s = StretchState

        # Compute solution and save displacement
        Solver.solve()

        # First Piola Kirchoff (nominal) stress
        P = fe.diff(Psi, F)

        # Nominal stress vectors normal to upper surface
        p = fe.dot(P,Normal)

        # Reaction force on the upper surface
        f = fe.assemble(fe.inner(p,Direction)*ds(2))

        # Mean nominal stress on the upper surface
        Pm = f/fe.assemble(1*ds(2))

        # Save values to table
        df.loc[Step].Stretches = StretchState
        df.loc[Step].P = Pm

        # Plot
        if Plot == True:
            ax.cla()
            ax.plot(df.Stretches, df.P,  color = 'r', linestyle = '--', label = 'P', marker = 'o', markersize = 8, fillstyle='none')
            ax.set_xlabel('Stretch ratio (-)')
            ax.set_ylabel('Stresses (kPa)')
            ax.xaxis.set_major_locator(plt.MultipleLocator(0.02))
            ax.legend(loc='upper left', frameon=True, framealpha=1)
            display(fig)
            clear_output(wait=True)
            
        if Paraview == True:
            # Project the displacement onto the vector function space
            u_project = fe.project(u, V, solver_type='cg')
            u_project.rename('Displacement (mm)', '')
            ResultsFile.write(u_project,Step)
            
            # Compute nominal stress vector
            p_project = fe.project(p, V)
            p_project.rename("Nominal stress vector (kPa)","")
            ResultsFile.write(p_project,Step)



        # Update the stretch state
        StretchState += DeltaStretch

    return df
Example #22
0
def quality_map(experiment,
                file_format=None,
                ki=None,
                urange=None,
                ustep=1,
                colored=True,
                fill=False,
                with_title=True,
                signature=True,
                q_full_scale=True,
                start_calc=1,
                cull=None):
    """Plots the log scale quality map for a given kNN[j] of all sample test sequences.

    Plots all or ppi (calc, sim) points between each major tick interval

    Args:
        experiment (str): insights experiment results full file path
        file_format (str): One of the file extensions supported by the active backend.
            Most backends support png, pdf, ps, eps and svg.
            if is None, the plot is displayed and not saved.
        ki (int): zero-based index of the NN in kNN list, if None all kNNs are plotted.
        ustep (int): a different color is chosen in the color palette for every `ustep` number of updates;
            irrelevant for colored=False.
        urange (tuple): range of updates to plot given as (start, end) where both is inclusive;
            if given as (start, ), max update is used for end;
            if None, all updates are plotted.
        colored (bool): If True, each update is plotted in a different color; otherwise all plotted black,
        fill (bool): if True, propagates the quality for the intermediate calc values;
            if False, plots only the given points provided as sparse list.
        with_title (bool): if True, shows the figure title.
        signature (bool): If True, name of the plotting function is also displayed.
        q_full_scale (bool): if True, quality (i.e. y) axis starts from 0.0; otherwise minimum quality is used.
        start_calc (int): The start value for the calculations (i.e. x) axis.
        cull (float): The percentage (.0, 1.] to cull the data points to be plotted

    Returns:
        None

    Note:
        Start: 20190918, End:20191023
    """
    # Create a figure
    plt.figure(num=1)  # , figsize=(10, 8))
    # Read experiment data
    result = common.load_obj(experiment)
    knn_calc_sim_history = result.data.knn_calc_sim_history
    k = len(knn_calc_sim_history[0]) - 1  # k of kNN
    max_update = max([test[0] for test in knn_calc_sim_history])
    if urange is None:
        urange = (1, max_update)
    elif len(urange) == 1 or urange[1] > max_update:
        urange = (urange[0], max_update)
    max_X = 0
    # Fill in plot data
    CALCS = np.array([])
    QUALITY = np.array([])
    UPDATE = np.array([])
    knn_calc_sim_history = sorted(knn_calc_sim_history,
                                  key=lambda point: point[0],
                                  reverse=False)  # sort by updates
    for test_history in knn_calc_sim_history:
        # print(test_history)
        update = test_history[0]
        if urange[0] <= update <= urange[1]:
            # print("update: ", update)
            for nn_ind, nn_i_history in enumerate(test_history[1:]):
                if ki is None or nn_ind == ki:
                    points = pdp.quality(nn_i_history)
                    # print(points)
                    X, Y = helper.to_arr(points, fill=fill)
                    # Eliminate (0, 0.0) entries
                    if X[0] == 0:
                        X = X[1:]
                    if Y[0] == 0:
                        Y = Y[1:]
                    if max(X) > max_X:
                        max_X = max(X)
                    # Eliminate the entries < start_calc
                    X = X[X >= start_calc]
                    Y = Y[-len(X):]
                    CALCS = np.concatenate((CALCS, X))
                    QUALITY = np.concatenate((QUALITY, Y))
                    # UPDATE = np.concatenate((UPDATE, np.full((len(X),), math.ceil(update / ustep), dtype=np.int)))
                    UPDATE = np.concatenate(
                        (UPDATE, np.full((len(X), ), update, dtype=np.int)))
    if cull:
        CALCS, ind_removed = helper.cull_arr(CALCS, pct=cull)
        QUALITY, _ = helper.cull_arr(QUALITY, ind=ind_removed)
        UPDATE, _ = helper.cull_arr(UPDATE, ind=ind_removed)
    if colored:
        # Color palette
        cmap = "autumn"  # "autumn"  "tab20"  # "Blues_r"
        cmap_size = math.ceil(max_update / ustep)
        my_palette = plt.cm.get_cmap(cmap, cmap_size)
        _ = plt.scatter(CALCS,
                        QUALITY,
                        marker=".",
                        s=1,
                        c=UPDATE,
                        cmap=my_palette,
                        vmin=1,
                        vmax=max_update,
                        alpha=1.)
        cbar = plt.colorbar(orientation="vertical")
        cbar.set_label("updates")
    else:
        _ = plt.scatter(CALCS, QUALITY, marker=".", s=1, c="black")
    ax = plt.gca()
    ax.yaxis.set_major_locator(plt.MultipleLocator(.1))
    ax.yaxis.set_minor_locator(plt.MultipleLocator(.05))
    plt.grid(True,
             which="major",
             linestyle="-",
             linewidth=1,
             color="lightgrey")
    plt.grid(True,
             which="minor",
             linestyle=":",
             linewidth=1,
             color="lightgrey")
    xticks_ = plt_common.get_ticks_log_scale(max_X, start=start_calc)
    y_min = 0.0 if q_full_scale else math.floor(
        np.nanmin(QUALITY[start_calc - 1:]) * 10) / 10
    yticks_ = np.arange(y_min, 1.01, .1)
    plt.rcParams['axes.axisbelow'] = True
    plt.xscale('log')
    plt.xticks(xticks_)
    plt.xlim(left=start_calc, right=max(xticks_))
    plt.yticks(yticks_)
    if signature:
        plt_common.sign_plot(plt, quality_map.__name__)
    fn_wo_ext = common.file_name_wo_ext(experiment)
    lbl_ki = str(ki if ki is not None else list([0, k - 1]))  # zero-based
    lbl_update = str(list(urange) if urange[0] != urange[1] else urange[0])
    if with_title:
        title_ = "Quality Map\n"
        title_ = "{}Exp: {}\n".format(title_, fn_wo_ext)
        title_ = "{}ki:{}, update:{}".format(title_, lbl_ki, lbl_update)
        if colored:
            title_ = "{}, color step:{}".format(title_, ustep)
        if cull:
            title_ = "{}, cull:{:.0%}".format(title_, cull)
        plt.title(title_ + "\n\n")
    save_fn = "QUALITY_MAP_{}_ki_{}_u_{}{}{}{}{}{}".format(
        fn_wo_ext, lbl_ki, lbl_update,
        "_s_{}".format(ustep) if colored else "", "_f" if fill else "",
        "_t" if with_title else "", "_c_{:.2f}".format(cull) if cull else "",
        "_z" if not q_full_scale else "")
    # axis labels
    matplotlib.rcParams['text.usetex'] = True  # Allow LaTeX in text
    ax.set_xlabel("$\#$ of similarity calculations ($c$)")
    ax.set_ylabel(r"quality ($\mathcal{Q}_c$)")
    # Tight layout
    plt.tight_layout()
    if file_format:
        save_fpath = os.path.join(common.APP.FOLDER.FIGURE,
                                  "{}.{}".format(save_fn, file_format))
        plt.savefig(save_fpath, dpi=300, bbox_inches="tight")
        print("Quality Map figure saved into '{}'.".format(save_fpath))
    else:
        # Update the title of the plot window
        plt.gcf().canvas.set_window_title(save_fn)
        plt.show()
    plt.close()

    return None
Example #23
0
def obj_fig(simz_tab, objtype, summ_stats, outfile=None):
    """Generate QA plot for a given object type
    """
    from astropy.stats import sigma_clip
    logs = get_logger()
    gdz_tab = slice_simz(simz_tab,objtype=objtype, survey=True,goodz=True, all_zwarn0=True)
    if objtype == 'ELG':
        allgd_tab = slice_simz(simz_tab,objtype=objtype, survey=False,goodz=True, all_zwarn0=True)

    if len(gdz_tab) <= 1:
        logs.info("Not enough objects of type {:s} for QA".format(objtype))
        return

    # Plot
    sty_otype = get_sty_otype()
    fig = plt.figure(figsize=(8, 6.0))
    gs = gridspec.GridSpec(2,2)
    # Title
    fig.suptitle('{:s}: Summary'.format(sty_otype[objtype]['lbl']),
        fontsize='large')

    # Offset
    for kk in range(4):
        yoff = 0.
        ax= plt.subplot(gs[kk])
        if kk == 0:
            yval = calc_dzsig(gdz_tab)
            ylbl = (r'$(z_{\rm red}-z_{\rm true}) / \sigma(z)$')
            ylim = 5.
            # Stats with clipping
            clip_y = sigma_clip(yval, sigma=5.)
            rms = np.std(clip_y)
            redchi2 = np.sum(clip_y**2)/np.sum(~clip_y.mask)
            #
            xtxt = 0.05
            ytxt = 1.0
            for req_tst in ['EFF','CAT_RATE']:
                ytxt -= 0.12
                if summ_stats[objtype]['REQ_INDIV'][req_tst] == 'FAIL':
                    tcolor='red'
                else:
                    tcolor='green'
                ax.text(xtxt, ytxt, '{:s}: {:.3f}'.format(req_tst,
                    summ_stats[objtype][req_tst]), color=tcolor,
                    transform=ax.transAxes, ha='left', fontsize='small')
            # Additional
            ytxt -= 0.12
            ax.text(xtxt, ytxt, '{:s}: {:.3f}'.format('RMS:', rms),
                    color='black', transform=ax.transAxes, ha='left', fontsize='small')
            ytxt -= 0.12
            ax.text(xtxt, ytxt, '{:s}: {:.3f}'.format(r'$\chi^2_\nu$:',
                redchi2), color='black', transform=ax.transAxes,
                ha='left', fontsize='small')
        else:
            yval = calc_dz(gdz_tab)
            if kk == 1:
                ylbl = (r'$(z_{\rm red}-z_{\rm true}) / (1+z)$')
            else:
                ylbl = r'$\delta v_{\rm red-true}$ [km/s]'
            ylim = max(5.*summ_stats[objtype]['RMS_DZ'],1e-5)
            if (np.median(summ_stats[objtype]['MEDIAN_DZ']) >
                summ_stats[objtype]['RMS_DZ']):
                yoff = summ_stats[objtype]['MEDIAN_DZ']

        if kk==1:
            # Stats
            xtxt = 0.05
            ytxt = 1.0
            dx = ((ylim/2.)//0.0001 +1)*0.0001
            ax.xaxis.set_major_locator(plt.MultipleLocator(dx))
            for stat in ['RMS_DZ','MEAN_DZ', 'MEDIAN_DZ']:
                ytxt -= 0.12
                try:
                    pfail = summ_stats[objtype]['REQ_INDIV'][stat]
                except KeyError:
                    tcolor='black'
                else:
                    if pfail == 'FAIL':
                        tcolor='red'
                    else:
                        tcolor='green'
                ax.text(xtxt, ytxt, '{:s}: {:.5f}'.format(stat,
                    summ_stats[objtype][stat]), color=tcolor,
                    transform=ax.transAxes, ha='left', fontsize='small')
        # Histogram
        if kk < 2:
            binsz = ylim/10.
            #i0, i1 = int( np.min(yval) / binsz) - 1, int( np.max(yval) / binsz) + 1
            i0, i1 = int(-ylim/binsz) - 1, int( ylim/ binsz) + 1
            rng = tuple( binsz*np.array([i0,i1]) )
            nbin = i1-i0
            # Histogram
            hist, edges = np.histogram(yval, range=rng, bins=nbin)
            xhist = (edges[1:] + edges[:-1])/2.
            #ax.hist(xhist, color='black', bins=edges, weights=hist)#, histtype='step')
            ax.hist(xhist, color=sty_otype[objtype]['color'], bins=edges, weights=hist)#, histtype='step')
            ax.set_xlabel(ylbl)
            ax.set_xlim(-ylim, ylim)

        else:
            if kk == 2:
                lbl = r'$z_{\rm true}$'
                xval = gdz_tab['TRUEZ']
                xmin,xmax=np.min(xval),np.max(xval)
                dx = np.maximum(1,(xmax-xmin)//0.5)*0.1
                ax.xaxis.set_major_locator(plt.MultipleLocator(dx))
                #xmin,xmax=0.6,1.65
            elif kk == 3:
                if objtype == 'ELG':
                    lbl = r'[OII] Flux ($10^{-16}$)'
                    #xval = gdz_tab['OIIFLUX']*1e16
                    xval = allgd_tab['OIIFLUX']*1e16
                    yval = calc_dz(allgd_tab)
                    # Avoid NAN
                    gdy = np.isfinite(yval)
                    xval = xval[gdy]
                    yval = yval[gdy]
                    xmin,xmax=0.5,20
                    ax.set_xscale("log", nonposx='clip')
                elif objtype == 'QSO':
                    lbl = 'g (Mag)'
                    xval = 22.5 - 2.5 * np.log10(gdz_tab['FLUX_G'])
                    xmin,xmax=np.min(xval),np.max(xval)
                else:
                    lbl = 'r (Mag)'
                    xval = 22.5 - 2.5 * np.log10(gdz_tab['FLUX_R'])
                    xmin,xmax=np.min(xval),np.max(xval)
            # Labels
            ax.set_xlabel(lbl)
            ax.set_ylabel(ylbl)
            ax.set_xlim(xmin,xmax)
            v_ylim = ylim * C_LIGHT  # redshift to km/s
            ax.set_ylim(-v_ylim+yoff, v_ylim+yoff)

            # Points
            ax.plot([xmin,xmax], [0.,0], '--', color='gray')
            #ax.scatter(xval, yval, marker='o', s=1, label=objtype,
            #    color=sty_otype[objtype]['color'])
            cm = plt.get_cmap(sty_otype[objtype]['pcolor'])
            if objtype == 'ELG':
                xbins = 10**np.linspace(np.log10(xmin), np.log10(xmax), 20)
            else:
                xbins = np.linspace(xmin, xmax, 20)
            ybins = np.linspace(-v_ylim+yoff, v_ylim+yoff, 40) # km/s
            #import pdb; pdb.set_trace()
            counts, xedges, yedges = np.histogram2d(xval, yval * C_LIGHT, bins=(xbins, ybins))
            max_c = np.max(counts)
            #if kk == 3:
            ax.pcolormesh(xedges, yedges, counts.transpose(), cmap=cm, vmin=0, vmax=max_c/5.)

            #ax.hist2d(xval, yval, bins=20, cmap=cm)
            #ax.scatter(xval, yval, marker='o', s=1, label=objtype,
            #           color=sty_otype[objtype]['color'])

    # Finish
    plt.tight_layout(pad=0.2,h_pad=0.2,w_pad=0.3)
    plt.subplots_adjust(top=0.92)
    if outfile is not None:
        plt.savefig(outfile, dpi=700)
        plt.close()
        print("Wrote {:s}".format(outfile))
Example #24
0
ax1 = fig.add_subplot(121)
ax1.hist(data,
         bins=np.linspace(0, 10, 11),
         density=True,
         histtype='stepfilled',
         fc='gray',
         alpha=0.5)
ax1.plot(x, px, '-k')
ax1.set_xlim(-1, 11)
ax1.set_ylim(0, 0.18)
ax1.set_xlabel('$x$')
ax1.set_ylabel('$p(x)$')

# right panel: construct and plot the likelihood
ax2 = fig.add_subplot(122)
ax2.xaxis.set_major_locator(plt.MultipleLocator(0.01))

a = np.linspace(-0.01, 0.02, 1000)
Npts = (500, 100, 20)
styles = ('-k', '--b', '-.g')

for n, s in zip(Npts, styles):
    logL = linprob_logL(data[:n], a, xmin, xmax)
    logL = np.exp(logL - logL.max())
    logL /= logL.sum() * (a[1] - a[0])

    ax2.plot(a, logL, s, label=r'$\rm %i\ pts$' % n)
ax2.legend(loc=2, prop=dict(size=8))

ax2.set_xlim(-0.011, 0.02)
Example #25
0
    def visual_state_action(self):
        """This method visualizes the state_action pair lively, with the red arrow
            referring to the maximum Q_value state_action pair."""

        q_dict = self.q_table_dict
        plt.figure(dpi=220, figsize=(7, 7))
        ax = plt.axes()
        ax.set(xlim=[0, 10], ylim=[0, 10])

        ax.xaxis.set_major_locator(plt.MultipleLocator(1.0))  # 设置x主坐标间隔 1
        ax.yaxis.set_major_locator(plt.MultipleLocator(1.0))  # 设置y主坐标间隔 1
        ax.grid(True, linestyle="-", color="0.6", linewidth="1")
        # ax.scatter(8.5, 7.5)

        keys = sorted(q_dict.keys())
        x, y, i = 0.5, 9.5, 1
        for key in keys:
            # print("key: " + str(key))
            while key[0] * 10 + key[1] != i - 1:
                i = i + 1
                x = x + 1
                if x == 10.5:
                    x = 0.5
                    y = y - 1

            if key == self.goal_state:
                ax.scatter(x, y)
                i = i + 1
                x = x + 1
                continue

            if np.average(q_dict[key]) == 0:
                i = i + 1
                x = x + 1
                if x == 10.5:
                    x = 0.5
                    y = y - 1
                continue

            if q_dict[key].index(np.max(q_dict[key])) == 0:
                plt.annotate('',
                             xy=(x - 0.5, y),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3",
                                             color='red'))
            else:
                plt.annotate('',
                             xy=(x - 0.5, y),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3"))

            if q_dict[key].index(np.max(q_dict[key])) == 1:
                plt.annotate('',
                             xy=(x, y + 0.5),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3",
                                             color='red'))
            else:
                plt.annotate('',
                             xy=(x, y + 0.5),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3"))

            if q_dict[key].index(np.max(q_dict[key])) == 2:
                plt.annotate('',
                             xy=(x + 0.5, y),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3",
                                             color='red'))
            else:
                plt.annotate('',
                             xy=(x + 0.5, y),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3"))

            if q_dict[key].index(np.max(q_dict[key])) == 3:
                plt.annotate('',
                             xy=(x, y - 0.5),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3",
                                             color='red'))
            else:
                plt.annotate('',
                             xy=(x, y - 0.5),
                             xytext=(x, y),
                             arrowprops=dict(arrowstyle="->",
                                             connectionstyle="arc3"))

            x = x + 1
            if x == 10.5:
                x = 0.5
                y = y - 1
            i = i + 1

        # 设置刻度标记的大小
        plt.tick_params(axis='both', labelsize=10)

        plt.show()
                ts2= np.array(ys.values).astype(np.double)
                ts2mask = np.isfinite(ts2)
#                print len(ts2mask), len(ts2), len(xs), len(ys)

# Define general chart characteristics, titles and descriptive text:
                
#            fig = plt.figure()
#            chart = fig.add_subplot(111)
            fig, chart = plt.subplots()

    
            fontP = FontProperties()
            fontP.set_size('xx-small')
                     
            chart.set_autoscale_on(True)
            minorlocator = plt.MultipleLocator(10)
            
            chart.text(0.99, 0.01, 'Plots created: '+ timeStamp + \
                    '\nReviewed by the Hydrogeology Unit of the Water Supply Bureau',
                    verticalalignment='bottom', horizontalalignment='right',
                    transform=chart.transAxes,
                    color='green', fontsize=7)

            if frequency == 'DA':
                title= station +'\n' + dataSource +' ' + options.Aquifer +' Daily Water Level\n' + \
                       sdateDT.strftime("%m/%d/%Y") + ' thru ' + ThisDate.strftime("%m/%d/%Y")
            else:
                title= station +'\n' + dataSource +' '+ options.Aquifer + ' Random Interval Water Level\n' + \
                       sdateDT.strftime("%m/%d/%Y") + ' thru ' + ThisDate.strftime("%m/%d/%Y")
                
            plt.title(str(title),fontsize = 8)
               zorder=1,
               extent=xlim + ylim)
im.set_clim(0, 1.5)
ax.contour(xx, yy, Z, [0.5], colors='k')

ax.set_xlim(xlim)
ax.set_ylim(ylim)

ax.set_xlabel('$u-g$')
ax.set_ylabel('$g-r$')

# Plot completeness vs Ncolors
ax = plt.subplot(222)
ax.plot(Ncolors, completeness, 'o-k', ms=6)

ax.xaxis.set_major_locator(plt.MultipleLocator(1))
ax.yaxis.set_major_locator(plt.MultipleLocator(0.2))
ax.xaxis.set_major_formatter(plt.NullFormatter())

ax.set_ylabel('completeness')
ax.set_xlim(0.5, 4.5)
ax.set_ylim(-0.1, 1.1)
ax.grid(True)

# Plot contamination vs Ncolors
ax = plt.subplot(224)
ax.plot(Ncolors, contamination, 'o-k', ms=6)

ax.xaxis.set_major_locator(plt.MultipleLocator(1))
ax.yaxis.set_major_locator(plt.MultipleLocator(0.2))
ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%i'))
Example #28
0
        return 'h'
    elif x == -1:
        return '-h'
    else:
        return '%ih' % x


for i, kernel in enumerate(
    ['gaussian', 'tophat', 'epanechnikov', 'exponential', 'linear', 'cosine']):
    axi = ax.ravel()[i]
    log_dens = KernelDensity(kernel=kernel).fit(X_src).score_samples(X_plot)
    axi.fill(X_plot[:, 0], np.exp(log_dens), '-k', fc='#AAAAFF')
    axi.text(-2.6, 0.95, kernel)

    axi.xaxis.set_major_formatter(plt.FuncFormatter(format_func))
    axi.xaxis.set_major_locator(plt.MultipleLocator(1))
    axi.yaxis.set_major_locator(plt.NullLocator())

    axi.set_ylim(0, 1.05)
    axi.set_xlim(-2.9, 2.9)

ax[0, 1].set_title('Available Kernels')

#----------------------------------------------------------------------
# Plot a 1D density example
N = 100
np.random.seed(1)
X = np.concatenate((np.random.normal(0, 1, int(0.3 * N)),
                    np.random.normal(5, 1, int(0.7 * N))))[:, np.newaxis]

X_plot = np.linspace(-5, 10, 1000)[:, np.newaxis]
Example #29
0
    x, y = np.meshgrid(
        time_series,
        np.logspace(np.log10(fmin), np.log10(fmax), scalogram.shape[0]))

    fig, ax = plt.subplots(figsize=(9, 9))
    ax.pcolormesh(x, 1 / y, np.abs(scalogram), cmap=CBname_map)
    plt.xlabel('Time around ' + 'Sdiff' + ' (s)', fontsize=18)
    plt.ylabel('Period (s)', fontsize=18)
    ax.set_yscale('log')  # set linear(detail in 1-2-3) or log(even)
    ax.set_ylim(Tmin, Tmax)
    majors = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ax.yaxis.set_major_locator(plt.FixedLocator(majors))
    # ax.yaxis.set_minor_locator(plt.NullLocator())
    ax.yaxis.set_major_formatter(ScalarFormatter())
    ax.yaxis.set_minor_formatter(NullFormatter())
    ax.xaxis.set_major_locator(plt.MultipleLocator(20))
    ax.xaxis.set_minor_locator(plt.MultipleLocator(5))
    # ax.set_yticks([1/10,1/5,1/3,1/2,1], ['10','5','3','2','1'])
    plt.gca().invert_yaxis()

    ax2 = ax.twinx()
    ax2.plot(time_series, stack_data_series / stack_data_series.max(),
             'k')  # plot the waveform at the bottom
    ax2.set_ylim([-1, 50])
    ax2.set_yticklabels([])
    plt.xlim([StartTime, EndTime])

    plt.title('Wavelet Spectrum at Azimuth ' + str(select_azi) +
              ';\n Stacking ' + str(count) + ' Traces')

    # plt.show()
Example #30
0
def fig_ionstate(outfil=None):

    # Init COS-Halos sightline
    cos_halos = COSHalos()
    cos_halos.load_single(('J1016+4706', '274_6'))
    cgm_abs = cos_halos.cgm_abs[0]
    FeH = -1.

    # CUBA
    cuba = CUBA()
    npt = 30
    xeV = np.linspace(13.6, 130., npt) * u.eV
    phi = np.zeros(npt)
    for kk, ixeV in enumerate(xeV):
        phi[kk] = cuba.phi(0.2, min_energy=ixeV).value

    # Ions
    Zions = [
        (1, 1),  # HI
        (6, 2),  # CII
        (6, 3),  # CIII
        (7, 2),  # NII
        (7, 3),  # NIII
        (7, 5),  # NV
        (8, 6),  # OVI
        (12, 1),  # MgI
        (12, 2),  # MgII
        (14, 2),  # SiII
        (14, 3),  # SiIII
        (14, 4),  # SiIV
    ]

    # Start the plot
    if outfil is None:
        outfil = 'fig_ionstate.pdf'
    pp = PdfPages(outfil)

    # Lya spec
    lclr = 'red'
    pclr = 'blue'
    xmnx = (0., 130.)  # eV
    ymnx = (-1.5, 2.)

    for ss in range(5):
        plt.figure(figsize=(7, 5))
        plt.clf()
        gs = gridspec.GridSpec(1, 1)

        ax = plt.subplot(gs[0, 0])
        #ax.xaxis.set_minor_locator(plt.MultipleLocator(0.5))
        ax.yaxis.set_major_locator(plt.MultipleLocator(1.))
        #ax.get_xaxis().get_major_formatter().set_useOffset(False)
        ax.set_xlim(xmnx)
        ax.set_ylim(ymnx)
        ax.set_xlabel('Ionization Potential (eV)')
        ax.set_ylabel(
            r'$\log N_{\rm ion} - \log N_{\rm HI} + 12 - \epsilon_\odot$ - [Fe/H]'
        )
        ax.minorticks_on()

        # Zero line
        ax.plot(xmnx, (0., 0.), '--', color='gray')

        # Data
        HIclm = cgm_abs.abs_sys.ions[(1, 1)]['CLM']
        for Zion in Zions:
            # Column and flag
            try:
                clm = cgm_abs.abs_sys.ions[Zion]['CLM']
            except KeyError:
                print('Zion = {:d},{:d} not analyzed'.format(Zion[0], Zion[1]))
                continue
            else:
                flg_clm = cgm_abs.abs_sys.ions[Zion]['FLG_CLM']
                sig_clm = cgm_abs.abs_sys.ions[Zion]['SIG_CLM']

            # IP
            elm = ELEMENTS[Zion[0]]
            IP = elm.ionenergy[Zion[1] - 1]

            # Solar abund
            abund = xsolar.abund(Zion[0])

            yval = clm - HIclm + 12 - abund
            if Zion != (1, 1):
                yval = yval + FeH

            # Plot
            if flg_clm == 1:
                mark = 'o'
                pclr = 'blue'
                yoff = 0.1
            elif flg_clm == 2:
                mark = (3, 0, 0)
                pclr = 'red'
                yoff = -0.3
            elif flg_clm == 3:
                mark = (3, 0, 180)
                pclr = 'gray'
                yoff = 0.1
            ax.scatter(IP, yval, marker=mark, color=pclr, s=35.)
            # Label
            ion_name = xion.ion_name(Zion)
            ax.text(IP,
                    yval + yoff,
                    ion_name,
                    color=pclr,
                    fontsize=17.,
                    ha='center')

        #ax.text(1480., 0.4, cgm_abs.field, ha='left', fontsize=21., color=lclr)

        if ss >= 1:  # Add EUVB
            ax_euvb = ax.twinx()
            y0 = np.log10(phi[0])
            ax_euvb.plot(xeV, np.log10(phi) - y0, 'k-')  # Normalize 1 Ryd to 1
            ax_euvb.set_ylabel(r'$\log \; \Phi_{\rm EUVB} ({\rm E > IP})$')
            xputils.set_fontsize(ax_euvb, 17.)
            ax_euvb.set_xlim(xmnx)
            ax_euvb.set_ylim(-1.2, 0.)
            ax_euvb.text(100., -1., 'EUVB', color='black', size=17.)
        if ss >= 2:  # Add shading
            xrng = np.array([10., 55])
            ax.fill_between(xrng, [ymnx[0]] * 2, [ymnx[1]] * 2,
                            color='blue',
                            alpha=0.3)
            ax.text(np.mean(xrng),
                    -1.2,
                    'Photoionization\n' + r'$U \equiv \Phi/cn_{\rm H}$',
                    color='black',
                    size=19.,
                    ha='center')
        if ss >= 3:  # Add shading
            xrng = np.array([70., 120.])
            ax.fill_between(xrng, [ymnx[0]] * 2, [ymnx[1]] * 2,
                            color='red',
                            alpha=0.3)
            ax.text(np.mean(xrng),
                    -1.2,
                    'Another phase',
                    color='black',
                    size=19.,
                    ha='center')

        if ss >= 4:  # EUV lines
            Zions2 = [(10, 8), (16, 4), (16, 5), (8, 2), (8, 3), (8, 5)]
            for Zion2 in Zions2:
                elm = ELEMENTS[Zion2[0]]
                IP = elm.ionenergy[Zion2[1] - 1]
                yval = 0.2
                yoff = 0.1
                ax.scatter(IP, yval, marker='o', color='black', s=35.)
                # Label
                try:
                    ion_name = xion.ion_name(Zion2)
                except TypeError:
                    ion_name = 'NeVIII'
                ax.text(IP,
                        yval + yoff,
                        ion_name,
                        color='black',
                        fontsize=17.,
                        ha='center')
            #ax.set_xlim(0.,200)

        # Fonts
        xputils.set_fontsize(ax, 17.)

        # Write
        plt.tight_layout(pad=0.2, h_pad=0., w_pad=0.1)
        pp.savefig()
        plt.close()

    # Finish
    print('tlk_coshalos: Wrote {:s}'.format(outfil))
    pp.close()