예제 #1
0
def imgMeans(Img):
  """
  Compute different mean images for different groups of images.

  Input
    Img   -  a list of images, n x m, h x w x 3
               n: #image in each group
               m: #groups

  Output
    imgs  -  mean image, m (cell), h x w x 3
  """
  # dimension
  n, m = Img.shape

  imgs = cells(n)
  for i in range(n):
    imgs[i] = imgMean(Img[i])

  return imgs
예제 #2
0
def shHst(mes, devs=[], ax=None, xs=[], barWid=0.8, devWid=0.8, bdWid=0, ori='ver', clG=None):
  """
  Plot histogram.

  Input
    mes       -  histogram, 1 x n
    devs      -  deviation, {[]} | 1 x n
    ax        -  axis, {None}
    xs        -  x position, {[]} | n x
    barWid    -  width of histogram bar, {.8} <= 1
    devWid    -  width of deviation line in terms of the 'barWid', {1} <= 1
    bdWid     -  width of boundary of bar, {1}
    ori       -  orientation, {'ver'} | 'hor'
    clG       -  global color, {'None'} | 'red' | ...
      lnWid   -  width of deviation line, {2}
      dev     -  dev flag, {'y'} | 'n'
      val     -  value flag, 'y' | {'n'}
      form    -  value form, {'%d'}
      yLim    -  threshold in y axis, {[]}
      leg     -  legend, {[]}

  Output
    haBars
  """
  if ax is not None:
      fig = plt.gcf()
      fig.sca(ax)

  # mk & cl
  mks, cls = genMkCl()

  # dimension
  n = len(mes)

  # label position
  if len(xs) == 0:
    xs = np.arange(1, n + 1)
  gap = xs[1] - xs[0] if n > 1 else .5

  # width
  barWid2 = 1.0 * barWid / 2 * gap
  devWid2 = 1.0 * devWid / 2 * gap

  # import pdb; pdb.set_trace()
  # plot mean
  haBars = cells(n)
  for i in range(n):
    x = xs[i]
    y = mes[i]
    xL = x - barWid2
    xR = x + barWid2

    if y == 0:
      y = 1e-7
      # continue

    # color
    if clG is None:
      cl = cls[i % len(cls)]
    else:
      cl = clG

    # draw
    if bdWid == 0:
      if ori == 'ver':
        ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y], color=cl, edgecolor=cl)
      else:
        ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL], color=cl, edgecolor=cl)
    else:
      if ori == 'ver':
        ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y], color=cl, LineWidth=bdWid)
      else:
        ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL], color=cl, LineWidth=bdWid)
    haBars[i] = ha

  # plot dev
  if len(devs) > 0:
    ys = mes + devs

    for i in range(n):
      x = xs(i)
      y = mes(i)
      dev = devs(i)
      xL = x - devWid2
      xR = x + devWid2

      if y == 0:
        continue

      # color
      cl = cls[i % len(cls)]
      cl = 'k'
      lnWid = 1

      # vertical line
      plt.plot([x, x], [-dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)

      # horizontal line
      plt.plot([xL, xR], [dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)
      plt.plot([xL, xR], [-dev, -dev] + y, 'Color', cl, 'LineWidth', lnWid)

  return haBars
예제 #3
0
def shHstG(Me, Dev=[], ax=None, xs=[], barWid=0.8, barWidG=.8, devWid=0.8, bdWid=0, ori='ver', clG=None):
  """
  Plot histogram groups.

  Input
    Me        -  histogram, k x n
                   k: #bin in each group
                   n: #group
    Dev       -  deviation, {[]} | 1 x n
    ax        -  axis, {None}
    xs        -  x position, {[]} | n x
    barWid    -  width of histogram bar, {.8} <= 1
    devWid    -  width of deviation line in terms of the 'barWid', {1} <= 1
    bdWid     -  width of boundary of bar, {1}
    ori       -  orientation, {'ver'} | 'hor'
    clG       -  global color, {'None'} | 'red' | ...
      lnWid   -  width of deviation line, {2}
      dev     -  dev flag, {'y'} | 'n'
      val     -  value flag, 'y' | {'n'}
      form    -  value form, {'%d'}
      yLim    -  threshold in y axis, {[]}
      leg     -  legend, {[]}

  Output
    haBars
  """
  # axis
  if ax is not None:
      fig = plt.gcf()
      fig.sca(ax)

  # mk & cl
  mks, cls = genMkCl()

  # dimension
  k, n = Me.shape

  # label position
  if len(xs) == 0:
      xs = np.arange(1, n + 1)
  gap = xs[1] - xs[0] if n > 1 else .5

  # width
  barWidG2 = barWidG / 2
  barWid = barWid * (barWidG / k)
  gapWid = (barWidG - barWid * k) / (k - 1)
  devWid2 = barWid * devWid / 2

  # plot mean
  HaBar = cells((k, n))
  for i in range(n):
      x = xs[i] - barWidG2
      for c in range(k):
          xL = x + (gapWid + barWid) * c
          xR = xL + barWid
          y = Me[c, i]

          # color
          if clG is None:
              cl = cls[c % len(cls)]
          else:
              cl = clG

          # draw
          if bdWid == 0:
              if ori == 'ver':
                  ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y], color=cl, edgecolor=cl)
              else:
                  ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL], color=cl, edgecolor=cl)
          else:
              if ori == 'ver':
                  ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y], color=cl, LineWidth=bdWid)
              else:
                  ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL], color=cl, LineWidth=bdWid)
          HaBar[c, i] = ha

  # plot dev
  if Dev is not None:
      ys = mes + devs

      for i in range(n):
          x = xs(i)
          y = mes(i)
          dev = devs(i)
          xL = x - devWid2
          xR = x + devWid2

          if y == 0:
              continue

          # color
          cl = cls[i % len(cls)]
          cl = 'k'
          lnWid = 1

          # vertical line
          plt.plot([x, x], [-dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)

          # horizontal line
          plt.plot([xL, xR], [dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)
          plt.plot([xL, xR], [-dev, -dev] + y, 'Color', cl, 'LineWidth', lnWid)

  return HaBar
예제 #4
0
def shHst(mes,
          devs=[],
          ax=None,
          xs=[],
          barWid=0.8,
          devWid=0.8,
          bdWid=0,
          ori='ver',
          clG=None):
    """
  Plot histogram.

  Input
    mes       -  histogram, 1 x n
    devs      -  deviation, {[]} | 1 x n
    ax        -  axis, {None}
    xs        -  x position, {[]} | n x
    barWid    -  width of histogram bar, {.8} <= 1
    devWid    -  width of deviation line in terms of the 'barWid', {1} <= 1
    bdWid     -  width of boundary of bar, {1}
    ori       -  orientation, {'ver'} | 'hor'
    clG       -  global color, {'None'} | 'red' | ...
      lnWid   -  width of deviation line, {2}
      dev     -  dev flag, {'y'} | 'n'
      val     -  value flag, 'y' | {'n'}
      form    -  value form, {'%d'}
      yLim    -  threshold in y axis, {[]}
      leg     -  legend, {[]}

  Output
    haBars
  """
    if ax is not None:
        fig = plt.gcf()
        fig.sca(ax)

    # mk & cl
    mks, cls = genMkCl()

    # dimension
    n = len(mes)

    # label position
    if len(xs) == 0:
        xs = np.arange(1, n + 1)
    gap = xs[1] - xs[0] if n > 1 else .5

    # width
    barWid2 = 1.0 * barWid / 2 * gap
    devWid2 = 1.0 * devWid / 2 * gap

    # import pdb; pdb.set_trace()
    # plot mean
    haBars = cells(n)
    for i in range(n):
        x = xs[i]
        y = mes[i]
        xL = x - barWid2
        xR = x + barWid2

        if y == 0:
            y = 1e-7
            # continue

        # color
        if clG is None:
            cl = cls[i % len(cls)]
        else:
            cl = clG

        # draw
        if bdWid == 0:
            if ori == 'ver':
                ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y],
                              color=cl,
                              edgecolor=cl)
            else:
                ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL],
                              color=cl,
                              edgecolor=cl)
        else:
            if ori == 'ver':
                ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y],
                              color=cl,
                              LineWidth=bdWid)
            else:
                ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL],
                              color=cl,
                              LineWidth=bdWid)
        haBars[i] = ha

    # plot dev
    if len(devs) > 0:
        ys = mes + devs

        for i in range(n):
            x = xs(i)
            y = mes(i)
            dev = devs(i)
            xL = x - devWid2
            xR = x + devWid2

            if y == 0:
                continue

            # color
            cl = cls[i % len(cls)]
            cl = 'k'
            lnWid = 1

            # vertical line
            plt.plot([x, x], [-dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)

            # horizontal line
            plt.plot([xL, xR], [dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)
            plt.plot([xL, xR], [-dev, -dev] + y, 'Color', cl, 'LineWidth',
                     lnWid)

    return haBars
예제 #5
0
def shHstG(Me,
           Dev=[],
           ax=None,
           xs=[],
           barWid=0.8,
           barWidG=.8,
           devWid=0.8,
           bdWid=0,
           ori='ver',
           clG=None):
    """
  Plot histogram groups.

  Input
    Me        -  histogram, k x n
                   k: #bin in each group
                   n: #group
    Dev       -  deviation, {[]} | 1 x n
    ax        -  axis, {None}
    xs        -  x position, {[]} | n x
    barWid    -  width of histogram bar, {.8} <= 1
    devWid    -  width of deviation line in terms of the 'barWid', {1} <= 1
    bdWid     -  width of boundary of bar, {1}
    ori       -  orientation, {'ver'} | 'hor'
    clG       -  global color, {'None'} | 'red' | ...
      lnWid   -  width of deviation line, {2}
      dev     -  dev flag, {'y'} | 'n'
      val     -  value flag, 'y' | {'n'}
      form    -  value form, {'%d'}
      yLim    -  threshold in y axis, {[]}
      leg     -  legend, {[]}

  Output
    haBars
  """
    # axis
    if ax is not None:
        fig = plt.gcf()
        fig.sca(ax)

    # mk & cl
    mks, cls = genMkCl()

    # dimension
    k, n = Me.shape

    # label position
    if len(xs) == 0:
        xs = np.arange(1, n + 1)
    gap = xs[1] - xs[0] if n > 1 else .5

    # width
    barWidG2 = barWidG / 2
    barWid = barWid * (barWidG / k)
    gapWid = (barWidG - barWid * k) / (k - 1)
    devWid2 = barWid * devWid / 2

    # plot mean
    HaBar = cells((k, n))
    for i in range(n):
        x = xs[i] - barWidG2
        for c in range(k):
            xL = x + (gapWid + barWid) * c
            xR = xL + barWid
            y = Me[c, i]

            # color
            if clG is None:
                cl = cls[c % len(cls)]
            else:
                cl = clG

            # draw
            if bdWid == 0:
                if ori == 'ver':
                    ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y],
                                  color=cl,
                                  edgecolor=cl)
                else:
                    ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL],
                                  color=cl,
                                  edgecolor=cl)
            else:
                if ori == 'ver':
                    ha = plt.fill([xL, xR, xR, xL], [0, 0, y, y],
                                  color=cl,
                                  LineWidth=bdWid)
                else:
                    ha = plt.fill([0, 0, y, y], [xL, xR, xR, xL],
                                  color=cl,
                                  LineWidth=bdWid)
            HaBar[c, i] = ha

    # plot dev
    if Dev is not None:
        ys = mes + devs

        for i in range(n):
            x = xs(i)
            y = mes(i)
            dev = devs(i)
            xL = x - devWid2
            xR = x + devWid2

            if y == 0:
                continue

            # color
            cl = cls[i % len(cls)]
            cl = 'k'
            lnWid = 1

            # vertical line
            plt.plot([x, x], [-dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)

            # horizontal line
            plt.plot([xL, xR], [dev, dev] + y, 'Color', cl, 'LineWidth', lnWid)
            plt.plot([xL, xR], [-dev, -dev] + y, 'Color', cl, 'LineWidth',
                     lnWid)

    return HaBar