Ejemplo n.º 1
0
def combi_var(mean_RMSE_700, mean_RMSE_2000, method):
    RMSE_combi = np.mean([mean_RMSE_700, mean_RMSE_2000], axis=0)
    RMSE_combi_fit = polyfit(KIconc, RMSE_combi, 1)
    RMSE_combi_r2 = 100 * RMSE_combi_fit['determination']
    RMSE_combi_label = 'y = {0:0.3f}x + {1:0.3f}; R$^2$ {2:0.0f}%' \
                       .format(RMSE_combi_fit['polynomial'][0],
                               RMSE_combi_fit['polynomial'][1], RMSE_combi_r2)

    return RMSE_combi, RMSE_combi_fit['function'], RMSE_combi_label
Ejemplo n.º 2
0
def profile(name, fit_var, profile, prfl_fld, stats_fld, test, plt_fld):
    """
    Creates plots for the potential thermometer/positioning profiles.
    =============
    --VARIABLES--
    name:        Profile name ('peak', 'q1', etc.)
    fit_var:     Y axis variable (temperature, y location, or index)
    profile:     X axis variable ('peak', 'q1', etc.)
    prfl_fld:    Location where profiles (X axis values) will be saved.
    stats_fld:   Location where statistics (polynomial fit) will be saved.
    test:        Type of liquid ("Water", "Ethanol", "Dodecane")
    plt_fld      Location where plots will be saved.
    """

    np.savetxt('{0}/profile_{1}.txt'.format(prfl_fld, name), profile)

    # Do fitting and plots only for the temperature/y location folders
    # (not for the pooled IJ ramping case)
    if fit_var[-1] != 252:
        prfl_fit = polyfit(profile, fit_var, 1)
        np.savetxt('{0}/{1}_polynomial.txt'.format(stats_fld, name),
                   prfl_fit['polynomial'])

        plt.figure()
        plt.plot(profile,
                 fit_var,
                 ' o',
                 markerfacecolor='none',
                 markeredgecolor='b',
                 label='Data')
        plt.plot(profile,
                 prfl_fit['function'](profile),
                 'k',
                 linewidth=2.0,
                 label='y = {0:0.2f}x + {1:0.2f}'.format(
                     prfl_fit['polynomial'][0], prfl_fit['polynomial'][1]))
        plt.title('{0} {1} - R$^2$ = {2:0.4f}'.format(
            test, name, prfl_fit['determination']))
        plt.legend()
        if any(x in prfl_fld for x in
               ['IJ Ramping/Temperature', 'IJ Ambient', 'IJ65', 'Cold']):
            plt.ylabel('Y Location (mm)')
        else:
            plt.ylabel('Temperature (K)')
        plt.xlabel(name)
        plt.autoscale(enable=True, axis='x', tight=True)
        plt.minorticks_on()
        plt.tick_params(which='both', direction='in')
        plt.tight_layout()
        plt.savefig('{0}/{1}.png'.format(plt_fld, name))
        plt.close()
Ejemplo n.º 3
0
def main():
    # Location of APS 2018-1 data
    prj_fld = '/mnt/r/X-ray Radiography/APS 2018-1/'

    # Save location for the plots
    plots_folder = create_folder(
        '{0}/Figures/Jet_EPL_Summary/'.format(prj_fld))

    # Scintillator
    scintillators = ['LuAG', 'YAG']

    # KI %
    KI_conc = [0, 1.6, 3.4, 5.3, 8.1, 10, 11.1]

    # Test matrix
    test_matrix = pd.read_csv('{0}/APS White Beam.txt'.format(prj_fld),
                              sep='\t+',
                              engine='python')

    # Crop down the test matrix
    test_matrix = test_matrix[['Test', 'Nozzle Diameter (um)', 'KI %']].copy()

    for scint in scintillators:
        # Processed data sets location
        prc_fld = '{0}/Processed/{1}/Summary/'.format(prj_fld, scint)

        # Vertical variation
        vert_mat = test_matrix[~test_matrix['Test'].str.contains('mm')].copy()

        # Groups
        grp1 = 'Nozzle Diameter (um)'
        grp2 = 'KI %'
        rpk = 'Ratio Peak'
        rel = 'Ratio Ellipse'

        # Get vertical values
        vert_mat[rel] = [
                         get_elps('{0}/{1}_{2}.pckl'\
                                  .format(prc_fld, scint, x))
                         for x in vert_mat['Test']
                         ]
        vert_mat[rpk] = [
                         get_peak('{0}/{1}_{2}.pckl'\
                                  .format(prc_fld, scint, x))
                         for x in vert_mat['Test']
                         ]
        vert_peak_grp = vert_mat.groupby([grp1, grp2])\
                                .apply(lambda x: np.mean(x[rpk], axis=0))
        vert_elps_grp = vert_mat.groupby([grp1, grp2])\
                                .apply(lambda x: np.mean(x[rel], axis=0))
        axial_loc = np.linspace(start=20,
                                stop=325,
                                num=325 - 20 + 1,
                                dtype=int)

        linecolors = [
            'dimgray', 'firebrick', 'goldenrod', 'mediumseagreen', 'steelblue',
            'mediumpurple', 'hotpink'
        ]
        linelabels = ['0%', '1.6%', '3.4%', '4.8%', '8%', '10%', '11.1%']

        warnings.filterwarnings('ignore')
        # Vertical peak plot
        fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
        fig.set_size_inches(12, 6)
        for i, x in enumerate(linecolors):
            ax1.plot(axial_loc[2:-1],
                     vert_peak_grp[700, KI_conc[i]][2:-1],
                     color=x,
                     linewidth=2.0)
            ax2.plot(axial_loc[2:-1],
                     vert_peak_grp[2000, KI_conc[i]][2:-1],
                     color=x,
                     linewidth=2.0)
        ax1.title.set_text(r'700 $\mu$m')
        ax2.title.set_text(r'2000 $\mu$m')
        ax1.set_xlabel('Axial Position (px)')
        ax2.set_xlabel('Axial Position (px)')
        ax1.set_ylabel('Correction Factor (-)')
        fig.suptitle('Vertical Variation - Ratio Peak')
        fig.legend(ax1,
                   labels=linelabels,
                   loc='center right',
                   borderaxespad=0.1,
                   title=grp2)
        plt.subplots_adjust(wspace=0.05, top=0.90)
        plt.savefig('{0}/{1}_vert_peak.png'.format(plots_folder, scint))
        plt.close()

        # Vertical elps plot
        fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
        fig.set_size_inches(12, 6)
        for i, x in enumerate(linecolors):
            ax1.plot(axial_loc[2:-1],
                     vert_elps_grp[700, KI_conc[i]][2:-1],
                     linewidth=2.0)
            ax2.plot(axial_loc[2:-1],
                     vert_elps_grp[2000, KI_conc[i]][2:-1],
                     linewidth=2.0)
        ax1.title.set_text(r'700 $\mu$m')
        ax2.title.set_text(r'2000 $\mu$m')
        ax1.set_xlabel('Axial Position (px)')
        ax2.set_xlabel('Axial Position (px)')
        ax1.set_ylabel('Correction Factor (-)')
        fig.suptitle('Vertical Variation - Ratio Ellipse')
        fig.legend(ax1,
                   labels=linelabels,
                   loc='center right',
                   borderaxespad=0.1,
                   title=grp2)
        plt.subplots_adjust(wspace=0.05, top=0.90)
        plt.savefig('{0}/{1}_vert_elps.png'.format(plots_folder, scint))
        warnings.filterwarnings('default')

        ######################################################################

        # Horizontal variation
        horiz_matrix = test_matrix[test_matrix['Test'].str\
                                                      .contains('mm')]\
                                                      .copy()
        horiz_matrix['X Position'] = [
                                      get_xpos('{0}/{1}_{2}.pckl'\
                                               .format(prc_fld, scint, x))
                                      for x in horiz_matrix['Test']
                                      ]

        # Sort horiz_matrix by X position, re-index, and drop the outliers
        horiz_matrix.sort_values(by=['X Position'], inplace=True)
        horiz_matrix.reset_index(inplace=True)
        horiz_matrix.drop([0, len(horiz_matrix) - 1], inplace=True)

        # Get horizontal values
        horiz_matrix[rel] = [
                             get_mean_elps('{0}/{1}_{2}.pckl'\
                                           .format(prc_fld, scint, x))
                             for x in horiz_matrix['Test']
                             ]
        horiz_matrix[rpk] = [
                             get_mean_peak('{0}/{1}_{2}.pckl'\
                                           .format(prc_fld, scint, x))
                             for x in horiz_matrix['Test']
                             ]

        # Normalize the horiz values to remove KI% dependency
        horiz_matrix[rel] /= max(horiz_matrix[rel])
        horiz_matrix[rpk] /= max(horiz_matrix[rpk])

        # Fit a quadratic to the horizontal values
        rel_quad = np.poly1d(
            np.polyfit(x=horiz_matrix['X Position'],
                       y=horiz_matrix[rel],
                       deg=2))

        rpk_quad = np.poly1d(
            np.polyfit(x=horiz_matrix['X Position'],
                       y=horiz_matrix[rpk],
                       deg=2))

        # Horizontal plot
        plt.figure()
        plt.plot(horiz_matrix['X Position'],
                 horiz_matrix[rpk],
                 color='olivedrab',
                 marker='s',
                 label=rpk)
        plt.plot(horiz_matrix['X Position'],
                 rpk_quad(horiz_matrix['X Position']),
                 color='black',
                 marker='s',
                 linestyle='dashed',
                 alpha=0.5,
                 label='Peak Fit')
        plt.legend()
        plt.title('{0} - Horizontal Variation - 700 um, 10% KI'.format(scint))
        plt.savefig('{0}/{1}_horiz_peak.png'.format(plots_folder, scint))
        plt.close()

        plt.figure()
        plt.plot(horiz_matrix['X Position'],
                 horiz_matrix[rel],
                 fillstyle='none',
                 color='olivedrab',
                 marker='s',
                 label=rel)
        plt.plot(horiz_matrix['X Position'],
                 rel_quad(horiz_matrix['X Position']),
                 fillstyle='none',
                 color='black',
                 marker='s',
                 linestyle='dashed',
                 alpha=0.5,
                 label='Ellipse Fit')
        plt.legend()
        plt.title('{0} - Horizontal Variation - 700 um, 10% KI'.format(scint))
        plt.savefig('{0}/{1}_horiz_elps.png'.format(plots_folder, scint))
        plt.close()

        ######################################################################

        # Mean vertical variation
        mean_matrix = test_matrix[~test_matrix['Test'].str\
                                                      .contains('mm')]\
                                                      .copy()

        # Get mean vertical values
        mean_matrix[rel] = [
                            get_mean_elps('{0}/{1}_{2}.pckl'\
                                          .format(prc_fld, scint, x))
                            for x in mean_matrix['Test']
                            ]
        mean_matrix[rpk] = [
                            get_mean_peak('{0}/{1}_{2}.pckl'\
                                          .format(prc_fld, scint, x))
                            for x in mean_matrix['Test']
                            ]

        # Calculate the mean values as needed
        pivot_mean_peak = mean_matrix.pivot_table(values=rpk,
                                                  index=[grp2],
                                                  columns=[grp1],
                                                  aggfunc=np.nanmean)
        pivot_mean_elps = mean_matrix.pivot_table(values=rel,
                                                  index=[grp2],
                                                  columns=[grp1],
                                                  aggfunc=np.nanmean)
        # Create arrays from the pivot tables
        # Could plot directly from the pivot table but I didn't want to delve
        # too deep into that
        mean_peak_700 = pivot_mean_peak[700]
        peak_700_fit = polyfit(KI_conc, mean_peak_700, 1)
        peak_700_fit_r2 = peak_700_fit['determination']
        peak_700_fit_lbl = 'y$_{700}$ = ' + '{0:0.3f}x + {1:0.3f};'\
                              'R$^2$ {2:0.0f}%'\
                              .format(
                                      peak_700_fit['polynomial'][0],
                                      peak_700_fit['polynomial'][1],
                                      100*peak_700_fit_r2
                                      )

        mean_peak_2000 = pivot_mean_peak[2000]
        peak_2000_fit = polyfit(KI_conc, mean_peak_2000, 1)
        peak_2000_fit_r2 = peak_2000_fit['determination']
        peak_2000_fit_lbl = 'y$_{2000}$ = ' + '{0:0.3f}x + {1:0.3f};'\
                               'R$^2$ {2:0.0f}%'\
                               .format(
                                       peak_2000_fit['polynomial'][0],
                                       peak_2000_fit['polynomial'][1],
                                       100*peak_2000_fit_r2
                                       )

        mean_elps_700 = pivot_mean_elps[700]
        elps_700_fit = polyfit(KI_conc, mean_elps_700, 1)
        elps_700_fit_r2 = elps_700_fit['determination']
        elps_700_fit_lbl = 'y$_{700}$ = ' + '{0:0.3f}x + {1:0.3f};'\
                              'R$^2$ {2:0.0f}%'\
                              .format(
                                      elps_700_fit['polynomial'][0],
                                      elps_700_fit['polynomial'][1],
                                      100*elps_700_fit_r2
                                      )

        mean_elps_2000 = pivot_mean_elps[2000]
        elps_2000_fit = polyfit(KI_conc, mean_elps_2000, 1)
        elps_2000_fit_r2 = elps_2000_fit['determination']
        elps_2000_fit_lbl = 'y$_{2000}$ = ' + '{0:0.3f}x + {1:0.3f};'\
                               'R$^2$ {2:0.0f}%'\
                               .format(
                                       elps_2000_fit['polynomial'][0],
                                       elps_2000_fit['polynomial'][1],
                                       100*elps_2000_fit_r2
                                       )

        # Peak plot (markers filled)
        plt.figure()
        plt.plot(KI_conc,
                 mean_peak_700,
                 color='olivedrab',
                 marker='s',
                 label='700 um')
        plt.plot(KI_conc,
                 peak_700_fit['function'](KI_conc),
                 color='teal',
                 label=peak_700_fit_lbl)
        plt.plot(KI_conc,
                 mean_peak_2000,
                 color='indianred',
                 marker='^',
                 label='2000 um')
        plt.plot(KI_conc,
                 peak_2000_fit['function'](KI_conc),
                 color='darkorange',
                 label=peak_2000_fit_lbl)
        plt.legend()
        plt.title('{0} - Ratio Peak'.format(scint))
        plt.xlabel('KI (%)')
        plt.ylabel('Correction Factor (-)')
        plt.savefig('{0}/{1}_mean_peak.png'.format(plots_folder, scint))
        plt.close()

        # Ellipse plot (markers not filled)
        plt.figure()
        plt.plot(KI_conc,
                 mean_elps_700,
                 fillstyle='none',
                 color='olivedrab',
                 marker='s',
                 label='700 um')
        plt.plot(KI_conc,
                 elps_700_fit['function'](KI_conc),
                 color='teal',
                 label=elps_700_fit_lbl)
        plt.plot(KI_conc,
                 mean_elps_2000,
                 fillstyle='none',
                 color='indianred',
                 marker='^',
                 label='2000 um')
        plt.plot(KI_conc,
                 elps_2000_fit['function'](KI_conc),
                 color='darkorange',
                 label=elps_2000_fit_lbl)
        plt.legend()
        plt.title('{0} - Ratio Ellipse'.format(scint))
        plt.xlabel('KI (%)')
        plt.ylabel('Correction Factor (-)')
        plt.savefig('{0}/{1}_mean_elps.png'.format(plots_folder, scint))
        plt.close()

        ######################################################################

        mean_peak_combi = np.mean([mean_peak_700, mean_peak_2000], axis=0)
        peak_combi_fit = polyfit(KI_conc, mean_peak_combi, 1)
        peak_combi_fit_r2 = peak_combi_fit['determination']
        peak_combi_fit_lbl = 'y = {0:0.3f}x + {1:0.3f};'\
                                 'R$^2$ {2:0.0f}%'\
                                 .format(peak_combi_fit['polynomial'][0],
                                         peak_combi_fit['polynomial'][1],
                                         100*peak_combi_fit_r2
                                         )

        mean_elps_combi = np.mean([mean_elps_700, mean_elps_2000], axis=0)
        elps_combi_fit = polyfit(KI_conc, mean_elps_combi, 1)
        elps_combi_fit_r2 = elps_combi_fit['determination']
        elps_combi_fit_lbl = 'y = {0:0.3f}x + {1:0.3f}; R$^2$ {2:0.0f}%'\
                                 .format(
                                         elps_combi_fit['polynomial'][0],
                                         elps_combi_fit['polynomial'][1],
                                         100*elps_combi_fit_r2
                                         )

        # Save the linear fitted correction factors
        with open('{0}/Processed/{1}/{1}_peak_cf.txt'\
                  .format(prj_fld, scint), 'wb') as f:
            np.savetxt(f, peak_combi_fit['function'](KI_conc))

        with open('{0}/Processed/{1}/{1}_elps_cf.txt'\
                  .format(prj_fld, scint), 'wb') as f:
            np.savetxt(f, elps_combi_fit['function'](KI_conc))

        # Map out the correction factor horizontally over an image array
        cf_fld = create_folder('{0}/Processed/{1}/CF_Map/'.format(
            prj_fld, scint))
        image_x = np.linspace(0, 767, 768)
        for KI in KI_conc:
            KIstr = str(KI).replace('.', 'p')

            # Create CF based on elps
            elps_mat = np.ones((352, 768)) * elps_combi_fit['function'](KI)
            elps_mat *= rel_quad(image_x)
            elps_im = Image.fromarray(elps_mat)
            elps_im.save('{0}/elps_{1}.tif'.format(cf_fld, KIstr))

            # Create CF based on peak
            peak_mat = np.ones((352, 768)) * peak_combi_fit['function'](KI)
            peak_mat *= rpk_quad(image_x)
            peak_im = Image.fromarray(peak_mat)
            peak_im.save('{0}/peak_{1}.tif'.format(cf_fld, KIstr))

        plt.figure()
        plt.plot(KI_conc,
                 mean_peak_combi,
                 color='lightcoral',
                 marker='s',
                 linestyle='',
                 label=rpk,
                 zorder=2)
        plt.plot(KI_conc,
                 peak_combi_fit['function'](KI_conc),
                 linestyle='-',
                 color='maroon',
                 label=peak_combi_fit_lbl,
                 zorder=1)
        plt.plot(KI_conc,
                 mean_elps_combi,
                 color='cornflowerblue',
                 marker='^',
                 linestyle='',
                 label=rel,
                 zorder=2)
        plt.plot(KI_conc,
                 elps_combi_fit['function'](KI_conc),
                 linestyle='-',
                 color='mediumblue',
                 label=elps_combi_fit_lbl,
                 zorder=1)
        plt.title('{0} - Combined'.format(scint))
        plt.legend()
        plt.xlabel('KI (%)')
        plt.ylabel('Correction Factor (-)')
        plt.savefig('{0}/{1}_combi.png'.format(plots_folder, scint))
        plt.close()
Ejemplo n.º 4
0
def main():
    # Setup initial parameters
    prj_fld = '/mnt/r/X-ray Temperature/APS 2017-2'
    fld = prj_fld + '/Processed/Ethanol'

    # Select profiles to be used as thermometers
    profiles = ['aratio', 'peak', 'peakq', 'var', 'skew', 'kurt', 'pca']

    # Select calibration data sets
    calibrations = ['408', '409', 'Combined']

    # Load in IJ Ramping folders
    flds = glob.glob(fld + '/IJ Ramping/Positions/y*/')

    # Load in and process data sets
    # Iterate through each selected calibration jet
    for calibration in calibrations:
        # Initialize summary arrays
        summary_rmse = np.nan * np.zeros((len(flds), len(profiles)))
        summary_mape = np.nan * np.zeros((len(flds), len(profiles)))
        summary_zeta = np.nan * np.zeros((len(flds), len(profiles)))
        summary_mdlq = np.nan * np.zeros((len(flds), len(profiles)))

        # Iterate through each selected profile
        for j, profile in enumerate(profiles):
            # Create polynomial object based on selected profile & calib jet
            # Load APS 2018-1 for 'Combined'
            if calibration == 'Combined':
                cmb_fld = fld.replace('APS 2017-2', 'APS 2018-1')\
                             .replace('Ethanol', 'Ethanol_700umNozzle')
                p = np.poly1d(np.loadtxt(cmb_fld + '/' + calibration +
                              '/Statistics/' + profile + '_polynomial.txt'))
            else:
                p = np.poly1d(np.loadtxt(fld + '/' + calibration +
                              '/Statistics/' + profile + '_polynomial.txt'))

            # Create flds
            plots_fld = '{0}/IJ Ramping/PositionsInterp/{1}_{2}'\
                        .format(fld, calibration, profile)
            if not os.path.exists(plots_fld):
                os.makedirs(plots_fld)

            summary_nozzleT = []
            summary_interpT = []

            # Load in Positions
            pos_y = []
            for i, fldr in enumerate(flds):
                # Y position string (y00p25, y00p05, etc.)
                yp = fldr.rsplit('/')[-2]
                pos_y.append(float(yp[1:].replace('p', '.')))

                # Profile data for the IJ Ramping Positions
                data = np.loadtxt('{0}/Profiles/profile_{1}.txt'
                                  .format(fldr, profile)
                                  )

                # Nozzle T
                nozzleT = np.loadtxt(fldr + '/temperature.txt')
                # Interpolated temperature
                interpT = p(data)
                # Fit a linear line of interpT vs. nozzleT
                fit = polyfit(interpT, nozzleT, 1)

                # Calculate RMSE (root mean squared error)
                rmse = np.sqrt(((interpT - nozzleT)**2).mean())

                # Calculate MAPE (mean absolute percentage error)
                mape = 100*(np.sum(np.abs((interpT - nozzleT) / nozzleT)) /
                            len(nozzleT))

                # Calculate median symmetric accuracy
                zeta = 100*np.exp(np.median(np.abs(np.log(interpT/nozzleT)))
                                  - 1)

                # Calculate MdLQ (median log accuracy ratio)
                # Positive/negative: systematic (over/under)-prediction
                mdlq = np.median(np.log(interpT/nozzleT))

                # Build up summaries
                summary_rmse[i, j] = rmse
                summary_mape[i, j] = mape
                summary_zeta[i, j] = zeta
                summary_mdlq[i, j] = mdlq
                summary_nozzleT.append(nozzleT)
                summary_interpT.append(interpT)

                # Plot results
                plt.figure()
                plt.plot(
                         interpT,
                         nozzleT,
                         ' o',
                         markerfacecolor='none',
                         markeredgecolor='b',
                         label='Data'
                         )
                plt.plot(
                         nozzleT,
                         nozzleT,
                         'k',
                         linewidth=2.0,
                         label='y = x'
                         )
                plt.plot(
                         interpT,
                         fit['function'](interpT),
                         'r',
                         linewidth=2.0,
                         label='y = ' + '%0.2f'%fit['polynomial'][0] + 'x + '
                               + '%0.2f'%fit['polynomial'][1]
                         )
                plt.title('y = ' + yp[1:].replace('p', '.') + ' mm - ' +
                          calibration + ': ' + profile)
                plt.legend()
                plt.xlabel('Interpolated Temperature (K)')
                plt.ylabel('Nozzle Temperature (K)')
                plt.tight_layout()
                plt.savefig(plots_fld + '/' + yp + '.png')
                plt.close()

            slices = [1, 3, 5, 7, 9, 11, 12, 13]
            plt.figure()
            [
             plt.plot(
                      summary_nozzleT[i],
                      summary_interpT[i],
                      linewidth=2.0,
                      label=str(pos_y[i]) + ' mm'
                      )
             for i in slices
             ]
            plt.legend()
            plt.xlim([270, 350])
            plt.ylim([270, 350])
            plt.ylabel('Interpolated Temperature (K)')
            plt.xlabel('Nozzle Temperature (K)')
            plt.title(calibration + ': ' + profile)
            plt.savefig(plots_fld + '/temperatures.png')
            plt.close()

        # Plot summaries
        plt.figure()
        [
         plt.plot(
                  pos_y,
                  summary_rmse[:, j],
                  linewidth=2.0,
                  label=profiles[j]
                  )
         for j in range(len(profiles))
         ]
        plt.legend()
        plt.ylabel('RMSE (K)')
        plt.xlabel('Vertical Location (mm)')
        plt.title(calibration)
        plt.savefig(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                    '_rmse.png')
        plt.close()

        plt.figure()
        [
         plt.plot(
                  pos_y,
                  summary_mape[:, j],
                  linewidth=2.0,
                  label=profiles[j]
                  )
         for j in range(len(profiles))
         ]
        plt.legend()
        plt.ylabel('MAPE (%)')
        plt.xlabel('Vertical Location (mm)')
        plt.title(calibration)
        plt.savefig(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                    '_mape.png')
        plt.close()

        plt.figure()
        [
         plt.plot(
                  pos_y,
                  summary_zeta[:, j],
                  linewidth=2.0,
                  label=profiles[j]
                  )
         for j in range(len(profiles))
         ]
        plt.legend()
        plt.ylabel(r'$\zeta$ (%)')
        plt.xlabel('Vertical Location (mm)')
        plt.title(calibration)
        plt.savefig(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                    '_zeta.png')
        plt.close()

        plt.figure()
        [
         plt.plot(
                  pos_y,
                  summary_mdlq[:, j],
                  linewidth=2.0,
                  label=profiles[j]
                  )
         for j in range(len(profiles))
         ]
        plt.legend()
        plt.ylabel('MdLQ (-)')
        plt.xlabel('Vertical Location (mm)')
        plt.title(calibration)
        plt.savefig(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                    '_mdlq.png')
        plt.close()

        # Save summary file
        np.savetxt(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                   '_rmse.txt', summary_rmse, delimiter='\t',
                   header='\t'.join(str(x) for x in profiles))
        np.savetxt(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                   '_mape.txt', summary_mape, delimiter='\t',
                   header='\t'.join(str(x) for x in profiles))
        np.savetxt(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                   '_zeta.txt', summary_zeta, delimiter='\t',
                   header='\t'.join(str(x) for x in profiles))
        np.savetxt(fld + '/IJ Ramping/PositionsInterp/' + calibration +
                   '_mdlq.txt', summary_mdlq, delimiter='\t',
                   header='\t'.join(str(x) for x in profiles))
Ejemplo n.º 5
0
def main():
    prj_fld = '/mnt/r/X-ray Temperature/APS 2017-2'
    folder = prj_fld + '/Processed/Ethanol'

    # Create summary of Temperature data sets (for most consistent profile)
    flds = glob.glob(folder + '/IJ Ramping/Temperature/*/')
    for fld in flds:
        temp = fld.rsplit('/')[-1]
        files = glob.glob(fld + '/Profiles/profile*')
        names = [
            x.rsplit('/')[-1].rsplit('_')[-1].rsplit('.')[0] for x in files
        ]
        df = pd.DataFrame(columns=['R^2', 'Mean', 'StD', 'CfVar', 'CfDisp'],
                          index=names)
        for name, file in zip(names, files):
            # Profile data
            data = np.loadtxt(file)

            # Y positions
            y = np.loadtxt(fld + '/positions.txt')

            # Calculate R^2
            r2 = polyfit(data, y, 1)['determination']

            # Calculate Coefficient of Variation
            mean = np.mean(data)
            std = np.std(data)
            cv = np.std(data) / np.mean(data)

            # Calculate Coefficient of Dispersion
            Q1 = np.percentile(data, 25, interpolation='midpoint')
            Q3 = np.percentile(data, 75, interpolation='midpoint')
            cd = (Q1 - Q3) / (Q1 + Q3)

            # Fill in specific profile in DataFrame
            df.loc[name] = pd.Series({
                'R^2': round(r2, 3),
                'Mean': round(mean, 3),
                'StD': round(std, 3),
                'CfVar': round(cv, 3),
                'CfDisp': round(cd, 3)
            })

        df.to_csv(fld + '/' + temp + 'summary.txt', sep='\t')

    # Create summary of Position data sets (to find best profile)
    flds = glob.glob(folder + '/IJ Ramping/Positions/*/')
    for fld in flds:
        pos = fld.rsplit('/')[-1]
        files = glob.glob(fld + '/Profiles/profile*')
        names = [
            x.rsplit('/')[-1].rsplit('_')[-1].rsplit('.')[0] for x in files
        ]
        df = pd.DataFrame(columns=['R^2', 'Mean', 'StD', 'CfVar', 'CfDisp'],
                          index=names)
        for name, file in zip(names, files):
            # Profile data
            data = np.loadtxt(file)

            # Temperature
            T = np.loadtxt(fld + '/temperature.txt')

            # Calculate R^2
            r2 = polyfit(data, T, 1)['determination']

            # Calculate Coefficient of Variation
            mean = np.mean(data)
            std = np.std(data)
            cv = np.std(data) / np.mean(data)

            # Calculate Coefficient of Dispersion
            Q1 = np.percentile(data, 25, interpolation='midpoint')
            Q3 = np.percentile(data, 75, interpolation='midpoint')
            cd = (Q1 - Q3) / (Q1 + Q3)

            # Fill in specific profile in DataFrame
            df.loc[name] = pd.Series({
                'R^2': round(r2, 3),
                'Mean': round(mean, 3),
                'StD': round(std, 3),
                'CfVar': round(cv, 3),
                'CfDisp': round(cd, 3)
            })

        df.to_csv(fld + '/' + pos + 'summary.txt', sep='\t')

    # Summarize the Temperature/Position summaries
    for param in ["Temperature", "Positions"]:
        flds = glob.glob(folder + '/IJ Ramping/' + param + '/?*p*/')

        # Get Temperature/Position values
        y_axis = [float(x.rsplit('/')[-2][1:].replace('p', '.')) for x in flds]

        # Profile names (kurtosis, A1, q2, etc.)
        profiles = glob.glob(flds[0] + '/Profiles/profile*')
        names = [
            x.rsplit('/')[-1].rsplit('_')[-1].rsplit('.')[0] for x in profiles
        ]

        # R^2 summary
        r2_mean = np.array([
            np.mean([
                pd.read_csv(fld + '/summary.txt',
                            sep='\t',
                            index_col=0,
                            header=0).loc[name]['R^2'] for fld in flds
            ]) for name in names
        ])
        r2_std = np.array([
            np.std([
                pd.read_csv(fld + '/summary.txt',
                            sep='\t',
                            index_col=0,
                            header=0).loc[name]['R^2'] for fld in flds
            ]) for name in names
        ])
        r2_cv = r2_std / r2_mean
        r2_q1 = np.array([
            np.percentile([
                pd.read_csv(
                    fld + '/summary.txt', sep='\t', index_col=0,
                    header=0).loc[name]['R^2'] for fld in flds
            ],
                          25,
                          interpolation='midpoint') for name in names
        ])
        r2_q3 = np.array([
            np.percentile([
                pd.read_csv(
                    fld + '/summary.txt', sep='\t', index_col=0,
                    header=0).loc[name]['R^2'] for fld in flds
            ],
                          75,
                          interpolation='midpoint') for name in names
        ])
        r2_cd = (r2_q1 - r2_q3) / (r2_q1 + r2_q3)
        r2_data = {
            'Mean of R^2': r2_mean,
            'StD of R^2': r2_std,
            'CfVar of R^2': r2_cv,
            'CfDis of R^2': r2_cd
        }
        df = pd.DataFrame(r2_data, index=names)
        df.to_csv('{0}/IJ Ramping/{1}_r2_summary.txt'.format(
            folder, param.lower()),
                  sep='\t')

        # Plot R^2 values
        plots_folder = folder + '/IJ Ramping/' + param + '/Plots_R2/'
        if not os.path.exists(plots_folder):
            os.makedirs(plots_folder)

        r2_data = [[
            pd.read_csv(fld + '/summary.txt', sep='\t', index_col=0,
                        header=0).loc[name]['R^2'] for fld in flds
        ] for name in names]

        for i, x in enumerate(r2_data):
            plt.figure()
            plt.plot(x, y_axis, ' o')
            plt.xlabel(names[i])
            plt.ylabel(param)
            plt.tight_layout()
            plt.savefig(plots_folder + names[i] + '.png')
            plt.close()

        # Mean summary
        mean_mean = np.array([
            np.mean([
                pd.read_csv(fld + '/summary.txt',
                            sep='\t',
                            index_col=0,
                            header=0).loc[name]['Mean'] for fld in flds
            ]) for name in names
        ])
        mean_std = np.array([
            np.std([
                pd.read_csv(fld + '/summary.txt',
                            sep='\t',
                            index_col=0,
                            header=0).loc[name]['Mean'] for fld in flds
            ]) for name in names
        ])
        mean_cv = mean_std / mean_mean
        mean_q1 = np.array([
            np.percentile([
                pd.read_csv(
                    fld + '/summary.txt', sep='\t', index_col=0,
                    header=0).loc[name]['Mean'] for fld in flds
            ],
                          25,
                          interpolation='midpoint') for name in names
        ])
        mean_q3 = np.array([
            np.percentile([
                pd.read_csv(
                    fld + '/summary.txt', sep='\t', index_col=0,
                    header=0).loc[name]['Mean'] for fld in flds
            ],
                          75,
                          interpolation='midpoint') for name in names
        ])
        mean_cd = (mean_q1 - mean_q3) / (mean_q1 + mean_q3)
        mean_data = {
            'Mean of Mean': mean_mean,
            'StD of Mean': mean_std,
            'CfVar of Mean': mean_cv,
            'CfDis of Mean': mean_cd
        }
        df = pd.DataFrame(mean_data, index=names)
        df.to_csv('{0}/IJ Ramping/{1}_mean_summary.txt'.format(
            folder, param.lower()),
                  sep='\t')

        # Plot Mean values
        plots_folder = folder + '/IJ Ramping/' + param + '/Plots_Mean/'
        if not os.path.exists(plots_folder):
            os.makedirs(plots_folder)

        mean_data = [[
            pd.read_csv(fld + '/summary.txt', sep='\t', index_col=0,
                        header=0).loc[name]['Mean'] for fld in flds
        ] for name in names]

        for i, x in enumerate(mean_data):
            plt.figure()
            plt.plot(x, y_axis, ' o')
            plt.xlabel(names[i])
            plt.ylabel(param)
            plt.tight_layout()
            plt.savefig(plots_folder + names[i] + '.png')
            plt.close()
Ejemplo n.º 6
0
def mean_var(mean_mat, method, corr_pckl, scint):
    # Create new column
    clmn = method + ' RMSE'
    mean_mat[clmn] = np.nan

    # Get mean vertical values
    for i, x in enumerate(mean_mat['Test']):
        pckl_file = '{0}{1}.pckl'.format(corr_pckl,
                                         x).replace('XYZ', method.lower())
        if 'T' in method:
            mean_mat.iloc[i, -1] = get_rmse(pckl_file, method[:-1])
        else:
            mean_mat.iloc[i, -1] = get_rmse(pckl_file, method)

    # Calculate the mean values as needed
    pivot_mean = mean_mat.pivot_table(values=clmn,
                                      index=['KI %'],
                                      columns=['Nozzle Diameter (um)'],
                                      aggfunc=np.nanmean)

    # Calculate the standard deviation values as needed
    pivot_stdv = mean_mat.pivot_table(values=clmn,
                                      index=['KI %'],
                                      columns=['Nozzle Diameter (um)'],
                                      aggfunc=np.nanstd)

    # Create arrays from the pivot tables
    # Could plot directly from table but I didn't want to delve too deep
    RMSE_700 = pivot_mean[700]
    StDv_700 = pivot_stdv[700]
    RMSE_700_fit = polyfit(KIconc, RMSE_700, 1)
    RMSE_700_r2 = 100 * RMSE_700_fit['determination']
    RMSE_700_label = 'y$_{{{0}}}$ = {1:.3f}x + {2:.3f}; R$^2$ {3:.0f}%' \
                     .format(700, RMSE_700_fit['polynomial'][0],
                             RMSE_700_fit['polynomial'][1], RMSE_700_r2)

    RMSE_2000 = pivot_mean[2000]
    StDv_2000 = pivot_stdv[2000]
    RMSE_2000_fit = polyfit(KIconc, RMSE_2000, 1)
    RMSE_2000_r2 = 100 * RMSE_2000_fit['determination']
    RMSE_2000_label = 'y$_{{{0}}}$ = {1:.3f}x + {2:.3f}; R$^2$ {3:.0f}%' \
                      .format(2000, RMSE_2000_fit['polynomial'][0],
                              RMSE_2000_fit['polynomial'][1], RMSE_2000_r2)

    # RMSE plot (Peak = markers filled, Ellipse = markers empty)
    if 'Peak' in method:
        fstyle = 'full'
    else:
        fstyle = 'none'

    plt.figure()
    plt.plot(KIconc,
             RMSE_700,
             fillstyle=fstyle,
             color='olivedrab',
             marker='s',
             label='700 um')
    plt.plot(KIconc,
             RMSE_700_fit['function'](KIconc),
             color='teal',
             label=RMSE_700_label)
    plt.plot(KIconc,
             RMSE_2000,
             fillstyle=fstyle,
             color='indianred',
             marker='^',
             label='2000 um')
    plt.plot(KIconc,
             RMSE_2000_fit['function'](KIconc),
             color='darkorange',
             label=RMSE_2000_label)
    plt.legend()
    plt.title('{0} - {1} RMSE'.format(scint, method))
    plt.xlabel('KI (%)')
    plt.ylabel(r'RMSE ($\mu$m)')
    plt.savefig('{0}/{1}_mean_{2}_.png'.format(plt_fld, scint, method))
    plt.close()

    pckl_file = '{0}/{1}_mean_{2}.pckl'.format(plt_fld, scint, method)
    with open(pckl_file, 'wb') as f:
        pickle.dump([mean_mat, RMSE_700, StDv_700, RMSE_2000, StDv_2000], f)

    return mean_mat, RMSE_700, RMSE_2000