def upgradeAppionDB(self):
     if self.appion_dbupgrade.columnExists('ApContourData', 'runname'):
         if not self.appion_dbupgrade.columnExists(
                 'ApContourData', 'REF|ApSelectionRunData|selectionrun'):
             self.appion_dbupgrade.addColumn(
                 'ApContourData',
                 'REF|ApSelectionRunData|selectionrun',
                 self.appion_dbupgrade.link,
                 index=True)
         contourq = appiondata.ApContourData()
         contours = contourq.query()
         for contourdata in contours:
             if not contourdata['runname']:
                 continue
             selectionrundata = apParticle.getSelectionRunDataFromName(
                 contourdata['image'], contourdata['runname'])
             contourid = contourdata.dbid
             selectionid = selectionrundata.dbid
             if contourid and selectionid:
                 updateq = (
                     "UPDATE ApContourData AS contour " + " SET " +
                     "   contour.`REF|ApSelectionRunData|selectionrun` =  "
                     + " %d " % selectionid +
                     " WHERE contour.`DEF_id` = %d " % contourid)
                 self.appion_dbupgrade.executeCustomSQL(updateq)
	def upgradeAppionDB(self):
		if self.appion_dbupgrade.columnExists('ApContourData', 'runname'):
			if not self.appion_dbupgrade.columnExists('ApContourData', 'REF|ApSelectionRunData|selectionrun'):
				self.appion_dbupgrade.addColumn('ApContourData', 'REF|ApSelectionRunData|selectionrun', self.appion_dbupgrade.link, index=True)
			contourq = appiondata.ApContourData()
			contours = contourq.query()
			for contourdata in contours:
				if not contourdata['runname']:
					continue
				selectionrundata = apParticle.getSelectionRunDataFromName(contourdata['image'], contourdata['runname'])
				contourid = contourdata.dbid
				selectionid = selectionrundata.dbid
				if contourid and selectionid:
					updateq = ("UPDATE ApContourData AS contour "
						+" SET "
						+"   contour.`REF|ApSelectionRunData|selectionrun` =  "
						+" %d " % selectionid
						+" WHERE contour.`DEF_id` = %d " % contourid
					)
					self.appion_dbupgrade.executeCustomSQL(updateq)
	def runManualPicker(self, imgdata):
		# Contourpicker does not do assessment.  Setting the current assessment to be
		# the same as the old prevents committing it to the database
		self.assessold = None
		self.assess = self.assessold
		#reset targets
		self.targets = {}
		for label in self.labels:
			self.app.panel.setTargets(label, [])
			self.targets[label] = []

					
		#open new file
		imgname = imgdata['filename']+'.dwn.mrc'
		imgpath = os.path.join(self.params['rundir'],imgname)
		self.app.panel.openImageFile(imgpath)

		self.app.panel.originaltargets = {}

		#set vital stats
		self.app.vitalstats.SetLabel("Vital Stats: Image "+str(self.stats['count'])
			+" of "+str(self.stats['imagecount'])
			+" image name: "+imgdata['filename'])
		#run the picker
		self.loadOld(imgdata)
		self.app.MainLoop()

		#targets are copied to self.targets by app
		#parse and return the targets in peaktree form
		self.app.panel.openImageFile(None)
		peaktree=[]
		for label,targets in self.targets.items():
			for target in targets:
				peaktree.append(self.XY2particle(target.x, target.y, label=label))
		
		targetsList = self.getPolyParticlePoints()
		contourTargets = self.app.panel.getTargets('Auto Create Contours')

		try:
			rundata = apParticle.getSelectionRunDataFromName(imgdata,self.params['runname'])
		except IndexError:
			# the first image does not have rundata in the database, yet
			rundata = self.commitRunToDatabase(imgdata['session'], True)
		bin = rundata['manparams']['bin']
		c = None
		counter = 0
		for i in range(len(targetsList)):
			# safe-guard from 1 or 2 point target
			if len(targetsList[i]) < 3:
				apDisplay.printWarning('contour %d has only %d points....IGNORED' % (i,len(targetsList[i])))
				counter += 1
				continue
			c=appiondata.ApContourData(name="contour"+str(int(self.startPoint)+i), image=imgdata, x=contourTargets[counter].x, y=contourTargets[counter].y,version=self.maxVersion+1, method='auto', particleType=self.app.particleTypeList[counter], selectionrun=rundata)
			c.insert()
			counter += 1
			for point in targetsList[i]:
				# save points in the scale of the original image, like particles
				point1=appiondata.ApContourPointData(x=point[0]*bin, y=point[1]*bin, contour=c)
				point1.insert()

		return peaktree