def commitToDatabase(self, imgdata):
        # if a kept mask was created in a previous mask run and the
        # assess flag was used (basically combining the 2 runs) there is
        # nothing new to commit.
        if self.useAcceptedMask: return

        sessiondata = imgdata['session']
        rundir = self.params['rundir']
        maskname = self.params['runname']
        assessname = self.params['assessname']
        bin = self.params['bin']
        maskdir = os.path.join(rundir, "masks")
        maskrundata, maskparamsdata = apMask.getMaskParamsByRunName(
            maskname, sessiondata)
        if not maskrundata:
            apMask.insertManualMaskRun(sessiondata, rundir, maskname, bin)
            maskrundata, maskparamsdata = apMask.getMaskParamsByRunName(
                maskname, sessiondata)
            try:
                apParam.createDirectory(maskdir)
            except:
                apDisplay.printWarning('can not create mask directory')
        massessrundata, exist = apMask.insertMaskAssessmentRun(
            sessiondata, maskrundata, assessname)

        mask = self.maskimg
        maskfilename = imgdata['filename'] + '_mask.png'

        image = self.image
        labeled_regions, clabels = nd.label(mask)
        testlog = [False, 0, ""]
        infos = {}
        infos, testlog = apCrud.getLabeledInfo(image, mask, labeled_regions,
                                               range(1, clabels + 1), False,
                                               infos, testlog)
        offset = 1
        for l1 in range(0, len(infos)):

            l = l1 + offset
            info = infos[l]
            info.append(l)
            regiondata = apMask.insertMaskRegion(maskrundata, imgdata, info)
            print "Inserting mask region in database"

        # PIL alpha channel read does not work
        #apImage.arrayMaskToPngAlpha(mask, os.path.join(maskdir,maskfilename))
        apImage.arrayMaskToPng(mask, os.path.join(maskdir, maskfilename))

        allregiondata = apMask.getMaskRegions(maskrundata, imgdata)

        for regiondata in allregiondata:
            apMask.insertMaskAssessment(massessrundata, regiondata, 1)
            print "Inserting mask assessment in database."

        if self.assess != self.assessold and self.assess is not None:
            #imageaccessor run is always named run1
            apDatabase.insertImgAssessmentStatus(imgdata, 'run1', self.assess)
        return
	def commitToDatabase(self,imgdata):
        # if a kept mask was created in a previous mask run and the 
        # assess flag was used (basically combining the 2 runs) there is 
        # nothing new to commit.  
		if self.useAcceptedMask: return
        
		sessiondata = imgdata['session']
		rundir = self.params['rundir']
		maskname = self.params['runname']
		assessname = self.params['assessname']
		bin = self.params['bin']
		maskdir=os.path.join(rundir,"masks")
		maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata)
		if not maskrundata:
			apMask.insertManualMaskRun(sessiondata,rundir,maskname,bin)
			maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata)
			try:
				apParam.createDirectory(maskdir)
			except:
				apDisplay.printWarning('can not create mask directory')
		massessrundata,exist = apMask.insertMaskAssessmentRun(sessiondata,maskrundata,assessname)
		
		mask = self.maskimg
		maskfilename = imgdata['filename']+'_mask.png'
		
		image = self.image
		labeled_regions,clabels=nd.label(mask)
		testlog = [False,0,""]
		infos={}
		infos,testlog=apCrud.getLabeledInfo(image,mask,labeled_regions,range(1,clabels+1),False,infos,testlog)
		offset=1
		for l1 in range(0,len(infos)):

			l=l1+offset
			info=infos[l]
			info.append(l)
			regiondata= apMask.insertMaskRegion(maskrundata,imgdata,info)
			print "Inserting mask region in database"
		
		# PIL alpha channel read does not work
		#apImage.arrayMaskToPngAlpha(mask, os.path.join(maskdir,maskfilename))
		apImage.arrayMaskToPng(mask, os.path.join(maskdir,maskfilename))

		allregiondata = apMask.getMaskRegions(maskrundata,imgdata)
			
		for regiondata in allregiondata:
			apMask.insertMaskAssessment(massessrundata,regiondata,1)
			print "Inserting mask assessment in database."

		if self.assess != self.assessold and self.assess is not None:
			#imageaccessor run is always named run1
			apDatabase.insertImgAssessmentStatus(imgdata, 'run1', self.assess)
		return
    def commitToDatabase(self, imgdata):
        # if a kept mask was created in a previous mask run and the
        # assess flag was used (basically combining the 2 runs) there is
        # nothing new to commit.
        if self.useAcceptedMask: return

        sessiondata = imgdata['session']
        rundir = self.params['rundir']
        maskname = self.params['runname']
        assessname = self.params['assessname']
        bin = self.params['bin']
        maskdir = os.path.join(rundir, "masks")
        maskrundata, maskparamsdata = apMask.getMaskParamsByRunName(
            maskname, sessiondata)
        if not maskrundata:
            apMask.insertManualMaskRun(sessiondata, rundir, maskname, bin)
            maskrundata, maskparamsdata = apMask.getMaskParamsByRunName(
                maskname, sessiondata)
        massessrundata, exist = apMask.insertMaskAssessmentRun(
            sessiondata, maskrundata, assessname)

        mask = self.maskimg
        maskfilename = imgdata['filename'] + '_mask.png'

        image = self.image
        labeled_regions, clabels = nd.label(mask)
        testlog = [False, 0, ""]
        infos = {}
        infos, testlog = apCrud.getLabeledInfo(image, mask, labeled_regions,
                                               range(1, clabels + 1), False,
                                               infos, testlog)
        offset = 1
        for l1 in range(0, len(infos)):

            l = l1 + offset
            info = infos[l]
            info.append(l)
            regiondata = apMask.insertMaskRegion(maskrundata, imgdata, info)
            print "Inserting mask region in database"

        allregiondata = apMask.getMaskRegions(maskrundata, imgdata)

        for regiondata in allregiondata:
            apMask.insertMaskAssessment(massessrundata, regiondata, 1)
            print "Inserting mask assessment in database."
        return
	def commitToDatabase(self,imgdata):
        # if a kept mask was created in a previous mask run and the 
        # assess flag was used (basically combining the 2 runs) there is 
        # nothing new to commit.  
		if self.useAcceptedMask: return
        
		sessiondata = imgdata['session']
		rundir = self.params['rundir']
		maskname = self.params['runname']
		assessname = self.params['assessname']
		bin = self.params['bin']
		maskdir=os.path.join(rundir,"masks")
		maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata)
		if not maskrundata:
			apMask.insertManualMaskRun(sessiondata,rundir,maskname,bin)
			maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata)
		massessrundata,exist = apMask.insertMaskAssessmentRun(sessiondata,maskrundata,assessname)
		
		mask = self.maskimg
		maskfilename = imgdata['filename']+'_mask.png'
		
		image = self.image
		labeled_regions,clabels=nd.label(mask)
		testlog = [False,0,""]
		infos={}
		infos,testlog=apCrud.getLabeledInfo(image,mask,labeled_regions,range(1,clabels+1),False,infos,testlog)
		offset=1
		for l1 in range(0,len(infos)):

			l=l1+offset
			info=infos[l]
			info.append(l)
			regiondata= apMask.insertMaskRegion(maskrundata,imgdata,info)
			print "Inserting mask region in database"
		
		allregiondata = apMask.getMaskRegions(maskrundata,imgdata)
			
		for regiondata in allregiondata:
			apMask.insertMaskAssessment(massessrundata,regiondata,1)
			print "Inserting mask assessment in database."
		return
    def commitToDatabase(self, imgdata):
        sessiondata = imgdata['session']
        rundir = self.params['rundir']
        maskname = self.params['runname']
        assessname = self.params['runname']
        bin = self.params['bin']
        maskdir = os.path.join(rundir, "masks")

        maskrundata, maskparamsdata = apMask.getMaskParamsByRunName(
            maskname, sessiondata)

        if not maskrundata:
            apMask.insertManualMaskRun(sessiondata, rundir, maskname, bin)
            maskrundata, maskparamsdata = apMask.getMaskParamsByRunName(
                maskname, sessiondata)
            try:
                apParam.createDirectory(maskdir)
            except:
                apDisplay.printWarning('can not create mask directory')

        massessrundata, exist = apMask.insertMaskAssessmentRun(
            sessiondata, maskrundata, assessname)

        # The mask file should only exist if the em_hole_finder found a region to mask.
        # If it does not exist, do not insert anything to the DB.
        if (os.path.exists(self.outfile)):
            apDisplay.printMsg("Writing results to database: " +
                               time.asctime())

            # Set black pixels to white and anything else to 0
            img1 = Image.open(self.outfile)
            img2 = Image.eval(img1, lambda px: 255 if px == 0 else 0)

            # make sure the images have the same shape
            imgshape = numpy.asarray(imgdata['image'].shape)
            apDisplay.printMsg("MRC Image Shape:")
            print imgshape
            imgsize = imgshape[0] * imgshape[1]
            apDisplay.printMsg("MRC Image Size:")
            print imgsize

            maskshape = numpy.shape(img2)
            apDisplay.printMsg("Mask Image Shape:")
            print maskshape

            apDisplay.printMsg("resizing mask image with scale:")
            scaleFactorx = float(imgshape[0]) / float(maskshape[0])
            scaleFactory = float(imgshape[1]) / float(maskshape[1])
            scale = scaleFactorx, scaleFactory
            print scale

            img3 = imagefun.scale(img2, scale)
            maskshape = numpy.shape(img3)
            apDisplay.printMsg("Mask Image Shape:")
            print maskshape
            #img3 = numpy.resize(img2, imgshape) # not working
            img3path = self.outfile + "_tmp.jpg"
            scipy.misc.imsave(img3path, img3)

            labeled_regions, clabels = ndimage.label(img3)

            testlog = [False, 0, ""]
            infos = {}

            apDisplay.printMsg("getting mask region info.")
            infos, testlog = apCrud.getLabeledInfo(imgdata['image'], img3,
                                                   labeled_regions,
                                                   range(1, clabels + 1),
                                                   False, infos, testlog)

            apDisplay.printMsg("inserting mask regions to DB.")
            print len(infos)

            area_max = imgsize * .9
            offset = 1
            for l1 in range(0, len(infos)):
                l = l1 + offset
                info = infos[l]
                area = info[0]
                print area
                if (area > 400 and area < area_max):
                    apDisplay.printMsg("saving a region of size:")
                    print area
                    info.append(l)
                    regiondata = apMask.insertMaskRegion(
                        maskrundata, imgdata, info)

        # Insert mask assessment regions. This keeps track of the mask regions that the user wants to reject.
        allregiondata = apMask.getMaskRegions(maskrundata, imgdata)

        for regiondata in allregiondata:
            apMask.insertMaskAssessment(massessrundata, regiondata, True)

#               if self.assess != self.assessold and self.assess is not None:
#                       #imageaccessor run is always named run1
#                       apDatabase.insertImgAssessmentStatus(imgdata, 'run1', self.assess)
        return
	def commitToDatabase(self,imgdata):
		sessiondata   = imgdata['session']
		rundir        = self.params['rundir']
		maskname      = self.params['runname']
		assessname    = self.params['runname']
		bin           = self.params['bin']
		maskdir       = os.path.join(rundir,"masks")
		
		maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata)
		
		if not maskrundata:
			apMask.insertManualMaskRun(sessiondata,rundir,maskname,bin)
			maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata)
			try:
				apParam.createDirectory(maskdir)
			except:
				apDisplay.printWarning('can not create mask directory')
		
		massessrundata,exist = apMask.insertMaskAssessmentRun(sessiondata,maskrundata,assessname)
		
		# The mask file should only exist if the em_hole_finder found a region to mask.
		# If it does not exist, do not insert anything to the DB.
		if ( os.path.exists(self.outfile) ):
			apDisplay.printMsg("Writing results to database: " + time.asctime())
			
			# Set black pixels to white and anything else to 0
			img1 = Image.open(self.outfile)
			img2 = Image.eval(img1, lambda px: 255 if px==0 else 0)

			# make sure the images have the same shape
			imgshape = numpy.asarray(imgdata['image'].shape)
			apDisplay.printMsg("MRC Image Shape:")
			print imgshape
			imgsize = imgshape[0]*imgshape[1]
			apDisplay.printMsg("MRC Image Size:")
			print imgsize

			maskshape = numpy.shape(img2)
			apDisplay.printMsg("Mask Image Shape:")
			print maskshape
			
			apDisplay.printMsg("resizing mask image with scale:")
			scaleFactorx = float(imgshape[0])/float(maskshape[0])
			scaleFactory = float(imgshape[1])/float(maskshape[1])
			scale = scaleFactorx, scaleFactory
			print scale

			img3 = imagefun.scale( img2, scale )
			maskshape = numpy.shape(img3)
			apDisplay.printMsg("Mask Image Shape:")
			print maskshape
			#img3 = numpy.resize(img2, imgshape) # not working
			img3path = self.outfile + "_tmp.jpg"
			scipy.misc.imsave(img3path, img3)			
			
			labeled_regions,clabels = ndimage.label(img3)
				
			testlog = [False,0,""]
			infos={}
			
			apDisplay.printMsg("getting mask region info.")
			infos,testlog = apCrud.getLabeledInfo( imgdata['image'], img3, labeled_regions, range(1,clabels+1), False, infos, testlog)
	
			apDisplay.printMsg("inserting mask regions to DB.")
			print len(infos)
			
			area_max = imgsize*.9
			offset = 1
			for l1 in range(0,len(infos)):
				l = l1 + offset
				info = infos[l]
				area=info[0]
				print area
				if (area > 400 and area < area_max):
					apDisplay.printMsg("saving a region of size:")
					print area
					info.append(l)
					regiondata = apMask.insertMaskRegion( maskrundata, imgdata, info )

		# Insert mask assessment regions. This keeps track of the mask regions that the user wants to reject.
		allregiondata = apMask.getMaskRegions(maskrundata,imgdata)
		
		for regiondata in allregiondata:
			apMask.insertMaskAssessment(massessrundata,regiondata,True)

#		if self.assess != self.assessold and self.assess is not None:
#			#imageaccessor run is always named run1
#			apDatabase.insertImgAssessmentStatus(imgdata, 'run1', self.assess)
		return