Example #1
0
 def __init__(self, N, lb=-0.5, rb=0.5, perturb=0.02):
     self.name = 'SparseGrid2D'
     points_per_axis = int(N**0.5)
     data = gen_grid(2, points_per_axis, lb=lb, rb=rb)
     # Add noise
     data += np.random.normal(loc=np.zeros_like(data), scale=perturb, size=data.shape)
     self.data = data
Example #2
0
    def __init__(self, n_modes=9, lb=-0.5, rb=0.5):
        self.name = 'MixGrid'
        self.dim = 2
        self.n_modes = n_modes

        points_per_axis = int(n_modes**0.5)
        self.sigma = (rb - lb) / points_per_axis / 4

        self.centers = gen_grid(2,
                                points_per_axis,
                                lb=lb + 2 * self.sigma,
                                rb=rb - 2 * self.sigma)
Example #3
0
def plot_grid(points_per_axis,
              gen_func,
              d=2,
              bounds=(0.0, 1.0),
              scale=.8,
              save_path=None):
    ''' Uniformly plots synthesized shapes in the latent space
        K : number of samples for each point in the latent space '''

    scale *= (bounds[1] - bounds[0]) / points_per_axis

    Z = gen_grid(2, points_per_axis, bounds[0], bounds[1])  # Generate a grid
    plot_synthesized(Z, gen_func, d=d, scale=scale, save_path=save_path)
Example #4
0
def plot_samples(Z, X_list, alphas=None, scale=None, scatter=True, 
                 mirror=False, annotate=False, fname=None, **kwargs):
    
    ''' Plot shapes given design sapce and latent space coordinates '''
    
    plt.rc("font", size=12)
    
    if Z is None or Z.shape[1] != 2:
        N = X_list[0].shape[0]
        points_per_axis = int(N**.5)
        bounds = (-1., 1.)
        Z = gen_grid(2, points_per_axis, bounds[0], bounds[1]) # Generate a grid
        scale = 0.8*2.0/points_per_axis

    if scale is None:
        r0 = np.max(Z[:,0]) - np.min(Z[:,0])
        r1 = np.max(Z[:,1]) - np.min(Z[:,1])
        scale = 0.8*np.minimum(r0, r1)/10
        
    # Create a 2D plot
    fig = plt.figure(figsize=(5, 5))
    ax = fig.add_subplot(111)
    kwargs2 = kwargs.copy()
    if 'samples' not in fname and 'assemblies' not in fname:
        kwargs2['c'] = 'gray'
    for (i, z) in enumerate(Z):
        if alphas is not None:
            kwargs['alpha'] = alphas[i]
            kwargs2['alpha'] = alphas[i]
        for (j, X) in enumerate(X_list):
            if j == 0:
                plot_shape(X[i], z[0], z[1], ax, scale, scatter, mirror, **kwargs)
            else:
                plot_shape(X[i], z[0], z[1], ax, scale, scatter, mirror, **kwargs2)
            if annotate:
                label = '{0}'.format(i+1)
                plt.annotate(label, xy = (z[0], z[1]), size=10)
    
    plt.xticks([])
    plt.yticks([])
    plt.axis('off')
    plt.axis('equal')
    plt.tight_layout()
    plt.savefig(fname+'.svg', dpi=300)
#    plt.savefig(fname+'.png', dpi=300)
    plt.close()
Example #5
0
def black_box_function(winSize, winSizeNCC, target="ATA"):
    # optimizer.probe(
    #     params={"pointsInGrid": 10,
    #             "winSize": 3,
    #             "maxLevel": 5,
    #             "termCriteria_maxCount": 20,
    #             "termCriteria_epsilon": 2.9999999999999999e-01,
    #             "winSizeNCC": 30,
    #             "maxMedianLengthOfDisplacementDifference": 10.0},
    #     lazy=True,
    # )
    grid = [{
        'maxLevel': [5],
        'maxMedianLengthOfDisplacementDifference': [10.0],
        'pointsInGrid': [10],
        'termCriteria_epsilon': [2.9999999999999999e-01],
        'termCriteria_maxCount': [20],
        'tracker': ['MEDIANFLOW'],
        'video': vid,
        'winSize': [[winSize, winSize]],
        'winSizeNCC': [[winSizeNCC, winSizeNCC]]
    }]
    results = utils.gen_grid(grid)
    pool = ThreadPool(8)

    # print(results[1])
    # Analise(results[1])
    # print(results[1])
    # pbar = tqdm(total=len(results))
    res = [0.0, 0]
    for result in results:
        pool.apply_async(utils.Analise, args=[result], callback=update1)

    # pool.map(lambda p: Analise(vid, p), results)
    pool.close()
    pool.join()
    # [print(i["ATA"]) for i in results]
    for result in results:
        try:
            res[0] += result[target]
            res[1] += 1
        except:
            return -1
    res[0] /= res[1]
    return res[0]
def plot_samples(Z,
                 X,
                 scale=0.8,
                 points_per_axis=None,
                 scatter=True,
                 symm_axis=None,
                 annotate=False,
                 fname=None,
                 **kwargs):
    ''' Plot shapes given design sapce and latent space coordinates '''

    plt.rc("font", size=12)

    if Z is None or Z.shape[1] != 2 or points_per_axis is None:
        N = X.shape[0]
        points_per_axis = int(N**.5)
        bounds = (0., 1.)
        Z = gen_grid(2, points_per_axis, bounds[0],
                     bounds[1])  # Generate a grid

    scale /= points_per_axis

    # Create a 2D plot
    fig = plt.figure(figsize=(5, 5))
    ax = fig.add_subplot(111)

    for (i, z) in enumerate(Z):
        plot_shape(X[i], z[0], .3 * z[1], ax, scale, scatter, symm_axis,
                   **kwargs)
        if annotate:
            label = '{0}'.format(i + 1)
            plt.annotate(label, xy=(z[0], z[1]), size=10)


#    plt.xlabel('c1')
#    plt.ylabel('c2')
    plt.xticks([])
    plt.yticks([])
    plt.axis('off')
    plt.axis('equal')
    plt.tight_layout()
    plt.savefig(fname + '.svg', dpi=600)
    plt.savefig(fname + '.png', dpi=600)
    plt.close()
def plot_samples(Z,
                 X,
                 P=None,
                 W=None,
                 scale=0.8,
                 scatter=True,
                 symm_axis=None,
                 annotate=False,
                 fname=None,
                 **kwargs):
    ''' Plot shapes given design sapce and latent space coordinates '''

    plt.rc("font", size=12)

    if Z is None or Z.shape[1] != 2:
        N = X.shape[0]
        points_per_axis = int(N**.5)
        bounds = (-1., 1.)
        Z = gen_grid(2, points_per_axis, bounds[0],
                     bounds[1])  # Generate a grid

    r0 = np.max(Z[:, 0]) - np.min(Z[:, 0])
    r1 = np.max(Z[:, 1]) - np.min(Z[:, 1])
    scale *= np.minimum(r0, r1) / points_per_axis

    # Create a 2D plot
    fig = plt.figure(figsize=(5, 5))
    ax = fig.add_subplot(111)

    for (i, z) in enumerate(Z):
        if P is not None and W is not None:
            ind = W[i] >= 0
            p = P[i][ind]
            w = W[i][ind]
            w /= np.max(w)
            plot_shape(p,
                       z[0],
                       .3 * z[1],
                       ax,
                       scale,
                       scatter,
                       symm_axis,
                       c='g',
                       s=10 * w,
                       marker='P',
                       alpha=.5)
            plot_shape(p,
                       z[0],
                       .3 * z[1],
                       ax,
                       scale,
                       False,
                       symm_axis,
                       lw=.5,
                       alpha=.5,
                       c='g')
        for j in range(X.shape[1]):
            plot_shape(X[i, j], z[0], .3 * z[1], ax, scale, scatter, symm_axis,
                       **kwargs)
        if annotate:
            label = '{0}'.format(i + 1)
            plt.annotate(label, xy=(z[0], z[1]), size=10)


#    plt.xlabel('c1')
#    plt.ylabel('c2')
    plt.xticks([])
    plt.yticks([])
    plt.axis('off')
    plt.axis('equal')
    plt.tight_layout()
    plt.savefig(fname + '.svg', dpi=600)
    plt.close()
Example #8
0
def plot_samples(Z, X1, X2=None, scale=.05, annotate=False, save_path=None):
    ''' Plot shapes given design sapce and latent space coordinates '''

    plt.rc("font", size=12)

    if Z is None:
        N = X1.shape[0]
        points_per_axis = int(N**.5)
        bounds = (-1., 1.)
        Z = gen_grid(2, points_per_axis, bounds[0],
                     bounds[1])  # Generate a grid
        scale = 0.8 * 2.0 / points_per_axis

    # Create a 2D plot
    fig = plt.figure()
    ax = fig.add_subplot(111)

    for (i, z) in enumerate(Z):
        if annotate:
            label = '{0}'.format(i + 1)
            plt.annotate(label, xy=(z[0], z[1]), size=10)

        if X2 is None:
            plot_shape(X1[i],
                       z[0],
                       z[1],
                       ax,
                       scale=scale,
                       lw=1.0,
                       ls='-',
                       c='k',
                       alpha=1)

        else:
            plot_shape(X1[i],
                       z[0],
                       z[1],
                       ax,
                       scale=scale,
                       lw=1.0,
                       ls=':',
                       c='k',
                       alpha=1)
            plot_shape(X2[i],
                       z[0],
                       z[1],
                       ax,
                       scale=scale,
                       lw=1.0,
                       ls='-',
                       c='k',
                       alpha=1)


#    plt.xlabel('c1')
#    plt.ylabel('c2')
    plt.xticks([])
    plt.yticks([])
    plt.axis('off')
    plt.tight_layout()
    plt.savefig(save_path, dpi=600)
    plt.close()