Example #1
0
def upload():
    if request.method == 'POST':
        df = pd.read_csv(request.files.get('file'))
        summary = df.describe().transpose()
        print(df.columns)
        df.columns = df.columns.str.replace(' ', '')
        df_clean = df.copy()

        #Remove Outliers
        Q1 = df_clean.quantile(0.25)
        Q3 = df_clean.quantile(0.75)
        IQR = Q3 - Q1

        df_clean = df_clean[~((df_clean < (Q1 - 1.5 * IQR)) |
                              (df_clean > (Q3 + 1.5 * IQR))).any(axis=1)]
        df_clean = df_clean.dropna(axis=1, how='all')
        #print(df_clean.columns)
        #print(cat_df)

        # calculating percentage of distresses sales
        percentageofDistressedCount = df.apply(
            lambda x: True if x['Ownership'] != "Private - Owned" else False,
            axis=1)
        PercentageofDistressedSales = round(
            100 * (len(percentageofDistressedCount[percentageofDistressedCount
                                                   == True].index)) /
            (len(df.index) - 1))

        # calculating percentage of sales with seller concessions
        percentageofSaleswithSellercount = df.apply(
            lambda x: True if x['SellerConcessionYN'] == True else False,
            axis=1)
        PercentageofDSaleswithSellerConcessions = round(
            100 * (len(percentageofSaleswithSellercount[
                percentageofSaleswithSellercount == True].index)) /
            (len(df.index) - 1), 2)

        # calculating average seller concessions amt and %
        SellerConcession = df[df['SellerConcessionYN'] == True]
        AverageSellerConcession = SellerConcession['SellerConcessionAmount']
        AverageSellerConcessionRatio = SellerConcession[
            'SellerConcessionAmount'] / SellerConcession['ClosePrice']
        AverageSellerConcessionAmount = AverageSellerConcession.mean(
            axis=0, skipna=True)
        AverageSellerConcessionAmount = round(AverageSellerConcessionAmount, 2)
        AverageSellerConcessionPercent = AverageSellerConcessionRatio.mean(
            axis=0, skipna=True)
        AverageSellerConcessionPercent = round(
            AverageSellerConcessionPercent * 100, 2)

        # calculating median seller concessions
        MedianSellerConcessionAmount = round(
            AverageSellerConcession.median(axis=0, skipna=True), 2)

        # calculating statistics - Pvalue

        def pearsonr_pval(x, y):
            return pearsonr(x, y)[1]

        cor_data_pval = (df.drop(columns=['WaterFrontageFeet']).corr(
            method=pearsonr_pval).stack().reset_index().rename(
                columns={
                    0: 'pvalues',
                    'level_0': 'variable',
                    'level_1': 'variable2'
                }))
        #print(cor_data_pval)
        cor_data_pval = cor_data_pval[cor_data_pval['variable'] ==
                                      'ClosePrice']
        #print("check2")
        cor_data_pval = cor_data_pval[
            cor_data_pval['variable2'] != 'ClosePrice']
        #print(cor_data_pval)
        cor_data_pval.assign(SignificantYN='NA')
        cor_data_pval.loc[(cor_data_pval.pvalues <= 0.05),
                          'SignificantYN'] = 'Yes'
        cor_data_pval.loc[(cor_data_pval.pvalues > 0.05),
                          'SignificantYN'] = 'No'

        # calculating statistics - Pearson's r value

        cor_data = (df.drop(columns=['WaterFrontageFeet']).corr(
            method='pearson').stack().reset_index().rename(
                columns={
                    0: 'correlation',
                    'level_0': 'variable',
                    'level_1': 'variable2'
                }))
        cor_data['correlation_label'] = cor_data['correlation'].map(
            '{:.2f}'.format)  # Round to 2 decimal
        cor_data = cor_data[cor_data['variable'] == 'ClosePrice']
        cor_data = cor_data[cor_data['variable2'] != 'ClosePrice']
        cor_datatable = cor_data.copy()
        #print(cor_data)
        cor_datatable.rename(columns={
            'variable2': 'Dimension',
            'correlation_label': 'Correlation'
        },
                             inplace=True)
        #print("cor_data")
        #print(cor_data)
        df_suffix = pd.merge(cor_data_pval,
                             cor_datatable,
                             left_on='variable2',
                             right_on='Dimension',
                             how='outer',
                             suffixes=('_left', '_right'))
        df_suffix_p = df_suffix[[
            'Dimension', 'pvalues', 'Correlation', 'SignificantYN'
        ]]
        cor_datatable_sig = df_suffix[[
            'Dimension', 'Correlation', 'SignificantYN'
        ]]

        cor_datatable_sig.rename(columns={
            'Dimension': 'Feature',
            'Correlation': 'Correlation with Closing Price',
            'SignificantYN': 'Significant(Y/N)'
        },
                                 inplace=True)

        #Calculate feature range table

        ##################################################Function to calculate coeff. of linear regression

        def calc_slope(df, feature):
            df = df.fillna(0)
            X = df[feature].values.reshape(-1, 1)
            Y = df['ClosePrice'].values.reshape(-1, 1)
            regressor = LinearRegression()
            regressor.fit(X, Y)
            lrcoeff = math.ceil(regressor.coef_)
            listeval = [feature, lrcoeff]
            #print(listeval)
            return listeval

        ##################################################Function to calculate confidence interval from stackoverflow
        def r_to_z(r):
            return math.log((1 + r) / (1 - r)) / 2.0

        def z_to_r(z):
            e = math.exp(2 * z)
            return ((e - 1) / (e + 1))

        def r_confidence_interval(r, data):
            alpha = 0.05
            n = len(data.index) + 1
            z = r_to_z(r)
            se = 1.0 / math.sqrt(n - 3)
            z_crit = stats.norm.ppf(1 - alpha / 2)  # 2-tailed z critical value

            lo = z - z_crit * se
            hi = z + z_crit * se

            r_lo = z_to_r(lo) * 100
            r_hi = z_to_r(hi) * 100
            inter = [math.ceil(r_lo), math.ceil(r_hi)]
            return (inter)

        # print(r_confidence_interval(0.66))
        ##################################### calculate pearson's correlation values

        cor_datatable_sig = cor_datatable_sig[
            cor_datatable_sig['Significant(Y/N)'] == 'Yes']
        cor_datatable_sig = cor_datatable_sig[[
            'Feature', 'Correlation with Closing Price'
        ]]
        cor_datatable_sig = cor_datatable_sig.sort_values(
            'Correlation with Closing Price', ascending=False)
        cor_datatable_sig.assign(Rangecorr='Default')
        interval = []
        for i in cor_datatable_sig['Correlation with Closing Price']:
            interval.append('  -  '.join(
                str(elem) for elem in (r_confidence_interval(float(i), df))))
        cor_datatable_sig['Rangecorr'] = interval
        cor_datatable_sig = cor_datatable_sig[['Feature', 'Rangecorr']]

        # slope = pd.DataFrame(columns=["Feature","Slope"])
        slope_df = pd.DataFrame(columns=['Features', 'Slope'])
        for i in cor_datatable_sig['Feature']:
            slope_df = slope_df.append(
                pd.DataFrame([calc_slope(df, i)], columns=slope_df.columns))
        cor_datatable_sig.reset_index(inplace=True)
        slope_df.reset_index(inplace=True)
        Combined_table = cor_datatable_sig.join(slope_df,
                                                how='inner',
                                                lsuffix='Features',
                                                rsuffix='Features')
        Combined_table[['LCI', 'UCI'
                        ]] = Combined_table.Rangecorr.str.split(" - ",
                                                                expand=True)
        Combined_table['LCI'] = pd.to_numeric(Combined_table['LCI'])
        Combined_table['UCI'] = pd.to_numeric(Combined_table['UCI'])
        Combined_table['Slope'] = pd.to_numeric(Combined_table['Slope'])
        Combined_table[
            'LCI'] = Combined_table['LCI'] * Combined_table['Slope'] / 100
        Combined_table[
            'UCI'] = Combined_table['UCI'] * Combined_table['Slope'] / 100
        Combined_table['LCI'] = Combined_table['LCI'].map(
            lambda a: math.ceil(a))
        Combined_table['UCI'] = Combined_table['UCI'].map(
            lambda a: math.ceil(a))
        Combined_table['LCI'] = Combined_table['LCI'].apply(str)
        Combined_table['UCI'] = Combined_table['UCI'].apply(str)
        Combined_table['Adjusted Tweak Amount'] = Combined_table[
            'LCI'].str.cat(Combined_table['UCI'], sep="-")
        cor_datatable_sig.rename(columns={'Rangecorr': 'Tweak Amount'},
                                 inplace=True)
        Combined_table = Combined_table[['Features', 'Adjusted Tweak Amount']]
        Combined_table['Adjusted Tweak Amount'] = '$' + Combined_table[
            'Adjusted Tweak Amount'].astype(str)
        Combined_table['Features'] = Combined_table['Features'].replace([
            'EstFinAbvGrdSqFt', 'YearRemodeled', 'BathsFull', 'BathsHalf',
            'GarageYN', 'YearBuilt', 'Acreage'
        ], [
            'Reported Living Area', 'Year Remodeled',
            'Number of Full Bathrooms', 'Number of Half Bathrooms',
            'Garage Count', 'Year Built', 'Reported Site Size(ac)'
        ])
        Combined_table.set_index('Features', inplace=True)
        #print(Combined_table)
        #cor_data.set_index('variable',inplace=True)

        fig1, ax1 = plt.subplots()
        x1 = df_clean.EstFinAbvGrdSqFt
        #x1CI = df_clean.EstFinAbvGrdSqFt.to_numpy()
        #y1CI = df_clean.ClosePrice.to_numpy()
        y1 = df_clean.ClosePrice
        slope1, intercept1, r_value1, p_value1, std_err1 = stats.linregress(
            x1, y1)
        sns.regplot(x="EstFinAbvGrdSqFt",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax1,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        sns.regplot(x="EstFinAbvGrdSqFt",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax1,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        plt.xlim(
            min(df_clean['EstFinAbvGrdSqFt']) - 50,
            max(df_clean['EstFinAbvGrdSqFt']) + 50)
        #est = sm.OLS(y1CI, x1CI)
        #est2 = est.fit()
        #ci1 = est2.conf_int(alpha=0.05, cols=None)
        #print(x1.to_numpy())
        #scatter = ax1.scatter(x1,y1)
        line1 = slope1 * x1 + intercept1
        #plt.plot(x1, line1, 'r', label='y={:.2f}x+{:.2f}'.format(slope1, intercept1))
        #ax1.legend()
        #ax1.fill_between(x1CI, (y1CI - ci1[0][0]), (y1CI + ci1[0][1]), color='b', alpha=.1)
        ax1.grid(color='lightgrey', linestyle='solid')
        ax1.set_xlabel("Reported Living Area(SF)", fontsize=15)
        ax1.set_ylabel("Reported Closing Price($)", fontsize=15)
        ax1.set_title(
            "Scatter Plot for Reported Living Area and Reported Sales Price",
            size=20)
        #sns.lineplot(data=df_clean, x="EstFinAbvGrdSqFt", y="ClosePrice", ax=ax1)
        json01 = json.dumps(mpld3.fig_to_dict(fig1))

        fig2, ax2 = plt.subplots()
        x2 = df_clean.Acreage
        y2 = df_clean.ClosePrice
        slope2, intercept2, r_value2, p_value2, std_err2 = stats.linregress(
            x2, y2)
        sns.regplot(x="Acreage",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax2,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        sns.regplot(x="Acreage",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax2,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        plt.xlim(
            min(df_clean['Acreage']) - 0.01,
            max(df_clean['Acreage']) + 0.01)

        #scatter2 = ax2.scatter(x2, y2)
        #line2 = slope2 * x2 + intercept2
        #plt.plot(x2, line2, 'r', label='y={:.2f}x+{:.2f}'.format(slope2, intercept2))
        ax2.legend()
        ax2.grid(color='lightgrey', linestyle='solid')
        ax2.set_xlabel("Reported Site Size (ac)", fontsize=15)
        ax2.set_ylabel("Reported Closing Price($)", fontsize=15)
        ax2.set_title(
            "Scatter Plot for Reported Site Size (ac) and Reported Sales Price",
            size=20)
        json02 = json.dumps(mpld3.fig_to_dict(fig2))

        fig3, ax3 = plt.subplots()
        x3 = df_clean.YearBuilt
        y3 = df_clean.ClosePrice
        slope3, intercept3, r_value3, p_value3, std_err3 = stats.linregress(
            x3, y3)
        sns.regplot(x="YearBuilt",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax3,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        sns.regplot(x="YearBuilt",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax3,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        plt.xlim(
            min(df_clean['YearBuilt']) - 1,
            max(df_clean['YearBuilt']) + 1)

        #scatter3 = ax3.scatter(x3, y3)
        #line3 = slope3 * x3 + intercept3
        #plt.plot(x3, line3, 'r', label='y={:.2f}x+{:.2f}'.format(slope3, intercept3))
        ax3.legend()
        ax3.grid(color='lightgrey', linestyle='solid')
        ax3.set_xlabel("Year Built", fontsize=15)
        ax3.set_ylabel("Reported Closing Price($)", fontsize=15)
        ax3.set_title(
            "Scatter Plot for Seller Concession Amount and Reported Sales Price",
            size=20)
        json03 = json.dumps(mpld3.fig_to_dict(fig3))

        fig4, ax4 = plt.subplots()
        x4 = df_clean.YearRemodeled
        y4 = df_clean.ClosePrice
        slope4, intercept4, r_value4, p_value4, std_err4 = stats.linregress(
            x4, y4)
        sns.regplot(x="YearRemodeled",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax4,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        sns.regplot(x="YearRemodeled",
                    y="ClosePrice",
                    data=df_clean,
                    ax=ax4,
                    truncate=True,
                    scatter_kws={"color": "#1A5276"},
                    line_kws={
                        "color": "red",
                        "alpha": 1
                    })
        #print(min(df_clean['YearRemodeled']))
        #print(max(df_clean['YearRemodeled']))
        #plt.xlim(min(df_clean['YearRemodeled']) - 1, max(df_clean['YearRemodeled']) + 1)

        #scatter4 = ax4.scatter(x4, y4)
        #line4 = slope4 * x4 + intercept4
        #plt.plot(x4, line4, 'r', label='y={:.2f}x+{:.2f}'.format(slope4, intercept4))
        ax4.legend()
        ax4.grid(color='lightgrey', linestyle='solid')
        ax4.set_xlabel("Year Remodeled", fontsize=15)
        ax4.set_ylabel("Reported Closing Price($)", fontsize=15)
        ax4.set_title(
            "Scatter Plot for Year Remodeled and Reported Sales Price",
            size=20)
        json04 = json.dumps(mpld3.fig_to_dict(fig4))

        fig_bar1, ax_bar1 = plt.subplots()
        keys1 = list(df_clean.BathsFull)
        values1 = list(df_clean.ClosePrice)
        ax_bar1 = plt.bar(keys1, values1, width=0.5)
        #mean_bar1 = df_clean['ClosePrice'].mean()
        #ax_bar1.axvline(mean_bar1, color='r', linestyle='-')
        #print(df_clean['BathsFull'])
        plt.xticks([0.0, 1.0, 2.0, 3.0, 4.0])
        plt.xlabel("Number of Full Bathrooms")
        plt.ylabel("Reported Closing Price($)")
        #ax_bar1.set_title("Full Baths v. Reported Sales Price", size=20)
        fig_bar1 = ax_bar1[0].figure
        json_bar1 = json.dumps(mpld3.fig_to_dict(fig_bar1))
        #bar_chart = mpld3.fig_to_html(fig_bar1)
        #print(df_clean.dtypes)
        #sns.barplot(x='BathsHalf', y='ClosePrice', data=df_clean, ax=ax_bar1)
        #ax_bar1 = df_clean.plot.bar(x='BathsHalf', y='ClosePrice' )
        #json_bar1 = json.dumps(mpld3.fig_to_dict(fig_bar1))

        fig_bar2, ax_bar2 = plt.subplots()
        keys2 = list(df_clean.BathsHalf)
        values2 = list(df_clean.ClosePrice)
        ax_bar2 = plt.bar(keys2, values2, width=0.5)
        plt.xticks([0.0, 1.0, 2.0, 3.0, 4.0])
        plt.xlabel("Number of Half Bathrooms")
        plt.ylabel("Reported Closing Price($)")
        #ax_bar2.set_title("Half Baths v. Reported Sales Price", size=20)
        fig_bar2 = ax_bar2[0].figure
        json_bar2 = json.dumps(mpld3.fig_to_dict(fig_bar2))

        fig_bar3, ax_bar3 = plt.subplots()
        keys3 = list(df_clean.BedsTotal)
        values3 = list(df_clean.ClosePrice)
        ax_bar3 = plt.bar(keys3, values3, width=0.5)
        plt.xticks([0.0, 1.0, 2.0, 3.0, 4.0])
        plt.xlabel("Number of Bedrooms")
        plt.ylabel("Reported Closing Price($)")
        #ax_bar3.set_title("Bedroom count v. Reported Sales Price", size=20)
        fig_bar3 = ax_bar3[0].figure
        json_bar3 = json.dumps(mpld3.fig_to_dict(fig_bar3))

        # Histogram plot
        fig_h1, ax_h1 = plt.subplots()
        mean = df_clean['ClosePrice'].mean()
        ax_h1 = sns.distplot(df_clean.ClosePrice)
        ax_h1.axvline(mean, color='r', linestyle='-')
        ax_h1.set_xlabel("Reported Sales Price($)", fontsize=15)
        ax_h1.set_ylabel("")
        ax_h1.set_title("Sales Price Distribution", size=20)
        json_h1 = json.dumps(mpld3.fig_to_dict(fig_h1))

        fig_h2, ax_h2 = plt.subplots()
        mean2 = df_clean['EstFinAbvGrdSqFt'].mean()
        ax_h2 = sns.distplot(df_clean.EstFinAbvGrdSqFt, label="mean2")
        ax_h2.axvline(mean2, color='r', linestyle='-')
        ax_h2.set_xlabel("Reported Living Area (SF)", fontsize=15)
        ax_h2.set_ylabel("")
        ax_h1.legend(['mean:', mean2])
        ax_h2.set_title("Size Distribution", size=20)
        json_h2 = json.dumps(mpld3.fig_to_dict(fig_h2))

        #heatmap
        cor_data = cor_data[['variable', 'variable2', 'correlation_label']]
        #cor_data.set_index('variable',inplace=True)
        cor_data['variable'] = cor_data['variable'].astype('str')
        cor_data['variable2'] = cor_data['variable2'].astype('str')
        cor_data['correlation_label'] = cor_data['correlation_label'].astype(
            'float')
        result = cor_data.pivot(index='variable',
                                columns='variable2',
                                values='correlation_label')
        column_labels = list(cor_data.variable2)
        row_labels = ['Closing Price']
        print(result.values)
        fig_hm, ax_hm = plt.subplots()
        im = ax_hm.imshow(result.values)
        #ax_hm.set_xticklabels(column_labels)
        #ax_hm.set_yticklabels(row_labels)
        #plt.setp(ax_hm.get_xticklabels(), rotation=45, ha="right",rotation_mode="anchor")
        #fig_hm.tight_layout()
        ax_hm.xaxis.set_major_formatter(plt.NullFormatter())
        ax_hm.yaxis.set_major_formatter(plt.NullFormatter())
        print((np.array(result.values[0]))[0])
        print(result.values[0, 0])

        #ax_hm.remove()
        ax_hm.pcolormesh(result.values, cmap=plt.cm.Reds)
        #ax_hm.remove()
        #rc = {"axes.spines.left": False,
        #"ytick.left": False}
        #plt.rcParams.update(rc)
        #ax_hm.set_xticks(np.arange(result.shape[0]) + 0.5, minor=False)
        #ax_hm.set_yticks(np.arange(result.shape[1]) + 0.5, minor=False)
        #ax_hm.set_frame_on(False)
        #ax_hm.axes.get_yaxis().set_visible(False)
        #fig_hm.tight_layout()

        #ax_hm.set_xticklabels(column_labels, minor=False)
        #ax_hm.set_yticklabels(row_labels, minor=False)
        ax_hm.invert_yaxis()

        json_hm = json.dumps(mpld3.fig_to_dict(fig_hm))
        #plt.show()

        #summary.to_html(classes='male')

        return render_template(
            'upload.html',
            tables=[Combined_table.to_html(classes='male')],
            json01=json01,
            json02=json02,
            json03=json03,
            json04=json04,
            json_h1=json_h1,
            json_h2=json_h2,
            json_bar1=json_bar1,
            json_bar2=json_bar2,
            json_bar3=json_bar3,
            json_hm=json_hm,
            PercentageofDistressedSales=PercentageofDistressedSales,
            PercentageofDSaleswithSellerConcessions=
            PercentageofDSaleswithSellerConcessions,
            AverageSellerConcessionAmount=AverageSellerConcessionAmount,
            AverageSellerConcessionPercent=AverageSellerConcessionPercent,
            MedianSellerConcessionAmount=MedianSellerConcessionAmount)
    return render_template('upload.html')
Example #2
0
    def plot(self):
        t = self.t
        simResult = self.simResult
        plt.figure(figsize=(15, 15))
        x, y = (4, 3)
        plt.subplot(x, y, 1)
        plt.plot(simResult['Susceptibles'] / simResult['Population'] * 100.0)
        plt.title(f'Susceptibles')
        plt.ylabel('Part of population [%]')
        plt.xlim(plt.xlim([self.plotStartDate, self.plotEndDate]))
        plt.gca().axes.get_xaxis().set_major_formatter(plt.NullFormatter())
        plt.grid(True)

        plt.subplot(x, y, 2)
        plt.plot(simResult['Susceptibles'])
        plt.yscale('log')
        plt.title('Susceptibles (' +
                  datetime.datetime.now().strftime("%Y-%m-%d, %H:%M:%S") + ')')
        plt.ylabel('Number of people')
        plt.gca().axes.get_xaxis().set_major_formatter(plt.NullFormatter())
        plt.grid(True)

        ax = plt.subplot(x, y, 3)
        #plt.step(self.Ro.index,self.Ro.values,where='post')
        plt.step(self.dti, self.Ro(self.t), where='post')
        plt.title(f'Reproduction number')
        plt.ylabel('Ro [-]')
        plt.xlim(plt.xlim([self.plotStartDate, self.plotEndDate]))
        plt.gcf().autofmt_xdate()
        plt.grid(True)
        ax.text(0.99,
                0.96,
                self.RoText,
                verticalalignment='top',
                horizontalalignment='right',
                transform=ax.transAxes,
                color='black',
                fontsize=10,
                bbox=dict(boxstyle="square",
                          ec=(1., 1., 1.),
                          fc=(1., 1., 1.),
                          alpha=0.6))

        plt.subplot(x, y, 4)
        plt.plot(simResult['Infected'] / simResult['Population'] * 100.0)
        plt.title('Infected')
        plt.ylabel('Part of population [%]')
        plt.xlim(plt.xlim([self.plotStartDate, self.plotEndDate]))
        plt.gca().axes.get_xaxis().set_major_formatter(plt.NullFormatter())
        plt.grid(True)

        plt.subplot(x, y, 5)
        plt.plot(simResult['Infected'])
        plt.yscale('log')
        plt.title('Infected')
        plt.ylabel('Number of people')
        plt.gca().axes.get_xaxis().set_major_formatter(plt.NullFormatter())
        plt.ylim((1))
        plt.grid(True)

        #k01 * So / k12
        plt.subplot(x, y, 6)
        plt.plot(self.dti,
                 self.Ro(self.t) * simResult['Susceptibles'].values / self.So)
        plt.title(f'Effective reproduction number')
        plt.ylabel('R [-]')
        plt.xlim(plt.xlim([self.plotStartDate, self.plotEndDate]))
        plt.gcf().autofmt_xdate()
        plt.grid(True)

        plt.subplot(x, y, 7)
        plt.plot(simResult['Recovered'] / simResult['Population'] * 100.0)
        plt.title('Recovered')
        plt.ylabel('Part of population [%]')
        plt.xlim(plt.xlim([self.plotStartDate, self.plotEndDate]))
        plt.gca().axes.get_xaxis().set_major_formatter(plt.NullFormatter())
        plt.grid(True)

        plt.subplot(x, y, 8)
        plt.plot(simResult['Recovered'])
        plt.yscale('log')
        plt.title('Recovered (Sw: Friska immuna)')
        plt.ylabel('Number of people')
        plt.gca().axes.get_xaxis().set_major_formatter(plt.NullFormatter())
        plt.ylim((1))
        plt.grid(True)

        plt.subplot(x, y, 10)
        plt.plot(simResult['Death'] / self.So * 1E6)
        plt.plot(self.FHMData.data.Antal_avlidna.cumsum() / self.So * 1E6)
        plt.title('Cumulative deaths per million people')
        plt.ylabel('Deaths per million')
        plt.legend(['Simulation', 'FHM Sweden'])
        plt.xlim([self.plotStartDate, self.plotEndDate])
        plt.gcf().autofmt_xdate()
        plt.grid(True)

        plt.subplot(x, y, 11)
        plt.plot(simResult['Death'])
        plt.plot(self.FHMData.data.Antal_avlidna.cumsum())
        plt.yscale('log')
        plt.title('Deaths')
        plt.ylabel('Number of deaths')
        plt.ylim(1)
        plt.gcf().autofmt_xdate()
        plt.grid(True)

        plt.subplot(x, y, 12)
        plt.plot(simResult['Death'].diff())
        plt.plot(self.FHMData.data.Antal_avlidna)
        plt.title('Deaths')
        plt.ylabel('Daily deaths')
        plt.xlim([self.plotStartDate, self.plotEndDate])
        plt.grid(True)
        plt.gcf().autofmt_xdate()
        plt.tight_layout(pad=0.2, w_pad=0.1, h_pad=0.1)
        plt.subplots_adjust(wspace=0.25, hspace=0.15)
        plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)

        filename = "Doc/covid-19_sim.png"
        modified = os.path.getmtime(filename)
        if True:  #modified < time.time()-60*60*24:
            print("Update the file:" + filename)
            plt.savefig(filename, bbox_inches='tight')
        #mpld3.show()

        mng = plt.get_current_fig_manager()
        mng.full_screen_toggle()

        plt.show()
ax.text('2012-1-1', 3950, "New Year's Day", **style)
ax.text('2012-7-4', 4250, 'Independence Day', ha='center', **style)
ax.text('2012-9-4', 4850, 'Labor Day', ha='center', **style)
ax.text('2012-10-31', 4600, 'Halloween', ha='right', **style)
ax.text('2012-11-25', 4450, 'Thanksgiving', ha='center', **style)
ax.text('2012-12-25', 3850, 'Christmas', ha='right', **style)

# Label the axes
ax.set(title='USA births by day of year (1969-1988)',
       ylabel='average daily births')

# Format the x axis with centered month labels
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
ax.xaxis.set_minor_locator(mpl.dates.MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%h'))

# %%
# Transforms and Text Position
fig, ax = plt.subplots(facecolor='lightgray')
ax.axis([0, 10, 0, 10])

# transform=ax.transData is the default, but we'll specify it anyway
ax.text(1, 5, '. Data: (1, 5)', transform=ax.transData)
ax.text(0.5, 0.1, '. Axes: (0.5, 0.1)', transform=ax.transAxes)
ax.text(0.2, 0.2, '. Figure: (0.2, 0.2)', transform=fig.transFigure)

# %%
ax.set_xlim(0, 2)
ax.set_ylim(-6, 6)
Example #4
0
def main():
    global PLOT_NUMBER
    f, axs = plt.subplots(ROWS, COLS, figsize=(9,10))
    # plt.suptitle('Influence of unknown data to prediction', fontsize=12, y=1, fontweight='bold')
    plt.NullFormatter()

    for pp in [10, 20, 30, 40, 50]: 
        plot_hist(pp)
        plot_data_unknown(pp)
        plot_data_multifurc(pp)

    plt.tight_layout()
    plt.show()
    # ------------------------------------------------------------------------
    # ------------------------------------------------------------------------

    f, axs = plt.subplots(1, 1, figsize=(4,3))

    # -------------------------------------plot distribution------------------------------------
    beta_distribution_parameters = [7, 5.5, 2.75, 7]   # 40 P - 60 FL
    #   for freeliving_distribution
    A_FL = beta_distribution_parameters[0]
    B_FL = beta_distribution_parameters[1]
    #   for parasite_distribution
    A_P = beta_distribution_parameters[2]
    B_P = beta_distribution_parameters[3]

    freeliving_distribution = []
    parasite_distribution = []
    for item in range(0, 1000000):
        freeliving_distribution.append(betavariate(A_FL, B_FL))
        parasite_distribution.append(betavariate(A_P, B_P))

    ax = plt.subplot(1, 1, 1)

    # the histogram of the data
    n, bins, patches = plt.hist(parasite_distribution, 100, normed=1, facecolor='r', alpha=0.75)
    n, bins, patches = plt.hist(freeliving_distribution, 100, normed=1, facecolor='b', alpha=0.75)

    plt.xlabel('drawn random number', fontsize=9)
    plt.ylabel('number of draws', fontsize=9)

    title = str(40) + '% P - ' + str(100-40) + '% FL'
    plt.axvline(x=40/100, color='black')#, xmin=0.25, xmax=0.402, linewidth=2)
    
    blue_patch = mpatches.Patch(color='blue', label='free-livings')
    red_patch = mpatches.Patch(color='red', label='parasites')
    black_line = mlines.Line2D([], [], color='black', label='threshold')#, marker='*', markersize=15)
    plt.legend(handles=[blue_patch, red_patch, black_line], title=title, loc="upper right", fontsize=9) # shadow=True, fancybox=True)
    ax.get_legend().get_title().set_fontweight("bold")
    # plt.axis([0, 1, 0, 8])
    plt.axis([0, 1, -0.2, 8])
    plt.grid(True)

    # -------------------------------------plot points------------------------------------
    plt.plot([0.15,0.33,0.52], [0,0,0], color='black', marker='o', markersize=10, markerfacecolor='red') #maroon
    plt.plot([0.44], [0], color='black', marker='o', markersize=10, markerfacecolor='blue') #navy
    ax.annotate('1', xy=(0.33, 0), xytext=(0.34, 0.1), color='white', fontweight='bold')
    ax.annotate('2', xy=(0.15, 0), xytext=(0.16, 0.1), color='white', fontweight='bold')
    ax.annotate('3', xy=(0.52, 0), xytext=(0.53, 0.1), color='white', fontweight='bold')
    ax.annotate('4', xy=(0.44, 0), xytext=(0.45, 0.1), color='white', fontweight='bold')
    plt.tight_layout()
    plt.show()
    # ------------------------------------------------------------------------
    # ------------------------------------------------------------------------

    PLOT_NUMBER = 1
    f, axs = plt.subplots(2, 2, figsize=(12, 5))
    # plt.suptitle('Fitch Versions and Unknown Data', fontsize=12, y=1)
    # filepath = 'data/simulation/30-fitch-unknown_plot.csv'
    filepath = 'data/simulation/fitch_30-unknown.csv'
    plt.subplot(2, 2, 1)
    plot_fitch_versions(filepath, 30)
    # filepath = 'data/simulation/40-fitch-unknown_plot.csv'
    filepath = 'data/simulation/fitch_40-unknown.csv'
    plt.subplot(2, 2, 2)
    plot_fitch_versions(filepath, 40)
    # filepath = 'data/simulation/50-fitch-unknown_plot.csv'
    filepath = 'data/simulation/fitch_50-unknown.csv'
    plt.subplot(2, 2, 3)
    plot_fitch_versions(filepath, 50)

    # plt.tight_layout()
    plt.show()

    return
Example #5
0
def set_geogrid(ax,
                resolution='110m',
                dlon=60,
                dlat=30,
                manual_ticks=False,
                xticks=None,
                yticks=None,
                bottom=True,
                left=True,
                right=False,
                top=False,
                linewidth=0.5,
                fontsize=15,
                labelsize=15,
                color='grey',
                alpha=0.8,
                linestyle='-'):
    """
    parameter
    -------------
    ax        :cartopy.mpl.geoaxes
    dlon      :float  grid interval of longitude
    dlat      :float  grid interval of latitude
    linewidth,fontsize,labelsize,alpha :float
    color     :string
    resolution :string '10m','50m','110m'
    bottom    :boolean draw  xaxis ticklabel
    lfet      :boolean draw  yaxis ticklabel
    
    return 
    -------------
    ax
    """
    #    labelpos=[bottom,left,top,right]
    #
    #    plt.rcParams['ytick.left']=plt.rcParams['ytick.labelleft']=left
    #    plt.rcParams['ytick.right']=plt.rcParams['ytick.labelright']=right
    #    plt.rcParams['xtick.top']=plt.rcParams['xtick.labeltop']=top
    #    plt.rcParams['xtick.bottom']=plt.rcParams['xtick.labelbottom']=bottom

    ax.coastlines(resolution=resolution)
    gl = ax.gridlines(crs=ccrs.PlateCarree(),
                      draw_labels=False,
                      linewidth=linewidth,
                      alpha=alpha,
                      color=color,
                      linestyle=linestyle)
    if manual_ticks == False:
        xticks = np.arange(0, 360.1, dlon)
        yticks = np.arange(-90, 90.1, dlat)
    gl.xlocator = mticker.FixedLocator(xticks)
    gl.ylocator = mticker.FixedLocator(yticks)

    if (type(ax.projection) == type(ccrs.PlateCarree())):
        ax.set_xticks(xticks, crs=ccrs.PlateCarree())
        ax.set_yticks(yticks, crs=ccrs.PlateCarree())

        latfmt = LatitudeFormatter()
        lonfmt = LongitudeFormatter(zero_direction_label=True)
        ax.xaxis.set_major_formatter(lonfmt)
        ax.yaxis.set_major_formatter(latfmt)
        if (bottom == False):
            ax.xaxis.set_major_formatter(plt.NullFormatter())
        if (left == False):
            ax.yaxis.set_major_formatter(plt.NullFormatter())
        ax.axes.tick_params(labelsize=labelsize)
    return ax
Example #6
0
            fig.add_axes((0.15 + 7 * 0.04, 0.12, 0.79 - 7 * 0.04, 0.17))]
ylims = [(22750, 22850),
         (26675, 26775)]
omega0 = [17.22, 8.61]

for i in range(2):
    # Plot full panel
    ax[i].plot(terms, BIC_max[i], '-k')
    ax[i].set_xlim(0, 20)
    ax[i].set_ylim(0, 30000)
    ax[i].text(0.02, 0.95, r"$\omega_0 = %.2f$" % omega0[i],
               ha='left', va='top', transform=ax[i].transAxes)

    ax[i].set_ylabel(r'$\Delta BIC$')
    if i == 1:
        ax[i].set_xlabel('N frequencies')
    ax[i].grid(color='gray')

    # plot inset
    ax_inset[i].plot(terms, BIC_max[i], '-k')
    ax_inset[i].xaxis.set_major_locator(plt.MultipleLocator(5))
    ax_inset[i].xaxis.set_major_formatter(plt.NullFormatter())
    ax_inset[i].yaxis.set_major_locator(plt.MultipleLocator(25))
    ax_inset[i].yaxis.set_major_formatter(plt.FormatStrFormatter('%i'))
    ax_inset[i].set_xlim(7, 19.75)
    ax_inset[i].set_ylim(ylims[i])
    ax_inset[i].set_title('zoomed view')
    ax_inset[i].grid(color='gray')

plt.show()
number_train = Number(train_out)
center = Center(train_in, train_out)
radius = Radius(train_in, train_out, number_train, center)
radiusnorm = radius / number_train
centerdistance = CenterDistance(center)

print("Number of images per digit\n", number_train)
print("Radius per digit\n", radius)
print("Normalised radius\n", radiusnorm)
print("Distance between centers\n", np.around(centerdistance, decimals=1))

# Plot centers for every cloud
fig, ax = plt.subplots(2, 5, figsize=(12, 4), tight_layout=True)
for i, cent in enumerate(center):
    ax[i // 5, i % 5].imshow(cent.reshape(16, 16), cmap='binary')
    ax[i // 5, i % 5].xaxis.set_major_formatter(plt.NullFormatter())
    ax[i // 5, i % 5].yaxis.set_major_formatter(plt.NullFormatter())
plt.savefig("centers.png")

plt.matshow(centerdistance, cmap='binary')
plt.suptitle('Center distance')
plt.savefig("centerdistance.png")

# Task 2: Apply distance classifier, calculate accuracy, confusion matrix and wrongly classified digits


def ApplyClassifierEuclidian(data_in, c):
    """
    Apply distance-based classifier (only Euclidian)
    Input:
        data_in - all images
Example #8
0
def plot_ROC_curves(sample, y_true, y_prob, output_dir, ROC_type, ECIDS,
                    ROC_values=None, combine_plots=False):
    LLH_fpr, LLH_tpr = LLH_rates(sample, y_true, ECIDS)
    if ROC_values != None: fpr, tpr = ROC_values[0]
    #if ROC_values != None:
    #    index = output_dir.split('_')[-1]
    #    index = ROC_values[0].shape[1]-1 if index == 'bkg' else int(index)
    #    fpr_full, tpr_full = ROC_values[0][:,index], ROC_values[0][:,0]
    #    fpr     , tpr      = ROC_values[1][:,index], ROC_values[1][:,0]
    else:
        #if ECIDS: y_prob = sample['p_ECIDSResult']
        #y_prob = y_prob + (sample['p_ECIDSResult']/2 +0.5)
        fpr, tpr, threshold = metrics.roc_curve(y_true, y_prob, pos_label=0)
        pickle.dump({'fpr':fpr, 'tpr':tpr}, open(output_dir+'/'+'pos_rates.pkl','wb'), protocol=4)
    signal_ratio       = np.sum(y_true==0)/len(y_true)
    accuracy           = tpr*signal_ratio + (1-fpr)*(1-signal_ratio)
    best_tpr, best_fpr = tpr[np.argmax(accuracy)], fpr[np.argmax(accuracy)]
    colors  = ['red', 'blue', 'green', 'red', 'blue', 'green']
    labels  = ['tight'      , 'medium'      , 'loose'       ,
               'tight+ECIDS', 'medium+ECIDS', 'loose+ECIDS']
    markers = 3*['o'] + 3*['D']
    sig_eff, bkg_eff = '$\epsilon_{\operatorname{sig}}$', '$\epsilon_{\operatorname{bkg}}$'
    plt.figure(figsize=(12,8)); pylab.grid(True); axes = plt.gca()
    if ROC_type == 1:
        pylab.grid(False)
        len_0 = np.sum(fpr==0)
        x_min = min(80, 10*np.floor(10*min(LLH_tpr)))
        if fpr[np.argwhere(tpr >= x_min/100)[0]] != 0:
            y_max = 10*np.ceil( 1/min(np.append(fpr[tpr >= x_min/100], min(LLH_fpr)))/10 )
            if y_max > 200: y_max = 100*(np.ceil(y_max/100))
        else: y_max = 1000*np.ceil(max(1/fpr[len_0:])/1000)
        plt.xlim([x_min, 100]); plt.ylim([1, y_max])
        #LLH_scores = [1/fpr[np.argwhere(tpr >= value)[0]][0] for value in LLH_tpr]
        #LLH_scores = [1/fpr[np.argmin(abs(tpr-value))] for value in LLH_tpr]
        def interpolation(tpr, fpr, value):
            try:
                idx1 = np.where(tpr>value)[0][np.argmin(tpr[tpr>value])]
                idx2 = np.where(tpr<value)[0][np.argmax(tpr[tpr<value])]
                M = (1/fpr[idx2]-1/fpr[idx1]) / (tpr[idx2]-tpr[idx1])
                return 1/fpr[idx1] + M*(value-tpr[idx1])
            except ValueError:
                return np.max(1/fpr)
        LLH_scores = [interpolation(tpr, fpr, value) for value in LLH_tpr]
        for n in np.arange(len(LLH_scores)):
            axes.axhline(LLH_scores[n], xmin=(LLH_tpr[n]-x_min/100)/(1-x_min/100), xmax=1,
            ls='--', linewidth=0.5, color='tab:gray', zorder=10)
            axes.axvline(100*LLH_tpr[n], ymin=abs(1/LLH_fpr[n]-1)/(plt.yticks()[0][-1]-1),
            ymax=abs(LLH_scores[n]-1)/(plt.yticks()[0][-1]-1), ls='--', linewidth=0.5, color='tab:gray', zorder=5)
            plt.text(100.2, LLH_scores[n], str(int(LLH_scores[n])),
                     {'color':colors[n], 'fontsize':11 if ECIDS else 12}, va="center", ha="left")
        axes.xaxis.set_major_locator(MultipleLocator(10))
        axes.xaxis.set_minor_locator(AutoMinorLocator(10))
        yticks = plt.yticks()[0]
        axes.yaxis.set_ticks( np.append([1], yticks[1:]) )
        axes.yaxis.set_minor_locator(FixedLocator(np.arange(yticks[1]/5, yticks[-1], yticks[1]/5 )))
        plt.xlabel(sig_eff+' (%)', fontsize=25)
        plt.ylabel('1/'+bkg_eff, fontsize=25)
        #label = '{$w_{\operatorname{bkg}}$} = {$f_{\operatorname{bkg}}$}'
        P, = plt.plot(100*tpr[len_0:], 1/fpr[len_0:], color='#1f77b4', lw=2, zorder=10)
        if combine_plots:
            def get_legends(n_zip, output_dir):
                file_path, ls = n_zip
                file_path += '/' + output_dir.split('/')[-1] + '/pos_rates.pkl'
                fpr, tpr = pickle.load(open(file_path, 'rb')).values()
                len_0 = np.sum(fpr==0)
                P, = plt.plot(100*tpr[len_0:], 1/fpr[len_0:], color='tab:gray', lw=2, ls=ls, zorder=10)
                return P
            file_paths = ['outputs/6c_180m_match2class0/scalars_only/bkg_optimal',
                          'outputs/2c_180m_match2class0/scalars+images']
            linestyles = ['--', ':']
            leg_labels = ['HLV+images (6-class)', 'HLV only (6-class)', 'HLV+images (2-class)']
            Ps = [P] + [get_legends(n_zip, output_dir) for n_zip in zip(file_paths,linestyles)]
            L = plt.legend(Ps, leg_labels, loc='upper left', bbox_to_anchor=(0,1), fontsize=14,
                           facecolor='ghostwhite', framealpha=1); L.set_zorder(10)
        if ROC_values != None:
            tag = 'combined bkg'
            extra_labels = {1:'{$w_{\operatorname{bkg}}$} for best AUC',
                            2:'{$w_{\operatorname{bkg}}$} for best TNR @ 70% $\epsilon_{\operatorname{sig}}$'}
            extra_colors = {1:'tab:orange', 2:'tab:green'   , 3:'tab:red'     , 4:'tab:brown'}
            for n in [1,2]:#range(1,len(ROC_values)):
                extra_fpr, extra_tpr = ROC_values[n]
                extra_len_0 = np.sum(extra_fpr==0)
                plt.plot(100*extra_tpr[extra_len_0:], 1/extra_fpr[extra_len_0:],
                         label=extra_labels[n], color=extra_colors[n], lw=2, zorder=10)
        #if ROC_values != None:
        #    plt.rcParams['agg.path.chunksize'] = 1000
        #    plt.scatter(100*tpr_full[len_0:], 1/fpr_full[len_0:], color='silver', marker='.')
        if best_fpr != 0:
            plt.scatter( 100*best_tpr, 1/best_fpr, s=40, marker='D', c='tab:blue',
                         label="{0:<15s} {1:>3.2f}%".format('Best accuracy:',100*max(accuracy)), zorder=10 )
        for n in np.arange(len(LLH_fpr)):
            plt.scatter( 100*LLH_tpr[n], 1/LLH_fpr[n], s=40, marker=markers[n], c=colors[n], zorder=10,
                         label='$\epsilon_{\operatorname{sig}}^{\operatorname{LH}}$'
                         +'='+format(100*LLH_tpr[n],'.1f') +'%' +r'$\rightarrow$'
                         +'$\epsilon_{\operatorname{bkg}}^{\operatorname{LH}}$/'
                         +'$\epsilon_{\operatorname{bkg}}^{\operatorname{NN}}$='
                         +format(LLH_fpr[n]*LLH_scores[n], '>.1f')
                         +' ('+labels[n]+')' )
        axes.tick_params(axis='both', which='major', labelsize=12)
        plt.legend(loc='upper right', fontsize=13 if ECIDS else 14, numpoints=3,
                   facecolor='ghostwhite', framealpha=1).set_zorder(10)
        if combine_plots: plt.gca().add_artist(L)
    if ROC_type == 2:
        plt.xlim([0.6, 1]); plt.ylim([0.9, 1-1e-4])
        plt.xticks([0.6, 0.7, 0.8, 0.9, 1], [60, 70, 80, 90, 100])
        plt.yscale('logit')
        plt.yticks([0.9, 0.99, 0.999, 0.9999], [90, 99, 99.9, 99.99])
        axes.xaxis.set_minor_locator(AutoMinorLocator(10))
        axes.xaxis.set_minor_formatter(plt.NullFormatter())
        axes.yaxis.set_minor_formatter(plt.NullFormatter())
        plt.xlabel('Signal Efficiency '+sig_eff+' (%)', fontsize=25)
        plt.ylabel('Background Rejection $1\!-\!$'+bkg_eff+' (%)', fontsize=25)
        plt.text(0.8, 0.67, 'AUC: '+format(metrics.auc(fpr,tpr),'.4f'),
                 {'color':'black', 'fontsize':22},  va='center', ha='center', transform=axes.transAxes)
        val = plt.plot(tpr, (1-fpr), label='Signal vs Background', color='#1f77b4', lw=2)
        plt.scatter( best_tpr, (1-best_fpr), s=40, marker='o', c=val[0].get_color(),
                     label="{0:<16s} {1:>3.2f}%".format('Best accuracy:', 100*max(accuracy)) )
        for LLH in zip(LLH_tpr, LLH_fpr, colors, labels, markers):
            plt.scatter(LLH[0], 1-LLH[1], s=40, marker=LLH[4], c=LLH[2], label='('+format(100*LLH[0],'.1f')
                        +'%, '+format(100*(1-LLH[1]),'.1f')+')'+r'$\rightarrow$'+LLH[3])
        axes.tick_params(axis='both', which='major', labelsize=12)
        plt.legend(loc='upper right', fontsize=15, numpoints=3)
    if ROC_type == 3:
        def make_plot(location):
            plt.xlabel('Signal probability threshold (%)', fontsize=25); plt.ylabel('(%)',fontsize=25)
            val_1 = plt.plot(threshold[1:],   tpr[1:], color='tab:blue'  , label='Signal Efficiency'   , lw=2)
            val_2 = plt.plot(threshold[1:], 1-fpr[1:], color='tab:orange', label='Background Rejection', lw=2)
            val_3 = plt.plot(threshold[1:], accuracy[1:], color='black'  , label='Accuracy', zorder=10 , lw=2)
            for LLH in zip(LLH_tpr, LLH_fpr):
                p1 = plt.scatter(threshold[np.argwhere(tpr>=LLH[0])[0]], LLH[0],
                                 s=40, marker='o', c=val_1[0].get_color())
                p2 = plt.scatter(threshold[np.argwhere(tpr>=LLH[0])[0]], 1-LLH[1],
                                 s=40, marker='o', c=val_2[0].get_color())
            l1 = plt.legend([p1, p2], ['LLH '+sig_eff, 'LLH $1\!-\!$'+bkg_eff], loc='lower left', fontsize=13)
            plt.scatter( best_threshold, max(accuracy), s=40, marker='o', c=val_3[0].get_color(),
                         label='{0:<10s} {1:>5.2f}%'.format('Best accuracy:',100*max(accuracy)), zorder=10 )
            plt.legend(loc=location, fontsize=15, numpoints=3); plt.gca().add_artist(l1)
        best_threshold = threshold[np.argmax(accuracy)]
        plt.figure(figsize=(12,16))
        plt.subplot(2, 1, 1); pylab.grid(True); axes = plt.gca()
        plt.xlim([0, 1]);   plt.xticks(np.arange(0,1.01,0.1)   , np.arange(0,101,10))
        plt.ylim([0.6, 1]); plt.yticks(np.arange(0.6,1.01,0.05), np.arange(60,101,5))
        make_plot('lower center')
        plt.subplot(2, 1, 2); pylab.grid(True); axes = plt.gca()
        x_min=-2; x_max=3; y_min=0.1; y_max=1-1e-4;
        pylab.ylim(y_min, y_max); pylab.xlim(10**x_min, 1-10**(-x_max))
        pos  =                       [  10**float(n)  for n in np.arange(x_min,0)           ]
        pos += [0.5]               + [1-10**float(n)  for n in np.arange(-1,-x_max-1,-1)    ]
        lab  =                       [ '0.'+n*'0'+'1' for n in np.arange(abs(x_min)-3,-1,-1)]
        lab += [1, 10, 50, 90, 99] + ['99.'+n*'9'     for n in np.arange(1,x_max-1)         ]
        plt.xscale('logit'); plt.xticks(pos, lab)
        plt.yscale('logit'); plt.yticks([0.1, 0.5, 0.9, 0.99, 0.999, 0.9999], [10, 50, 90, 99, 99.9, 99.99])
        axes.tick_params(axis='both', which='major', labelsize=12)
        axes.xaxis.set_minor_formatter(plt.NullFormatter())
        axes.yaxis.set_minor_formatter(plt.NullFormatter())
        make_plot('upper center')
    if ROC_type == 4:
        best_tpr = tpr[np.argmax(accuracy)]
        plt.xlim([60, 100.0])
        plt.ylim([80, 100.0])
        plt.xticks(np.arange(60,101,step=5))
        plt.yticks(np.arange(80,101,step=5))
        plt.xlabel('Signal Efficiency (%)',fontsize=25)
        plt.ylabel('(%)',fontsize=25)
        plt.plot(100*tpr[1:], 100*(1-fpr[1:]), label='Background rejection', color='darkorange', lw=2)
        val = plt.plot(100*tpr[1:], 100*accuracy[1:], label='Accuracy', color='black', lw=2, zorder=10)
        plt.scatter( 100*best_tpr, 100*max(accuracy), s=40, marker='o', c=val[0].get_color(),
                     label="{0:<10s} {1:>5.2f}%".format('Best accuracy:',100*max(accuracy)), zorder=10 )
        plt.legend(loc='lower center', fontsize=15, numpoints=3)
    file_name = output_dir+'/ROC'+str(ROC_type)+'_curve.png'
    print('Saving test sample ROC'+str(ROC_type)+' curve to   :', file_name); plt.savefig(file_name)
Example #9
0
def draw_guidestarplot(filelist, title=None, plot_filename=None):

    #
    # Start plot
    #
    fig = plot.figure()
    fig.set_facecolor('#e0e0e0')
    fig.canvas.set_window_title(
        title if title is not None else "Guide Star details")
    fig.suptitle(
        title if title is not None else "Guide Star details")
    #
    # Set all plot dimensions
    #
    plot_width = 0.73
    ax_flux = plot.axes([0.10, 0.57, 0.73, 0.36])
    ax_fwhm = plot.axes([0.10, 0.20, 0.73, 0.36])

    hist_flux = plot.axes([0.84, 0.57, 0.14, 0.36])
    hist_fwhm = plot.axes([0.84, 0.20, 0.14, 0.36])

    # 
    # Set all other plot-specific properties
    #

    null_fmt = plot.NullFormatter()

    # Top panel
    ax_flux.grid(True)
    #ax_flux.set_xlabel("time [seconds]")
    ax_flux.set_ylabel("normalized flux")
    ax_flux.xaxis.set_major_formatter(null_fmt)
    ax_fwhm.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%.1f"))
    
    # Bottom panel
    ax_fwhm.grid(True)
    ax_fwhm.set_xlabel("time [seconds]")
    ax_fwhm.set_ylabel("FWHM [arcsec]")
    ax_fwhm.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%.1f"))

    hist_fwhm.xaxis.set_major_formatter(null_fmt)
    hist_fwhm.yaxis.set_major_formatter(null_fmt)
    hist_flux.xaxis.set_major_formatter(null_fmt)
    hist_flux.yaxis.set_major_formatter(null_fmt)

    # from matplotlib.patches import Rectangle
    # currentAxis = fig.canvas
    # currentAxis.add_patch(Rectangle((0, 0), 1, 0.10, facecolor="white", fill=True))

    global_min_flux = 1e9
    shutter_delay = 1.25
    pixelscale = 0.118

    max_time = -1.

    all_flux = []
    all_fwhm = []

    colors = ['blue', 
              'red', 
              'green',
              'purple', 
              'orange', 
              'dimgrey',
              'sienna', ]

    text_positions = numpy.array([
        [0.01, 0.05],
        [0.01, 0.01],
        [0.50, 0.05],
        [0.50, 0.01],
        [0.99, 0.05],
        [0.99, 0.01],
        ])
    text_align = ['left', 'left', 'center', 'center', 'right', 'right']

    guidestats = {}
    if (len(filelist) <= 0):
        return guidestats

    for idx, infile in enumerate(filelist):
        
        hdulist = pyfits.open(infile)

        data = hdulist['VideoPhotometry'].data
        timestamp = data['TimeStamp']
        flux = data["ApertureBasedFluxDN"]
        
        fwhm_x = data["FWHMX"]
        fwhm_y = data["FWHMY"]
        fwhm = numpy.hypot(fwhm_x, fwhm_y)

        # correct timestamp to seconds since exposure start
        timestamp = (timestamp - numpy.min(timestamp)) / 1000.
        
        shutter_open = (timestamp > shutter_delay) & \
                       (timestamp < (numpy.max(timestamp) - shutter_delay))

        flux = flux[shutter_open]
        fwhm = fwhm[shutter_open] * pixelscale
        timestamp = timestamp[shutter_open]

        min_flux, max_flux = numpy.min(flux), numpy.max(flux)
        global_min_flux = numpy.min([global_min_flux, min_flux/max_flux])
        max_time = numpy.max([max_time, numpy.max(timestamp)])

        min_fwhm, max_fwhm = numpy.min(fwhm), numpy.max(fwhm)

        #ax_fwhm.plot(timestamp, fwhm)
        ax_fwhm.scatter(timestamp, fwhm, 
                        linewidth=0, alpha=0.3,
                        c=colors[idx])

        #ax_flux.plot(timestamp, flux/max_flux)
        ax_flux.scatter(timestamp, flux/max_flux,
                        linewidth=0, alpha=0.3,
                        c=colors[idx])

        #
        # Plot some smooth curves through the data points
        #
        spline_t = numpy.linspace(timestamp[1], timestamp[-2], timestamp[-1]/5.)

        # smooth_flux = scipy.interpolate.UnivariateSpline(
        #     x=timestamp, y=flux/max_flux, w=None, bbox=[None, None], k=3, 
        #     s=0.3)
        smooth_flux = scipy.interpolate.LSQUnivariateSpline(
            x=timestamp, y=flux/max_flux, t=spline_t, k=3)
        ax_flux.plot(timestamp, smooth_flux(timestamp),
                     color=colors[idx])

        smooth_fwhm = scipy.interpolate.UnivariateSpline(
            x=timestamp, y=fwhm, k=3) #, w=None, bbox=[None, None], s=0.8*fwhm.shape[0])

        smooth_fwhm = scipy.interpolate.LSQUnivariateSpline(
            x=timestamp, y=fwhm, 
            t=spline_t,
            )
        #ax_fwhm.scatter(spline_t, smooth_fwhm(spline_t))
        #ax_fwhm.plot(spline_t, smooth_fwhm(spline_t), linewidth=5)
        ax_fwhm.plot(timestamp, smooth_fwhm(timestamp), 
                     #linewidth=4,
                     color=colors[idx])


        #print fwhm

        #
        # Make histograms
        #

        # h_flux_count, h_flux = numpy.histogram(flux/max_flux, bins=15, range=[min_flux/max_flux, 1.0])
        # h_flux_pos = 0.5*(h_flux[:-1] + h_flux[1:])
        # print h_flux
        # hist_flux.plot(h_flux_count, h_flux_pos,
        #              color=colors[idx])

        # h_fwhm_count, h_fwhm = numpy.histogram(fwhm, bins=15, range=[min_fwhm, max_fwhm])
        # h_fwhm_pos = 0.5*(h_fwhm[:-1] + h_fwhm[1:])
        # print h_fwhm
        #hist_fwhm.plot(h_fwhm_count, h_fwhm_pos,
        #             color=colors[idx])

        flux_range = (max_flux - min_flux)/max_flux
        x_flux = numpy.linspace(min_flux/max_flux-0.05*flux_range, 1.+0.05*flux_range, 100)
        density_flux = gaussian_kde(flux/max_flux)
        #density_flux.covariance_factor = lambda : .1
        density_flux._compute_covariance()
        peak_flux = 1. #numpy.max(density_flux(x_flux))
        hist_flux.plot(density_flux(x_flux)/peak_flux, x_flux, "-", color=colors[idx])

        fwhm_range = max_fwhm - min_fwhm
        x_fwhm = numpy.linspace(min_fwhm-0.05*fwhm_range, max_fwhm+0.05*fwhm_range, 100)
        density_fwhm = gaussian_kde(fwhm)
        #density_fwhm.covariance_factor = lambda : .1
        density_fwhm._compute_covariance()
        peak_fwhm = 1. #numpy.max(density_fwhm(x_fwhm))
        hist_fwhm.plot(density_fwhm(x_fwhm)/peak_fwhm, x_fwhm, "-", color=colors[idx])


        #
        #
        #
        all_flux.append(flux/max_flux)
        all_fwhm.append(fwhm)

        s2p = lambda x : 0.5*(1+scipy.special.erf(x/numpy.sqrt(2)))*100
        #print s2p([-3,-1,1,3])
        try:
            sigmas = scipy.stats.scoreatpercentile(flux/max_flux, s2p([-3,-1,1,3]))
            sigma1 = sigmas[2] - sigmas[1]
            sigma3 = sigmas[3] - sigmas[0]
        except:
            sigma1, sigma3 = -1., -1.
            pass
        txt = u'GS %d: 1\u03C3=%.3f - 3\u03C3=%.3f' % (
            idx+1, sigma1, sigma3)
        fig.text(text_positions[idx,0], text_positions[idx,1], 
                 txt,
                 horizontalalignment=text_align[idx])

        #hist_fwhm.hist(flux/max_flux, bins=15, orientation='horizontal')
        #axHisty.hist(y, bins=bins, orientation='horizontal')

        one_return = {
            'fwhm_min': min_fwhm,
            'fwhm_max': max_fwhm,
            'flux_min': min_flux,
            'flux_max': max_flux,
            'flux_1sigma': sigma1,
            'flux_3sigma': sigma3,
            'n_guide_samples': fwhm.shape[0]
        }
        guidestats[infile] = one_return

    #
    # Determine and set all axis limits
    #
    all_flux = numpy.array(all_flux)
    all_fwhm = numpy.array(all_fwhm)
    #print all_fwhm.shape
    #numpy.savetxt("fwhm", all_fwhm.reshape((-1,1)))

    try:
        _min_flux, _max_flux = numpy.min(all_flux), 1.0 # normalized !
    except:
        _min_flux, _max_flux = 0.0, 1.0
    ax_flux.set_ylim((_min_flux-0.05, 1.05))
    hist_flux.set_ylim((_min_flux-0.05, 1.05))

    try:
        _min_fwhm, _max_fwhm = numpy.min(all_fwhm), numpy.max(all_fwhm)
    except:
        _min_fwhm, _max_fwhm = 0.0, limit_fwhm_max
    #print _min_fwhm, _max_fwhm
    if (_max_fwhm > limit_fwhm_max): _max_fwhm = limit_fwhm_max
    _range_fwhm = _max_fwhm - _min_fwhm
    ax_fwhm.set_ylim((_min_fwhm-0.05*_range_fwhm, _max_fwhm+0.05*_range_fwhm))
    hist_fwhm.set_ylim((_min_fwhm-0.05*_range_fwhm, _max_fwhm+0.05*_range_fwhm))

    d_time = numpy.max([2, 0.03*max_time])

    ax_flux.set_xlim((0-d_time, max_time+d_time))
    ax_fwhm.set_xlim((0-d_time, max_time+d_time))

    hist_flux.set_ylim((global_min_flux-0.05, 1.05))

    if (plot_filename is not None):
        fig.set_size_inches(8,6)
        fig.savefig(plot_filename, dpi=100,
                    facecolor=fig.get_facecolor(), edgecolor='none')
    else:
        fig.show()
        plot.show()
        pass


    # catfile = infile[:-5]+".cat"

    # sex_cmd = """

    # %(sex)s -c %(qr_base)s/config/wcsfix.sex
    # -PARAMETERS_NAME %(qr_base)s/config/wcsfix.sexparam
    # -CATALOG_NAME %(catfile)s
    # %(infile)s """ % {
    #     'sex': sitesetup.sextractor,
    #     'qr_base': qr_dir,
    #     'catfile': catfile,
    #     'infile': infile,
    # }
    # print " ".join(sex_cmd.split())
    # os.system(" ".join(sex_cmd.split()))

    return guidestats
Example #10
0
    def compare_aggregated_values(self, profiles, strfile=None):
        '''
        Validates the measurements of power vs. energetic measurements in
        hourly resolution. Plots their data quality as a scatter plot and
        saves percentiles to excel files.

        Parameters
        ----------
        profiles : pd.DataFrame
            A dataframe containing both measurement streams.
        strfile : str, optional
            The directory or filename of the plot. If it is a filename,
            filename is overwritten by strfile. If it is a directory, the plot
            is stored in this directory. If it is None, the plot is shown and
            not saved. The default is None.

        Returns
        -------

        '''
        print(dt.now().strftime("%m/%d/%Y, %H:%M:%S")
              + ' Plotting P vs. E overview.')
        fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(14, 17))
        y_fig = 0.68
        plt.subplots_adjust(bottom=1 - y_fig, top=0.97, right=0.95, left=0.07)
        # timesteps with nans are irrelevant for the plot, but we can't drop
        # the whole line. Fill them with a negative value and set axis limits
        # to 0 later
        profiles = profiles.fillna(-1)
        feed_dict = {
            'HOUSEHOLD': 0,
            'HEATPUMP': 1,
            'TRANSFORMER': 2
            }
        current_dict = {
            'S': 0,
            'P': 1,
            'Q': 2
            }
        objs = profiles.columns.get_level_values(1).unique()
        colors = cm.brg(np.linspace(0, 1, len(objs)))
        perc_steps = [1, 5, 25, 75, 95, 99]
        perc_df = pd.DataFrame(
            index=perc_steps, columns=pd.MultiIndex.from_product(
                [objs.append(pd.Index(['All'])),
                 profiles.columns.get_level_values(2).unique(),
                 profiles.columns.get_level_values(3).unique()]))
        # object specific
        for obj, c in zip(objs, colors):
            df = profiles.xs(obj, level=1, axis=1).copy()
            df = df.droplevel(3, axis=1)
            for feed in df.columns.get_level_values(1).unique():
                axis = axes[feed_dict[feed]]
                dff = df.xs(feed, axis=1, level=1)
                for current in dff.columns.get_level_values(1).unique():
                    ax = axis[current_dict[current]]
                    # calculate percentiles
                    x = dff.loc[:, idx['Power', current]].to_numpy().ravel()
                    y = dff.loc[:, idx['Energy', current]].to_numpy().ravel()
                    nonzeros = np.where(y > 0)[0]
                    try:
                        perc = np.percentile(x[nonzeros] / y[nonzeros],
                                             perc_steps)
                        perc_df.loc[perc_steps, idx[obj, feed, current]] = \
                            perc
                    except IndexError:
                        print(dt.now().strftime("%m/%d/%Y, %H:%M:%S")
                              + f'\tValues for object {obj}, feed {feed} and '
                              f'current {current} are empty or zero. '
                              'Skipping..')
                    # plot
                    ax.scatter(x=dff.loc[:, idx['Power', current]],
                               y=dff.loc[:, idx['Energy', current]],
                               marker='x', color=c, label=obj)
        
        # get the overall percentiles per feed and current
        for feed, current in profiles.columns.droplevel(
                ['measure', 'obj', 'phase']).unique():
            x = (profiles.loc[:, idx['Power', :, feed, current, :]]
                 .to_numpy().ravel())
            y = (profiles.loc[:, idx['Energy', :, feed, current, :]]
                 .to_numpy().ravel())
            nonzeros = np.where(y > 0)[0]
            perc = np.percentile(x[nonzeros] / y[nonzeros], perc_steps)
            perc_df.loc[perc_steps, idx['All', feed, current]] = perc
        # global legend
        handles_labels = [ax.get_legend_handles_labels() for ax in fig.axes]
        handles, labels = [sum(lol, []) for lol in zip(*handles_labels)]
        unique = [(h, l) for i, (h, l) in enumerate(zip(handles, labels))
                  if l not in labels[:i]]
        fig.legend(*zip(*unique), ncol=5, loc='upper left',
                   bbox_to_anchor=(0.07, 1 - y_fig - 0.07))
        # set y labels
        for feed in profiles.columns.get_level_values(2).unique():
            ax = axes[feed_dict[feed], 2]
            axt = ax.twinx()
            axt.set_ylabel(feed, rotation=90, labelpad=15)
            axt.yaxis.set_major_formatter(plt.NullFormatter())
            axt.yaxis.set_minor_locator(plt.NullLocator())
        for ax in axes.flatten():
            ax.tick_params(axis='y', rotation=45)
            ax.yaxis.set_major_locator(mticker.MaxNLocator(3))
        # set column titles
        for current in profiles.columns.get_level_values(3).unique():
            ax = axes[0, current_dict[current]]
            ax.set_title(current)
        # set lower left point to (0, 0). Additionally, set upper ylim to the
        # upper xlim. Energy measurements sometimes produce errors themselves,
        # but we only want to look at power measurements. Therefore, let power
        # measurements be the leader concerning axis limits
        for ax in axes.flatten():
            ax.set_xlim(left=0)
            ax.set_ylim(bottom=0, top=ax.get_xlim()[1])
            ax.plot(ax.get_xlim(), ax.get_ylim(), ls='--', c='.3')
        fig.text(0, 1 - y_fig / 2,
                 r'$\frac{E(t_2)-E(t_1)}{t_2-t_1}$ in kWh/h', va='center',
                 rotation='vertical', fontsize=22
        )
        fig.text(
            0.5, 1 - y_fig - 0.05,
            r'$\frac{1}{t_2-t_1} \int_{t_1}^{t_2} P(t) \,dt$ in kWh/h',
            ha='center', fontsize=22
        )
        perc_df.unstack().reset_index().to_excel(
            os.path.join(strfile, 'ys_rel_percentiles.xlsx'))
        # percentiles of actual hourly consumption
        consumption = (profiles.drop('ES1', level=1, axis=1)
                       .drop('Q', axis=1, level=3).xs('TOT', axis=1, level=4)
                       .xs('Power', level=0, axis=1)
        )
        perc_steps = [1, 5, 25, 50, 75, 95, 99]
        perc_df = pd.Series(np.percentile(consumption, perc_steps),
                            index=perc_steps)
        perc_df.to_excel(os.path.join(strfile, 'hs_abs_PandS_percentiles.xlsx'))
        if strfile:
            if os.path.isdir(strfile):
                strfile = os.path.join(strfile, 'OVERVIEW_P_VS_E_SCATTER.png')
            if os.path.isfile(strfile):
                os.remove(strfile)
            plt.savefig(strfile, dpi=300)
            plt.close()
        else:
            plt.show()
Example #11
0
def plotMazePolicy(
    agents, bs, gx, gy, w, h, **kwargs
):  #show and save default to false. title, colorbar-label, filename
    fig, ax1 = plt.subplots()
    COLOUR_PALATINATE = "#72246C"

    # make grid
    minor_locator1 = AutoMinorLocator(2)
    minor_locator2 = FixedLocator([j for j in range(h)])
    plt.gca().xaxis.set_minor_locator(minor_locator1)
    plt.gca().yaxis.set_minor_locator(minor_locator2)
    for i in range(7):
        plt.plot([i, i], [0, 3], lw=0.5, alpha=0.5, c='grey')
    for i in range(4):
        plt.plot([0, 8], [i, i], lw=0.5, alpha=0.5, c='grey')

    if 'title' in kwargs and kwargs['title'] != None:
        ax1.set_title(kwargs['title'])

    onGoal = False
    # Shade and label agents
    for i, (sx, sy) in zip([1, 2, 3], agents):
        if sy == 3: onGoal = True
        ax1.text(sx + 0.3,
                 sy + 0.4,
                 '$A_' + str(i) + '$',
                 color=COLOUR_PALATINATE,
                 fontsize=20)
        fill([sx, sx + 1, sx + 1, sx], [sy, sy, sy + 1, sy + 1],
             COLOUR_PALATINATE,
             alpha=0.4,
             edgecolor=COLOUR_PALATINATE)

    # Shade and label goal cell
    if not onGoal:
        ax1.text(gx + 0.18, gy + 0.4, 'Goal', color="g", fontsize=15)
        fill([gx, gx + 1, gx + 1, gx], [gy, gy, gy + 1, gy + 1],
             'g',
             alpha=0.3,
             edgecolor='g')

    # Make obstacles:
    # for (x, y) in obstacles:
    #     fill([x,x+1,x+1,x], [y,y,y+1,y+1], 'k', alpha=0.2, edgecolor='k')
    # for (x, y) in obstacles:
    b1 = [4, 7, 7, 4]
    b2 = [0, 3, 3, 0]
    if bs == 0:
        b2 = [1, 4, 4, 1]
    elif bs == 6:
        b1 = [3, 6, 6, 3]
    fill(b1, [3, 3, 4, 4], 'k', alpha=0.55, edgecolor='k')
    fill(b2, [3, 3, 4, 4], 'k', alpha=0.55, edgecolor='k')

    bs = [1, 5] if bs == 3 else [2, 5] if bs == 0 else [1, 4]
    for i, x in zip([1, 2], bs):
        ax1.text(x + 0.3, 3.4, '$B_' + str(i) + '$', color="k", fontsize=20)

    # make grid lines on center
    plt.xticks([0.5 + i for i in range(w)], [i for i in range(w)])
    plt.yticks([0.5 + i for i in range(h)], [i for i in range(h)])
    plt.xlim(0, w)
    plt.ylim(0, h)

    ax1.yaxis.set_major_locator(plt.NullLocator())
    ax1.xaxis.set_major_formatter(plt.NullFormatter())

    plt.tight_layout()

    if 'filename' in kwargs:
        plt.savefig(kwargs['filename'], dpi=kwargs['dpi'])
    plt.close(fig)
Example #12
0
def main():
    start_time = time.time()

    ## Generate model graph ##
    if args.graph == 'er':  # Erdős-Rényi model, Random graph
        n = 5000  # node
        p = 0.04  # probability of edge generation
        seed = 1234
        print(f'Graph type: Erdős-Rényi model\n'
              f'# n: {n}\n'
              f'# p: {p}\n'
              f'# seed: {seed}\n'
              f'Generating graph...')
        G = nx.fast_gnp_random_graph(n=n, p=p, seed=seed, directed=False)

    elif args.graph == 'ws':  # Watts–Strogatz model, Small-world graph
        n = 5000  # node
        k = 20  # the number of neighbor node to connect with respect to every node
        p = 0.01  # re-wiring probability. Generate random graph when p=1.
        seed = 1234
        print(f'Graph type: Watts–Strogatz model\n'
              f'# n: {n}\n'
              f'# k: {k}\n'
              f'# p: {p}\n'
              f'# seed: {seed}\n'
              f'Generating graph...')
        G = nx.watts_strogatz_graph(n=n, k=k, p=p, seed=seed)

    elif args.graph == 'ba':  # Barabási–Albert model, Scale-free graph
        n = 5000  # node
        m = 10  # the number of new edge to wire with the existing nodes
        seed = 1234
        print(f'Graph type: Barabási–Albert model\n'
              f'# n: {n}\n'
              f'# m: {m}\n'
              f'# seed: {seed}\n'
              f'Generating graph...')
        G = nx.barabasi_albert_graph(n=n, m=m, seed=seed)

    else:
        print('[ERROR] You need to select model graph.')

    ## Show graph summary ##
    print(
        f"-- graph summary --\n"
        f"# nodes: {nx.number_of_nodes(G)}\n"
        f"# edges: {nx.number_of_edges(G)}\n"
        f"# connected components: {nx.number_connected_components(G)}\n"
        f"# average shortest path: {nx.average_shortest_path_length(G)}\n"
        f"# average clustering coefficient: {nx.average_clustering(G)}\n"
        f"# degree assortativity coefficient: {nx.degree_assortativity_coefficient(G)}\n"
        f"# graph diameter: {nx.diameter(G)}\n"
        f"# graph density: {nx.density(G)}")

    ## Calculate average degree ##
    deg = []
    for k, l in G.degree():  # {node: degree, ...}
        deg.append(l)
    average_degree = sum(deg) / len(deg)
    print(f'# average degree: {average_degree}')

    ## Export generated graph as tsv file ##
    edge_type = "interaction"  # Tentative edge type name
    with open(args.output, "w") as fp:
        for e in G.edges():
            fp.write(str(e[0]) + "\t" + edge_type + "\t" + str(e[1]) + "\n")
    print(f'[SAVE] graph file: {args.output}')

    ## Calculate degree distribution probability ##
    pmf = ts.Pmf(deg)  # {degree: probability, ...}
    # print(f'pmf mean: {pmf.Mean()}, pmf std: {pmf.Std()}')
    pmf_degree = []  # degree
    pmf_prob = []  # degree distribution probability
    for i in pmf:
        pmf_degree.append(i)
        pmf_prob.append(pmf[i])

    ## power law fitting using mudule ##
    print(f'--- power law fitting parameter ---')
    np.seterr(divide='ignore', invalid='ignore')  # a magical spell
    fit = powerlaw.Fit(
        deg,
        discrete=True)  # fitting degree distribution probability to linear
    R, p = fit.distribution_compare('power_law', 'exponential')
    print(f'# power law gamma: {fit.power_law.alpha}\n'
          f'# gammma standard error(std): {fit.power_law.sigma}\n'
          f'# fix x min: {fit.fixed_xmin}\n'
          f'# discrete: {fit.discrete}\n'
          f'# x min: {fit.xmin}\n'
          f'# loglikelihood ratio: {R}\n'
          f'# significant value: {p}')

    ## Plot degree distbibution probability (normal scale) ##
    fig = plt.figure(figsize=(8, 6), tight_layout=True)
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['top'].set_linewidth(1)
    ax.spines['bottom'].set_linewidth(1)
    ax.spines['left'].set_linewidth(1)
    ax.spines['right'].set_linewidth(1)

    ax.scatter(pmf_degree, pmf_prob, c='black', s=30, alpha=1, linewidths=0)
    ax.tick_params(direction='out',
                   which='major',
                   axis='both',
                   length=4,
                   width=1,
                   labelsize=20,
                   pad=10)
    ax.set_xlabel('k', fontsize=25, labelpad=10)
    ax.set_ylabel('P(k)', fontsize=25, labelpad=10)
    deg_fig_name = args.fig + '_degdist_plot.pdf'
    plt.savefig(deg_fig_name, dpi=300, format='pdf', transparent=True)
    plt.clf()
    print(f'[SAVE] degree distribution figure: {deg_fig_name}')

    ## Plot degree distbibution probability (log scale) ##
    fig = plt.figure(figsize=(6, 6), tight_layout=True)
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['top'].set_linewidth(1)
    ax.spines['bottom'].set_linewidth(1)
    ax.spines['left'].set_linewidth(1)
    ax.spines['right'].set_linewidth(1)
    ax.set_xscale('log', base=10)
    ax.set_yscale('log', base=10)
    ax.xaxis.set_major_locator(
        ticker.LogLocator(base=10.0))  # Set x axis major tick for log10
    ax.yaxis.set_major_locator(
        ticker.LogLocator(base=10.0))  # Set y axis major tick for log10
    ax.xaxis.set_minor_formatter(
        ticker.NullFormatter())  # Set x axis minor tick unvisible
    # ax.xaxis.set_minor_formatter(ticker.ScalarFormatter()) # Set x axis minot tick as integ, Activate only for WS
    ax.yaxis.set_minor_formatter(
        ticker.NullFormatter())  # Set y axis minor tick unvisible

    ax.scatter(pmf_degree, pmf_prob, c='black', s=30,
               linewidths=0)  # plot probability of degree distribution
    if R > 0:
        fit.power_law.plot_pdf(c='#766AFF',
                               linestyle='dotted',
                               linewidth=2,
                               alpha=1)  # plot power law fitting

    ax.tick_params(direction='in',
                   which='major',
                   axis='both',
                   length=7,
                   width=1,
                   labelsize=20,
                   pad=10)  # Set major tick parameter
    ax.tick_params(direction='in',
                   which='minor',
                   axis='both',
                   length=4,
                   width=1,
                   labelsize=20,
                   pad=10)  # Set minor tick parameter
    ax.set_xlabel('k', fontsize=25, labelpad=10)
    ax.set_ylabel('P(k)', fontsize=25, labelpad=10)
    ymin = min(pmf_prob)
    ymin_ = pow(10, round(np.log10(ymin))) - pow(10, round(np.log10(ymin) - 1))
    ax.set_ylim(ymin_, )
    log_fig_name = args.fig + '_Pk_plot.pdf'
    fig.savefig(log_fig_name, dpi=300, format='pdf', transparent=True)
    plt.clf()
    print(f'[SAVE] degree distribution figure (log-scale): {log_fig_name}')

    elapsed_time = time.time() - start_time
    print(f'[TIME]: {elapsed_time} sec')
    print(f'Completed!')
Example #13
0
def plot(ifiles, args):
    import matplotlib.pyplot as plt
    from PseudoNetCDF.coordutil import getpresbnds, getsigmabnds
    plt.rcParams['text.usetex'] = False
    from matplotlib.colors import LinearSegmentedColormap, BoundaryNorm, LogNorm, Normalize
    map = not args.nomap
    scale = args.scale
    minmax = eval(args.minmax)
    minmaxq = eval(args.minmaxq)
    sigma = args.sigma
    maskzeros = args.maskzeros
    try:
        f, = ifiles
    except Exception:
        raise ValueError(
            'curtain plot expects one file when done. Try stack time --stack=time to concatenate')

    if sigma:
        vertcrd = getsigmabnds(f)
    else:
        vertcrd = getpresbnds(f, pref=101325., ptop=getattr(f, 'VGTOP', 10000))
        if vertcrd.max() > 2000:
            vertcrd /= 100.

    reversevert = not (np.diff(vertcrd) > 0).all()
    for var_name in args.variables:
        temp = defaultdict(lambda: 1)
        try:
            eval(var_name, None, temp)
            var = eval(var_name, None, f.variables)[:]
        except Exception:
            temp[var_name]
            var = f.variables[var_name][:]
        if args.itertime:
            vars = [('time%02d' % vi, v) for vi, v in enumerate(var)]
        else:
            if var.shape[0] != 1:
                parser.print_help()
                sys.stderr.write(
                    '\n*****\n\nFile must have only one time or use the itertime options; to reduce file to one time, see the --slice and --reduce operators\n\n')
                exit()
            vars = [('', var[0])]

        for lstr, var in vars:
            bmap = None
            if maskzeros:
                var = np.ma.masked_values(var, 0)
            vmin, vmax = np.percentile(
                np.ma.compressed(var).ravel(), list(minmaxq))
            if minmax[0] is not None:
                vmin = minmax[0]
            if minmax[1] is not None:
                vmax = minmax[1]

            if args.normalize is not None:
                norm = eval(args.normalize)
                if norm.scaled():
                    vmin = norm.vmin
                    vmax = norm.vmax
            else:
                if scale == 'log':
                    bins = np.logspace(np.log10(vmin), np.log10(vmax), 11)
                elif scale == 'linear':
                    bins = np.linspace(vmin, vmax, 11)
                elif scale == 'deciles':
                    bins = np.percentile(np.ma.compressed(np.ma.masked_greater(np.ma.masked_less(var, vmin).view(
                        np.ma.MaskedArray), vmax)).ravel(), [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
                    bins[0] = vmin
                    bins[-1] = vmax
                norm = BoundaryNorm(bins, ncolors=256)

            if map:
                fig = plt.figure(figsize=(8, 8))
                fig.subplots_adjust(hspace=.3, wspace=.3)
                axmap = fig.add_subplot(3, 3, 5)
                try:
                    cmaqmap = getmap(f, resolution=args.resolution)
                    cmaqmap.drawcoastlines(ax=axmap)
                    cmaqmap.drawcountries(ax=axmap)
                    if not args.states:
                        cmaqmap.drawstates(ax=axmap)
                    if args.counties:
                        cmaqmap.drawcounties(ax=axmap)
                    cmaqmap.drawparallels(
                        np.arange(-90, 100, 10), labels=[True, True, False, False], ax=axmap)
                    cmaqmap.drawmeridians(
                        np.arange(-180, 190, 20), labels=[False, False, True, True], ax=axmap)
                except Exception as e:
                    warn('An error occurred and no map will be shown:\n%s' % str(e))
                axn = fig.add_subplot(3, 3, 2, sharex=axmap)
                axw = fig.add_subplot(3, 3, 4, sharey=axmap)
                axe = fig.add_subplot(3, 3, 6, sharey=axmap)
                axs = fig.add_subplot(3, 3, 8, sharex=axmap)
                cax = fig.add_axes([.8, .7, .05, .25])
                for ax in [axmap, axe]:
                    ax.yaxis.set_major_formatter(plt.NullFormatter())
                for ax in [axmap, axn]:
                    ax.xaxis.set_major_formatter(plt.NullFormatter())
                for ax in [axn, axs]:
                    if sigma:
                        ax.set_ylabel('sigma')
                    else:
                        ax.set_ylabel('pressure')
                for ax in [axe, axw]:
                    if sigma:
                        ax.set_xlabel('sigma')
                    else:
                        ax.set_xlabel('pressure')
                xyfactor = 1
            else:
                fig = plt.figure(figsize=(16, 4))
                fig.subplots_adjust(bottom=0.15)
                axw = fig.add_subplot(1, 4, 1)
                axn = fig.add_subplot(1, 4, 2)
                axe = fig.add_subplot(1, 4, 3)
                axs = fig.add_subplot(1, 4, 4)
                cax = fig.add_axes([.91, .1, .025, .8])
                if sigma:
                    axw.set_ylabel('sigma')
                else:
                    axw.set_ylabel('pressure')

                xyfactor = 1e-3  # m -> km

            x = f.NCOLS + 1
            y = f.NROWS + 1
            start_south = 0
            end_south = start_south + x
            start_east = end_south
            end_east = start_east + y
            start_north = end_east
            end_north = start_north + x
            start_west = end_north
            end_west = start_west + y
            X, Y = np.meshgrid(np.arange(x + 1) * f.XCELL * xyfactor, vertcrd)
            # patchess =
            axs.pcolor(
                X, Y, var[:, start_south:end_south], cmap=bmap, vmin=vmin, vmax=vmax, norm=norm)
            if not map:
                if reversevert:
                    axs.set_ylim(*axs.get_ylim()[::-1])
                axs.set_title('South')
                axs.set_xlabel('E to W km')
                axs.set_xlim(*axs.get_xlim()[::-1])
            else:
                if not reversevert:
                    axs.set_ylim(*axs.get_ylim()[::-1])

            X, Y = np.meshgrid(np.arange(-1, x) * f.XCELL * xyfactor, vertcrd)
            # patchesn =
            axn.pcolor(
                X, Y, var[:, start_north:end_north], cmap=bmap, vmin=vmin, vmax=vmax, norm=norm)
            if reversevert:
                axn.set_ylim(*axn.get_ylim()[::-1])
            if not map:
                axn.set_title('North')
                axn.set_xlabel('W to E km')

            if map:
                X, Y = np.meshgrid(vertcrd, np.arange(y + 1) * f.YCELL)
                # patchese =
                axe.pcolor(X, Y, var[:, start_east:end_east].swapaxes(
                    0, 1), cmap=bmap, vmin=vmin, vmax=vmax, norm=norm)
                if reversevert:
                    axe.set_xlim(*axe.get_xlim()[::-1])
            else:
                X, Y = np.meshgrid(np.arange(y + 1) *
                                   f.YCELL * xyfactor, vertcrd)
                # patchese =
                axe.pcolor(
                    X, Y, var[:, start_east:end_east], cmap=bmap, vmin=vmin, vmax=vmax, norm=norm)
                if reversevert:
                    axe.set_ylim(*axe.get_ylim()[::-1])
                axe.set_title('East')
                axe.set_xlabel('N to S km')
                axe.set_xlim(*axe.get_xlim()[::-1])
            if map:
                X, Y = np.meshgrid(vertcrd, np.arange(-1, y) * f.YCELL)
                patchesw = axw.pcolor(X, Y, var[:, start_west:end_west].swapaxes(
                    0, 1), cmap=bmap, vmin=vmin, vmax=vmax, norm=norm)
                if not reversevert:
                    axw.set_xlim(*axw.get_xlim()[::-1])
            else:
                X, Y = np.meshgrid(np.arange(-1, y) *
                                   f.YCELL * xyfactor, vertcrd)
                patchesw = axw.pcolor(
                    X, Y, var[:, start_west:end_west], cmap=bmap, vmin=vmin, vmax=vmax, norm=norm)
                if reversevert:
                    axw.set_ylim(*axw.get_ylim()[::-1])
                axw.set_title('West')
                axw.set_xlabel('S to N km')
            if map:
                for ax in [axe, axw]:
                    ax.axis('tight', axis='x')
                    plt.setp(ax.xaxis.get_majorticklabels(), rotation=90)
                for ax in [axs, axn]:
                    ax.axis('tight', axis='y')
            else:
                for ax in [axe, axn, axw, axs] + ([axmap] if map else []):
                    ax.axis('tight')

            if 'TFLAG' in f.variables.keys():
                SDATE = f.variables['TFLAG'][:][0, 0, 0]
                EDATE = f.variables['TFLAG'][:][-1, 0, 0]
                STIME = f.variables['TFLAG'][:][0, 0, 1]
                ETIME = f.variables['TFLAG'][:][-1, 0, 1]
                if SDATE == 0:
                    SDATE = 1900001
                    EDATE = 1900001
                try:
                    sdate = datetime.strptime(
                        '%07d %06d' % (SDATE, STIME), '%Y%j %H%M%S')
                    edate = datetime.strptime(
                        '%07d %06d' % (EDATE, ETIME), '%Y%j %H%M%S')
                except Exception as e:
                    warn('Unable to convert time:' + str(e))
            elif 'tau0' in f.variables.keys():
                sdate = datetime(1985, 1, 1, 0) + \
                    timedelta(hours=f.variables['tau0'][0])
                edate = datetime(1985, 1, 1, 0) + \
                    timedelta(hours=f.variables['tau1'][-1])
            else:
                sdate = datetime(1900, 1, 1, 0)
                edate = datetime(1900, 1, 1, 0)
            try:
                title = '%s %s to %s' % (var_name, sdate.strftime(
                    '%Y-%m-%d'), edate.strftime('%Y-%m-%d'))
            except Exception:
                title = var_name
            fig.suptitle(title.replace('O3', 'Ozone at Regional Boundaries'))
            if var.min() < vmin and var.max() > vmax:
                extend = 'both'
            elif var.min() < vmin:
                extend = 'min'
            elif var.max() > vmax:
                extend = 'max'
            else:
                extend = 'neither'
            fig.colorbar(patchesw, cax=cax, extend=extend)
            cax.set_xlabel('ppm')
            figpath = args.outpath + var_name + lstr + '.' + args.figformat
            fig.savefig(figpath)
            if args.verbose > 0:
                print('Saved fig', figpath)
            plt.close(fig)
Example #14
0
def plot_data(ax, x, y, u, v, label):

    #-- dimensions & size
    xpc, ypc = 0, 0

    ax.set_aspect(1)

    ws = 1.5  # add some white space
    xmin1 = xmin - ws
    xmax1 = xmax + ws

    ax.set_xlim(xmin1, xmax1)
    ax.set_ylim(xmin1, xmax1)

    ##-- draw lines
    #ax.plot([xmin1,xmax1],[0,0],'--k', alpha=0.2)
    #ax.plot([0,0],[xmin1,xmax1],'--k', alpha=0.2)

    #-- cover sphere inside
    c1 = plt.Circle((xpc, ypc), radius=0.3, color='r')
    ax.add_patch(c1)

    #-- draw arrows & add texts
    if label == 'dipole':
        ax.arrow(0, 0, 1, 0, width=0.15, color='r')
        ax.text(2., -0.5, 'F', color='r', fontsize=16)

    #-- velocity

    s = 1  # step
    dxh = 0.

    # direction
    vel = np.sqrt(np.add(np.square(u), np.square(v)))
    #u0 = np.divide(u,vel)
    #v0 = np.divide(v,vel)
    """ The following two lines are more robust """
    u0 = np.divide(u, vel, out=np.zeros_like(u), where=abs(vel) > 1e-9)
    v0 = np.divide(v, vel, out=np.zeros_like(v), where=abs(vel) > 1e-9)

    q0 = ax.quiver(x[::s, ::s],
                   y[::s, ::s],
                   u0[::s, ::s],
                   v0[::s, ::s],
                   scale=15.,
                   width=0.0075,
                   pivot='mid',
                   color=['#A9A9A9'],
                   edgecolors=['#A9A9A9'])  # darkgray
    # magnitude
    q1 = ax.quiver(x[::s, ::s],
                   y[::s, ::s],
                   u[::s, ::s],
                   v[::s, ::s],
                   scale=20,
                   width=0.012,
                   pivot='mid',
                   color=colors[0],
                   edgecolors=colors[0])

    #-- No labels, no ticks

    ax.xaxis.set_major_formatter(plt.NullFormatter())
    ax.yaxis.set_major_formatter(plt.NullFormatter())

    ax.tick_params(bottom='off', top='off', left='off', right='off')

    return ax
Example #15
0
def plot_matrix(mat,
                title=None,
                labels=None,
                figure=None,
                axes=None,
                colorbar=True,
                cmap=plt.cm.RdBu_r,
                tri='full',
                auto_fit=True,
                grid=False,
                reorder=False,
                **kwargs):
    """ Plot the given matrix.

        Parameters
        ----------
        mat : 2-D numpy array
            Matrix to be plotted.
        title : string or None, optional
            A text to add in the upper left corner.
        labels : list, ndarray of strings, empty list, False, or None, optional
            The label of each row and column. Needs to be the same
            length as rows/columns of mat. If False, None, or an
            empty list, no labels are plotted.
        figure : figure instance, figsize tuple, or None
            Sets the figure used. This argument can be either an existing
            figure, or a pair (width, height) that gives the size of a
            newly-created figure.
            Specifying both axes and figure is not allowed.
        axes : None or Axes, optional
            Axes instance to be plotted on. Creates a new one if None.
            Specifying both axes and figure is not allowed.
        colorbar : boolean, optional
            If True, an integrated colorbar is added.
        cmap : matplotlib colormap, optional
            The colormap for the matrix. Default is RdBu_r.
        tri : {'lower', 'diag', 'full'}, optional
            Which triangular part of the matrix to plot:
            'lower' is the lower part, 'diag' is the lower including
            diagonal, and 'full' is the full matrix.
        auto_fit : boolean, optional
            If auto_fit is True, the axes are dimensioned to give room
            for the labels. This assumes that the labels are resting
            against the bottom and left edges of the figure.
        grid : color or False, optional
            If not False, a grid is plotted to separate rows and columns
            using the given color.
        reorder : boolean or {'single', 'complete', 'average'}, optional
            If not False, reorders the matrix into blocks of clusters.
            Accepted linkage options for the clustering are 'single',
            'complete', and 'average'. True defaults to average linkage.

            .. note::
                This option is only available with SciPy >= 1.0.0.

            .. versionadded:: 0.4.1

        kwargs : extra keyword arguments
            Extra keyword arguments are sent to pylab.imshow

        Returns
        -------
        display : instance of matplotlib
            Axes image.
    """
    # we need a list so an empty one will be cast to False
    if isinstance(labels, np.ndarray):
        labels = labels.tolist()
    if labels and len(labels) != mat.shape[0]:
        raise ValueError("Length of labels unequal to length of matrix.")

    if reorder:
        if not labels:
            raise ValueError("Labels are needed to show the reordering.")
        try:
            from scipy.cluster.hierarchy import (linkage,
                                                 optimal_leaf_ordering,
                                                 leaves_list)
        except ImportError:
            raise ImportError("A scipy version of at least 1.0 is needed "
                              "for ordering the matrix with "
                              "optimal_leaf_ordering.")
        valid_reorder_args = [True, 'single', 'complete', 'average']
        if reorder not in valid_reorder_args:
            raise ValueError("Parameter reorder needs to be "
                             "one of {}.".format(valid_reorder_args))
        if reorder is True:
            reorder = 'average'
        linkage_matrix = linkage(mat, method=reorder)
        ordered_linkage = optimal_leaf_ordering(linkage_matrix, mat)
        index = leaves_list(ordered_linkage)
        # make sure labels is an ndarray and copy it
        labels = np.array(labels).copy()
        mat = mat.copy()
        # and reorder labels and matrix
        labels = labels[index].tolist()
        mat = mat[index, :][:, index]

    if tri == 'lower':
        mask = np.tri(mat.shape[0], k=-1, dtype=np.bool) ^ True
        mat = np.ma.masked_array(mat, mask)
    elif tri == 'diag':
        mask = np.tri(mat.shape[0], dtype=np.bool) ^ True
        mat = np.ma.masked_array(mat, mask)
    if axes is not None and figure is not None:
        raise ValueError("Parameters figure and axes cannot be specified "
                         "together. You gave 'figure=%s, axes=%s'" %
                         (figure, axes))
    if figure is not None:
        if isinstance(figure, plt.Figure):
            fig = figure
        else:
            fig = plt.figure(figsize=figure)
        axes = plt.gca()
        own_fig = True
    else:
        if axes is None:
            fig, axes = plt.subplots(1, 1, figsize=(7, 5))
            own_fig = True
        else:
            fig = axes.figure
            own_fig = False
    display = axes.imshow(mat,
                          aspect='equal',
                          interpolation='nearest',
                          cmap=cmap,
                          **kwargs)
    axes.set_autoscale_on(False)
    ymin, ymax = axes.get_ylim()
    if not labels:
        axes.xaxis.set_major_formatter(plt.NullFormatter())
        axes.yaxis.set_major_formatter(plt.NullFormatter())
    else:
        axes.set_xticks(np.arange(len(labels)))
        axes.set_xticklabels(labels, size='x-small')
        for label in axes.get_xticklabels():
            label.set_ha('right')
            label.set_rotation(50)
        axes.set_yticks(np.arange(len(labels)))
        axes.set_yticklabels(labels, size='x-small')
        for label in axes.get_yticklabels():
            label.set_ha('right')
            label.set_va('top')
            label.set_rotation(10)

    if grid is not False:
        size = len(mat)
        # Different grids for different layouts
        if tri == 'lower':
            for i in range(size):
                # Correct for weird mis-sizing
                i = 1.001 * i
                axes.plot([i + 0.5, i + 0.5], [size - 0.5, i + 0.5],
                          color='grey')
                axes.plot([i + 0.5, -0.5], [i + 0.5, i + 0.5], color='grey')
        elif tri == 'diag':
            for i in range(size):
                # Correct for weird mis-sizing
                i = 1.001 * i
                axes.plot([i + 0.5, i + 0.5], [size - 0.5, i - 0.5],
                          color='grey')
                axes.plot([i + 0.5, -0.5], [i - 0.5, i - 0.5], color='grey')
        else:
            for i in range(size):
                # Correct for weird mis-sizing
                i = 1.001 * i
                axes.plot([i + 0.5, i + 0.5], [size - 0.5, -0.5], color='grey')
                axes.plot([size - 0.5, -0.5], [i + 0.5, i + 0.5], color='grey')

    axes.set_ylim(ymin, ymax)

    if auto_fit:
        if labels:
            fit_axes(axes)
        elif own_fig:
            plt.tight_layout(pad=.1,
                             rect=((0, 0, .95, 1) if colorbar else
                                   (0, 0, 1, 1)))

    if colorbar:
        cax, kw = make_axes(axes,
                            location='right',
                            fraction=0.05,
                            shrink=0.8,
                            pad=.0)
        fig.colorbar(mappable=display, cax=cax)
        # make some room
        fig.subplots_adjust(right=0.8)
        # change current axis back to matrix
        plt.sca(axes)

    if title is not None:
        # Adjust the size
        text_len = np.max([len(t) for t in title.split('\n')])
        size = axes.bbox.size[0] / text_len
        axes.text(0.95,
                  0.95,
                  title,
                  horizontalalignment='right',
                  verticalalignment='top',
                  transform=axes.transAxes,
                  size=size)

    return display
Example #16
0
def pdfout(s3d,
           plane,
           smoothx=0,
           smoothy=0,
           name='',
           xmin=0,
           xmax=-1,
           ymin=0,
           ymax=-1,
           errsize=0.5,
           label=None,
           vmin=None,
           vmax=None,
           ra=None,
           dec=None,
           source='',
           ra2=None,
           dec2=None,
           source2='',
           median=None,
           axis='WCS',
           size=(11.5, 9.15),
           psf=None,
           cmap='viridis',
           dy1=0.95,
           dy2=0.97,
           twoc=True,
           norm='lin',
           fs=24):
    """ Simple 2d-image plot function

    Parameters:
    ----------

    plane : np.array
        Either a 2d array for normal images, or a list of three 2d arrays for
        an RGB image
    smoothx, smoothy : integer
        Integer values of gaussian smoothing for the input plane
    xmin, xmax, ymin, ymax : integer
        Zoom into the specific pixel values of the input plane
    errsize : float
        Size of error radius with which to highlight a specific region
    label : str
        Label of region
    ra, dec : sexagesimal
        Location of label
    label2 : str
        Label of region 2
    ra2, dec2 : sexagesimal
        Location of label 2
    median : integer
        median filter the input plane
    axis : string
        WCS axis or none
    psf : float
        FWHM of the PSF which we draw at the left bottom corner of the output
    cmap : string
        Color map, default virdis
    twoc : boolean
        Write text in image in black and white letters
    norm : string
        How to normalize the RGB image (lin, sqrt, log)
    fs : integer
        Fontsize (default 24)
    """

    if xmax == -1:
        xmax = plane.shape[0]
    if ymax == -1:
        ymax = plane.shape[1]

    plane = plane[ymin:ymax, xmin:xmax]

    if twoc == True:
        myeffect = withStroke(foreground="w", linewidth=4)
        kwargs = dict(path_effects=[myeffect])
    else:
        kwargs = {}
#        hfont = {'fontname':'Helvetica'}

    if smoothx > 0:
        plane = blur_image(plane, smoothx, smoothy)
    if median != None:
        plane = sp.ndimage.filters.median_filter(plane,
                                                 median,
                                                 mode='constant')

    if ra != None and dec != None:
        try:
            posx, posy = s3d.skytopix(ra, dec)
        except TypeError:
            posx, posy = s3d.sexatopix(ra, dec)
        if xmin != 0:
            posx -= xmin
        if ymin != 0:
            posy -= ymin

    if ra2 != None and dec2 != None:
        try:
            posx2, posy2 = s3d.skytopix(ra2, dec2)
        except TypeError:
            posx2, posy2 = s3d.sexatopix(ra2, dec2)
        if xmin != 0:
            posx2 -= xmin
        if ymin != 0:
            posy2 -= ymin

    if plane.ndim == 2:
        fig = plt.figure(figsize=size)
        if fs > 20 and axis == 'WCS':
            fig.subplots_adjust(bottom=0.22, top=0.99, left=0.12, right=0.96)
        elif axis == 'WCS':
            fig.subplots_adjust(bottom=0.20, top=0.99, left=0.08, right=0.96)
        else:
            fig.subplots_adjust(bottom=0.005,
                                top=0.995,
                                left=0.005,
                                right=0.94)
    else:
        fig = plt.figure(figsize=(9, 9))
        fig.subplots_adjust(bottom=0.18, top=0.99, left=0.19, right=0.99)
    ax = fig.add_subplot(1, 1, 1)

    ax.set_ylim(5, plane.shape[0] - 5)
    ax.set_xlim(5, plane.shape[1] - 5)

    if norm == 'lin':
        plt.imshow(
            plane,
            vmin=vmin,
            vmax=vmax,  #extent=[],
            cmap=cmap,
            interpolation="nearest")  #, aspect="auto")#, cmap='Greys')

    elif norm == 'log':
        plt.imshow(
            plane,
            vmin=vmin,
            vmax=vmax,  #extent=[],
            cmap=cmap,
            norm=LogNorm(),
            interpolation="nearest")  #, aspect="auto")#, cmap='Greys')

    if plane.ndim == 2:
        bar = plt.colorbar(shrink=0.9, pad=0.01)
        if not label == None:
            bar.set_label(label, size=fs + 15, family='serif')
            bar.ax.tick_params(labelsize=max(24, fs - 4))
        if norm == 'log':
            bar.formatter = plt.LogFormatterMathtext()
        labels = [item.get_text() for item in bar.ax.get_yticklabels()]
        newlab = []
        for label in labels:
            if norm == 'log':
                newl = label.replace('mathdefault', 'mathrm', 1)
            else:
                newl = r'$%s$' % label
            newlab.append(newl)
        bar.ax.set_yticklabels(newlab)


#            bar.update_ticks()

    if psf != None:
        psfrad = psf / 2.3538 / s3d.pixsky
        psfsize = plt.Circle((8 * plane.shape[0] / 9., plane.shape[1] / 9.),
                             psfrad,
                             color='black',
                             alpha=1,
                             **kwargs)
        ax.add_patch(psfsize)
        plt.text(8 * plane.shape[0] / 9.,
                 plane.shape[0] / 6.5,
                 r'PSF',
                 fontsize=fs,
                 ha='center',
                 va='center',
                 **kwargs)

    if ra != None and dec != None:
        psfrad = errsize / 2.3538 / s3d.pixsky
        psfsize = plt.Circle((posx, posy),
                             psfrad,
                             lw=5,
                             fill=False,
                             color='white',
                             **kwargs)
        ax.add_patch(psfsize)
        psfsize = plt.Circle((posx, posy),
                             psfrad,
                             lw=1.5,
                             fill=False,
                             color='black',
                             **kwargs)
        ax.add_patch(psfsize)
        plt.text(posx,
                 posy * dy1,
                 source,
                 fontsize=fs,
                 ha='center',
                 va='top',
                 **kwargs)

    if ra2 != None and dec2 != None:
        #        psfrad = 0.2*errsize/2.3538/s3d.pixsky
        #        psfsize = plt.Circle((posx2,posy2), psfrad, lw=5, fill=False,
        #                             color='white', **kwargs)
        #        ax.add_patch(psfsize)
        #        psfsize = plt.Circle((posx2,posy2), psfrad, lw=1.5, fill=False,
        #                             color='black', **kwargs)
        #        ax.add_patch(psfsize)
        print posx2, posy2
        ax.plot(posx2 + 1, posy2 + 1, 'o', ms=9, mec='black', c='black')
        plt.text(posx2,
                 posy2 * dy2,
                 source2,
                 fontsize=fs,
                 ha='center',
                 va='top',
                 **kwargs)

    if axis == 'WCS':

        [xticks, xlabels], [yticks, ylabels] = _createaxis(s3d, plane)

        sel = (xmin + 5 < xticks) * (xmax - 5 > xticks)
        xticks = xticks[sel]
        xlabels = xlabels[sel]
        xticks -= xmin

        sel = (ymin + 5 < yticks) * (ymax - 5 > yticks)
        yticks = yticks[sel]
        ylabels = ylabels[sel]
        yticks -= ymin

        plt.xticks(rotation=50)
        plt.yticks(rotation=50)

        ax.set_xticks(xticks)
        ax.set_xticklabels(xlabels, size=fs)
        ax.set_yticks(yticks)
        ax.set_yticklabels(ylabels, size=fs)
        ax.set_xlabel(r'Right Ascension (J2000)', size=fs)
        ax.set_ylabel(r'Declination (J2000)', size=fs)

    else:
        ax.yaxis.set_major_formatter(plt.NullFormatter())
        ax.xaxis.set_major_formatter(plt.NullFormatter())

    plt.savefig('%s_%s_%s.pdf' % (s3d.inst, s3d.target, name))
    plt.close(fig)
Example #17
0
def save_dep_heatmap(hists, eventfile):
	dets = 9
	subdets = 4
	print('\nPlotting heat map ...\n')
	sim =  eventfile.split('/')
	sim = sim[len(sim)-1]
	sim = sim.split('entries_')
	sim = sim[len(sim)-1]
	sim = sim.split('.')
	sim = sim[0]
	y_min = 0
	dir = './Plots/%s/' %(sim)
	entries = []
	x = []

	for i in range(dets):
		i_h = i+1
		locals()['hist_%i' %(i_h)] = get_detHist(hists, i)
		for j in range(subdets):
			j_h = j+1
			entries.append(locals()['hist_%i' %(i_h)].GetBinContent(j_h))
		x.append(entries)
		entries=[]
	
	#print(x)

	x_transf = np.zeros((9, 2, 2))
	for i in range(9):
		x_transf[i][0][0] = x[i][0]
		x_transf[i][0][1] = x[i][1]
		x_transf[i][1][0] = x[i][2]
		x_transf[i][1][1] = x[i][3]

	#print(x_transf)

	sns.set()
	asp = x_transf.shape[0]/float(x_transf.shape[1])
	figw = 5
	figh = 5
	cmap= plt.cm.inferno
	norm = matplotlib.colors.Normalize(vmin= 0, vmax= x_transf.max())

	gridspec_kw = {"height_ratios":[2,2,2], "width_ratios" : [2,2,2]}
	heatmapkws = dict(square=False, cbar=False, linewidths=0.0, cmap=cmap, vmin=0, vmax= x_transf.max() ) 
	tickskw =  dict(xticklabels=False, yticklabels=False)

	left = 0.1; right=0.8
	bottom = 0.1; top = 0.9
	fig, axes = plt.subplots(ncols=3, nrows=3, figsize=(figw, figh), gridspec_kw=gridspec_kw)
	plt.subplots_adjust(left=left, right=right,bottom=bottom, top=top, wspace=0.1, hspace=0.1 )

	k=0
	for i in range(3):
		for j in range(3):
			sns.heatmap(x_transf[k], ax=axes[i,j], xticklabels=False, yticklabels=False, **heatmapkws)
			k=k+1
	cax = fig.add_axes([0.85,0.1,0.04,0.8])
	axes1 = fig.add_axes([0.09,0.101,0.72,0.8], frameon=False)
	axes1.grid(None)
	axes1.yaxis.set_major_locator(plt.NullLocator())
	axes1.xaxis.set_major_formatter(plt.NullFormatter())
	axes1.set_xlabel('x')
	axes1.set_ylabel('y')
	sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=norm)
	sm.set_array([])
	clb = fig.colorbar(sm, cax=cax)
	clb.set_label(r'N in $\frac{\mathrm{counts}}{\mathrm{kg\,yr}}$', labelpad=-20, y=1.075, rotation=0)
	plt.savefig('%sheatmap_%s.pdf' %(dir, sim))
Example #18
0
def plot(arrivals,
         plot_type="spherical",
         plot_all=True,
         legend=True,
         label_arrivals=False,
         ax=None,
         show=True,
         plot_one=None,
         color=None):
    """
        Plot the ray paths if any have been calculated.

        :param plot_type: Either ``"spherical"`` or ``"cartesian"``.
            A spherical plot is always global whereas a Cartesian one can
            also be local.
        :type plot_type: str
        :param plot_all: By default all rays, even those travelling in the
            other direction and thus arriving at a distance of *360 - x*
            degrees are shown. Set this to ``False`` to only show rays
            arriving at exactly *x* degrees.
        :type plot_all: bool
        :param legend: If boolean, specify whether or not to show the legend
            (at the default location.) If a str, specify the location of the
            legend. If you are plotting a single phase, you may consider using
            the ``label_arrivals`` argument.
        :type legend: bool or str
        :param label_arrivals: Label the arrivals with their respective phase
            names. This setting is only useful if you are plotting a single
            phase as otherwise the names could be large and possibly overlap
            or clip. Consider using the ``legend`` parameter instead if you
            are plotting multiple phases.
        :type label_arrivals: bool
        :param ax: Axes to plot to. If not given, a new figure with an axes
            will be created. Must be a polar axes for the spherical plot and
            a regular one for the Cartesian plot.
        :type ax: :class:`matplotlib.axes.Axes`
        :param show: Show the plot.
        :type show: bool

        :returns: The (possibly created) axes instance.
        :rtype: :class:`matplotlib.axes.Axes`
        """
    arrivalstmp = []
    for _i in [arrivals[0]]:
        if _i.path is None:
            continue
        dist = _i.purist_distance % 360.0
        distance = _i.distance
        if abs(dist - distance) / dist > 1E-5:
            if plot_all is False:
                continue
            # Mirror on axis.
            _i = copy.deepcopy(_i)
            _i.path["dist"] *= -1.0
        arrivalstmp.append(_i)
    if not arrivalstmp:
        raise ValueError("Can only plot arrivals with calculated ray "
                         "paths.")
    discons = arrivals.model.s_mod.v_mod.get_discontinuity_depths()
    if plot_type == "spherical":
        if not ax:
            plt.figure(figsize=(10, 10))
            if MATPLOTLIB_VERSION < [1, 1]:
                from .matplotlib_compat import NorthPolarAxes
                from matplotlib.projections import register_projection
                register_projection(NorthPolarAxes)
                ax = plt.subplot(111, projection='northpolar')
            else:
                ax = plt.subplot(111, polar=True)
                ax.set_theta_zero_location('N')
                ax.set_theta_direction(-1)
        ax.set_xticks([])
        ax.set_yticks([])
        intp = matplotlib.cbook.simple_linear_interpolation
        radius = arrivals.model.radius_of_planet
        for _i, ray in enumerate(arrivalstmp):
            # Requires interpolation otherwise diffracted phases look
            # funny.
            if color is None:
                ax.plot(intp(ray.path["dist"], 100),
                        radius - intp(ray.path["depth"], 100),
                        color=COLORS[_i % len(COLORS)],
                        label=ray.name,
                        lw=1.0)
            else:
                ax.plot(intp(ray.path["dist"], 100),
                        radius - intp(ray.path["depth"], 100),
                        color=color,
                        label=ray.name,
                        lw=1.0)
        ax.set_yticks(radius - discons)
        ax.xaxis.set_major_formatter(plt.NullFormatter())
        ax.yaxis.set_major_formatter(plt.NullFormatter())
        # Pretty earthquake marker.
        ax.plot([0], [radius - arrivalstmp[0].source_depth],
                marker="*",
                color="#FEF215",
                markersize=20,
                zorder=10,
                markeredgewidth=1.5,
                markeredgecolor="0.3",
                clip_on=False)

        #            # Pretty station marker.
        #            arrowprops = dict(arrowstyle='-|>,head_length=0.8,head_width=0.5',
        #                              color='#C95241',
        #                              lw=1.5)
        #            ax.annotate('',
        #                        xy=(np.deg2rad(distance), radius),
        #                        xycoords='data',
        #                        xytext=(np.deg2rad(distance), radius * 1.02),
        #                        textcoords='data',
        #                        arrowprops=arrowprops,
        #                        clip_on=False)
        #            arrowprops = dict(arrowstyle='-|>,head_length=1.0,head_width=0.6',
        #                              color='0.3',
        #                              lw=1.5,
        #                              fill=False)
        #            ax.annotate('',
        #                        xy=(np.deg2rad(distance), radius),
        #                        xycoords='data',
        #                        xytext=(np.deg2rad(distance), radius * 1.01),
        #                        textcoords='data',
        #                        arrowprops=arrowprops,
        #                        clip_on=False)
        if label_arrivals:
            name = ','.join(sorted(set(ray.name for ray in arrivalstmp)))
            # We cannot just set the text of the annotations above because
            # it changes the arrow path.
            t = _SmartPolarText(np.deg2rad(distance),
                                radius * 1.07,
                                name,
                                clip_on=False)
            ax.add_artist(t)

        ax.set_rmax(radius)
        ax.set_rmin(0.0)
        if legend:
            if isinstance(legend, bool):
                if 0 <= distance <= 180.0:
                    loc = "upper left"
                else:
                    loc = "upper right"
            else:
                loc = legend
            plt.legend(loc=loc, prop=dict(size="small"))

    elif plot_type == "cartesian":
        if not ax:
            plt.figure(figsize=(12, 8))
            ax = plt.gca()
        ax.invert_yaxis()
        for _i, ray in enumerate(arrivalstmp):
            ax.plot(np.rad2deg(ray.path["dist"]),
                    ray.path["depth"],
                    color=COLORS[_i % len(COLORS)],
                    label=ray.name,
                    lw=1.0)
        ax.set_ylabel("Depth [km]")
        if legend:
            if isinstance(legend, bool):
                loc = "lower left"
            else:
                loc = legend
            ax.legend(loc=loc, prop=dict(size="small"))
        ax.set_xlabel("Distance [deg]")
        # Pretty station marker.
        ms = 14
        station_marker_transform = matplotlib.transforms.offset_copy(
            ax.transData, fig=ax.get_figure(), y=ms / 2.0, units="points")
        ax.plot([distance], [0.0],
                marker="v",
                color="#C95241",
                markersize=ms,
                zorder=10,
                markeredgewidth=1.5,
                markeredgecolor="0.3",
                clip_on=False,
                transform=station_marker_transform)
        if label_arrivals:
            name = ','.join(sorted(set(ray.name for ray in arrivals)))
            ax.annotate(name,
                        xy=(distance, 0.0),
                        xytext=(0, ms * 1.5),
                        textcoords='offset points',
                        ha='center',
                        annotation_clip=False)

        # Pretty earthquake marker.
        ax.plot([0], [arrivals[0].source_depth],
                marker="*",
                color="#FEF215",
                markersize=20,
                zorder=10,
                markeredgewidth=1.5,
                markeredgecolor="0.3",
                clip_on=False)
        x = ax.get_xlim()
        x_range = x[1] - x[0]
        ax.set_xlim(x[0] - x_range * 0.1, x[1] + x_range * 0.1)
        x = ax.get_xlim()
        y = ax.get_ylim()
        for depth in discons:
            if not (y[1] <= depth <= y[0]):
                continue
            ax.hlines(depth, x[0], x[1], color="0.5", zorder=-1)
        # Plot some more station markers if necessary.
        possible_distances = [_i * (distance + 360.0) for _i in range(1, 10)]
        possible_distances += [-_i * (360.0 - distance) for _i in range(1, 10)]
        possible_distances = [
            _i for _i in possible_distances if x[0] <= _i <= x[1]
        ]
        if possible_distances:
            ax.plot(possible_distances, [0.0] * len(possible_distances),
                    marker="v",
                    color="#C95241",
                    markersize=ms,
                    zorder=10,
                    markeredgewidth=1.5,
                    markeredgecolor="0.3",
                    clip_on=False,
                    lw=0,
                    transform=station_marker_transform)

    else:
        raise NotImplementedError
    if show:
        plt.show()
    return ax
Example #19
0
def fit_and_plot(ax=None,
                 power=None,
                 xlabel=None,
                 ylabel=None,
                 image_name=None,
                 filename=None,
                 show=None,
                 pad=None,
                 xlim=None,
                 ylim=None,
                 title=None,
                 figsize=None,
                 xlog=False,
                 ylog=False,
                 scatter=False,
                 legend=False,
                 ncol=1,
                 fontsize=None,
                 legend_fontsize=None,
                 markersize=None,
                 linewidth=None,
                 hor=False,
                 fig_format='eps',
                 dpi=300,
                 ver_lines=None,
                 xy_line=None,
                 x_nbins=None,
                 alpha=0.8,
                 fill=False,
                 first=True,
                 last=True,
                 convex=None,
                 dashes=None,
                 corner_letter=None,
                 hide_ylabels=None,
                 hide_xlabels=None,
                 annotate=None,
                 **data):
    """
    Plot multiple plots on one axes using *data*
    
    return filename of saved plot

    ax (axes) - matplotlib axes object - to create multiple axes plots

    data - each entry should be 
        (X, Y, fmt) 
        or 
        (X, Y, fmt, label) 
        or
        {'x':,'y':, 'fmt':, 'label', 'xticks' }    not implemented for powers and scatter yet
        or
        (X, Y, R, fmt) - for scatter = 1, R - size of spots

    first, last - allows to call this function multiple times to put several plots on one axes. Use first = 1, last = 0 for the first plot, 0, 0 for intermidiate, and 0, 1 for last

    power (int) - the power of polynom, turn on fitting

    scatter (bool) - plot scatter points - the data format is slightly different - see *data*

    convex (bool) - plot convex hull around points like in ATAT

    fill (bool) - fill under the curves

    filename (str) - name of file with figure, image_name - deprecated
    fig_format (str) - format of saved file.
    dpi    - resolution of saved file


    ver_lines - list of dic args for  vertical lines {'x':, 'c', 'lw':, 'ls':}

    hide_ylabels - just hide numbers

    ncol - number of legend columns

    corner_letter - letter in the corner of the plot

    pad - additional padding, experimental

    annotate - annotate each point, 'annotates' list should be in data dic!

    linewidth - was 3 !
    markersize - was 10

    x_nbins - number of ticks

    TODO:
    remove some arguments that can be provided in data dict


    """

    if image_name == None:
        image_name = filename

    if fontsize:
        header.mpl.rcParams.update({'font.size': fontsize + 4})
        if legend_fontsize is None:
            legend_fontsize = fontsize

        header.mpl.rc('legend', fontsize=legend_fontsize)

    if hasattr(header, 'first'):
        first = header.first

    if hasattr(header, 'last'):
        last = header.last
    # print('fit_and_plot, first and last', first, last)

    if ax is None:
        if first:
            # fig, ax = plt.subplots(1,1,figsize=figsize)
            plt.figure(figsize=figsize)

        ax = plt.gca()  # get current axes )))
        # ax  = fig.axes

    if title:
        ax.title(title)

    # print(dir(plt))
    # print(ax)

    # plt.ylabel(ylabel, axes = ax)
    # print(ylabel)
    if ylabel != None:

        ax.set_ylabel(ylabel)

    if xlabel != None:
        ''
        # plt.xlabel(xlabel, axes = ax)
        ax.set_xlabel(xlabel)

    if corner_letter:
        # print(corner_letter)
        sz = header.mpl.rcParams['font.size']
        ax.text(0.05,
                0.85,
                corner_letter,
                size=sz * 1.5,
                transform=ax.transAxes
                )  # transform = None - by default in data coordinates!

        # text(x, y, s, bbox=dict(facecolor='red', alpha=0.5))

    if convex:
        from scipy.spatial import ConvexHull

    for key in sorted(data):

        if scatter:

            ax.scatter(data[key][0],
                       data[key][1],
                       s=data[key][2],
                       c=data[key][-1],
                       alpha=alpha,
                       label=key)

        else:

            con = data[key]
            # print(con)
            # sys.exit()
            if type(con) == list or type(con) == tuple:
                try:
                    label = con[3]
                except:
                    label = key

                try:
                    fmt = con[2]
                except:
                    fmt = ''

                xyf = [con[0], con[1], fmt]
                con = {'label': label}  #fmt -color style

            elif type(con) == dict:
                if 'fmt' not in con:
                    con['fmt'] = ''
                # print(con)

                if 'x' not in con:
                    l = len(con['y'])
                    con['x'] = list(range(l))

                if 'xticks' in con:
                    # print(con['xticks'])
                    ax.set_xticklabels(con['xticks'])
                    ax.set_xticks(con['x'])
                    del con['xticks']

                xyf = [con['x'], con['y'], con['fmt']]

                # if 'lw' in
            if linewidth:
                con['lw'] = linewidth

            # print(con)
            # sys.exit()

            if markersize:
                con['ms'] = markersize

            # print('key is ', key)
            # print('x ', xyf[0])

            con_other_args = copy.deepcopy(con)
            for k in ['x', 'y', 'fmt', 'annotates']:
                if k in con_other_args:
                    del con_other_args[k]

            ax.plot(*xyf, alpha=alpha, **con_other_args)

            if power:
                coeffs1 = np.polyfit(xyf[0], xyf[1], power)

                fit_func1 = np.poly1d(coeffs1)
                x_range = np.linspace(min(xyf[0]), max(xyf[0]))
                fit_y1 = fit_func1(x_range)

                ax.plot(
                    x_range,
                    fit_y1,
                    xyf[2][0],
                )

                # x_min  = fit_func2.deriv().r[power-2] #derivative of function and the second cooffecient is minimum value of x.
                # y_min  = fit_func2(x_min)
                slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(
                    xyf[0], xyf[1])
                print('R^2 = {:5.2f} for {:s}'.format(r_value**2, key))

            if annotate:
                if adjustText_installed:

                    ts = []
                    for t, x, y in zip(con['annotates'], con['x'], con['y']):
                        ts.append(
                            ax.text(x,
                                    y,
                                    t,
                                    size=10,
                                    alpha=0.5,
                                    color=con['fmt'][0]))
                    adjust_text(
                        ts,
                        ax=ax,
                        # force_points=10, force_text=10, force_objects = 0.5,
                        expand_text=(2, 2),
                        expand_points=(2, 2),
                        # lim = 150,
                        expand_align=(2, 2),
                        # expand_objects=(0, 0),
                        text_from_text=1,
                        text_from_points=1,
                        # arrowprops=dict(arrowstyle='->', color='black')
                    )

                else:
                    for name, x, y in zip(con['annotates'], con['x'],
                                          con['y']):
                        ax.annotate(
                            name,
                            xy=(x, y),
                            xytext=(-20, 20),
                            fontsize=9,
                            textcoords='offset points',
                            ha='center',
                            va='bottom',
                            # bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3),
                            arrowprops=dict(arrowstyle='->',
                                            connectionstyle='arc3,rad=0.5',
                                            color='black'))

            # print(key)
            # print(con)
            if fill:
                ''
                ax.fill(xyf[0], xyf[1], facecolor=con['c'], alpha=0.6)

            if convex:
                points = np.asarray(list(zip(xyf[0], xyf[1])))
                hull = ConvexHull(points)
                for simplex in hull.simplices:
                    if max(points[simplex, 1]) > 0:
                        continue
                    ax.plot(points[simplex, 0], points[simplex, 1], 'k-')

    if hor:
        ax.axhline(color='k')  #horizontal line

    ax.axvline(color='k')  # vertical line at 0 always

    if ver_lines:
        for line in ver_lines:
            ax.axvline(**line)

    if xy_line:
        ylim = ax.get_ylim()
        # print(ylim)
        x = np.linspace(*ylim)
        # print(x)
        ax.plot(x, x)

    if x_nbins:
        ax.locator_params(nbins=x_nbins, axis='x')

    if xlim:
        ax.set_xlim(xlim)

    if ylim:
        ax.set_ylim(ymin=ylim[0])
        if ylim[1]:
            ax.set_ylim(ymax=ylim[1])

    if xlog:
        ax.set_xscale('log')

    if ylog:
        if "sym" in str(ylog):
            ax.set_yscale('symlog', linthreshx=0.1)
        else:
            ax.set_yscale('log')

    if hide_ylabels:
        ax.yaxis.set_major_formatter(plt.NullFormatter())
        # ax.yaxis.set_ticklabels([])
    if hide_xlabels:
        ax.xaxis.set_major_formatter(plt.NullFormatter())

    if legend:
        scatterpoints = 1  # for legend

        ax.legend(loc=legend, scatterpoints=scatterpoints, ncol=ncol)
        # plt.legend()

    # plt.tight_layout(pad = 2, h_pad = 0.5)

    plt.tight_layout()

    if pad:
        plt.subplots_adjust(left=0.13,
                            bottom=None,
                            right=None,
                            top=None,
                            wspace=0.07,
                            hspace=None)

    path2saved = ''

    if last:
        if image_name:
            # plt.subplots_adjust(hspace=0.1)

            path2saved, path2saved_png = process_fig_filename(
                image_name, fig_format)

            plt.savefig(path2saved, dpi=dpi, format=fig_format)
            plt.savefig(path2saved_png, dpi=300)

            print_and_log("Image saved to ", path2saved, imp='y')

        elif show is None:
            show = True
        # print_and_log(show)
        if show:
            plt.show()
        plt.clf()
        plt.close('all')
    else:
        printlog('Attention! last = False, no figure is saved')

    return path2saved
Example #20
0
    def plot_rays(self,
                  phase_list=None,
                  plot_type="spherical",
                  plot_all=True,
                  legend=False,
                  label_arrivals=False,
                  show=True,
                  fig=None,
                  ax=None):
        """
        Plot ray paths if any have been calculated.

        :param phase_list: List of phases for which ray paths are plotted,
            if they exist. See `Phase naming in taup`_ for details on
            phase naming and convenience keys like ``'ttbasic'``. Defaults to
            ``'ttall'``.
        :type phase_list: list of str
        :param plot_type: Either ``"spherical"`` or ``"cartesian"``.
            A spherical plot is always global whereas a Cartesian one can
            also be local.
        :type plot_type: str
        :param plot_all: By default all rays, even those travelling in the
            other direction and thus arriving at a distance of *360 - x*
            degrees are shown. Set this to ``False`` to only show rays
            arriving at exactly *x* degrees.
        :type plot_all: bool
        :param legend: If boolean, specify whether or not to show the legend
            (at the default location.) If a str, specify the location of the
            legend. If you are plotting a single phase, you may consider using
            the ``label_arrivals`` argument.
        :type legend: bool or str
        :param label_arrivals: Label the arrivals with their respective phase
            names. This setting is only useful if you are plotting a single
            phase as otherwise the names could be large and possibly overlap
            or clip. Consider using the ``legend`` parameter instead if you
            are plotting multiple phases.
        :type label_arrivals: bool
        :param show: Show the plot.
        :type show: bool
        :param fig: Figure to plot in. If not given, a new figure will be
            created.
        :type fig: :class:`matplotlib.figure.Figure`
        :param ax: Axes to plot in. If not given, a new figure with an axes
            will be created. Must be a polar axes for the spherical plot and
            a regular one for the Cartesian plot.
        :type ax: :class:`matplotlib.axes.Axes`
        :returns: Matplotlib axes with the plot
        :rtype: :class:`matplotlib.axes.Axes`
        """
        import matplotlib.pyplot as plt

        # I don't get this, but without sorting, I get a different
        # order each call:

        if phase_list is None:
            phase_list = ("ttall", )

        phase_names = sorted(parse_phase_list(phase_list))
        arrivals = []
        for arrival in self:
            if arrival.path is None:
                continue
            dist = arrival.purist_distance % 360.0
            distance = arrival.distance
            if distance < 0:
                distance = (distance % 360)
            if abs(dist - distance) / dist > 1E-5:
                if plot_all is False:
                    continue
                # Mirror on axis.
                arrival = copy.deepcopy(arrival)
                arrival.path["dist"] *= -1.0
            arrivals.append(arrival)

        if not arrivals:
            raise ValueError("Can only plot arrivals with calculated ray "
                             "paths.")

        # get the velocity discontinuities in your model, for plotting:
        discons = self.model.s_mod.v_mod.get_discontinuity_depths()

        if plot_type == "spherical":
            if ax and not isinstance(ax, mpl.projections.polar.PolarAxes):
                msg = ("Axes instance provided for plotting with "
                       "`plot_type='spherical'` but it seems the axes is not "
                       "a polar axes.")
                warnings.warn(msg)
            if fig and ax:
                pass
            elif not fig and not ax:
                fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
            elif not ax:
                ax = fig.add_subplot(1, 1, 1, polar=True)
            elif not fig:
                fig = ax.figure

            ax.set_theta_zero_location('N')
            ax.set_theta_direction(-1)
            ax.set_xticks([])
            ax.set_yticks([])

            intp = matplotlib.cbook.simple_linear_interpolation
            radius = self.model.radius_of_planet
            for ray in arrivals:
                if ray.name in phase_names:
                    # Requires interpolation,or diffracted phases look funny.
                    ax.plot(intp(ray.path["dist"], 100),
                            radius - intp(ray.path["depth"], 100),
                            color=COLORS[phase_names.index(ray.name) %
                                         len(COLORS)],
                            label=ray.name,
                            lw=2.0)
                else:
                    ax.plot(intp(ray.path["dist"], 100),
                            radius - intp(ray.path["depth"], 100),
                            color='k',
                            label=ray.name,
                            lw=2.0)
                ax.set_yticks(radius - discons)
                ax.xaxis.set_major_formatter(plt.NullFormatter())
                ax.yaxis.set_major_formatter(plt.NullFormatter())

            # Pretty earthquake marker.
            ax.plot([0], [radius - arrivals[0].source_depth],
                    marker="*",
                    color="#FEF215",
                    markersize=20,
                    zorder=10,
                    markeredgewidth=1.5,
                    markeredgecolor="0.3",
                    clip_on=False)

            # Pretty station marker.
            arrowprops = dict(arrowstyle='-|>,head_length=0.8,'
                              'head_width=0.5',
                              color='#C95241',
                              lw=1.5)
            station_radius = radius - arrivals[0].receiver_depth
            ax.annotate('',
                        xy=(np.deg2rad(distance), station_radius),
                        xycoords='data',
                        xytext=(np.deg2rad(distance),
                                station_radius + radius * 0.02),
                        textcoords='data',
                        arrowprops=arrowprops,
                        clip_on=False)
            arrowprops = dict(arrowstyle='-|>,head_length=1.0,'
                              'head_width=0.6',
                              color='0.3',
                              lw=1.5,
                              fill=False)
            ax.annotate('',
                        xy=(np.deg2rad(distance), station_radius),
                        xycoords='data',
                        xytext=(np.deg2rad(distance),
                                station_radius + radius * 0.01),
                        textcoords='data',
                        arrowprops=arrowprops,
                        clip_on=False)
            if label_arrivals:
                name = ','.join(sorted(set(ray.name for ray in arrivals)))
                # We cannot just set the text of the annotations above because
                # it changes the arrow path.
                t = _SmartPolarText(np.deg2rad(distance),
                                    station_radius + radius * 0.1,
                                    name,
                                    clip_on=False)
                ax.add_artist(t)

            ax.set_rmax(radius)
            ax.set_rmin(0.0)

            if legend:
                if isinstance(legend, bool):
                    if 0 <= distance <= 180.0:
                        loc = "upper left"
                    else:
                        loc = "upper right"
                else:
                    loc = legend
                ax.legend(loc=loc, prop=dict(size="small"))

        elif plot_type == "cartesian":
            if ax and isinstance(ax, mpl.projections.polar.PolarAxes):
                msg = ("Axes instance provided for plotting with "
                       "`plot_type='cartesian'` but it seems the axes is "
                       "a polar axes.")
                warnings.warn(msg)
            if fig and ax:
                pass
            elif not fig and not ax:
                fig, ax = plt.subplots()
                ax.invert_yaxis()
            elif not ax:
                ax = fig.add_subplot(1, 1, 1)
                ax.invert_yaxis()
            elif not fig:
                fig = ax.figure

            # Plot the ray paths:
            for ray in arrivals:
                if ray.name in phase_names:
                    ax.plot(np.rad2deg(ray.path["dist"]),
                            ray.path["depth"],
                            color=COLORS[phase_names.index(ray.name) %
                                         len(COLORS)],
                            label=ray.name,
                            lw=2.0)
                else:
                    ax.plot(np.rad2deg(ray.path["dist"]),
                            ray.path["depth"],
                            color='k',
                            label=ray.name,
                            lw=2.0)

            # Pretty station marker:
            ms = 14
            station_marker_transform = matplotlib.transforms.offset_copy(
                ax.transData, fig=ax.get_figure(), y=ms / 2.0, units="points")
            ax.plot([distance], [arrivals[0].receiver_depth],
                    marker="v",
                    color="#C95241",
                    markersize=ms,
                    zorder=10,
                    markeredgewidth=1.5,
                    markeredgecolor="0.3",
                    clip_on=False,
                    transform=station_marker_transform)
            if label_arrivals:
                name = ','.join(sorted(set(ray.name for ray in arrivals)))
                ax.annotate(name,
                            xy=(distance, arrivals[0].receiver_depth),
                            xytext=(0, ms * 1.5),
                            textcoords='offset points',
                            ha='center',
                            annotation_clip=False)

            # Pretty earthquake marker.
            ax.plot([0], [arrivals[0].source_depth],
                    marker="*",
                    color="#FEF215",
                    markersize=20,
                    zorder=10,
                    markeredgewidth=1.5,
                    markeredgecolor="0.3",
                    clip_on=False)

            # lines of major discontinuities:
            x = ax.get_xlim()
            y = ax.get_ylim()
            for depth in discons:
                if not (y[1] <= depth <= y[0]):
                    continue
                ax.hlines(depth, x[0], x[1], color="0.5", zorder=-1)

            # Plot some more station markers if necessary.
            possible_distances = [
                _i * (distance + 360.0) for _i in range(1, 10)
            ]
            possible_distances += [
                -_i * (360.0 - distance) for _i in range(1, 10)
            ]
            possible_distances = [
                _i for _i in possible_distances if x[0] <= _i <= x[1]
            ]
            if possible_distances:
                ax.plot(possible_distances,
                        [arrivals[0].receiver_depth] * len(possible_distances),
                        marker="v",
                        color="#C95241",
                        markersize=ms,
                        zorder=10,
                        markeredgewidth=1.5,
                        markeredgecolor="0.3",
                        clip_on=False,
                        lw=0,
                        transform=station_marker_transform)
            if legend:
                if isinstance(legend, bool):
                    loc = "lower left"
                else:
                    loc = legend
                ax.legend(loc=loc, prop=dict(size="small"))
            ax.set_xlabel("Distance [deg]")
            ax.set_ylabel("Depth [km]")
        else:
            msg = "Plot type '{}' is not a valid option.".format(plot_type)
            raise ValueError(msg)
        if show:
            plt.show()
        return ax
Example #21
0
def visualise_cards(cards, ax=None, cards_type='hand', show_spines=False):
    """

    :param cards:
    :param ax:
    :param cards_type:
    :param show_spines:
    :return:

    Example usage
    cards_hand = ['As', '7d', 'Kc', 'Wa', 'Wb']
    cards_pile = ['2s', '3s', '4s']

    ax = visualise_cards(cards_hand, ax=None, cards_type='hand', show_spines=False)
    visualise_cards(cards_pile, ax=ax, cards_type='pile', show_spines=False)
    visualise_cards(4, ax=ax, cards_type='opponent', show_spines=False)
    """
    assert cards_type in ['hand', 'pile', 'deck', 'opponent']

    show_card_values = True
    if 'hand' == cards_type:
        bottom = 0.1
    elif 'pile' == cards_type:
        bottom = YLIM / 2 - CARD_HEIGHT / 2
        cards_accessible = pile_top_accessible_cards(cards)
    elif 'opponent' == cards_type:
        bottom = YLIM / 2 + CARD_HEIGHT / 2 + 0.1
        assert isinstance(cards, int)
        cards = [None] * cards
        show_card_values = False

    hatch = None
    if not show_card_values:
        hatch = '/'

    if ax is None:
        fig, ax = plt.subplots(1, figsize=FIGSIZE)

    ncards = len(cards)
    left_most = CARD_WIDTH / 2 + (MAX_CARDS - ncards) * CARD_WIDTH / 2

    left = left_most
    dx, dy = 0.2, -0.1
    card_margin = 0.02
    for card in cards:
        alpha = 1
        if 'pile' == cards_type:
            if card not in cards_accessible:
                alpha = 0.3

        ax.add_patch(
            Rectangle((left, bottom),
                      CARD_WIDTH,
                      CARD_HEIGHT,
                      fill=False,
                      alpha=alpha,
                      hatch=hatch))

        if show_card_values:
            card_pretty = card_to_pretty(card)
            if 'W' in card_pretty:
                card_pretty = card_pretty[-1]

            ax.annotate(card_pretty, ((left + (left + CARD_WIDTH)) / 2 - dx,
                                      (bottom + CARD_HEIGHT / 2.2)),
                        rotation=0,
                        fontsize=30,
                        alpha=alpha,
                        color=card_to_color(card))

        left += CARD_WIDTH + card_margin

    plt.xlim(0., XLIM)
    plt.ylim(0., YLIM)

    if not show_spines:
        spines = ['right', 'top', 'left', 'bottom']
        [ax.spines[spine].set_visible(False) for spine in spines]
        ax.tick_params(bottom=False, left=False)
        ax.yaxis.set_major_locator(plt.NullLocator())
        ax.xaxis.set_major_formatter(plt.NullFormatter())

    return ax
Example #22
0
    def __init__(self,
                 cube=None,
                 interactive=True,
                 view=(0, 0, 10),
                 fig=None,
                 rect=[0, 0.16, 1, 0.84],
                 **kwargs):
        if cube is None:
            self.cube = Cube(3)
        elif isinstance(cube, Cube):
            self.cube = cube
        else:
            self.cube = Cube(cube)

        self._moves = []
        self._view = view
        self._start_rot = Quaternion.from_v_theta((1, -1, 0), -np.pi / 6)

        if fig is None:
            fig = plt.gcf()

        # disable default key press events
        callbacks = fig.canvas.callbacks.callbacks
        del callbacks['key_press_event']

        # add some defaults, and draw axes
        kwargs.update(
            dict(aspect=kwargs.get('aspect', 'equal'),
                 xlim=kwargs.get('xlim', (-2.0, 2.0)),
                 ylim=kwargs.get('ylim', (-2.0, 2.0)),
                 frameon=kwargs.get('frameon', False),
                 xticks=kwargs.get('xticks', []),
                 yticks=kwargs.get('yticks', [])))
        super(InteractiveCube, self).__init__(fig, rect, **kwargs)
        self.xaxis.set_major_formatter(plt.NullFormatter())
        self.yaxis.set_major_formatter(plt.NullFormatter())

        self._start_xlim = kwargs['xlim']
        self._start_ylim = kwargs['ylim']

        # Define movement for up/down arrows or up/down mouse movement
        self._ax_UD = (1, 0, 0)
        self._step_UD = 0.01

        # Define movement for left/right arrows or left/right mouse movement
        self._ax_LR = (0, -1, 0)
        self._step_LR = 0.01

        self._ax_LR_alt = (0, 0, 1)

        # Internal state variable
        self._active = False  # true when mouse is over axes
        self._button1 = False  # true when button 1 is pressed
        self._button2 = False  # true when button 2 is pressed
        self._event_xy = None  # store xy position of mouse event
        self._shift = False  # shift key pressed
        self._digit_flags = np.zeros(10, dtype=bool)  # digits 0-9 pressed

        self._current_rot = self._start_rot  #current rotation state
        self._face_polys = None
        self._sticker_polys = None

        self._draw_cube()

        # connect some GUI events
        self.figure.canvas.mpl_connect('button_press_event', self._mouse_press)
        self.figure.canvas.mpl_connect('button_release_event',
                                       self._mouse_release)
        self.figure.canvas.mpl_connect('motion_notify_event',
                                       self._mouse_motion)
        self.figure.canvas.mpl_connect('key_press_event', self._key_press)
        self.figure.canvas.mpl_connect('key_release_event', self._key_release)

        self._initialize_widgets()

        # write some instructions
        self.figure.text(0.05,
                         0.05, "Mouse/arrow keys adjust view\n"
                         "U/D/L/R/B/F keys turn faces\n"
                         "(hold shift for counter-clockwise)",
                         size=10)
Example #23
0
def syn_validation(N_samples, data_loc, data_scale, random_state=random_state):
    print("Generating synthetic data ...")
    #---------- create synthetic data -------------------------------------------------------------------------
    true_xyz = data_distribution(mean=data_loc, cov=data_scale, size=N_samples)

    #---- Transform XYZ to RA,DEC,parallax -------
    true_dst, ra, dec = cartesianToSpherical(true_xyz[:, 0], true_xyz[:, 1],
                                             true_xyz[:, 2])

    ra = np.degrees(ra)  # transform to degrees
    dec = np.degrees(dec)  # transform to degrees
    pax = 1.0 / true_dst  # Transform to parallax

    #----- assigns uncertainties and correlations similar to those present in Gaia data -------
    u_pax = st.chi2.rvs(df=u_pax_params[0],
                        loc=u_pax_params[1],
                        scale=u_pax_params[2],
                        size=N_samples,
                        random_state=random_state)  #Units in mas
    u_ra = st.chi2.rvs(df=u_ra_params[0],
                       loc=u_ra_params[1],
                       scale=u_ra_params[2],
                       size=N_samples,
                       random_state=random_state)  #Units in mas
    u_dec = st.chi2.rvs(df=u_dec_params[0],
                        loc=u_dec_params[1],
                        scale=u_dec_params[2],
                        size=N_samples,
                        random_state=random_state)  #Units in mas

    #------ Transform uncertainties to degrees and arcseconds --------
    u_pax = u_pax / 1000.0
    u_ra = u_ra / 3.6e6
    u_dec = u_dec / 3.6e6

    corr_ra_dec = st.norm.rvs(corr_ra_dec_params[0],
                              corr_ra_dec_params[1],
                              size=N_samples,
                              random_state=random_state)
    corr_ra_pax = st.norm.rvs(corr_ra_pax_params[0],
                              corr_ra_pax_params[1],
                              size=N_samples,
                              random_state=random_state)
    corr_dec_pax = st.norm.rvs(corr_dec_pax_params[0],
                               corr_dec_pax_params[1],
                               size=N_samples,
                               random_state=random_state)

    #----------------- array of data -------------------------
    data = np.column_stack((ra, dec, pax, u_ra, u_dec, u_pax, corr_ra_dec,
                            corr_ra_pax, corr_dec_pax))
    #----------------------------------------------------

    #------------------------------------------------------------
    print("Checking for singular matrices")
    for i, datum in enumerate(data):
        singular = True
        count = 0
        #------ Run loop until matrix is not singular
        while singular:
            count += 1
            corr = np.zeros((3, 3))
            corr[0, 1] = datum[6]
            corr[0, 2] = datum[7]
            corr[1, 2] = datum[8]
            corr = np.eye(3) + corr + corr.T
            cov = np.diag(datum[3:6]).dot(corr.dot(np.diag(datum[3:6])))

            try:
                s, logdet = np.linalg.slogdet(cov)
            except Exception as e:
                print(e)

            if s <= 0:
                print("Replacing singular matrix")
                #------- If matrix is singular then replace the values
                datum[6] = st.norm.rvs(corr_ra_dec_params[0],
                                       corr_ra_dec_params[1],
                                       size=1,
                                       random_state=random_state + count)[0]
                datum[7] = st.norm.rvs(corr_ra_pax_params[0],
                                       corr_ra_pax_params[1],
                                       size=1,
                                       random_state=random_state + count)[0]
                datum[8] = st.norm.rvs(corr_dec_pax_params[0],
                                       corr_dec_pax_params[1],
                                       size=1,
                                       random_state=random_state + count)[0]
            else:
                singular = False
                data[i, :] = datum
    #--------------------------

    # ------- Prepare plots --------------------
    pdf = PdfPages(filename=dir_graphs + "Sample_of_distances.pdf")
    random_sample = np.random.randint(0, N_samples, size=10)  #Only ten plots

    nullfmt = plt.NullFormatter()
    left, width = 0.1, 0.4
    bottom, height = 0.1, 0.4
    bottom_h = left_h = left + width + 0.0
    rect_scatter = [left, bottom, width, height]
    rect_histx = [left, bottom_h, width, 0.4]
    rect_histy = [left_h, bottom, 0.1, height]

    frac_error = np.zeros_like(true_xyz)
    maps = np.zeros_like(true_xyz)
    times = np.zeros(N_samples)
    sds = np.zeros_like(true_xyz)
    cis = np.zeros((N_samples, 2, 3))
    acc_fcs = np.zeros(N_samples)

    print("Sampling the posteriors ...")

    bar = progressbar.ProgressBar(maxval=N_samples).start()

    for d, (datum, t_xyz, t_dst) in enumerate(zip(data, true_xyz, true_dst)):
        ###################### Initialise the posterio_adw class ################################

        adw = posterior_adw(datum,
                            prior=prior,
                            prior_loc=prior_loc,
                            prior_scale=prior_scale)
        #------- run the p2d function ----------------------------
        MAP, Median, SD, CI, int_time, sample, mean_acceptance_fraction = adw.run(
            N_iter=N_iter)

        obs_xyz = sphericalToCartesian(MAP[2], MAP[0], MAP[1])

        #---- populate arrays----
        frac_error[d, 0] = (MAP[2] - t_dst) / t_dst
        frac_error[d, 1] = (MAP[0] - datum[0]) / datum[0]
        frac_error[d, 2] = (MAP[1] - datum[1]) / datum[1]
        maps[d, :] = MAP
        times[d] = int_time
        sds[d, :] = SD
        cis[d, :, :] = CI
        acc_fcs[d] = mean_acceptance_fraction

        #---- plot just random sample

        if d in random_sample:
            y_min, y_max = 0.95 * np.min(sample[:, :, 2]), 1.05 * np.max(
                sample[:, :, 2])

            fig = plt.figure(221, figsize=(6.3, 6.3))
            ax0 = fig.add_subplot(223, position=rect_scatter)
            ax0.set_xlabel("Iteration")
            ax0.set_ylabel("Distance [pc]")
            ax0.set_ylim(y_min, y_max)
            ax0.plot(sample[:, :, 2].T,
                     '-',
                     color='k',
                     alpha=0.3,
                     linewidth=0.3)
            ax0.axhline(t_dst,
                        color='black',
                        ls="solid",
                        linewidth=0.8,
                        label="True")
            ax0.axhline(MAP[2],
                        color='blue',
                        ls="--",
                        linewidth=0.5,
                        label="MAP")
            ax0.axhline(CI[0, 2],
                        color='blue',
                        ls=":",
                        linewidth=0.5,
                        label="CI 95%")
            ax0.axhline(CI[1, 2], color='blue', ls=":", linewidth=0.5)
            ax0.legend(loc="upper left", ncol=4, fontsize=4)

            ax1 = fig.add_subplot(224, position=rect_histy)
            ax1.set_ylim(y_min, y_max)
            ax1.axhline(t_dst,
                        color='black',
                        ls="solid",
                        linewidth=0.8,
                        label="True")
            ax1.axhline(MAP[2],
                        color='blue',
                        ls="--",
                        linewidth=0.5,
                        label="MAP")
            ax1.axhline(CI[0, 2],
                        color='blue',
                        ls=":",
                        linewidth=0.5,
                        label="CI")
            ax1.axhline(CI[1, 2], color='blue', ls=":", linewidth=0.5)

            ax1.set_xlabel("Density")
            ax1.yaxis.set_ticks_position('none')

            xticks = ax1.xaxis.get_major_ticks()
            xticks[0].label1.set_visible(False)

            ax1.yaxis.set_major_formatter(nullfmt)
            ax1.yaxis.set_minor_formatter(nullfmt)

            ax1.hist(sample[:, :, 2].flatten(),
                     bins=100,
                     density=True,
                     color="k",
                     orientation='horizontal',
                     fc='none',
                     histtype='step',
                     lw=0.5)
            pdf.savefig(bbox_inches='tight'
                        )  # saves the current figure into a pdf page
            plt.close()

        # ---- update progress bas ----
        bar.update(d)
    pdf.close()

    print("Acceptance fraction statistics:")
    print("Min.: {0:.3f}, Mean: {1:.3f}, Max.: {2:.3f}".format(
        np.min(acc_fcs), np.mean(acc_fcs), np.max(acc_fcs)))

    #---------- return data frame----
    data = pn.DataFrame(
        np.column_stack(
            (ra, dec, true_dst, pax, u_ra, u_dec, u_pax, u_pax / pax,
             u_ra / ra, u_dec / dec, maps, frac_error, sds, times)),
        columns=[
            'True RA', 'True DEC', 'True distance', 'True parallax',
            'RA uncertainty', 'DEC uncertainty', 'Parallax uncertainty',
            'Parallax fractional uncertainty', 'RA fractional uncertainty',
            'DEC fractional uncertainty', 'MAP RA', 'MAP DEC', 'MAP distance',
            'Distance fractional error', 'RA fractional error',
            'DEC fractional error', 'SD RA', 'SD DEC', 'SD distance',
            'Autocorrelation time'
        ])
    return data
Example #24
0
    def finetune_wavlength_solution(self):
        print("")
        print(
            "Cross correlating with synthetic sky to obtain refinement to wavlength solution ..."
        )
        print("")

        # Remove continuum
        mask = ~np.isnan(self.em_sky) & (self.haxis < 18000) & (self.haxis >
                                                                3500)
        hist, edges = np.histogram(self.em_sky[mask], bins="auto")
        max_idx = find_nearest(hist, max(hist))
        sky = self.em_sky - edges[max_idx]
        mask = ~np.isnan(sky) & (sky > 0) & (self.haxis < 18000) & (self.haxis
                                                                    > 3500)

        # Load synthetic sky
        sky_model = fits.open("statics/skytable_hres.fits")
        wl_sky = 1e4 * (sky_model[1].data.field('lam'))  # In micron
        flux_sky = sky_model[1].data.field('flux')

        # Convolve to observed grid
        from scipy.interpolate import interp1d
        f = interp1d(wl_sky,
                     convolve(sky_model[1].data.field('flux'),
                              Gaussian1DKernel(stddev=10)),
                     bounds_error=False,
                     fill_value=np.nan)
        synth_sky = f(self.haxis)

        # Cross correlate with redshifted spectrum and find velocity offset
        offsets = np.arange(-0.0005, 0.0005, 0.00001)
        correlation = np.zeros(offsets.shape)
        for ii, kk in enumerate(offsets):
            synth_sky = f(self.haxis * (1. + kk))
            correlation[ii] = np.correlate(
                sky[mask] * (np.nanmax(synth_sky) / np.nanmax(sky)),
                synth_sky[mask])

        # Index with maximal value
        max_idx = find_nearest(correlation, max(correlation))
        # Corrections to apply to original spectrum, which maximizes correlation.
        self.correction_factor = 1. + offsets[max_idx]
        print("Found preliminary velocity offset: " +
              str((self.correction_factor - 1.) * 3e5) + " km/s")
        print("")
        print(
            "Minimising residuals between observed sky and convolved synthetic sky to obtain the sky PSF ..."
        )
        print("")

        # Zero-deviation wavelength of arms, from http://www.eso.org/sci/facilities/paranal/instruments/xshooter/doc/VLT-MAN-ESO-14650-4942_v87.pdf
        if self.header['HIERARCH ESO SEQ ARM'] == "UVB":
            zdwl = 4050
            pixel_width = 50
        elif self.header['HIERARCH ESO SEQ ARM'] == "VIS":
            zdwl = 6330
            pixel_width = 50
        elif self.header['HIERARCH ESO SEQ ARM'] == "NIR":
            zdwl = 13100
            pixel_width = 50

        # Get seeing PSF by minimizing the residuals with a theoretical sky model, convovled with an increasing psf.
        psf_width = np.arange(1, pixel_width, 1)
        res = np.zeros(psf_width.shape)
        for ii, kk in enumerate(psf_width):
            # Convolve sythetic sky with Gaussian psf
            convolution = convolve(flux_sky, Gaussian1DKernel(stddev=kk))
            # Interpolate high-res syntheric sky onto observed wavelength grid.
            f = interp1d(wl_sky,
                         convolution,
                         bounds_error=False,
                         fill_value=np.nan)
            synth_sky = f(self.haxis * self.correction_factor)
            # Calculate squared residuals
            residual = np.nansum(
                (synth_sky[mask] *
                 (np.nanmax(sky[mask]) / np.nanmax(synth_sky[mask])) -
                 sky[mask])**2.)
            res[ii] = residual

        # Index of minimal residual
        min_idx = find_nearest(res, min(res))

        # Wavelegth step corresponding psf width in FWHM
        R, seeing = np.zeros(psf_width.shape), np.zeros(psf_width.shape)
        for ii, kk in enumerate(psf_width):
            dlambda = np.diff(wl_sky[::kk]) * 2 * np.sqrt(2 * np.log(2))
            # Interpolate to wavelegth grid
            f = interp1d(wl_sky[::kk][:-1],
                         dlambda,
                         bounds_error=False,
                         fill_value=np.nan)
            dlambda = f(self.haxis)

            # PSF FWHM in pixels
            d_pix = dlambda / (10 * self.header['CDELT1'])
            # Corresponding seeing PSF FWHM in arcsec
            spatial_psf = d_pix * self.header['CDELT2']

            # Index of zero-deviation
            zd_idx = find_nearest(self.haxis, zdwl)

            # Resolution at zero-deviation wavelength
            R[ii] = (self.haxis / dlambda)[zd_idx]
            # Seeing at zero-deviation wavelength
            seeing[ii] = spatial_psf[zd_idx]

        fig, ax1 = pl.subplots()
        ax1.yaxis.set_major_formatter(pl.NullFormatter())

        convolution = convolve(flux_sky,
                               Gaussian1DKernel(stddev=psf_width[min_idx]))
        f = interp1d(wl_sky,
                     convolution,
                     bounds_error=False,
                     fill_value=np.nan)
        synth_sky = f(self.haxis)

        # Cross correlate with redshifted spectrum and find velocity offset
        offsets = np.arange(-0.0005, 0.0005, 0.000001)
        correlation = np.zeros_like(offsets)
        for ii, kk in enumerate(offsets):
            synth_sky = f(self.haxis * (1. + kk))
            correlation[ii] = np.correlate(
                sky[mask] *
                (np.nanmax(synth_sky[mask]) / np.nanmax(sky[mask])),
                synth_sky[mask])

        # Smooth cross-correlation
        correlation = convolve(correlation, Gaussian1DKernel(stddev=20))

        # Index with maximum correlation
        max_idx = find_nearest(correlation, max(correlation))
        self.correction_factor = (1. + offsets[max_idx])
        self.header["WAVECORR"] = self.correction_factor
        print("Found refined velocity offset: " +
              str((self.correction_factor - 1.) * 3e5) + " km/s")
        print("")

        # Mask flux with > 3-sigma sky brightness
        self.sky_mask = f(self.haxis * self.correction_factor) > np.percentile(
            f(self.haxis * self.correction_factor), 99)
        ax2 = ax1.twiny()

        ax2.errorbar(
            offsets[max_idx] * 3e5,
            max(correlation) * (max(res) / max(correlation)),
            fmt=".k",
            capsize=0,
            elinewidth=0.5,
            ms=13,
            label="Wavelength correction:" +
            str(np.around(
                (self.correction_factor - 1.) * 3e5, decimals=1)) + " km/s",
            color="r")
        ax2.plot(offsets * 3e5,
                 correlation * (max(res) / max(correlation)),
                 color="r")
        ax2.set_xlabel("Offset velocity / [km/s]", color="r")
        ax2.set_ylabel("Cross correlation", color="r")
        ax2.yaxis.set_label_position("right")
        ax2.yaxis.set_major_formatter(pl.NullFormatter())
        ax2.legend(loc=2)
        pl.savefig(self.base_name + "Wavelength_cal.pdf")
        pl.clf()
Example #25
0
im1 = ax1.scatter(coeffs[:, 0], coeffs[:, 1], **scatter_kwargs)
im1.set_clim(clim)
ax1.set_ylabel('$c_2$')

ax2 = plt.subplot(223)
im2 = ax2.scatter(coeffs[:, 0], coeffs[:, 2], **scatter_kwargs)
im2.set_clim(clim)
ax2.set_xlabel('$c_1$')
ax2.set_ylabel('$c_3$')

ax3 = plt.subplot(224)
im3 = ax3.scatter(coeffs[:, 1], coeffs[:, 2], **scatter_kwargs)
im3.set_clim(clim)
ax3.set_xlabel('$c_2$')

fig.colorbar(im3, ax=ax3, cax=cax,
             ticks=cticks,
             format=formatter)

ax1.xaxis.set_major_formatter(plt.NullFormatter())
ax3.yaxis.set_major_formatter(plt.NullFormatter())

ax1.set_xlim(-0.01, 0.014)
ax2.set_xlim(-0.01, 0.014)

for ax in (ax1, ax2, ax3):
    ax.xaxis.set_major_locator(plt.MaxNLocator(5))
    ax.yaxis.set_major_locator(plt.MaxNLocator(5))

plt.show()
Example #26
0
    #ax[1].plot(plt_profile_2['oxygen_dry_c2'][i],depth_y_b,'-',color=plt_profile_2['color'][i],label=plt_profile_2['legend'][i] )
    #ax[2].plot(plt_profile_2['oxygen_dry_c3'][i],depth_y_b,'-',color=plt_profile_2['color'][i],label=plt_profile_2['legend'][i] )
    ax[0].plot(plt_profile_2['oxygen_dry_c1'][i],depth_y_a,'-',color=plt_profile_2['color'][i],label=plt_profile_2['legend'][i] )
    ax[1].plot(plt_profile_2['oxygen_dry_c2'][i],depth_y_b,'-',color=plt_profile_2['color'][i],label=plt_profile_2['legend'][i] )
    ax[2].plot(plt_profile_2['oxygen_dry_c3'][i],depth_y_d,'-',color=plt_profile_2['color'][i],label=plt_profile_2['legend'][i] )
    
ax[0].set_ylim([4.1,0.2])
ax[1].set_ylim([4.1,0.2])
ax[2].set_ylim([4.1,0.2])
ax[0].set_xlim([0,23])
ax[1].set_xlim([0,23])
ax[2].set_xlim([0,23])
ax[0].xaxis.set_major_locator(plt.MaxNLocator(5))
ax[1].xaxis.set_major_locator(plt.MaxNLocator(5))
ax[2].xaxis.set_major_locator(plt.MaxNLocator(5))
ax[1].yaxis.set_major_formatter(plt.NullFormatter())

ax[1].legend(bbox_to_anchor=(0.5, 1.07 ), loc='center', borderaxespad=0.,fontsize=10,handletextpad=0.03,labelspacing=0.02,ncol=2,columnspacing=0.4)
#ax[1].legend(bbox_to_anchor=(0.99, 0.004 ), loc='lower right', borderaxespad=0.,fontsize=10,handletextpad=0.23,labelspacing=0.22,ncol=1,columnspacing=0.4)
#ax[2].legend(bbox_to_anchor=(0.99, 0.004 ), loc='lower right', borderaxespad=0.,fontsize=10,handletextpad=0.23,labelspacing=0.22,ncol=1,columnspacing=0.4)

for i in ax:
      for axis in ['top','bottom','left','right']:
        i.spines[axis].set_linewidth(2)
        i.set_axisbelow(True)

ax[0].grid(True,which="both",ls=":",linewidth=grid_width,color = '0.5')
ax[1].grid(True,which="both",ls=":",linewidth=grid_width,color = '0.5')
ax[2].grid(True,which="both",ls=":",linewidth=grid_width,color = '0.5')

plt.show(block=False)
ax.text(
    0.5,
    0.1,
    "linear scale",
    transform=ax.transAxes,
    horizontalalignment="center",
    verticalalignment="bottom",
)
ax.set_yticks([])
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.xaxis.set_major_locator(ticker.MultipleLocator(0.10))
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter("%.1f"))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.02))
ax.xaxis.set_minor_formatter(plt.NullFormatter())
ax.tick_params(axis="both", which="major")


def forward(x):
    return x**2


def inverse(x):
    return x**(1 / 2)


# x**2 scale
# -----------------------------------------------------------------------------
ax = plt.subplot(3, 1, 2, xlim=[0.1, 1.0], ylim=[0, 1])
ax.patch.set_facecolor("none")
Example #28
0
    def plot_colormeshes(self, start_fig=1, dpi=200, r_pad=(0, 1)):
        """
        Plot figures of the 2D dedalus data slices at each timestep.

        # Arguments
            start_fig (int) :
                The number in the filename for the first write.
            dpi (int) :
                The pixel density of the output image
            r_pad (tuple of ints) :
                Padding values for the radial grid of polar plots            
        """
        with self.my_sync:
            axs, caxs = self._groom_grid()
            tasks = []
            bases = []
            for cm in self.colormeshes:
                if cm.field not in tasks:
                    tasks.append(cm.field)
                if cm.x_basis not in bases:
                    bases.append(cm.x_basis)
                if cm.y_basis not in bases:
                    bases.append(cm.y_basis)

            if self.idle: return

            while self.files_remain(bases, tasks):
                bs, tsk, writenum, times = self.read_next_file()
                

                for cm in self.colormeshes:
                    x = np.copy(bs[cm.x_basis])
                    y = np.copy(bs[cm.y_basis])
                    if cm.polar:
                        x = np.append(x, 2*np.pi)
                    elif cm.meridional:
                        x = np.pad(x, ((0,0), (1,1), (0,0)), mode='constant', constant_values=(np.pi,0))
                        x = np.pi/2 - x
                    elif cm.mollweide:
                        x -= np.pi
                        y = np.pi/2 - y
                    if cm.polar or cm.meridional:
                        y = np.pad(y, ((0,0), (0,0), (1,1)), mode='constant', constant_values=r_pad)

                    if cm.ortho:
                        y *= 180/np.pi
                        x *= 180/np.pi
                        x -= 180
                        y -= 90
                    cm.yy, cm.xx = np.meshgrid(y, x)

                for j, n in enumerate(writenum):
                    if self.reader.comm.rank == 0:
                        print('writing plot {}/{} on process 0'.format(j+1, len(writenum)))
                        stdout.flush()
                    for k, cm in enumerate(self.colormeshes):
                        field = np.squeeze(tsk[cm.field][j,:])
                        vector_ind = cm.vector_ind
                        if vector_ind is not None:
                            field = field[vector_ind,:]
                        xx, yy = cm.xx, cm.yy

                        #Subtract out m = 0
                        if cm.remove_mean:
                            field -= np.mean(field)
                        elif cm.remove_x_mean:
                            field -= np.mean(field, axis=0)
                        elif cm.remove_y_mean:
                            field -= np.mean(field, axis=1)


                        #Scale by magnitude of m = 0
                        if cm.divide_x_mean:
                            field /= np.mean(np.abs(field), axis=0)

 


                        if cm.meridional:
                            field = np.pad(field, ((0, 1), (1, 0)), mode='edge')
                        elif cm.polar:
                            field = np.pad(field, ((0, 0), (1, 0)), mode='edge')

                        if cm.polar or cm.meridional:
                            axs[k].set_xticks([])
                            axs[k].set_rticks([])
                            axs[k].set_aspect(1)

                        if cm.log: 
                            field = np.log10(np.abs(field))

                        vals = np.sort(field.flatten())
                        if cm.pos_def:
                            vals = np.sort(vals)
                            if np.mean(vals) < 0:
                                vmin, vmax = vals[int(0.002*len(vals))], 0
                            else:
                                vmin, vmax = 0, vals[int(0.998*len(vals))]
                        else:
                            vals = np.sort(np.abs(vals))
                            vmax = vals[int(0.998*len(vals))]
                            vmin = -vmax

                        if cm.vmin is not None:
                            vmin = cm.vmin
                        if cm.vmax is not None:
                            vmax = cm.vmax

                        if cm.ortho:
                            import cartopy.crs as ccrs
                            plot = axs[k].pcolormesh(xx, yy, field, cmap=cm.cmap, vmin=vmin, vmax=vmax, rasterized=True, transform=ccrs.PlateCarree())
                            axs[k].gridlines()#draw_labels=True, dms=True, x_inline=False, y_inline=False)

                        else:
                            plot = axs[k].pcolormesh(xx, yy, field, cmap=cm.cmap, vmin=vmin, vmax=vmax, rasterized=True)
                        cb = plt.colorbar(plot, cax=caxs[k], orientation='horizontal')
                        cb.solids.set_rasterized(True)
                        cb.set_ticks((vmin, vmax))
                        caxs[k].tick_params(direction='in', pad=1)
                        cb.set_ticklabels(('{:.2e}'.format(vmin), '{:.2e}'.format(vmax)))
                        caxs[k].xaxis.set_ticks_position('bottom')
                        if cm.label is None:
                            if vector_ind is not None:
                                caxs[k].text(0.5, 0.5, '{:s}[{}]'.format(cm.field, vector_ind), transform=caxs[k].transAxes, va='center', ha='center')
                            else:
                                caxs[k].text(0.5, 0.5, '{:s}'.format(cm.field), transform=caxs[k].transAxes, va='center', ha='center')
                        else:
                            caxs[k].text(0.5, 0.5, '{:s}'.format(cm.label), transform=caxs[k].transAxes, va='center', ha='center')

                        if cm.mollweide:
                            axs[k].yaxis.set_major_locator(plt.NullLocator())
                            axs[k].xaxis.set_major_formatter(plt.NullFormatter())

                    plt.suptitle('t = {:.4e}'.format(times[j]))
                    self.grid.fig.savefig('{:s}/{:s}_{:06d}.png'.format(self.out_dir, self.fig_name, int(n+start_fig-1)), dpi=dpi, bbox_inches='tight')
                    for ax in axs: ax.clear()
                    for cax in caxs: cax.clear()
Example #29
0
if not os.path.isdir(dir_graphs):
    os.mkdir(dir_graphs)
#-----------------------------------

file_out_csv = dir_graphs + "out_" + str(prior) + "_" + str(
    prior_loc) + "_" + str(
        prior_scale) + ".csv"  # file where the statistics will be written.
file_out_h5 = dir_graphs + "out_" + str(prior) + "_" + str(
    prior_loc) + "_" + str(prior_scale) + ".hdf5"

#------- Open plots and H5 files
pdf = PdfPages(filename=dir_graphs + "Distances.pdf")
fh5 = h5py.File(file_out_h5, 'w')

# ------- Prepare plots --------------------
nullfmt = plt.NullFormatter()
left, width = 0.1, 0.4
bottom, height = 0.1, 0.4
bottom_h = left_h = left + width + 0.0
rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.4]
rect_histy = [left_h, bottom, 0.1, height]

#------ initialise arrays ---------
maps = np.zeros((N, 3))
medians = np.zeros((N, 3))
sds = np.zeros((N, 3))
cis = np.zeros((N, 3, 2))
times = np.zeros(N)
acc_fcs = np.zeros(N)
Example #30
0
def plot_mcdterms(df, fname, num=1, xlabel='wavelength', ylabel='deltaA'):
    fig = plt.figure(figsize=(4, 4))
    gs = gridspec.GridSpec(2, 1, height_ratios=[1, 0.25])
    ax1 = fig.add_subplot(gs[0])
    ax2 = fig.add_subplot(gs[1])
    gs.update(hspace=0)
    p0 = [0, 0, 0]  #amplitude, center, width; adjust as needed
    bounds = []
    field_list = []
    width1_list = []
    width1_err_list = []
    for field in df:
        xdata = df[xlabel]
        ydata = df[ylabel]
        ax1.plot(xdata, ydata, "ko", ms=0.5)
        # use one peak
        if num == 1:
            popt_mcdterms, pcov_mcdterms = optimize.curve_fit(_mcdtermsAB,
                                                              xdata,
                                                              ydata,
                                                              p0=p0)
            perr_mcdterms = np.sqrt(np.diag(pcov_mcdterms))
            pars_1 = popt_mcdterms[0:3]
            mcd_term_peak_1 = _mcdtermsAB(xdata, *pars_1)
            ax1.plot(xdata, _mcdtermsAB(xdata, *popt_mcdterms), 'r--', lw=0.4)
            residual_mcd_terms = ydata - (_mcdtermsAB(xdata, *popt_mcdterms))
            ax2.plot(xdata, residual_mcd_terms, "bo", ms=0.4)
            ax2.fill_between(xdata,
                             0,
                             residual_mcd_terms,
                             facecolor='blue',
                             alpha=0.1)
            field_list.append(field)
            width1_list.append(pars_1[2])
            width1_err_list.append(perr_mcdterms[2])
            peak_params_df = pd.DataFrame(list(
                zip(field_list, width1_list, width1_err_list)),
                                          columns=['Pressure', 'FWHM', 'err'])

    # ax1.set_xlim(-10000,10000)
    # ax2.set_xlim(-10000,10000)
    # ax2.set_ylim(-25,25)

    ax2.set_xlabel(r'Frequency, $\nu-\nu_0$ (MHz)')
    ax1.set_ylabel(r'Signal, $\Delta$I$_{pr}$ (mV)', labelpad=10)
    ax2.set_ylabel("Residual", labelpad=4.5)

    ax1.xaxis.set_major_formatter(plt.NullFormatter())

    ax1.xaxis.set_major_locator(MultipleLocator(5000))
    ax2.xaxis.set_major_locator(MultipleLocator(5000))
    # ax2.yaxis.set_major_locator(MultipleLocator(10))

    ax1.xaxis.set_minor_locator(AutoMinorLocator())
    ax1.yaxis.set_minor_locator(AutoMinorLocator())
    ax2.xaxis.set_minor_locator(AutoMinorLocator())
    ax2.yaxis.set_minor_locator(AutoMinorLocator())

    ax1.xaxis.set_tick_params(
        which='major', size=5, width=0.8, direction='in', bottom='off'
    )  #axes.linewidth by default for matplotlib is 0.8, so that value is used here for aesthetic matching.
    ax1.xaxis.set_tick_params(which='minor',
                              size=2,
                              width=0.8,
                              direction='in',
                              bottom='off')
    ax1.yaxis.set_tick_params(which='major',
                              size=5,
                              width=0.8,
                              direction='in',
                              right='on',
                              bottom='off')
    ax1.yaxis.set_tick_params(which='minor',
                              size=2,
                              width=0.8,
                              direction='in',
                              right='on',
                              bottom='off')

    ax2.xaxis.set_tick_params(
        which='major', size=5, width=0.8, direction='in', top='off'
    )  #axes.linewidth by default for matplotlib is 0.8, so that value is used here for aesthetic matching.
    ax2.xaxis.set_tick_params(which='minor',
                              size=2,
                              width=0.8,
                              direction='in',
                              top='off')
    ax2.yaxis.set_tick_params(which='major',
                              size=5,
                              width=0.8,
                              direction='in',
                              right='on',
                              top='off')
    ax2.yaxis.set_tick_params(which='minor',
                              size=2,
                              width=0.8,
                              direction='in',
                              right='on',
                              top='off')

    fig.tight_layout()
    fig.savefig(fname + '.png', format="png", dpi=1000)

    return peak_params_df