Esempio n. 1
0
def plot_ellipsoid(experiment_row):
    fig = plt.figure()

    ax = fig.add_subplot(111, projection='3d')
    ax.set_aspect("equal")

    # for fil in experiment_row['Positions']:
    #     fil = np.array(fil)
    #     ells = nestle.bounding_ellipsoids(fil)
    #     ax.plot(fil.T[0], fil.T[1], fil.T[2], "ko")
    #
    #     for ell in ells:
    #         plot_ellipsoid_3d(ell, ax)

    fil = experiment_row['Positions'][10]
    fil = np.array(fil)
    ells = nestle.bounding_ellipsoids(fil,0.0215)
    ax.plot(fil.T[0], fil.T[1], fil.T[2], "ko")

    for ell in ells:
        plot_ellipsoid_3d(ell, ax)

    plt.show()
    ax.set_xlabel("X")
    ax.set_xlim(ax.get_xlim()[::-1])
    ax.set_ylabel("Y")
    ax.set_zlabel("Z")
    plt.show()
Esempio n. 2
0
 def update(self, points):
     # volume is larger than standard Ellipsoid computation
     # because we have a superset of various likelihood contours
     # increase proportional to number of points
     pointvol = exp(-self.iter / self.nlive_points) * (
         len(points) * 1. / self.nlive_points) / self.nlive_points
     self.ells = bounding_ellipsoids(numpy.asarray(points),
                                     pointvol=pointvol)
     for ell in self.ells:
         ell.scale_to_vol(ell.vol * self.enlarge)
Esempio n. 3
0
                      z,
                      rstride=4,
                      cstride=4,
                      color='#2980b9',
                      alpha=0.2)


# Generate points within a torus
npoints = 1000
R = 1.0
r = 0.1
points = rand_torus(R, r, npoints)
torus_vol = 2. * math.pi**2 * R * r**2

# Determine bounding ellipsoids
pointvol = torus_vol / npoints
ells = nestle.bounding_ellipsoids(points, pointvol=pointvol)

# plot
fig = plt.figure(figsize=(10., 10.))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:, 0], points[:, 1], points[:, 2], c='k', marker='.')
for ell in ells:
    plot_ellipsoid_3d(ell, ax)

ax.set_xlim(-1., 1.)
ax.set_ylim(-1., 1.)
ax.set_zlim(-1., 1.)
ax.set_aspect('equal')
fig.tight_layout()
plt.show()
	def update(self, points):
		pointvol = exp(-self.iter / len(points)) / len(points)
		self.ells = bounding_ellipsoids(numpy.asarray(points), pointvol=pointvol)
		for ell in self.ells:
			ell.scale_to_vol(ell.vol * self.enlarge)
Esempio n. 5
0
	def update(self, points):
		points = numpy.asarray(points)
		pointvol = exp(-self.iter / len(points)) / len(points)
		npoints, ndim = points.shape
		if self.bs_enabled:
			# bootstrapping rounds:
			for i in range(self.bs_rounds):
				choice = set(numpy.random.choice(numpy.arange(npoints), size=npoints))
				mask = numpy.array([c in choice for c in numpy.arange(npoints)])
				points_without_i = points[mask,:]
				ells = bounding_ellipsoids(points_without_i, pointvol=pointvol)
				# check distance of left-out points
				distmax = 1
				for i in numpy.where(~mask)[0]:
					dists = []
					for ell in ells:
						x = points[i,:] - ell.ctr
						d = numpy.dot(numpy.dot(x, ell.a), x)
						dists.append(d)
					distmax = max(distmax, min(dists))
				self.lastdistmax.append(distmax)
				if len(self.lastdistmax) > self.bs_rounds:
					break
			# remember back 50 steps
			enlarge = max(self.lastdistmax[-self.bs_memory:])
			enlarge = max(1, min(100, enlarge))
			enlarge = enlarge * ndim**0.5
			print('enlargement factor: %.2f' % enlarge, 'memory: ', ' '.join(['%.2f' % d for d in self.lastdistmax[-self.bs_memory:]]))
		else:
			enlarge = self.enlarge

		self.ells = bounding_ellipsoids(points, pointvol=pointvol)
		for ell in self.ells:
			ell.scale_to_vol(ell.vol * enlarge)
		
		# now update the metric. 
		# For this we need the individual clusters
		# Ideally we would find clusters by merging the overlapping 
		# ellipsoids. Unfortunately there is no analytic solution to
		# finding whether two n-ellipsoids overlap.
		# Instead, we check for points which live in more than one 
		# ellipsoid
		
		# for each point, find all ellipses that constain it
		ell_redirect = {i:i for i in range(len(self.ells))}
		ell_indices = []
		for u in points:
			ell_containing = None
			for i, ell in enumerate(self.ells):
				if ell.contains(u):
					if ell_containing is None:
						ell_containing = ell_redirect[i]
					else:
						ell_redirect[i] = ell_containing
			ell_indices.append(ell_containing)
		
		
		ell_indices = numpy.asarray([ell_redirect[i] for i in ell_indices])
		shifted_cluster_members = numpy.zeros_like(points)
		for i in set(ell_indices):
			mask = ell_indices == i
			clusterpoints = points[mask,:]
			mean = numpy.mean(clusterpoints, axis=0)
			shifted_cluster_members[mask] = clusterpoints - mean
		
		# construct whitening matrix
		metric_updated = False
		if self.metriclearner == 'none':
			metric = self.metric # stay with identity matrix
		elif self.metriclearner == 'simplescaling' or (self.metriclearner == 'mahalanobis' and ndim == 1):
			metric = SimpleScaling()
			metric.fit(shifted_cluster_members)
			metric_updated = True
		elif self.metriclearner == 'mahalanobis':
			metric = MahalanobisMetric()
			metric.fit(shifted_cluster_members)
			metric_updated = True
		elif self.metriclearner == 'sdml':
			metric = SDML()
			metric.fit(shifted_cluster_members, W = numpy.ones((len(shifted_cluster_members), len(shifted_cluster_members))))
			metric_updated = True
		else:
			assert False, self.metriclearner

		self.metric = metric
Esempio n. 6
0
            x[i,j], y[i,j], z[i,j] = ell.ctr + np.dot(ell.axes,
                                                      [x[i,j],y[i,j],z[i,j]])

    ax.plot_wireframe(x, y, z,  rstride=4, cstride=4, color='#2980b9', alpha=0.2)


# Generate points within a torus
npoints = 1000
R = 1.0
r = 0.1
points = rand_torus(R, r, npoints)
torus_vol = 2. * math.pi**2 * R * r**2

# Determine bounding ellipsoids
pointvol = torus_vol / npoints
ells = nestle.bounding_ellipsoids(points, pointvol=pointvol)

# plot
fig = plt.figure(figsize=(10., 10.))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:, 0], points[:, 1], points[:, 2], c='k', marker='.')
for ell in ells:
    plot_ellipsoid_3d(ell, ax)

ax.set_xlim(-1., 1.)
ax.set_ylim(-1., 1.)
ax.set_zlim(-1., 1.)
ax.set_aspect('equal')
fig.tight_layout()
plt.show()