예제 #1
0
    def SimLoad(self, sz=(USER.CHIP[1], USER.CHIP[0])):
        # Determine the appropriate filenames #
        kerstr = "NONE" if ((USER.KER_Z == 1) and
                            (USER.KER_T == 1)) else str(USER.KER_TYPE)
        fname_apr = 'APR_%ix%i' % sz
        fname_ker = '%s_%ix%ix%ix%i' % (kerstr, USER.KER_T, USER.KER_Z, *sz)

        if (USER.SIM_KER):
            # Simulate and save #
            apr = self.SimAperture(sz, USER.APR_RAD, shape=USER.APR_SHP)
            OP._SaveImg(apr, fname_apr, OP.FOLD_APR)

            ker = self.SimKernel(sz, apr)
            OP._SaveKer(ker, fname_ker, OP.FOLD_KER)
        else:
            # Check if the aperture exists #
            if (OP._CheckFile(fname_apr + '.tif',
                              OP.FOLD_APR)):  # Load them in then #
                apr = OP._LoadImg(fname_apr, OP.FOLD_APR)
            else:
                apr = self.SimAperture(sz, USER.APR_RAD, shape=USER.APR_SHP)
                OP._SaveImg(apr, fname_apr, OP.FOLD_APR)

            # Check if the kernel exists #
            if (OP._CheckFile(fname_ker + '.tif',
                              OP.FOLD_KER)):  # Load them in then #
                ker = OP._LoadKer(fname_ker, OP.FOLD_KER)
            else:
                ker = self.SimKernel(sz, apr)
                OP._SaveKer(ker, fname_ker, OP.FOLD_KER)

        ## Output ##
        return apr, ker
예제 #2
0
def RUN(code, *, update=False, visual=False):
    ## Update query ##
    if (update):
        # Create a new microscope #
        scope = Microscope(code)

        # No saving necessary here, as the image exists in \Images and the aperture and kernel exist in their respective folders
    else:
        # Load in what we already have #
        scope = Microscope()
        scope.code = code
        if (OP._CheckFile(code + str(FMT.TIF), OP.FOLD_IMG)):  # Experimental	#
            scope.img = OP._LoadMov('%s' % (code), fold=OP.FOLD_IMG)
        elif (OP._CheckFile(code + str(FMT.TIF), OP.FOLD_SIM)):  # Simulation	#
            scope.img = OP._LoadMov('%s' % (code), fold=OP.FOLD_SIM)
        else:
            # Override - make a fake image #
            scope.img = np.zeros([USER.SIM_FRAMES, 1, *USER.CHIP])
            #raise FileNotFoundError('The specified image or simulation code was not found')
        scope.apr, scope.ker = scope.SimLoad(sz=np.shape(scope.img)[2:])

    ## Visualization query ##
    if (visual):
        VIS._VisImg(scope.img[0, 0, :, :, ], 1000)
        VIS._VisImg(scope.ker[0, 0, :, :, ], 100)
        VIS._VisImg(scope.ker[0, (USER.KER_Z) // 4, :, :, ], 550)
        VIS._VisImg(scope.ker[0, (2 * USER.KER_Z) // 4, :, :, ], 550, 550)
        VIS._VisImg(scope.ker[0, (3 * USER.KER_Z) // 4, :, :, ], 100, 550)
        plt.pause(0.1)
        plt.show()

    pass  # TODO #

    ## Output ##
    return scope
예제 #3
0
    def __init__(self, code=None, *fxns, vis=False, blur=None):
        ## Initialization ##
        self.fxn_rot = _FxnRot(USER.KER_TYPE, USER.KER_Z > 1, USER.KER_T > 1)
        self.fxn_sep = _FxnSep(USER.KER_TYPE, USER.KER_Z > 1, USER.KER_T > 1)
        self.fxn_str = _FxnStr(USER.KER_TYPE, USER.KER_Z > 1, USER.KER_T > 1)

        # Check if we're even going to be doing anything #
        self.code = code
        if (code is None): return  # Do it manually #

        ## Image ##
        if (USER.SIM_IMG or len(fxns) > 0):
            # Simulate the aperture and kernel if needed #
            apr, ker = self.SimLoad()

            # Simulate the image (this simulates the appropriate aperture and kernel) #
            img, mot = self.SimImage((USER.CHIP[1], USER.CHIP[0]),
                                     apr,
                                     *fxns,
                                     blur=blur)
        else:
            # Check if the code has a `.tif` at the end, and if so, remove it #
            if (code[-4:] == '.tif'): code = code[:-4]

            # Check if an image or simulation exists and load it (Assumes [F, Z, Y, X]) #
            if (OP._CheckFile(code + str(FMT.TIF),
                              OP.FOLD_IMG)):  # Experimental	#
                img = OP._LoadMov(code, OP.FOLD_IMG)
            elif (OP._CheckFile(code + str(FMT.TIF),
                                OP.FOLD_SIM)):  # Simulation	#
                img = OP._LoadMov(code, OP.FOLD_SIM)
            else:  # Oops!			#
                raise FileNotFoundError(
                    'The specified image or simulation code was not found')

            sz = np.shape(img)[2:]  # Get the Y-X dimensions #

            # Simulate the aperture and kernel, if need be #
            apr, ker = self.SimLoad(sz)

            # Since we have no idea what the actual motion is, leave it blank #
            mot = None

        # Dump into the object #
        self.apr = apr
        self.ker = ker
        self.img = img
        self.mot = mot
예제 #4
0
파일: _SEGMENT.py 프로젝트: LandesLab/KNOT
def RUN(pos, wgt, img, *, code='', update=False, visual=False):
	## Update query ##
	if(not OP._CheckFile('%s\\%s_clouds.json' % (code, code)) or update):
		# Perform the segmentation #
		clouds = _Identify(pos, wgt, img, code=code, vis=visual)

		# Save data #
		clouds_json = [clouds[c].json for c in range(len(clouds))]
		OP._SaveJSON(clouds_json, '%s\\%s_clouds' % (code, code))
	else:
		# Load data #
		clouds_json = OP._LoadJSON('%s\\%s_clouds' % (code, code))
		clouds = []
		for c in range(len(clouds_json)):
			cloud = np.array(clouds_json[c])
			clouds.append(PointCloud(cloud))

	## Visualization query ##
	if(visual):
		plt.figure(figsize=(6,6))
		ax = plt.gca(projection='3d', position=[-0.05, -0.05, 1.1, 1.1])
		ax.set_xticklabels([])
		ax.set_yticklabels([])
		ax.set_zticklabels([])
		for c in clouds[:2]:
			ax.scatter(c.abs[:,0], c.abs[:,1], c.frm, s=400*c.wgt, c='k')	# 2x2
		ax.view_init(azim=60, elev=15)
		plt.show()

	## Output ##
	return clouds
예제 #5
0
def RUN(img, ker, eps, *, code='', update=False, visual=False):
    ## Update query ##
    if (not OP._CheckFile('%s\\%s_pts.json' % (code, code)) or update):
        # Perform ADMM and recover the point clouds #
        pos, wgt = _Recover(img, ker, eps, code=code, vis=visual)

        # Save data #
        pts = {
            'pos': [pos[f].tolist() for f in range(len(pos))],
            'wgt': [wgt[f].tolist() for f in range(len(wgt))]
        }
        OP._SaveJSON(pts, '%s\\%s_pts' % (code, code))
    else:
        # Load in the data #
        data = OP._LoadJSON('%s\\%s_pts' % (code, code))
        pos = [np.array(data['pos'][f]) for f in range(len(data['pos']))]
        wgt = [np.array(data['wgt'][f]) for f in range(len(data['wgt']))]

    ## Visualization query ##
    if (visual):
        f = 0
        plt.figure()
        plt.imshow(img[f, 0, :, :], cmap='gray')
        plt.scatter(pos[f][:, 0], pos[f][:, 1], s=100 * wgt[f], c='r')
        plt.show()

    ## Output ##
    return pos, wgt
예제 #6
0
def RUN(scope, *, code='', update=False, visual=False):
    ## Update query ##
    if (update or not OP._CheckFile('%s\\%s_prep.tif' % (code, code))):
        # Pre-process the movie and the kernel, obtaining the local threshold #
        img_, ker_, eps_ = _Preprocess2(code, scope.img, scope.ker)

        # Ensure that all values are non-negative #
        if (USER.KER_T > 1):
            img_ = np.maximum(img_, 0)
            ker_ = np.maximum(ker_, 0)
            eps_ = np.maximum(eps_, 0)

        # Save the processed image, kernel, and local threshold #
        OP._SaveMov(img_, '%s\\%s_prep' % (code, code))  # Processed Movie	#
        OP._SaveKer(ker_, '%s\\%s_ker' % (code, code))  # Processed Kernel	#
        OP._SaveMov(eps_, '%s\\%s_eps' % (code, code))  # Local Threshold	#
    else:
        # Check if the movies exist #
        if (OP._CheckFile('%s\\%s_prep.tif' % (code, code))):
            # Load the processed image, kernel, and local threshold #
            img_ = OP._LoadMov('%s\\%s_prep' %
                               (code, code))  # Processed Movie	#
            ker_ = OP._LoadKer('%s\\%s_ker' %
                               (code, code))  # Processed Kernel	#
            eps_ = OP._LoadMov('%s\\%s_eps' %
                               (code, code))  # Local Threshold	#
        else:
            # Just stuff some zeros in there for now #
            img_ = np.zeros_like(scope.img)
            ker_ = np.zeros_like(scope.ker)
            eps_ = np.zeros_like(scope.img)

    ## Visualization query ##
    if (visual):
        f = 0
        tru = OP._LoadTruth(code)
        pts = np.zeros([0, 3])
        for p in range(len(tru)):
            if (f in tru[p].frm):
                idx = np.nonzero(f == tru[p].frm)[0]
                pts = np.concatenate((pts, tru[p].res[idx, :]), axis=0)

        # Movie #
        VIS._VisImg(img_[f, 0, :, :], 550, 50, pts)

        # Kernel #
        if ((USER.KER_Z > 1) and (USER.KER_T == 1)):
            VIS._VisImg(ker_[0, 0, :, :], 100, 50)
            VIS._VisImg(ker_[0, (USER.KER_Z) // 4, :, :], 100, 550)
            VIS._VisImg(ker_[0, (2 * USER.KER_Z) // 4, :, :], 550, 550)
            VIS._VisImg(ker_[0, (3 * USER.KER_Z) // 4, :, :], 1000, 550)
            VIS._VisImg(ker_[0, -1, :, :], 1000, 50)
        elif ((USER.KER_Z == 1) and (USER.KER_T > 1)):
            VIS._VisImg(ker_[0, 0, :, :], 100, 50)
            VIS._VisImg(ker_[(USER.KER_T) // 4, 0, :, :], 100, 550)
            VIS._VisImg(ker_[(2 * USER.KER_T) // 4, 0, :, :], 550, 550)
            VIS._VisImg(ker_[(3 * USER.KER_T) // 4, 0, :, :], 1000, 550)
            VIS._VisImg(ker_[-1, 0, :, :], 1000, 50)

        # Local Threshold #
        VIS._VisImg(eps_[f, 0, :, :], 1450, 50)
        VIS._VisImg(((img_ - eps_) * (img_ > eps_))[f, 0, :, :], 1450, 550,
                    pts)

        plt.show()

    ## Output ##
    return img_, ker_, eps_
예제 #7
0
파일: _TRACK.py 프로젝트: LandesLab/KNOT
def RUN(clouds, img, *, code='', update=False, visual=False):
	## Update query ##
	if(not OP._CheckFile('%s\\%s_tracks.json' % (code, code)) or update):
		# Track the point clouds #
		if(USER.KER_T == 1):
			traj = _Track(clouds, img, code=code, vis=visual)
			OP._SaveTracks(traj, code)
		else:
			traj = _SubTrack(clouds, img, code=code, vis=visual)

		# Stitch the trajectories together # 
		#traj = _Stitch(traj, code=code)	# Skipping for today due to time constraints & testing #

		# Save data #
		OP._SaveTracks(traj, code)
	else:
		# Load data #
		pass

	## Visualization query ##
	if(visual and USER.KER_T == 1):
		clr = plt.get_cmap('tab10').colors

		# 2D #
		plt.figure(figsize=(6,6))
		ax = plt.axes(position=[0,0,1,1])
		ax.imshow(img[-1,0,:,:], cmap='gray')
		k = 0
		#for c in clouds:
		#	ax.scatter(c.pos[:,0], c.pos[:,1], s=100*c.wgt, linewidth=0, marker='o')
		for p in traj:
			F = len(p.frm)
			if(F < USER.TRK_MIN): continue
			seg = [(p.res[f,:2].tolist(), p.res[f+1,:2].tolist()) for f in range(F-1)]
			VIS._DispLineGrad(ax, seg, p.frm, clr[np.mod(k, len(clr))], linewidth=6)
			k += 1
		ax.set_xlim(0, np.shape(img)[3])
		ax.set_ylim(np.shape(img)[2], 0)
		
		# 3D #
		plt.figure(figsize=(6, 6))
		ax = plt.axes(projection='3d', position=[-0.15,-0.15,1.3,1.3])
		k = 0
		for p in traj:
			F = len(p.frm)
			if(F < USER.TRK_MIN): continue
			seg = [(p.res[f,:].tolist(), p.res[f+1,:].tolist()) for f in range(F-1)]
			VIS._DispLineGrad(ax, seg, p.frm, clr[np.mod(k, len(clr))], linewidth=6)
			k += 1
		ax.set_xlim(0, np.shape(img)[3])
		ax.set_ylim(np.shape(img)[2], 0)
		ax.set_zlim(0, USER.KER_Z)
		plt.show()
	elif(visual and USER.KER_T > 1):
		clr = plt.get_cmap('tab10').colors

		# 2D #
		plt.figure(figsize=(6,6))
		ax = plt.axes(position=[0,0,1,1])
		ax.imshow(np.sum(img, axis=(0,1)), cmap='gray')
		k = 0
		for p in traj:
			F = len(p.frm)
			if(F < 2): continue
			pos = np.empty([0, 3])
			for c in range(len(p.cloud)):
				pos = np.concatenate([pos, p.cloud[c].sres[:,:-1]], axis=0)
			seg = [(pos[f,:2].tolist(), pos[f+1,:2].tolist()) for f in range(np.shape(pos)[0]-1)]
			VIS._DispLineGrad(ax, seg, p.frm, clr[np.mod(k, len(clr))], linewidth=6)
			k += 1

		# 2D + time #
		plt.figure(figsize=(6,6))
		ax = plt.axes(projection='3d', position=[0,0,1,1])
		ax.set_xticklabels([])
		ax.set_yticklabels([])
		ax.set_zticklabels([])
		k = 0
		for p in traj:
			F = len(p.frm)
			if(F < 2): continue
			pos = np.empty([0, 4])
			for c in range(len(p.cloud)):
				pos = np.concatenate([pos, p.cloud[c].sres], axis=0)
			seg = [(pos[f,[0,1,3]].tolist(), pos[f+1,[0,1,3]].tolist()) for f in range(np.shape(pos)[0]-1)]
			VIS._DispLineGrad(ax, seg, p.frm, clr[np.mod(k, len(clr))], linewidth=6)
			k += 1
		ax.set_xlim(0, np.shape(img)[3])
		ax.set_ylim(0, np.shape(img)[2])
		ax.set_zlim(0, np.shape(img)[0])

		# 3D + time #
		plt.figure(figsize=(6,6))
		ax = plt.axes(projection='3d', position=[0,0,1,1])
		ax.set_xticklabels([])
		ax.set_yticklabels([])
		ax.set_zticklabels([])
		k = 0
		for p in traj:
			F = len(p.frm)
			if(F < 2): continue
			pos = np.empty([0, 4])
			for c in range(len(p.cloud)):
				pos = np.concatenate([pos, p.cloud[c].sres], axis=0)
			seg = [(pos[f,[0,1,2]].tolist(), pos[f+1,[0,1,2]].tolist()) for f in range(np.shape(pos)[0]-1)]
			VIS._DispLineGrad(ax, seg, p.frm, clr[np.mod(k, len(clr))], linewidth=6)
			k += 1
		ax.set_xlim(0, np.shape(img)[3])
		ax.set_ylim(0, np.shape(img)[2])
		ax.set_zlim(0, USER.KER_Z)
		plt.show()
	## Output ##
	return traj