Skip to content

Commit 3330653

Browse files
merge, match to and from file, other
1 parent 55bae06 commit 3330653

File tree

3 files changed

+88
-19
lines changed

3 files changed

+88
-19
lines changed

dsmm/dsmm.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@ void dsmm::_dsmm(double *X, double *Y, int M, int N, int D,
360360
// Find average of minimum distance between any Y and X.
361361
double mindist;
362362
double avgmindist=0.0;
363+
364+
/**for(int m=0;m<M;m++){
365+
mindist = pwise_dist[m*N];
366+
for(int m2=0;m2<M;m2++){
367+
if(mindist>pwise_dist[m*N+m2]){
368+
mindist=pwise_dist[m*N+m2];
369+
}
370+
}
371+
avgmindist += mindist;
372+
}
373+
avgmindist /= M;**/
363374

364375
for(int m=0;m<M;m++){
365376
mindist = pwise_dist[m*N];
@@ -395,22 +406,25 @@ void dsmm::_dsmm(double *X, double *Y, int M, int N, int D,
395406
Match[m] = -1;
396407
}
397408
}
398-
399-
// "Denormalize" in Vemuri's language. Since Y has been moved onto X,
400-
// denormalize both with the parameters originally used to normalize X.
401-
for(int n=0;n<N;n++){
402-
for(int d=0;d<D;d++) {
403-
X[n*D+d] *= MaxX[d];
404-
X[n*D+d] += AvgX[d];
405-
}
406-
}
409+
410+
bool denormalize = true;
411+
if(denormalize) {
412+
// "Denormalize" in Vemuri's language. Since Y has been moved onto X,
413+
// denormalize both with the parameters originally used to normalize X.
414+
for(int n=0;n<N;n++){
415+
for(int d=0;d<D;d++) {
416+
X[n*D+d] *= MaxX[d];
417+
X[n*D+d] += AvgX[d];
418+
}
419+
}
407420

408-
for(int m=0;m<M;m++){
409-
for(int d=0;d<D;d++) {
410-
Y[m*D+d] *= MaxX[d];
411-
Y[m*D+d] += AvgX[d];
412-
}
413-
}
421+
for(int m=0;m<M;m++){
422+
for(int d=0;d<D;d++) {
423+
Y[m*D+d] *= MaxX[d];
424+
Y[m*D+d] += AvgX[d];
425+
}
426+
}
427+
}
414428

415429
delete[] AvgX;
416430
delete[] MaxX;

wormbrain/brains.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __init__(self, coordZYX, nInVolume, zOfFrame=None, properties={},
6363
nPlane=self.boxNPlane, boxIndices=self.boxIndices,
6464
method="xyMaxCurvature")
6565

66-
self.coord = np.rint(self.coord)
66+
self.coord = np.rint(self.coord)
6767

6868
self.coord = self.coord.astype(int)
6969

@@ -89,6 +89,12 @@ def from_find_neurons(cls, coord, volFrame0=None, *args, **kwargs):
8989
'''
9090
if volFrame0 is None:
9191
volFrame0 = np.arange(len(coord)+1,dtype=int)
92+
try:
93+
rectype = kwargs.pop('rectype')
94+
except:
95+
rectype = "3d"
96+
if rectype == "2d":
97+
kwargs['stabilize_z'] = False
9298

9399
coordZYX, nInVolume, nInFrame = cls._conv_coord_2d_to_3d(coord,
94100
volFrame0, dtype=int)
@@ -136,7 +142,27 @@ def from_file(cls, folder, filename=""):
136142
stabilize_z = False
137143

138144
return cls(coordZYX, nInVolume, zOfFrame, properties, stabilize_z)
139-
145+
146+
def append(self, brains2):
147+
'''Append to this object the content of another instance of Brains.
148+
It does not perform any z-stabilization. Assumes the curvature in
149+
brains2 was extracted the same way as in this instance.
150+
(Modify stuff mimicking __init__()).
151+
'''
152+
self.nInVolume = np.append(self.nInVolume, brains2.nInVolume)
153+
self.coord = np.append(self.coord, brains2.coord, axis=0)
154+
self.coord = irrarray(self.coord, [self.nInVolume], strideNames=["vol"])
155+
156+
# Concatenate
157+
self.zOfFrame = self.zOfFrame + brains2.zOfFrame
158+
159+
self.curvature = np.append(self.curvature, brains2.curvature)
160+
self.curvature = irrarray(self.curvature, self.nInVolume,
161+
strideNames=["vol"])
162+
163+
self.coord = self.coord.astype(int)
164+
165+
140166
def __getitem__(self, i):
141167
'''
142168
Allow for direct indexing of the class to access the coordinates.
@@ -369,9 +395,14 @@ def _stabilize_z(coord, curvature, nPlane=7, boxIndices=
369395
centralIndices[pl] = boxIndices[pl][sh0//2]
370396
curv = curvature[:,centralIndices] #look just along z
371397

372-
coord[:,z_index] = coord[:,z_index].astype(float) + np.sum(z*curv,axis=1)/np.sum(curv,axis=1)
398+
coord_3d_out = np.zeros_like(coord,dtype=np.float)
399+
if coord_3d_ordering=="zyx":
400+
coord_3d_out[:,1:3] = coord[:,1:3]
401+
else:
402+
coord_3d_out[:,0:2] = coord[:,0:2]
403+
coord_3d_out[:,z_index] = coord[:,z_index].astype(np.float) + np.sum(z*curv,axis=1)/np.sum(curv,axis=1)
373404

374-
return coord
405+
return coord_3d_out
375406

376407
def fit_sphere(self):
377408
curvature = self.curvature

wormbrain/match.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from mpl_toolkits.mplot3d import Axes3D
44
import wormbrain as wormb
55

6+
filename_matches = "matches.txt"
7+
68
def pairwise_distance(A,B,returnAll=False,squared=False,thresholdDz=0.0):
79
'''
810
Calculates the pairwise distance between points belonging to two sets of
@@ -119,6 +121,28 @@ def _match_nearest(A, B, **kwargs): #TODO implement it on multiple As
119121

120122
return Match
121123

124+
def save_matches(MMatch, folder):
125+
if folder[-1]!="/": folder+="/"
126+
f = open(folder+filename_matches,"w")
127+
128+
if type(MMatch)!=list: MMatch = [MMatch]
129+
130+
for Match in MMatch:
131+
Match.tofile(f,sep=" ")
132+
f.write("\n")
133+
f.close()
134+
135+
def load_matches(folder):
136+
if folder[-1]!="/": folder+="/"
137+
f = open(folder+filename_matches,"r")
138+
139+
MMatch = []
140+
for line in f.readlines():
141+
Match = np.fromstring(line,dtype=int,sep=" ")
142+
MMatch.append(Match)
143+
144+
return MMatch
145+
122146

123147
def plot_matches(A, B, Match, mode='3d',plotNow=True,**kwargs):
124148

0 commit comments

Comments
 (0)