def plot_freq_errors(ax3, profilesAxaxis, abs_errors_profiles,
                     meanabserrorsprofiles, maxabserrorprofiles):
    """
    """
    ylim = 0.67
    boxwidth = 2.2
    font = {
        'size': 14,
    }

    ax3.plot(profilesAxaxis,
             meanabserrorsprofiles,
             linestyle='',
             marker='.',
             color='b')
    # ax3.plot(profilesAxaxis, maxabserrorprofiles, 'r--') # dotted line for min and max

    # plot boxlabels
    boxypos = np.asarray(maxabserrorprofiles) + 0.015
    boxxpos = np.asarray(profilesAxaxis) - boxwidth / 2.2
    for i, boxlabel in enumerate(boxlabelsA):
        ax3.text(boxxpos[i], boxypos[i], boxlabel, fontdict=font)

    bp = ax3.boxplot(
        abs_errors_profiles,
        positions=profilesAxaxis,
        widths=boxwidth,
    )

    _initaxis([ax3],
              legend='upper right',
              xlabel='frequency of reference pattern (bpm)',
              ylabel='absolute error (mm)')
    major_ticks = np.arange(40, 100, 10)
    ax3.set_ylim((0, ylim))
    ax3.set_xlim(40, 100)
    ax3.set_xticks(major_ticks)
    plt.setp(bp['boxes'], color='k')
    plt.setp(bp['whiskers'], color='k', linestyle='-')
    plt.setp(bp['fliers'], color='k', marker='+')
    plt.setp(bp['medians'], color='k')

    return bp
def plot_ampl_errors(ax1, profilesBxaxis, abs_errors_profiles,
                     meanabserrorsprofiles, maxabserrorprofiles):
    """
    """
    xlim = 1.45
    ylim = 0.67
    boxwidth = 0.06
    font = {
        'size': 14,
    }

    #plot line x=y
    ax1.plot([0, xlim], [0, xlim], 'k--')
    ax1.plot([0, xlim], [0, xlim / 2], 'k-.')

    #plot text x=y
    ax1.text(0.78, 0.5, 'y=0.5x', fontdict=font)
    ax1.text(0.39, 0.5, 'y=x', fontdict=font)

    ax1.plot(profilesBxaxis,
             meanabserrorsprofiles,
             linestyle='',
             marker='.',
             color='b')
    # ax1.plot(profilesBxaxis, maxabserrorprofiles, 'r--') # dotted line for min and max
    # meanvalues = np.asarray(meanabserrorsprofiles)
    # stdvalues = np.asarray(stdabserrorprofiles)
    # ax1.fill_between(profilesBxaxis, meanvalues-stdvalues,
    #             meanvalues+stdvalues, color='r', alpha=0.2)

    # plot boxlabels
    boxypos = np.asarray(maxabserrorprofiles) + 0.015
    boxxpos = np.asarray(profilesBxaxis) - boxwidth / 2.2
    for i, boxlabel in enumerate(boxlabelsB):
        ax1.text(boxxpos[i], boxypos[i], boxlabel, fontdict=font)

    bp = ax1.boxplot(
        abs_errors_profiles,
        positions=profilesBxaxis,
        widths=boxwidth,
        patch_artist=True,  # fill with color) 
    )  # dict

    # fill with colors
    gray = (211 / 255, 211 / 255, 211 / 255, 0.5)  # with alpha
    colors = ['white', 'white', 'white', 'white', gray, gray, 'white']
    for patch, color in zip(bp['boxes'], colors):
        patch.set_facecolor(color)

    _initaxis([ax1],
              legend='upper right',
              xlabel='amplitude of reference pattern (mm)',
              ylabel='absolute error (mm)')

    major_ticks = np.arange(0, xlim, 0.2)
    ax1.set_ylim((0, ylim))
    ax1.set_xlim(-0.04, xlim)
    ax1.set_xticks(major_ticks)
    # plt.setp(bp['boxes'], color='k')
    plt.setp(bp['whiskers'], color='k', linestyle='-')
    plt.setp(bp['fliers'], color='k', marker='+')
    # Tweak spacing to prevent clipping of ylabel
    # plt.subplots_adjust(left=0.15)

    return bp
Exemple #3
0
def show_periods_cam123(ttperiodsC1,
                        ttperiodsC2,
                        ttperiodsC3,
                        pperiodsC1,
                        pperiodsC2,
                        pperiodsC3,
                        shiftt0=True,
                        ax=None):

    colors = ['#d7191c', '#fdae61', '#2c7bb6'
              ]  # http://colorbrewer2.org/#type=diverging&scheme=RdYlBu&n=5
    for p, ttperiod in enumerate(ttperiodsC1):
        if shiftt0 == True:
            offsett = ttperiod[0]  # start at t=0
        else:
            offsett = 0  # no shift
        if p == 0:
            ax.plot(ttperiod - offsett,
                    pperiodsC1[p],
                    '.-',
                    color=colors[0],
                    alpha=0.5,
                    label='camera reference 1')
        else:
            ax.plot(ttperiod - offsett,
                    pperiodsC1[p],
                    '.-',
                    color=colors[0],
                    alpha=0.5)
    for p, ttperiod in enumerate(ttperiodsC2):
        if shiftt0 == True:
            offsett = ttperiod[0]  # start at t=0
        else:
            offsett = 0  # no shift
        if p == 0:
            ax.plot(ttperiod - offsett,
                    pperiodsC2[p],
                    '.-',
                    color=colors[1],
                    alpha=0.5,
                    label='camera reference 2')
        else:
            ax.plot(ttperiod - offsett,
                    pperiodsC2[p],
                    '.-',
                    color=colors[1],
                    alpha=0.5)
    for p, ttperiod in enumerate(ttperiodsC3):
        if shiftt0 == True:
            offsett = ttperiod[0]  # start at t=0
        else:
            offsett = 0  # no shift
        if p == 0:
            ax.plot(ttperiod - offsett,
                    pperiodsC3[p],
                    '.-',
                    alpha=0.5,
                    color=colors[2],
                    label='camera reference 3')
        else:
            ax.plot(ttperiod - offsett,
                    pperiodsC3[p],
                    '.-',
                    alpha=0.5,
                    color=colors[2])

    ax.set_ylim((-0.02, ylim))
    ax.set_xlim(-0.1, max((ttperiod - offsett)) + 0.1)
    _initaxis([ax],
              legend='upper right',
              xlabel='time (s)',
              ylabel='position (mm)')
Exemple #4
0

f1 = plt.figure(num=1, figsize=(7.6, 5))
ax1 = f1.add_subplot(111)
xlim = 1.45
ylim = 0.65

ax1.plot(profilesBxaxis, meanabserrorsprofiles, linestyle='r-', marker='.', color='b') 
# ax1.plot(profilesBxaxis, maxabserrorprofiles, 'r--') # dotted line for min and max
# meanvalues = np.asarray(meanabserrorsprofiles)
# stdvalues = np.asarray(stdabserrorprofiles)
# ax1.fill_between(profilesBxaxis, meanvalues-stdvalues,     
#             meanvalues+stdvalues, color='r', alpha=0.2)
bp = plt.boxplot(abs_errors_profiles, positions=profilesBxaxis, widths=0.06)

_initaxis([ax1], legend='upper right', xlabel='amplitude of pattern (mm)', 
          ylabel='absolute error (mm)')
major_ticks = np.arange(0, xlim, 0.2)
ax1.set_ylim((0, ylim))
ax1.set_xlim(-0.04,xlim)
ax1.set_xticks(major_ticks)
plt.setp(bp['boxes'], color='k')
plt.setp(bp['whiskers'], color='k', linestyle='-')
plt.setp(bp['fliers'], color='k', marker='+')

# save
if savefig:
    f1.savefig(os.path.join(dirsave, 'abserrorgraphampl.pdf'), papertype='a0', dpi=300)


## frequency patterns
abs_errors_profiles = read_error_cam123(exceldir, workbook, profilesA)
Exemple #5
0
def show_period_cam123_with_bounds(ttperiodsC1,
                                   ttperiodsC2,
                                   ttperiodsC3,
                                   pperiodsC1,
                                   pperiodsC2,
                                   pperiodsC3,
                                   shiftt0=True,
                                   fignum=3):

    pperiodsC1mean, pperiodsC1std = np.mean(pperiodsC1,
                                            axis=0), np.std(pperiodsC1, axis=0)
    pperiodsC2mean, pperiodsC2std = np.mean(pperiodsC2,
                                            axis=0), np.std(pperiodsC2, axis=0)
    pperiodsC3mean, pperiodsC3std = np.mean(pperiodsC3,
                                            axis=0), np.std(pperiodsC3, axis=0)

    # pperiodsC2q25 = np.percentile(pperiodsC2, 25, axis=0)
    # pperiodsC2q75 = np.percentile(pperiodsC2, 75, axis=0)

    # shift to t0 = 0
    if shiftt0 == True:
        ttperiodsC1 = [tt - tt[0] for tt in ttperiodsC1]
        ttperiodsC2 = [tt - tt[0] for tt in ttperiodsC2]
        ttperiodsC3 = [tt - tt[0] for tt in ttperiodsC3]
    # get mean tt per cam
    ttperiodsC1mean = np.mean(ttperiodsC1, axis=0)
    ttperiodsC2mean = np.mean(ttperiodsC2, axis=0)
    ttperiodsC3mean = np.mean(ttperiodsC3, axis=0)
    # get average of all tt periods of the 3 cams
    ttperiodsC123t0 = np.concatenate((ttperiodsC1, ttperiodsC2, ttperiodsC3))
    ttperiodsC123mean = np.mean(ttperiodsC123t0, axis=0)

    # get average of all pp periods of the 3 cams
    pperiodsC123 = np.concatenate((pperiodsC1, pperiodsC2, pperiodsC3))
    pperiodsC123mean, pperiodsC123std = np.mean(pperiodsC123,
                                                axis=0), np.std(pperiodsC123,
                                                                axis=0)

    # plot
    f3 = plt.figure(figsize=(18, 5.5), num=fignum)
    plt.clf()
    ax2 = f3.add_subplot(121)

    colors = ['#d7191c', '#fdae61', '#2c7bb6'
              ]  # http://colorbrewer2.org/#type=diverging&scheme=RdYlBu&n=5
    ax2.plot(ttperiodsC1mean,
             pperiodsC1mean,
             '.-',
             color=colors[0],
             label='camera reference 1')
    ax2.fill_between(ttperiodsC1mean,
                     pperiodsC1mean - pperiodsC1std,
                     pperiodsC1mean + pperiodsC1std,
                     color=colors[0],
                     alpha=0.2)
    ax2.plot(ttperiodsC2mean,
             pperiodsC2mean,
             '.-',
             color=colors[1],
             label='camera reference 2')
    ax2.fill_between(ttperiodsC2mean,
                     pperiodsC2mean - pperiodsC2std,
                     pperiodsC2mean + pperiodsC2std,
                     color=colors[1],
                     alpha=0.3)
    # ax2.fill_between(ttperiodsC2mean, pperiodsC2q25, pperiodsC2q75,
    #                 color=colors[1], alpha=0.3)
    ax2.plot(ttperiodsC3mean,
             pperiodsC3mean,
             '.-',
             color=colors[2],
             label='camera reference 3')
    ax2.fill_between(ttperiodsC3mean,
                     pperiodsC3mean - pperiodsC3std,
                     pperiodsC3mean + pperiodsC3std,
                     color=colors[2],
                     alpha=0.2)

    _initaxis([ax2],
              legend='upper right',
              xlabel='time (s)',
              ylabel='position (mm)')
    ax2.set_ylim((0, ylim))
    ax2.set_xlim(-0.1, max(ttperiodsC3mean) + 0.1)

    # add plot of average with bounds
    ax3 = f3.add_subplot(122)
    ax3.plot(ttperiodsC123mean,
             pperiodsC123mean,
             '.-',
             color='k',
             label='camera reference mean')
    ax3.fill_between(ttperiodsC123mean,
                     pperiodsC123mean - pperiodsC123std,
                     pperiodsC123mean + pperiodsC123std,
                     color='k',
                     alpha=0.2)

    _initaxis([ax3],
              legend='upper right',
              xlabel='time (s)',
              ylabel='position (mm)')
    ax3.set_ylim((0, ylim))
    ax3.set_xlim(-0.1, max(ttperiodsC123mean) + 0.1)

    return ttperiodsC123mean, pperiodsC123mean, pperiodsC123std
Exemple #6
0
def show_period_cam123bestCut_with_bounds(ttperiodmeanC1,
                                          ttperiodmeanC2,
                                          ttperiodmeanC3,
                                          ttperiodmeanC123,
                                          pperiodsC1bestCut,
                                          pperiodsC2bestCut,
                                          pperiodsC3bestCut,
                                          pperiodsC123bestCut,
                                          tt1a=None,
                                          aa1a=None,
                                          fignum=4,
                                          save=True):

    from stentseg.utils.aortamotionpattern import get_motion_pattern, plot_pattern_plt

    # mean and std per cam
    pperiodsC1mean, pperiodsC1std = np.nanmean(
        pperiodsC1bestCut, axis=0), np.nanstd(pperiodsC1bestCut, axis=0)
    pperiodsC2mean, pperiodsC2std = np.nanmean(
        pperiodsC2bestCut, axis=0), np.nanstd(pperiodsC2bestCut, axis=0)
    pperiodsC3mean, pperiodsC3std = np.nanmean(
        pperiodsC3bestCut, axis=0), np.nanstd(pperiodsC3bestCut, axis=0)

    # get average of all pp periods of the 3 cams
    pperiodsC123mean = np.nanmean(pperiodsC123bestCut, axis=0)
    pperiodsC123std = np.nanstd(pperiodsC123bestCut, axis=0)

    # plot
    f3 = plt.figure(figsize=(18, 5.5), num=fignum)
    plt.clf()
    ax2 = f3.add_subplot(121)

    # repeat period
    ttperiodmeanC1rep, pperiodsC1mean, pperiodsC1std = repeatCamPeriod(
        ttperiodmeanC1, pperiodsC1mean, pperiodsC1std)
    ttperiodmeanC2rep, pperiodsC2mean, pperiodsC2std = repeatCamPeriod(
        ttperiodmeanC2, pperiodsC2mean, pperiodsC2std)
    ttperiodmeanC3rep, pperiodsC3mean, pperiodsC3std = repeatCamPeriod(
        ttperiodmeanC3, pperiodsC3mean, pperiodsC3std)

    # # modify repeated for manual lag shift (for revision fig 2 B5)
    # ttperiodmeanC1rep = ttperiodmeanC1rep[:-1]
    # pperiodsC1mean = pperiodsC1mean[1:]
    # pperiodsC1std = pperiodsC1std[1:]

    # add input function simulator; first run gauspatterns.py
    if tt1a:
        plot_pattern_plt(*(tt1a, aa1a), label='input B5', ls='--', mark=False)

    colors = ['#d7191c', '#fdae61', '#2c7bb6'
              ]  # http://colorbrewer2.org/#type=diverging&scheme=RdYlBu&n=5
    ax2.plot(ttperiodmeanC1rep,
             pperiodsC1mean,
             '.-',
             color=colors[0],
             label='output day 1 (camera)')
    ax2.fill_between(ttperiodmeanC1rep,
                     pperiodsC1mean - pperiodsC1std,
                     pperiodsC1mean + pperiodsC1std,
                     color=colors[0],
                     alpha=0.2)
    ax2.plot(ttperiodmeanC2rep,
             pperiodsC2mean,
             '.-',
             color=colors[1],
             label='output day 2 (camera)')
    ax2.fill_between(ttperiodmeanC2rep,
                     pperiodsC2mean - pperiodsC2std,
                     pperiodsC2mean + pperiodsC2std,
                     color=colors[1],
                     alpha=0.3)
    # ax2.fill_between(ttperiodmeanC2rep, pperiodsC2q25, pperiodsC2q75,
    #                 color=colors[1], alpha=0.3)
    ax2.plot(ttperiodmeanC3rep,
             pperiodsC3mean,
             '.-',
             color=colors[2],
             label='output day 3 (camera)')
    ax2.fill_between(ttperiodmeanC3rep,
                     pperiodsC3mean - pperiodsC3std,
                     pperiodsC3mean + pperiodsC3std,
                     color=colors[2],
                     alpha=0.2)

    _initaxis([ax2],
              legend='upper right',
              xlabel='time (s)',
              ylabel='position (mm)')
    ax2.set_ylim((0, ylim))
    # ax2.set_xlim(-0.1,max(ttperiodmeanC3rep)+0.1)
    xlim = 1.54  # 1.5, 2.1, 1.1; 1.54 for fig 2 paper revision
    major_ticks = np.arange(0, xlim, 0.2)
    ax2.set_xlim(-0.02, xlim)
    ax2.set_xticks(major_ticks)

    # add plot of average with bounds
    ax3 = f3.add_subplot(122)
    # add input function simulator
    if tt1a:
        plot_pattern_plt(*(tt1a, aa1a), label='input B5',
                         ls='--')  # (A1: A=1.0, T=1.2)')

    ttperiodmeanC123rep, pperiodsC123meanRep, pperiodsC123stdRep = repeatCamPeriod(
        ttperiodmeanC123, pperiodsC123mean, pperiodsC123std, mark=True)
    ax3.plot(ttperiodmeanC123rep,
             pperiodsC123meanRep,
             '.-',
             color='k',
             label='output mean (camera)')
    ax3.fill_between(ttperiodmeanC123rep,
                     pperiodsC123meanRep - pperiodsC123stdRep,
                     pperiodsC123meanRep + pperiodsC123stdRep,
                     color='k',
                     alpha=0.2)

    _initaxis([ax3],
              legend='upper right',
              xlabel='time (s)',
              ylabel='position (mm)')
    ax3.set_ylim((0, ylim))
    # ax3.set_xlim(-0.1,max(ttperiodmeanC123rep)+0.1)
    ax3.set_xlim(-0.02, xlim)
    ax3.set_xticks(major_ticks)

    if save:
        f3.savefig(os.path.join(dirsave, 'simInputOutput.png'),
                   papertype='a0',
                   dpi=600)

    return pperiodsC123mean, pperiodsC123std, pperiodsC123meanRep, pperiodsC123stdRep, ttperiodmeanC123rep
Exemple #7
0
    def plot_curvature_during_cycle(self,
                                    patients=['LSPEAS_001'],
                                    analysis=[''],
                                    ctcode='discharge',
                                    ring='R1',
                                    ylim=[0, 2],
                                    ylimRel=[-0.1, 0.1],
                                    saveFig=False):
        """ plot change in curvature during the cardiac cycle for point with max curvature
        change at ring or ring quadrants at a certain time point (CT scan)
        * ctcode = discharge, 1month, 6 months, ... 24months
        * analysis = '' for ring or Q1, Q2, Q3, Q4
        """
        self.curveAll = {}
        self.curveRelAll = {}

        # init figure
        self.f1 = plt.figure(figsize=(11.5, 4.2))
        xlabels = ['0', '10', '20', '30', '40', '50', '60', '70', '80', '90']
        xrange = range(
            1, 1 + len(xlabels))  # not start from x=0 to play with margin xlim
        fontsize1 = self.fontsize1
        fontsize2 = self.fontsize2

        # init axis
        factor = 1.33  # 1.36
        gs = gridspec.GridSpec(1, 2, width_ratios=[1,
                                                   factor])  # plot right wider
        ax1 = plt.subplot(gs[0])
        plt.xticks(xrange, xlabels, fontsize=fontsize1)
        ax2 = plt.subplot(gs[1])
        plt.xticks(xrange, xlabels, fontsize=fontsize1)

        ax1.set_ylabel('Curvature (cm$^{-1}$)',
                       fontsize=fontsize2)  # absolute curvature values
        ax1.set_ylim(ylim)
        ax1.set_xlim([0.8, len(xlabels) + 0.2])  # xlim margins 0.2
        ax1.set_xlabel('Phase in cardiac cycle', fontsize=fontsize2)

        ax2.set_ylabel('Relative curvature change (cm$^{-1}$)',
                       fontsize=fontsize2)  # relative dist from avgreg
        ax2.set_ylim(ylimRel)
        ax2.set_xlim([0.8, len(xlabels) * factor + 0.2
                      ])  # xlim margins 0.2; # longer for legend
        ax2.set_xlabel('Phase in cardiac cycle', fontsize=fontsize2)

        # plot init
        lw = 1
        alpha = 0.9
        ls = '-'
        markers = ['o', 'D', '^', 'o', 's']
        colorsdirections = [  # from phantom paper in vivo plots
            'k',
            '#d73027',  # 1 red
            '#fc8d59',  # orange
            '#91bfdb',  # blue
            '#4575b4'  # 5
        ]
        Qnames = ['Full ring', 'QP', 'QA', 'QL', 'QR']
        Qs = ['', 'Q1', 'Q3', 'Q2', 'Q4']

        for patient in patients:
            for a in analysis:
                # read workbook
                filename = '{}_ringdynamics_{}.xlsx'.format(patient, ctcode)
                workbook_stent = os.path.join(self.exceldir, filename)
                wb = openpyxl.load_workbook(workbook_stent, data_only=True)
                sheetname = 'Curvature{}{}'.format(a, ring)
                sheet = wb.get_sheet_by_name(sheetname)
                # read cuvrature of point with max change
                rowstart = 12  # as in excel; during cycle
                colstart = 1  # 1 = B
                # read change
                curvatures = sheet.rows[rowstart - 1][colstart:colstart + 10]
                curvatures = [obj.value for obj in curvatures]
                # convert to array
                curvatures = np.asarray(curvatures)
                # get curvature at mid heart cycle
                avgcurvature = np.nanmean(curvatures)
                # relative curvatures from avgreg
                curveRel = curvatures - avgcurvature

                self.curveAll['{}_{}'.format(patient, a)] = curvatures
                self.curveRelAll['{}_{}'.format(patient, a)] = curveRel

                # plotting
                # color1 = color_per_patient(patient)
                color1 = colorsdirections[Qs.index(a)]
                marker = markers[Qs.index(a)]
                label = Qnames[Qs.index(a)]
                ax1.plot(xrange,
                         curvatures,
                         ls=ls,
                         lw=lw,
                         marker=marker,
                         color=color1,
                         label=label,
                         alpha=alpha)
                ax2.plot(xrange,
                         curveRel,
                         ls=ls,
                         lw=lw,
                         marker=marker,
                         color=color1,
                         label=label,
                         alpha=alpha)

        ax2.legend(loc='upper right',
                   fontsize=self.fontsize4,
                   numpoints=1,
                   title='Legend:')
        _initaxis([ax1, ax2], axsize=fontsize1)

        if saveFig:
            plt.savefig(os.path.join(
                self.dirsaveIm,
                'plot_curvature_over_cycle_{}.png'.format(analysis)),
                        papertype='a0',
                        dpi=600)
Exemple #8
0
    ax0.scatter(ttPeriodStarts2 - offsett2,
                np.array(peakmin2)[:, 1],
                color='green')
    ax0.scatter(ttPeriodPeaks2 - offsett2, np.array(peakmax2)[:, 1], color='k')
    ax0.plot(time_all_cam3t0,
             pos_all_cam3,
             'b.-',
             alpha=0.5,
             label='camera reference 3')
    ax0.scatter(ttPeriodStarts3 - offsett3,
                np.array(peakmin3)[:, 1],
                color='green')
    ax0.scatter(ttPeriodPeaks3 - offsett3, np.array(peakmax3)[:, 1], color='k')

    _initaxis([ax0],
              legend='upper right',
              xlabel='time (s)',
              ylabel='position (mm)')
    ax0.set_ylim((-0.02, ylim))
    ax0.set_xlim(xlim)

    ##  overlay signals with consecutive periods, smallest rmse

    ax1 = f1.add_subplot(212)

    numpeaks = min(len(peakmin1), len(peakmin2),
                   len(peakmin3))  # num peaks to analyse

    peakstart = int(peakmin2[0, 0])  # cam2 as ref
    peakend = int(peakmin2[numpeaks - 1, 0])
    tc2, pc2 = time_all_cam2t0[peakstart:peakend +
                               1], pos_all_cam2[peakstart:peakend + 1]
Exemple #9
0
def plot_tilt_lines(exceldir, workbook_stent, workbook_renal_cll, obs='obsMK',
                    ylim=[0,50], saveFig=False):
    """ Plot tilt over time for all patients
    uses the functions above to get data for each time point
    """
    ctcodes=['D', '1M', '6M', '12M', '24M']
    
    # data ring dimension
    PPVVdistances = read_distance_peaks_and_valleys(exceldir, workbook_stent)
    
    tiltPeaks_all = [] # all scans
    tiltValleys_all = []
    for ctcode in ctcodes:
        try:
            # data centerline renal
            VR, PA, VL, PP = read_dist_to_renal(exceldir, workbook_renal_cll, ctcode, obs=obs)
            # tilt
            tiltPeaks, tiltValleys = tilt_peaks_valleys_centerline(PPVVdistances, 
                                        ctcode, VR, PA, VL, PP)
        except TypeError: # when excel cells are Nonetype (empty/not scored)
            tiltPeaks = np.empty((15, 1,)) * np.nan
            tiltValleys = np.empty((15, 1,)) * np.nan
        tiltPeaks_all.append(tiltPeaks)
        tiltValleys_all.append(tiltValleys)
    
    # init figure
    f1 = plt.figure(num=1, figsize=(11.7, 5)) # 11.6,4.6 or 5
    xlabels = ['D', '1M', '6M', '12M', '24M']
    xrange = range(1,1+len(xlabels)) # not start from x=0 to play with margin xlim
    
    # init axis
    gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1.3]) # 6.4/5.2
    ax1 = plt.subplot(gs[0])
    plt.xticks(xrange, xlabels)
    ax2 = plt.subplot(gs[1])
    plt.xticks(xrange, xlabels)
    
    # Set the font name and size for axis tick labels
    fontName = "Arial" # most similar to Helvetica (not available), which Matlab uses
    ax1.set_ylabel('Tilt peaks ($^\circ$)', fontsize=15, fontname=fontName)
    ax2.set_ylabel('Tilt valleys ($^\circ$)', fontsize=15, fontname=fontName)
    ax1.set_ylim(ylim)
    ax2.set_ylim(ylim)
    ax1.set_yticks(np.arange(0,ylim[1], 5)) # steps of 5
    ax2.set_yticks(np.arange(0,ylim[1], 5))
    ax1.set_xlim([0.8, len(xlabels)+0.2]) # xlim margins 0.2
    ax2.set_xlim([0.8, len(xlabels)+1.4]) # longer for legend
    
    # lines and colors; 12-class Paired (ring evolution paper)
    colors = itertools.cycle(['#a6cee3','#1f78b4','#b2df8a','#33a02c',
    '#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99','#b15928'])
    # lStyles = ['-', '--']
    markers = ['D', 'o', '^', 's', '*'] # for device size
    lw = 1
    alpha = 1
    ls = '-'
    ls2 = '--'
    
    IDlegend =['01', '02','03', '05', '08', '09','11','15','17',	
                '18', '19', '20', '21', '22', '25']
    ptdevicesizes = [25.5, 30.5, 30.5, 28, 28, 30.5, 28, 28, 32, 30.5, 30.5, 34,
                        30.5, 28, 34]
    
    # to array
    tiltPeaks_all = np.stack(tiltPeaks_all, axis=1) # 15x(5x1) array # np.stack from 1.10.0
    tiltValleys_all = np.stack(tiltValleys_all, axis=1)
    
    tiltPeaks_mean = np.nanmean(tiltPeaks_all, axis=0)
    tiltValleys_mean = np.nanmean(tiltValleys_all, axis=0)
    
    tiltPeaks_median = np.nanmedian(tiltPeaks_all, axis=0)
    tiltValleys_median = np.nanmedian(tiltValleys_all, axis=0)
    
    # plot per patient the data of all CT scans
    for i, tiltPeak in enumerate(tiltPeaks_all):
        
        patient = IDlegend[i]
        devicesize = ptdevicesizes[i]
        color = next(colors)
        if devicesize == 25.5:
            marker = markers[0]
            olb = 'OLB25' 
        elif devicesize == 28:
            marker = markers[1]
            olb = 'OLB28' 
        elif devicesize == 30.5:
            marker = markers[2]
            olb = 'OLB30'
        elif devicesize == 32:
            marker = markers[3]
            olb = 'OLB32'
        else:
            marker = markers[4]
            olb = 'OLB34'
        
        # plot tilt
        ax1.plot(xrange, tiltPeaks_all[i], ls=ls, lw=lw, marker=marker, color=color, 
        label='%s:%s' % (patient[-2:], olb), alpha=alpha)
        ax2.plot(xrange, tiltValleys_all[i], ls=ls, lw=lw, marker=marker, color=color, 
        label='%s:%s' % (patient[-2:], olb), alpha=alpha)
    # plot mean (median?)
    ax1.plot(xrange, tiltPeaks_median, ls=ls2, lw=2, marker='p', color='k', 
    label='Median', alpha=alpha)
    ax2.plot(xrange, tiltValleys_median, ls=ls2, lw=2, marker='p', color='k', 
    label='Median', alpha=alpha)
    
    # Set the font name and for axis tick labels
    for tick in ax1.get_xticklabels():
        tick.set_fontname(fontName)
    for tick in ax2.get_xticklabels():
        tick.set_fontname(fontName)
    for tick in ax1.get_yticklabels():
        tick.set_fontname(fontName)
    for tick in ax2.get_yticklabels():
        tick.set_fontname(fontName)
    
    ax2.legend(loc='upper right', fontsize=10, numpoints=1)
    _initaxis([ax1, ax2], axsize=14) # sets also xtick ytick fontsize
    
    if saveFig:
        plt.savefig(os.path.join(dirsaveIm, 
        'plot_pp_vv_tilt.png'), papertype='a0', dpi=600)
    
    return f1, tiltPeaks_all, tiltValleys_all
         'o-',
         color=colors[1],
         alpha=alpha2,
         label='algorithm-Aquilion')  #mean of ring-stent points
ax4.plot(ttCamSrep, pzMax2, '-.',
         color=colors[1])  # dotted line for min and max
ax4.plot(ttCamSrep, pzMin2, '-.', color=colors[1])
ax4.fill_between(ttCamSrep,
                 pzMean2 - pzStd2,
                 pzMean2 + pzStd2,
                 color=colors[1],
                 alpha=alpha1)

_initaxis([ax4],
          legend='upper right',
          xlabel='time (s)',
          ylabel='position (mm)',
          legendtitle=profile)
major_ticksx = np.arange(0, xlim, 0.2)
major_ticksy = np.arange(0, ylim, 0.2)
ax4.set_ylim((0, ylim))
ax4.set_xlim(-0.02, xlim)
ax4.set_xticks(major_ticksx)
ax4.set_yticks(major_ticksy)

# store fig
if saveFig:
    name = 'alg_cam123mean_{}.pdf'.format(sheetProfile)
    f1.savefig(os.path.join(dirsave, name), papertype='a0', dpi=600)

# ============================================
    ax1.plot(xrange,
             ppEr[:, 1],
             '--',
             color=colors[1],
             marker=marker,
             label='reference - y')
    ax1.plot(xrange,
             ppEr[:, 2],
             '--',
             color=colors[2],
             marker=marker,
             label='reference - z')

    _initaxis([ax1],
              legend='upper right',
              xlabel='phases of cardiac cycle',
              ylabel='relative position (mm)',
              legendtitle='Landmark {}'.format(landmarknr))
    major_ticksx = np.arange(0, xlim, 10)
    major_ticksy = np.arange(-1, ylim, 0.2)
    #ax4.set_ylim((0, ylim))
    ax1.set_xlim(-0.5, xlim)
    ax1.set_xticks(major_ticksx)
    ax1.set_yticks(major_ticksy)

    # plot relative 3D displacement for one landmark
    fignum = 2
    f2 = plt.figure(figsize=(9, 5.5), num=fignum)
    plt.clf()
    ax1 = f2.add_subplot(111)
    def plot_displacement_mean_over_time(self,
                                         ylim=[0, 2],
                                         saveFig=False,
                                         showlegend=True):
        """ Plot mean displacement rings for full ring and all 4 directions using the 
        mean of the patients
        """

        # init figure
        f1 = plt.figure(figsize=(10, 9))  # from 11.6 to 10 to place legend
        xlabels = ['D', '1M', '6M', '12M', '24M']
        xrange = range(
            1, 1 + len(xlabels))  # not start from x=0 to play with margin xlim

        # init axis
        ax1 = f1.add_subplot(3, 2, 1)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)
        ax2 = f1.add_subplot(3, 2, 2)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)
        ax3 = f1.add_subplot(3, 2, 3)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)
        ax4 = f1.add_subplot(3, 2, 4)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)
        ax5 = f1.add_subplot(3, 2, 5)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)
        ax6 = f1.add_subplot(3, 2, 6)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)
        # ax7 = f1.add_subplot(4,2,7)
        # plt.xticks(xrange, xlabels, fontsize = self.fontsize5)
        # ax8 = f1.add_subplot(4,2,8)
        # plt.xticks(xrange, xlabels, fontsize = self.fontsize5)

        axes = [ax1, ax2, ax3, ax4, ax5, ax6]  #, ax7, ax8]

        yname2 = 'mm'
        yname = 'Displacement'

        for ax in axes:
            ax.set_ylabel('{} ({})'.format(yname, yname2),
                          fontsize=self.fontsize6)
            ax.set_ylim(ylim)
            ax.set_xlim([0.8, len(xlabels) + 0.2])  # xlim margins 0.2

        # set markers
        markers = ['p', 'D', '^', 'o', 's']
        colorsdirections = self.colorsdirections
        # capsize = 8
        lss = ['--', '-', '-', '-', '-']
        # lw = 1
        # lw2 = 1.1
        # alpha = 0.7

        Qnames = ['Full ring', 'QP', 'QA', 'QL', 'QR']
        Qs = ['', 'Q1', 'Q3', 'Q2', 'Q4']
        Rs = ['R1', 'R2']

        filedir = os.path.join(self.filedir, self.folderringdynamics,
                               'Displacement')

        def plot_displacement_line_errorbar(filedir,
                                            name,
                                            ax,
                                            Q,
                                            R,
                                            xrange,
                                            labelname,
                                            ls,
                                            marker,
                                            color,
                                            alpha=0.7,
                                            capsize=8,
                                            lw=1,
                                            lw2=1.1):
            """
            """
            filename = 'Displacement{}{}_{}_pts'.format(Q, R, name)
            matdict = scipy.io.loadmat(os.path.join(filedir,
                                                    filename + '.mat'))
            var = matdict[filename]
            # plot
            ax.plot(xrange,
                    np.nanmean(var, axis=0),
                    ls=ls,
                    lw=lw,
                    marker=marker,
                    color=color,
                    label=labelname,
                    alpha=alpha)
            ax.errorbar(xrange,
                        np.nanmean(var, axis=0),
                        yerr=np.nanstd(var, axis=0),
                        fmt=None,
                        ecolor=color,
                        capsize=capsize,
                        elinewidth=lw2,
                        capthick=lw2)

        # read mat files
        for i, labelname in enumerate(Qnames):
            R = Rs[0]
            plot_displacement_line_errorbar(filedir, 'x', ax1, Qs[i], R,
                                            xrange, labelname, lss[i],
                                            markers[i], colorsdirections[i])
            plot_displacement_line_errorbar(filedir, 'y', ax3, Qs[i], R,
                                            xrange, labelname, lss[i],
                                            markers[i], colorsdirections[i])
            plot_displacement_line_errorbar(filedir, 'z', ax5, Qs[i], R,
                                            xrange, labelname, lss[i],
                                            markers[i], colorsdirections[i])
            R = Rs[1]
            plot_displacement_line_errorbar(filedir, 'x', ax2, Qs[i], R,
                                            xrange, labelname, lss[i],
                                            markers[i], colorsdirections[i])
            plot_displacement_line_errorbar(filedir, 'y', ax4, Qs[i], R,
                                            xrange, labelname, lss[i],
                                            markers[i], colorsdirections[i])
            plot_displacement_line_errorbar(filedir, 'z', ax6, Qs[i], R,
                                            xrange, labelname, lss[i],
                                            markers[i], colorsdirections[i])

        if showlegend:
            ax2.legend(loc='upper right',
                       fontsize=self.fontsize3,
                       numpoints=1,
                       title='Legend')

        _initaxis(axes, axsize=self.fontsize5)

        if saveFig:
            plt.savefig(os.path.join(self.dirsaveIm,
                                     'plot_ring_quadrants_displacement.png'),
                        papertype='a0',
                        dpi=600)
    def plot_meancurvaturechange(self,
                                 ylim=[0, 0.1],
                                 saveFig=False,
                                 showlegend=True):
        """
        """
        # init figure
        f1 = plt.figure(figsize=(10, 4.6))  # from 11.6 to 10 to place legend
        xlabels = ['D', '1M', '6M', '12M', '24M']
        xrange = range(
            1, 1 + len(xlabels))  # not start from x=0 to play with margin xlim

        # init axis
        ax1 = f1.add_subplot(1, 2, 1)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)
        ax2 = f1.add_subplot(1, 2, 2)
        plt.xticks(xrange, xlabels, fontsize=self.fontsize5)

        axes = [ax1, ax2]

        yname2 = 'cm$^{-1}$'
        yname = 'Curvature change'

        for ax in axes:
            ax.set_ylabel('{} ({})'.format(yname, yname2),
                          fontsize=self.fontsize6)
            ax.set_ylim(ylim)
            ax.set_xlim([0.8, len(xlabels) + 0.2])  # xlim margins 0.2

        # set markers
        markers = ['p', 'D', '^', 'o', 's']
        colorsdirections = self.colorsdirections
        lss = ['--', '-', '-', '-', '-']

        Qnames = ['Full ring', 'QP', 'QA', 'QL', 'QR']
        Qs = ['', 'Q1', 'Q3', 'Q2', 'Q4']
        Rs = ['R1', 'R2']

        filedir = os.path.join(self.filedir, self.folderringdynamics,
                               'Curvature')

        def plot_curvaturechange_line_errorbar(filedir,
                                               name,
                                               ax,
                                               Q,
                                               R,
                                               xrange,
                                               labelname,
                                               ls,
                                               marker,
                                               color,
                                               alpha=0.7,
                                               capsize=8,
                                               lw=1,
                                               lw2=1.1):
            """
            """
            filename = 'Curvature{}{}_{}_pts'.format(Q, R, name)
            matdict = scipy.io.loadmat(os.path.join(filedir,
                                                    filename + '.mat'))
            var = matdict[filename]
            # plot
            ax.plot(xrange,
                    np.nanmean(var, axis=0),
                    ls=ls,
                    lw=lw,
                    marker=marker,
                    color=color,
                    label=labelname,
                    alpha=alpha)
            ax.errorbar(xrange,
                        np.nanmean(var, axis=0),
                        yerr=np.nanstd(var, axis=0),
                        fmt=None,
                        ecolor=color,
                        capsize=capsize,
                        elinewidth=lw2,
                        capthick=lw2)

        # read mat files
        for i, labelname in enumerate(Qnames):
            R = Rs[0]
            plot_curvaturechange_line_errorbar(filedir, 'meanchange', ax1,
                                               Qs[i], R, xrange, labelname,
                                               lss[i], markers[i],
                                               colorsdirections[i])
            R = Rs[1]
            plot_curvaturechange_line_errorbar(filedir, 'meanchange', ax2,
                                               Qs[i], R, xrange, labelname,
                                               lss[i], markers[i],
                                               colorsdirections[i])

        if showlegend:
            ax2.legend(loc='upper right',
                       fontsize=self.fontsize3,
                       numpoints=1,
                       title='Legend')

        _initaxis(axes, axsize=self.fontsize5)

        if saveFig:
            plt.savefig(os.path.join(
                self.dirsaveIm, 'plot_ring_quadrants_meanchangecurvature.png'),
                        papertype='a0',
                        dpi=600)