Пример #1
0
    def test_get_tier_boundaries(self):
        self.failUnlessEqual([int(x) for x in get_tier_boundaries(\
                                 self.withPoint['A'])],
                             [2, 179])

        self.failUnlessEqual([int(x) for x in get_tier_boundaries(\
                                 self.withPoint['P'])],
                             [2, 32])

        self.failUnlessEqual(get_tier_boundaries(None), None)

        self.failUnlessEqual(get_tier_boundaries(pd.DataFrame(None, columns=\
                                                              ['start_time',
                                                               'end_time',
                                                               'text'])),
                                                 None)

        self.failUnlessEqual([int(x) for x in get_tier_boundaries(\
                                  self.withPoint['P'][:1])], [2, 2])
Пример #2
0
    def test_get_tier_boundaries(self):
        self.failUnlessEqual([int(x) for x in get_tier_boundaries(\
                                 self.withPoint['A'])],
                             [2, 179])

        self.failUnlessEqual([int(x) for x in get_tier_boundaries(\
                                 self.withPoint['P'])],
                             [2, 32])

        self.failUnlessEqual(get_tier_boundaries(None), None)

        self.failUnlessEqual(get_tier_boundaries(pd.DataFrame(None, columns=\
                                                              ['start_time',
                                                               'end_time',
                                                               'text'])),
                                                 None)

        self.failUnlessEqual([int(x) for x in get_tier_boundaries(\
                                  self.withPoint['P'][:1])], [2, 2])
Пример #3
0
def plot_annotations(tierdict, time_begin=None, time_end=None, tierorder=None,
                     colorindex=None, hscale=1, vscale=1, linespan=20,
                     pointwidth=0.05, tickspacing=1,
                     text_kwargs=None, filepath=None):

    """Plot ELAN annotations, imported from textgrids

    arguments:
    tierdict    a dictionary of intervalframes

    keyword arguments:

    time_begin     -- minimum time (in seconds) of plot. If set to None, the
                       minimum time is found from the data
    time_end       -- maximum time (in seconds) of plot. If set to None, the
                       maximum time is found from the data
    tierorder      -- list of tierKey names to set the order of plotting.
                      Specifying less names than in the tierdict itself
                      results in only those tiers being drawn

    colorindex     -- list of colors to cycle through for each tier, or

                dictionary with labels as keys and colorcodes as values,
                for plotting annotation tiers with finite label sets

    hscale, vscale -- horizontal/vertical scaling of the plot

    linespan       -- time displayed per line of the plot (default 20 seconds)

    pointwidth     -- width of lines for point tiers (in seconds)

    tickspacing    -- number of seconds between ticks on x axis (default 1)

    text_kwargs    -- a fontdict for plt.text() in order to print the
                      text labels. Set to None to avoid printing any text

    filepath       -- path to filename to save the figure, e.g:
                      '~/Desktop/myfile.pdf'

                      The file must have one of the matplolib-supported
                      extensions, e.g. 'pdf', 'png', 'jpg'

    """
    if colorindex == None:
        colorindex = ['r', 'g', 'b', 'c', 'm', 'y', 'k']
    #determine tiers to draw and the order
    thekeys = []
    if tierorder == None:
        thekeys = tierdict.keys()
    else:
        thekeys = tierorder

    if len(thekeys) == 0:
        return "No tiers to plot!"

    #find boundaries
    tiermin = min([get_tier_boundaries(tierdict[tierkey])[0] for tierkey \
                   in thekeys])
    tiermax = max([get_tier_boundaries(tierdict[tierkey])[1] for tierkey \
                   in thekeys])
    if time_begin == None:
        time_begin = tiermin
    else:
        time_begin = max(time_begin, tiermin)
    if time_end == None:
        time_end = tiermax
    else:
        time_end = min(time_end, tiermax)

    #Check time limits
    duration = time_end - time_begin
    if duration <= 0:
        return "Wrong time limits!"
    elif duration < linespan:
        linespan = duration
        number_of_lines = 1
    else:
        number_of_lines = int((float(duration) / linespan)) + 1

    fig = plt.figure(figsize=(hscale * linespan,
                              vscale * number_of_lines * len(thekeys)))

    for line in range(number_of_lines):
        #Drawing loop of tiers
        for tiernumber, tierkey in enumerate(thekeys):
            #Trim tier at time limits
            t1 = time_begin + line * linespan
            t2 = min(t1 + linespan, time_end)


            if get_tier_type(tierdict[tierkey]) == 'point':
                tier = slice_intervalframe_by_time(\
                       __point_tier_to_intervals__(tierdict[tierkey],
                                                   pointwidth), t1, t2)
            elif get_tier_type(tierdict[tierkey]) == 'interval':
                tier = slice_intervalframe_by_time(tierdict[tierkey], t1, t2)
            else:
                continue
            #Add subplots
            ax = fig.add_subplot(number_of_lines * len(thekeys), 1,
                                 line * len(thekeys) + tiernumber + 1)
            xticks = range(int(t1), int(t2) + 1, tickspacing)
            if tiernumber == len(thekeys)-1:
                xlabels = xticks
            else:
                xlabels = []

            #Set the color
            if isinstance(colorindex, list):
                tiercolor = colorindex[tiernumber % len(colorindex)]
            elif isinstance(colorindex, dict):
                tiercolor = colorindex

            __draw_tier__(ax, fig.canvas.get_renderer(), tier, xmin=t1,
                          xmax=(t1 + linespan), ymin=0, ymax=1,
                          xticks=xticks, xlabels=xlabels, tierkey=tierkey,
                          tiercolor=tiercolor, edgecolor='w',
                          textkwargs=text_kwargs)
    fig.subplots_adjust(hspace=0.5)
    if filepath is not None:
        fig.savefig(filepath, format=filepath.split('.')[1])
Пример #4
0
def plot_annotations(tierdict,
                     time_begin=None,
                     time_end=None,
                     tierorder=None,
                     colorindex=None,
                     hscale=1,
                     vscale=1,
                     linespan=20,
                     pointwidth=0.05,
                     tickspacing=1,
                     text_kwargs=None,
                     filepath=None):
    """Plot ELAN annotations, imported from textgrids

    arguments:
    tierdict    a dictionary of intervalframes

    keyword arguments:

    time_begin     -- minimum time (in seconds) of plot. If set to None, the
                       minimum time is found from the data
    time_end       -- maximum time (in seconds) of plot. If set to None, the
                       maximum time is found from the data
    tierorder      -- list of tierKey names to set the order of plotting.
                      Specifying less names than in the tierdict itself
                      results in only those tiers being drawn

    colorindex     -- list of colors to cycle through for each tier, or

                dictionary with labels as keys and colorcodes as values,
                for plotting annotation tiers with finite label sets

    hscale, vscale -- horizontal/vertical scaling of the plot

    linespan       -- time displayed per line of the plot (default 20 seconds)

    pointwidth     -- width of lines for point tiers (in seconds)

    tickspacing    -- number of seconds between ticks on x axis (default 1)

    text_kwargs    -- a fontdict for plt.text() in order to print the
                      text labels. Set to None to avoid printing any text

    filepath       -- path to filename to save the figure, e.g:
                      '~/Desktop/myfile.pdf'

                      The file must have one of the matplolib-supported
                      extensions, e.g. 'pdf', 'png', 'jpg'

    """
    if colorindex == None:
        colorindex = ['r', 'g', 'b', 'c', 'm', 'y', 'k']
    #determine tiers to draw and the order
    thekeys = []
    if tierorder == None:
        thekeys = tierdict.keys()
    else:
        thekeys = tierorder

    if len(thekeys) == 0:
        return "No tiers to plot!"

    #find boundaries
    tiermin = min([get_tier_boundaries(tierdict[tierkey])[0] for tierkey \
                   in thekeys])
    tiermax = max([get_tier_boundaries(tierdict[tierkey])[1] for tierkey \
                   in thekeys])
    if time_begin == None:
        time_begin = tiermin
    else:
        time_begin = max(time_begin, tiermin)
    if time_end == None:
        time_end = tiermax
    else:
        time_end = min(time_end, tiermax)

    #Check time limits
    duration = time_end - time_begin
    if duration <= 0:
        return "Wrong time limits!"
    elif duration < linespan:
        linespan = duration
        number_of_lines = 1
    else:
        number_of_lines = int((float(duration) / linespan)) + 1

    fig = plt.figure(figsize=(hscale * linespan,
                              vscale * number_of_lines * len(thekeys)))

    for line in range(number_of_lines):
        #Drawing loop of tiers
        for tiernumber, tierkey in enumerate(thekeys):
            #Trim tier at time limits
            t1 = time_begin + line * linespan
            t2 = min(t1 + linespan, time_end)

            if get_tier_type(tierdict[tierkey]) == 'point':
                tier = slice_intervalframe_by_time(\
                       __point_tier_to_intervals__(tierdict[tierkey],
                                                   pointwidth), t1, t2)
            elif get_tier_type(tierdict[tierkey]) == 'interval':
                tier = slice_intervalframe_by_time(tierdict[tierkey], t1, t2)
            else:
                continue
            #Add subplots
            ax = fig.add_subplot(number_of_lines * len(thekeys), 1,
                                 line * len(thekeys) + tiernumber + 1)
            xticks = range(int(t1), int(t2) + 1, tickspacing)
            if tiernumber == len(thekeys) - 1:
                xlabels = xticks
            else:
                xlabels = []

            #Set the color
            if isinstance(colorindex, list):
                tiercolor = colorindex[tiernumber % len(colorindex)]
            elif isinstance(colorindex, dict):
                tiercolor = colorindex

            __draw_tier__(ax,
                          fig.canvas.get_renderer(),
                          tier,
                          xmin=t1,
                          xmax=(t1 + linespan),
                          ymin=0,
                          ymax=1,
                          xticks=xticks,
                          xlabels=xlabels,
                          tierkey=tierkey,
                          tiercolor=tiercolor,
                          edgecolor='w',
                          textkwargs=text_kwargs)
    fig.subplots_adjust(hspace=0.5)
    if filepath is not None:
        fig.savefig(filepath, format=filepath.split('.')[1])