Exemple #1
0
def main():

	path = './Tweak/'

	videoIDs = []
	listOfMetaDataFiles = os.listdir(path)
	for metaDataFile in listOfMetaDataFiles:
		parts = metaDataFile.split('.')
		ID = parts[0]
		kind = parts[1]
		if kind == 'm4v_metadata':
			videoIDs.append(ID)

	try:
		arg1 = sys.argv[1]
		if arg1 == '-?':
			print help_message
			return
	except:		
		print help_message
		return

	f = open('./DataSet/ignore.json','r')
	content = f.read()
	ignore = json.loads(content).get('ignore')

	errors = []
	for thisID in videoIDs:
		# print thisID
		if thisID in ignore:
			# print 'ignoring data in %s' % thisID
			continue

		# uncomment to only use "cut" segments
		# if 'part' not in thisID:
		# 	continue

		metadata_filename = path + thisID + '.m4v_metadata.txt'
		answer_filename = path + thisID + '.m4v.txt'
		metadata_exists = os.path.isfile(metadata_filename)
		answer_exists = os.path.isfile(answer_filename)

		if not metadata_exists:
			print 'Metadata for ' + thisID + ' does not exists'
		# if there is no answer file and the filename does not have 'part' in it
		# it is likely missing. otherwise we will create one
		if not answer_exists and ('part' not in thisID):
			print 'Answer-file for ' + thisID + ' does not exists'

		# if metadata_exists and answer_exists:
		if metadata_exists:

			f = open(metadata_filename,'r')
			content = f.read()
			metadata = json.loads(content)
			d = metadata
			f.close()

			if answer_exists:
				f = open(answer_filename,'r')
				content = f.read()
				answer_data = json.loads(content)
				f.close()
			else:
				answer_data = dict(states=[1 for x in metadata.get('shift_vectors')])

			shift_vectors = d['shift_vectors']
			# rmsdiffs = d['rmsdiffs']
			# shift_vectors_sliding = d['shift_vectors_sliding']
			stand_dev = d['stand_dev']

			# degree of smoothness
			degree = 12
			
			try:
				magnitudes = smoothTriangle((np.array([math.sqrt(x**2 + y**2) for x,y in shift_vectors])**2)/(63**2), degree)	
				contrast = smoothTriangle((127.5 - np.array(stand_dev)) / 127.5, degree)
			except IndexError as e:
				# too little data in segment will cause this error
				continue
			
			# compute if a frame is accepted, and the according value
			if arg1 == 'anders':
				frame_states, frame_values = computeFrameStateAnders(magnitudes, contrast)
			elif arg1 == 'anders2':
				frame_states, frame_values = computeFrameStateAnders2(magnitudes, contrast)				
			elif arg1 == 'square':
				frame_states, frame_values = computeFrameStateSquare(magnitudes, contrast)
			elif arg1 == 'cubic':
				frame_states, frame_values = computeFrameStateCubic(magnitudes, contrast)
			# elif arg1 == 'algox':
			# 	frame_states, frame_values = computeFrameStateX(magnitudes, contrast, 5.0, 0.7)
			elif arg1 == 'lauge':
				frame_states, frame_values = computeFrameStateLauge(magnitudes, contrast, (0.01,0.75))
			elif arg1 == 'naiive':
				frame_states, frame_values = computeFrameStateNaiive(magnitudes, contrast)
			elif arg1 == 'magnitude':
				frame_states, frame_values = computeFrameStateMagnitudeOnly(magnitudes)
			elif arg1 == 'contrast':
				frame_states, frame_values = computeFrameStateContrastOnly(contrast)
			else:
				return

			answer_states = answer_data.get('states')

			# Calculate error
			errors.append(calcErr.simpleCompare(frame_states, answer_states))

	

	totals = 0
	totalPositives = 0
	totalNegatives = 0
	corrects = 0
	truePositives = 0
	trueNegatives = 0
	falsePositives = 0
	falseNegatives = 0
	
	for error in errors:

		totals += int(error.get('total'))
		totalPositives += int(error.get('total positives'))
		totalNegatives += int(error.get('total negatives'))

		corrects += error.get('correct')

		truePositives += error.get('true positives')
		trueNegatives += error.get('true negatives')
		falsePositives += error.get('false positives')
		falseNegatives += error.get('false negatives')

	correctsRatio = float(corrects)/totals
	try:
		positivePrecision = float(truePositives) / (truePositives + falsePositives)
	except:
		positivePrecision = -1
	try:
		positiveRecall = float(truePositives) / totalPositives
	except:
		positiveRecall = -1
	try:
		negativePrecision = float(trueNegatives) / (trueNegatives + falseNegatives)
	except:
		negativePrecision = -1
	try:
		negativeRecall = float(trueNegatives) / totalNegatives
	except:
		negativeRecall = -1

	
	print '\nOverall accuracy: %2.3f%%' % (100.0 * correctsRatio)
	if positivePrecision >= 0:
		print 'Positive precision: %2.3f%%' % (100.0 * positivePrecision)
	else:
		print 'Positive precision: NOT DEFINED'
	if positiveRecall >= 0:
		print 'Positive recall: %2.3f%%' % (100.0 * positiveRecall)
	else:
		print 'Positive recall: NOT DEFINED'
	if negativePrecision >= 0:
		print 'Negative precision: %2.3f%%' % (100.0 * negativePrecision)
	else:
		print 'Negative precision: NOT DEFINED'
	if negativeRecall >= 0:
		print 'Negative recall: %2.3f%%' % (100.0 * negativeRecall)
	else:
		print 'Negative recall: NOT DEFINED'
Exemple #2
0
	def tweak(self):

		errors = []

		for i in range(len(self.answer_datas)):

			# metadata = self.metadatas[i]
			answer_data = self.answer_datas[i]
			sugg_data = self.suggestion_datas[i]

			answer_states = answer_data.get('states')
			frame_states = sugg_data.get('states')
			# Calculate error
			errors.append(calcErr.simpleCompare(frame_states, answer_states))

		totals = 0
		totalPositives = 0
		totalNegatives = 0
		corrects = 0
		truePositives = 0
		trueNegatives = 0
		falsePositives = 0
		falseNegatives = 0
		
		for error in errors:

			totals += int(error.get('total'))
			totalPositives += int(error.get('total positives'))
			totalNegatives += int(error.get('total negatives'))

			corrects += error.get('correct')

			truePositives += error.get('true positives')
			trueNegatives += error.get('true negatives')
			falsePositives += error.get('false positives')
			falseNegatives += error.get('false negatives')

		correctsRatio = float(corrects)/totals
		try:
			positivePrecision = float(truePositives) / (truePositives + falsePositives)
		except:
			positivePrecision = -1
		try:
			positiveRecall = float(truePositives) / totalPositives
		except:
			positiveRecall = -1
		try:
			negativePrecision = float(trueNegatives) / (trueNegatives + falseNegatives)
		except:
			negativePrecision = -1
		try:
			negativeRecall = float(trueNegatives) / totalNegatives
		except:
			negativeRecall = -1

		print '\nOverall accuracy: %2.3f%%' % (100.0 * correctsRatio)
		if positivePrecision >= 0:
			print 'Positive precision: %2.3f%%' % (100.0 * positivePrecision)
		else:
			print 'Positive precision: NOT DEFINED'
		if positiveRecall >= 0:
			print 'Positive recall: %2.3f%%' % (100.0 * positiveRecall)
		else:
			print 'Positive recall: NOT DEFINED'
		if negativePrecision >= 0:
			print 'Negative precision: %2.3f%%' % (100.0 * negativePrecision)
		else:
			print 'Negative precision: NOT DEFINED'
		if negativeRecall >= 0:
			print 'Negative recall: %2.3f%%' % (100.0 * negativeRecall)
		else:
			print 'Negative recall: NOT DEFINED'
		
		return truePositives, falsePositives, trueNegatives, falseNegatives
Exemple #3
0
def main():

	# Degree of smoothness (doesnt matter very much)
	degree = 12

	metadataDir = None

	try:
		arg1 = sys.argv[1]
		if arg1 == '-?':
			print help_message
			return
		
		metadataDir = sys.argv[2]
		if len(sys.argv) == 4:
			answerDir = sys.argv[3]
		else:
			answerDir = None
	except:		
		print help_message
		return

	f = open('./DataSet/ignore.json','r')
	content = f.read()
	ignore = json.loads(content).get('ignore')

	listOfMetaDataFiles = os.listdir(metadataDir)
	
	errors = []
	for metaDataFile in listOfMetaDataFiles:

		# Get video ID
		thisID = metaDataFile.split('.')[0]

		if thisID in ignore:
			continue

		# Get metadata file for video
		metadata_filename = metadataDir + '/' + thisID + '.m4v_metadata.txt'
		metadata_exists = os.path.isfile(metadata_filename)

		if answerDir:
			answer_filename = answerDir + '/' + thisID + '.m4v.txt'
			answer_exists = os.path.isfile(answer_filename)
		else:
			answer_filename = None
			answer_exists = None

		if (metadata_exists and answer_exists) or (metadata_exists and not answerDir):

			# Read metadata file
			f = open(metadata_filename,'r')
			content = f.read()
			metadata = json.loads(content)
			d = metadata
			f.close()

			shift_vectors = d['shift_vectors']
			# rmsdiffs = d['rmsdiffs']
			# shift_vectors_sliding = d['shift_vectors_sliding']
			stand_dev = d['stand_dev']
			
			# Smooth the data (doesnt matter very much)
			magnitudes = smoothTriangle((np.array([math.sqrt(x**2 + y**2) for x,y in shift_vectors])**2)/(63**2), degree)	
			contrast = smoothTriangle((127.5 - np.array(stand_dev)) / 127.5, degree)
			
			# Calculate/guess frame states
			if arg1 == 'anders':
				frame_states, frame_values = computeFrameStateAnders(magnitudes, contrast)
			elif arg1 == 'lauge':
				frame_states, frame_values = computeFrameStateLauge(magnitudes, contrast)
			elif arg1 == 'naiive':
				frame_states, frame_values = computeFrameStateNaiive(magnitudes, contrast)
			elif arg1 == 'magnitude':
				frame_states, frame_values = computeFrameStateMagnitudeOnly(magnitudes)
			elif arg1 == 'contrast':
				frame_states, frame_values = computeFrameStateContrastOnly(contrast)
			else:
				return


			# Was an answer directory supplied by the user
			if answerDir is not None:
				
				if answer_exists:
					f = open(answer_filename,'r')
					content = f.read()
					answer_data = json.loads(content)
					f.close()
					answer_states = answer_data.get('states')

					# Calculate error
					errors.append(calcErr.simpleCompare(frame_states, answer_states))
			else:
				# No! Assume that all frames should be GOOD
				errors.append(calcErr.simpleCompare(frame_states))

	totals = 0
	totalPositives = 0
	totalNegatives = 0
	corrects = 0
	truePositives = 0
	trueNegatives = 0
	falsePositives = 0
	falseNegatives = 0
	
	for error in errors:

		totals += int(error.get('total'))
		totalPositives += int(error.get('total positives'))
		totalNegatives += int(error.get('total negatives'))

		corrects += error.get('correct')

		truePositives += error.get('true positives')
		trueNegatives += error.get('true negatives')
		falsePositives += error.get('false positives')
		falseNegatives += error.get('false negatives')

	correctsRatio = float(corrects)/totals
	try:
		positivePrecision = float(truePositives) / (truePositives + falsePositives)
	except:
		positivePrecision = -1
	try:
		positiveRecall = float(truePositives) / totalPositives
	except:
		positiveRecall = -1
	try:
		negativePrecision = float(trueNegatives) / (trueNegatives + falseNegatives)
	except:
		negativePrecision = -1
	try:
		negativeRecall = float(trueNegatives) / totalNegatives
	except:
		negativeRecall = -1

	
	print '\nOverall accuracy: %2.3f%%' % (100.0 * correctsRatio)
	if positivePrecision >= 0:
		print 'Positive precision: %2.3f%%' % (100.0 * positivePrecision)
	else:
		print 'Positive precision: NOT DEFINED'
	if positiveRecall >= 0:
		print 'Positive recall: %2.3f%%' % (100.0 * positiveRecall)
	else:
		print 'Positive recall: NOT DEFINED'
	if negativePrecision >= 0:
		print 'Negative precision: %2.3f%%' % (100.0 * negativePrecision)
	else:
		print 'Negative precision: NOT DEFINED'
	if negativeRecall >= 0:
		print 'Negative recall: %2.3f%%' % (100.0 * negativeRecall)
	else:
		print 'Negative recall: NOT DEFINED'
	def tweak(self, p):

		errors = []

		for i in range(len(self.metadatas)):

			metadata = self.metadatas[i]
			answer_data = self.answer_datas[i]
			method = self.method
			videoID = self.videoIDs[i]

			shift_vectors = metadata['shift_vectors']
			stand_dev = metadata['stand_dev']

			# degree of smoothness
			degree = 12

			out_filename = '%s.%s.txt' % (videoID, str(p))
			path = './DataSet/frame_states/%s' % (method.__name__)
			out_filename = '%s/%s' % (path, out_filename)

			if os.path.isfile(out_filename):
				f = open(out_filename,'r')
				content = f.read()
				d = json.loads(content)	
				frame_states = d.get('frame_states', [])
				frame_values = d.get('frame_values', [])
			else:

				try:
					magnitudes = smoothTriangle((np.array([math.sqrt(x**2 + y**2) for x,y in shift_vectors])**2)/(63**2), degree)	
					contrast = smoothTriangle((127.5 - np.array(stand_dev)) / 127.5, degree)
				except IndexError as e:
					# too little data in segment will cause this error
					continue
				
				frame_states, frame_values = method(magnitudes, contrast, p)

				content = json.dumps(dict(frame_states=frame_states, frame_values=frame_values))
				# write to disc
				f = open(out_filename,'w')	
				f.write(content)
				f.close()

			smoothness_degree = self.smoothness_degree
			if smoothness_degree:
				frame_states = [round(x) for x in smoothTriangle(frame_states, smoothness_degree)]
			answer_states = answer_data.get('states')

			# Calculate error
			errors.append(calcErr.simpleCompare(frame_states, answer_states))

		totals = 0
		totalPositives = 0
		totalNegatives = 0
		corrects = 0
		truePositives = 0
		trueNegatives = 0
		falsePositives = 0
		falseNegatives = 0
		
		for error in errors:

			totals += int(error.get('total'))
			totalPositives += int(error.get('total positives'))
			totalNegatives += int(error.get('total negatives'))

			corrects += error.get('correct')

			truePositives += error.get('true positives')
			trueNegatives += error.get('true negatives')
			falsePositives += error.get('false positives')
			falseNegatives += error.get('false negatives')

		correctsRatio = float(corrects)/totals
		try:
			positivePrecision = float(truePositives) / (truePositives + falsePositives)
		except:
			positivePrecision = -1
		try:
			positiveRecall = float(truePositives) / totalPositives
		except:
			positiveRecall = -1
		try:
			negativePrecision = float(trueNegatives) / (trueNegatives + falseNegatives)
		except:
			negativePrecision = -1
		try:
			negativeRecall = float(trueNegatives) / totalNegatives
		except:
			negativeRecall = -1

		return truePositives, falsePositives, trueNegatives, falseNegatives