<commit_before>package org.genericsystem.cv.retriever;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.genericsystem.cv.Img;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.utils.Converters;

public class DescriptorManager {

	private Map<ImgDescriptor,Mat> descriptors = new HashMap<>();
	private ImgDescriptor reference; 
	private Map<ImgDescriptor,Double> distanceMap = new HashMap<>();
	private static final Mat IDENTITY_MAT = Mat.eye(new Size(3, 3), CvType.CV_64F);
	private Mat frame;

	public DescriptorManager(){

	}

	public void setFrame(Mat frame){
		this.frame = frame;
	}


	public Map<ImgDescriptor,Mat> getDescriptors() {
		return descriptors;
	}

	public void setReference(ImgDescriptor reference) {
		this.reference = reference;
	}


	public Map<ImgDescriptor,Double> getDistanceMap() {
		return distanceMap;
	}


	public Img add(ImgDescriptor deperspectivedImgDescriptor, Mat deperspectiveHomography) {
		if (descriptors.isEmpty()) {
			descriptors.put(deperspectivedImgDescriptor, IDENTITY_MAT);
			setReference(deperspectivedImgDescriptor);
			Mat stabilizationHomographyFromFrame = new Mat();
			Core.gemm(IDENTITY_MAT, deperspectiveHomography, 1, new Mat(), 0, stabilizationHomographyFromFrame);
			return CamLiveRetriever.warpPerspective(frame, stabilizationHomographyFromFrame);
		}
		List<Mat> possibleHomographies = new ArrayList<>();
		for (ImgDescriptor descriptor : descriptors.keySet()) {
			Mat joinHomography = deperspectivedImgDescriptor.computeStabilizationGraphy(descriptor);
			if (joinHomography != null) 				
				possibleHomographies.add(computeHomographyToRef(descriptor, joinHomography));
		}
		Mat pertinentHomography = possibleHomographies.size()==1?possibleHomographies.get(0):getPertinentDescriptor(possibleHomographies);
		if(pertinentHomography!=null)
			descriptors.put(deperspectivedImgDescriptor, pertinentHomography);
		else
			return null;
		updateReferenceDeperspectived(descriptors);		
		Mat stabilizationHomographyFromFrame = new Mat();
		Core.gemm(descriptors.get(deperspectivedImgDescriptor), deperspectiveHomography, 1, new Mat(), 0, stabilizationHomographyFromFrame);
		return CamLiveRetriever.warpPerspective(frame, stabilizationHomographyFromFrame);
	}

	private Mat getPertinentDescriptor(List<Mat> possibleHomographies) {
		return possibleHomographies.isEmpty()?null:getBestHomography(possibleHomographies);

	}

	private Mat getBestHomography(List<Mat> possibleHomographies){
		Map<Mat, Double> distancesByHomography = new HashMap<>();
		double totalDistance = 0.0;
		for(Mat h : possibleHomographies){
			double distance = computeDistanceBetweenDeperspectived(h);
			distancesByHomography.put(h, distance);
			totalDistance+=distance;
		}
		double meanDistance = totalDistance / distancesByHomography.size();
		double squareStandardDeviation = 0.0;
		for(Mat h : distancesByHomography.keySet()){
			double diff = meanDistance - distancesByHomography.get(h);
			squareStandardDeviation+=diff*diff;
			distancesByHomography.put(h, diff);
		}
		System.out.println("Standard deviation: "+Math.sqrt(squareStandardDeviation / distancesByHomography.size()));
		return (Math.sqrt(squareStandardDeviation / distancesByHomography.size())) > 10 ? null : Collections.min(distancesByHomography.entrySet(), Comparator.comparingDouble(Entry::getValue)).getKey();		
	}

	private Mat computeHomographyToRef(ImgDescriptor descriptor, Mat joinHomography) {<commit_msg>refactoring of a method name<commit_after>package org.genericsystem.cv.retriever;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.genericsystem.cv.Img;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.utils.Converters;

public class DescriptorManager {

	private Map<ImgDescriptor,Mat> descriptors = new HashMap<>();
	private ImgDescriptor reference; 
	private Map<ImgDescriptor,Double> distanceMap = new HashMap<>();
	private static final Mat IDENTITY_MAT = Mat.eye(new Size(3, 3), CvType.CV_64F);
	private Mat frame;

	public DescriptorManager(){

	}

	public void setFrame(Mat frame){
		this.frame = frame;
	}


	public Map<ImgDescriptor,Mat> getDescriptors() {
		return descriptors;
	}

	public void setReference(ImgDescriptor reference) {
		this.reference = reference;
	}


	public Map<ImgDescriptor,Double> getDistanceMap() {
		return distanceMap;
	}


	public Img add(ImgDescriptor deperspectivedImgDescriptor, Mat deperspectiveHomography) {
		if (descriptors.isEmpty()) {
			descriptors.put(deperspectivedImgDescriptor, IDENTITY_MAT);
			setReference(deperspectivedImgDescriptor);
			Mat stabilizationHomographyFromFrame = new Mat();
			Core.gemm(IDENTITY_MAT, deperspectiveHomography, 1, new Mat(), 0, stabilizationHomographyFromFrame);
			return CamLiveRetriever.warpPerspective(frame, stabilizationHomographyFromFrame);
		}
		List<Mat> possibleHomographies = new ArrayList<>();
		for (ImgDescriptor descriptor : descriptors.keySet()) {
			Mat joinHomography = deperspectivedImgDescriptor.computeStabilizationGraphy(descriptor);
			if (joinHomography != null) 				
				possibleHomographies.add(computeHomographyToRef(descriptor, joinHomography));
		}
		Mat pertinentHomography = possibleHomographies.size()==1?possibleHomographies.get(0):getPertinentHomography(possibleHomographies);
		if(pertinentHomography!=null)
			descriptors.put(deperspectivedImgDescriptor, pertinentHomography);
		else
			return null;
		updateReferenceDeperspectived(descriptors);		
		Mat stabilizationHomographyFromFrame = new Mat();
		Core.gemm(descriptors.get(deperspectivedImgDescriptor), deperspectiveHomography, 1, new Mat(), 0, stabilizationHomographyFromFrame);
		return CamLiveRetriever.warpPerspective(frame, stabilizationHomographyFromFrame);
	}

	private Mat getPertinentHomography(List<Mat> possibleHomographies) {
		return possibleHomographies.isEmpty()?null:getBestHomography(possibleHomographies);

	}

	private Mat getBestHomography(List<Mat> possibleHomographies){
		Map<Mat, Double> distancesByHomography = new HashMap<>();
		double totalDistance = 0.0;
		for(Mat h : possibleHomographies){
			double distance = computeDistanceBetweenDeperspectived(h);
			distancesByHomography.put(h, distance);
			totalDistance+=distance;
		}
		double meanDistance = totalDistance / distancesByHomography.size();
		double squareStandardDeviation = 0.0;
		for(Mat h : distancesByHomography.keySet()){
			double diff = meanDistance - distancesByHomography.get(h);
			squareStandardDeviation+=diff*diff;
			distancesByHomography.put(h, diff);
		}
		System.out.println("Standard deviation: "+Math.sqrt(squareStandardDeviation / distancesByHomography.size()));
		return (Math.sqrt(squareStandardDeviation / distancesByHomography.size())) > 10 ? null : Collections.min(distancesByHomography.entrySet(), Comparator.comparingDouble(Entry::getValue)).getKey();		
	}

	private Mat computeHomographyToRef(ImgDescriptor descriptor, Mat joinHomography) {
{'fraction_non_alphanumeric': 0.10148217385498731, 'fraction_numerical': 0.004272933635999466, 'mean_word_length': 6.527638190954774, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5397907', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2190, 'n_words': 428}