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

com.arosbio.data.NamedLabels Maven / Gradle / Ivy

Go to download

Conformal AI package, including all data IO, transformations, machine learning models and predictor classes. Without inclusion of chemistry-dependent code.

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright (C) Aros Bio AB.
 *
 * CPSign is an Open Source Software that is dual licensed to allow you to choose a license that best suits your requirements:
 *
 * 1) GPLv3 (GNU General Public License Version 3) with Additional Terms, including an attribution clause as well as a limitation to use the software for commercial purposes.
 *
 * 2) CPSign Proprietary License that allows you to use CPSign for commercial activities, such as in a revenue-generating operation or environment, or integrate CPSign in your proprietary software without worrying about disclosing the source code of your proprietary software, which is required if you choose to use the software under GPLv3 license. See arosbio.com/cpsign/commercial-license for details.
 */
package com.arosbio.data;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class NamedLabels {
	
	private Map mapping = new HashMap<>();
	private Map reverseMapping = new HashMap<>();
	
	public NamedLabels() {
	}
	
	public NamedLabels(String... sequentialLabels) {
		this(Arrays.asList(sequentialLabels));
	}
	
	public NamedLabels(List sequentialLabels){
		for(int i=0; i labels){
		mapping = new HashMap<>(labels);
		updateReverseMapping();
	}
	
	public static NamedLabels fromReversedLabels(Map reverseLabels) {
		Map correctLabels = new HashMap<>();
		for (Map.Entry entry: reverseLabels.entrySet()) {
			correctLabels.put(entry.getValue(), entry.getKey());
		}
		return new NamedLabels(correctLabels);
	}
	
	public NamedLabels(NamedLabels labels) {
		mapping = new HashMap<>(labels.mapping);
		reverseMapping = new HashMap<>(labels.reverseMapping);
	}
	
	public boolean isEmpty() {
		return mapping.isEmpty();
	}
	
	public NamedLabels clone() {
		return new NamedLabels(mapping);
	}
	
	public void addLabel(int numericValue, String label){
		mapping.put(numericValue, label);
		reverseMapping.put(label, numericValue);
	}
	
	public void setLabels(Map labels){
		if (labels == null)
			mapping.clear();
		else {
			mapping = labels;
			updateReverseMapping();
		}
	}
	
	private void updateReverseMapping(){
		reverseMapping.clear();
		for(Map.Entry label: mapping.entrySet())
			reverseMapping.put(label.getValue(), label.getKey());
	}
	
	public void updateLabels(Map newLabels) throws IllegalArgumentException {
		if (newLabels == null || newLabels.isEmpty())
			throw new IllegalArgumentException("New labels cannot be null or empty");
		for (Map.Entry label: mapping.entrySet()){
			if (newLabels.containsKey(label.getValue()))
				label.setValue(newLabels.get(label.getValue()));
		}
		updateReverseMapping();
	}
	
	/**
	 * Returns a copy - setting new values must be done through this class
	 * @return the mapping between numerical values to textual labels
	 */
	public Map getLabels(){
		return new HashMap<>(mapping);
	}
	
	public Map getReverseLabels(){
		return new HashMap<>(reverseMapping);
	}
	
	/**
	 * Returns a copy of the labels
	 * @return The textual labels used
	 */
	public Set getLabelsSet(){
		return new HashSet<>(mapping.values());
	}
	
	public int getNumLabels(){
		return mapping.size();
	}
	
	public boolean contain(String label){
		return reverseMapping.containsKey(label);
	}
	
	public Integer getValue(String label) throws IllegalArgumentException{
		Integer val = reverseMapping.get(label);
		if (val == null)
			throw new IllegalArgumentException("Label not found: " + label);
		return val;
	}
	
	public String getLabel(int numericValue) throws IllegalArgumentException{
		String label = mapping.get(numericValue);
		if (label == null)
			throw new IllegalArgumentException("Label not found for numeric value: " + numericValue);
		return label;
	}
	
	public String toString() {
		return mapping.toString();
	}
	
	public int size() {
		return (mapping!=null? mapping.size() : 0);
	}
	
	public Map convert(Map input){
		Map res = new HashMap<>();
		for (Map.Entry entry: input.entrySet()) {
			res.put(mapping.get(entry.getKey()), entry.getValue());
		}
		return res;
	}
	
	public Map reverse(Map textual){
		Map res = new HashMap<>();
		for (Map.Entry entry: textual.entrySet()) {
			res.put(reverseMapping.get(entry.getKey()), entry.getValue());
		}
		return res;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy