All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.citec.tcs.alignment.KernelDTWFullAlgorithm Maven / Gradle / Ivy

/* 
 * TCS Alignment Toolbox
 * 
 * Copyright (C) 2013-2015
 * Benjamin Paaßen, Georg Zentgraf
 * AG Theoretical Computer Science
 * Centre of Excellence Cognitive Interaction Technology (CITEC)
 * University of Bielefeld
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package de.citec.tcs.alignment;

import de.citec.tcs.alignment.comparators.Comparator;
import de.citec.tcs.alignment.sequence.Node;
import de.citec.tcs.alignment.sequence.Sequence;

/**
 * Implements soft alignment DTW. The result is the soft alignment score
 * between 2 sequences as well as the derivative of the score-function
 * regarding each individual parameter.
 *
 * @author
 */
public class KernelDTWFullAlgorithm implements AlignmentAlgorithm {

	private final AlignmentSpecification alignmentSpecification;

	public KernelDTWFullAlgorithm(AlignmentSpecification alignmentSpecification) {
		this.alignmentSpecification = alignmentSpecification;
	}

	public AlignmentSpecification getSpecification() {
		return alignmentSpecification;
	}

	/**
	 * {@inheritDoc }
	 */
	@Override
	public SoftDTWModel calculateAlignment(final Sequence a, final Sequence b) {

		//check validity
		if (a.getNodeSpecification() != alignmentSpecification.getNodeSpecification()
			&& !a.getNodeSpecification().equals(alignmentSpecification.getNodeSpecification())) {
			throw new RuntimeException(
				"The first input sequence has an unexpected node specification!");
		}
		if (a.getNodeSpecification() != b.getNodeSpecification()
			&& !a.getNodeSpecification().equals(b.getNodeSpecification())) {
			throw new RuntimeException(
				"The node specifications of both input sequences to not match!");
		}

		// number of comparators
		int compCount = alignmentSpecification.size();

		// get comparators, weights and original indezes (used for accessing NodeSpec and Sequence)	
		final double[] weights = alignmentSpecification.getWeighting();
		final Comparator[] comparators = new Comparator[compCount];
		final int[] originalIndices = new int[compCount];
		for (int k = 0; k < compCount; k++) {
			comparators[k] = (Comparator) alignmentSpecification.getComparator(k);
			originalIndices[k] = alignmentSpecification.getOriginalIndex(k);
		}

		// get sequenze lengths
		final int n = a.getNodes().size();
		final int m = b.getNodes().size();
		Node aNode, bNode;

		// initialize the alignment matrix.
		// use indezes 1-N, 1-M and initialize 0.th row and 0th. column with [1,0,0,0..] for 
		// simplified dynamic programming scheme 
		final double[][] alignMat = new double[n + 1][m + 1];

		// initialize first entry.
		alignMat[0][0] = 1;

		// initialize first row and column
		for (int j = 1; j <= m; j++) {
			alignMat[0][j] = 0;
		}
		for (int i = 1; i <= n; i++) {
			alignMat[i][0] = 0;
		}

		// calculate alignment matrix via dynamic programming
		double weightedCompSum;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				// get current nodes
				aNode = a.getNodes().get(i - 1);
				bNode = b.getNodes().get(j - 1);
				// calculate weighted comparator sum
				weightedCompSum = SoftDTWModel.calc_weighted_comp_sum(comparators, weights, originalIndices, aNode, bNode);
				// calculate matrix update
				alignMat[i][j] = weightedCompSum / (1 - weightedCompSum) * (alignMat[i - 1][j - 1] + alignMat[i - 1][j] + alignMat[i][j - 1]);
			}
		}
		// create SoftDTWModel from alignMat
		final SoftDTWModel result = new SoftDTWModel(alignmentSpecification,
			a, b, alignMat);

		return result;
	}

	/**
	 * {@inheritDoc }
	 */
	public Class getResultClass() {
		return SoftDTWModel.class;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy