Java Code Examples for gnu.trove.list.array.TDoubleArrayList#add()

The following examples show how to use gnu.trove.list.array.TDoubleArrayList#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeSeries.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
void parseToken(int i, String token) {
    if (dataTypes[i - 2] == null) {
        // test double parsing, in case of error we consider it a string time series
        if (Doubles.tryParse(token) != null) {
            dataTypes[i - 2] = TimeSeriesDataType.DOUBLE;
            TDoubleArrayList doubleValues = createDoubleValues();
            doubleValues.add(parseDouble(token));
            values[i - 2] = doubleValues;
        } else {
            dataTypes[i - 2] = TimeSeriesDataType.STRING;
            List<String> stringValues = createStringValues();
            stringValues.add(checkString(token));
            values[i - 2] = stringValues;
        }
    } else {
        if (dataTypes[i - 2] == TimeSeriesDataType.DOUBLE) {
            ((TDoubleArrayList) values[i - 2]).add(parseDouble(token));
        } else if (dataTypes[i - 2] == TimeSeriesDataType.STRING) {
            ((List<String>) values[i - 2]).add(checkString(token));
        } else {
            throw assertDataType(dataTypes[i - 2]);
        }
    }
}
 
Example 2
Source File: ConfiguredBusImpl.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
ConfiguredBusImpl(String id, String name, boolean fictitious, VoltageLevelExt voltageLevel) {
    super(id, name, fictitious, voltageLevel);
    network = voltageLevel.getNetwork().getRef();
    int variantArraySize = network.get().getVariantManager().getVariantArraySize();
    terminals = new ArrayList<>(variantArraySize);
    v = new TDoubleArrayList(variantArraySize);
    angle = new TDoubleArrayList(variantArraySize);
    connectedComponentNumber = new TIntArrayList(variantArraySize);
    synchronousComponentNumber = new TIntArrayList(variantArraySize);
    for (int i = 0; i < variantArraySize; i++) {
        terminals.add(new ArrayList<>());
        v.add(Double.NaN);
        angle.add(Double.NaN);
        connectedComponentNumber.add(-1);
        synchronousComponentNumber.add(-1);
    }
}
 
Example 3
Source File: SpatialPooler.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Update the inhibition radius. The inhibition radius is a measure of the
 * square (or hypersquare) of columns that each a column is "connected to"
 * on average. Since columns are are not connected to each other directly, we
 * determine this quantity by first figuring out how many *inputs* a column is
 * connected to, and then multiplying it by the total number of columns that
 * exist for each input. For multiple dimension the aforementioned
 * calculations are averaged over all dimensions of inputs and columns. This
 * value is meaningless if global inhibition is enabled.
 * 
 * @param c     the {@link Connections} (spatial pooler memory)
 */
public void updateInhibitionRadius(Connections c) {
    if(c.getGlobalInhibition()) {
        c.setInhibitionRadius(ArrayUtils.max(c.getColumnDimensions()));
        return;
    }

    TDoubleArrayList avgCollected = new TDoubleArrayList();
    int len = c.getNumColumns();
    for(int i = 0;i < len;i++) {
        avgCollected.add(avgConnectedSpanForColumnND(c, i));
    }
    double avgConnectedSpan = ArrayUtils.average(avgCollected.toArray());
    double diameter = avgConnectedSpan * avgColumnsPerInput(c);
    double radius = (diameter - 1) / 2.0d;
    radius = Math.max(1, radius);
    c.setInhibitionRadius((int)(radius + 0.5));
}
 
Example 4
Source File: FloodFillTransformedCylinder3D.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private void addIfInside(
		final TLongArrayList labelCoordinates,
		final TDoubleArrayList worldCoordinates,
		final long lx,
		final long ly,
		final long lz,
		final double wx,
		final double wy,
		final double wz,
		final double cx,
		final double cy,
		final double zMinInclusive,
		final double zMaxInclusive)
{
	if (wz >= zMinInclusive && wz <= zMaxInclusive)
	{
		final double dx = wx - cx;
		final double dy = wy - cy;
		if (dx * dx * radiusYSquared + dy * dy * radiusXSquared <= radiusXSquaredRadiusYSquared)
		{
			labelCoordinates.add(lx);
			labelCoordinates.add(ly);
			labelCoordinates.add(lz);

			worldCoordinates.add(wx);
			worldCoordinates.add(wy);
			worldCoordinates.add(wz);
		}
	}
}
 
Example 5
Source File: FloodFillTransformedPlane.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
private void addIfInside(
		final boolean isRelevant,
		final long fillLabel,
		final long pixelLabel,
		final TLongArrayList labelCoordinates,
		final TDoubleArrayList worldCoordinates,
		final long lx,
		final long ly,
		final long lz,
		final double wx,
		final double wy,
		final double wz,
		final double zMinInclusive,
		final double zMaxInclusive)
{
	if (isRelevant && fillLabel != pixelLabel && wz >= zMinInclusive && wz <= zMaxInclusive)
	{
		if (wx > minX && wx < maxX && wy > minY && wy < maxY)
		{
			labelCoordinates.add(lx);
			labelCoordinates.add(ly);
			labelCoordinates.add(lz);

			worldCoordinates.add(wx);
			worldCoordinates.add(wy);
			worldCoordinates.add(wz);
		}
	}
}
 
Example 6
Source File: AbstractTerminal.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
AbstractTerminal(Ref<? extends VariantManagerHolder> network) {
    this.network = network;
    int variantArraySize = network.get().getVariantManager().getVariantArraySize();
    p = new TDoubleArrayList(variantArraySize);
    q = new TDoubleArrayList(variantArraySize);
    for (int i = 0; i < variantArraySize; i++) {
        p.add(Double.NaN);
        q.add(Double.NaN);
    }
}
 
Example 7
Source File: NodeTerminal.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
NodeTerminal(Ref<? extends VariantManagerHolder> network, int node) {
    super(network);
    this.node = node;
    int variantArraySize = network.get().getVariantManager().getVariantArraySize();
    v = new TDoubleArrayList(variantArraySize);
    angle = new TDoubleArrayList(variantArraySize);
    connectedComponentNumber = new TIntArrayList(variantArraySize);
    synchronousComponentNumber = new TIntArrayList(variantArraySize);
    for (int i = 0; i < variantArraySize; i++) {
        v.add(Double.NaN);
        angle.add(Double.NaN);
        connectedComponentNumber.add(0);
        synchronousComponentNumber.add(0);
    }
}
 
Example 8
Source File: ArrayUtils.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns an array of values that test true for all of the
 * specified {@link Condition}s.
 *
 * @param values
 * @param conditions
 * @return
 */
public static double[] retainLogicalAnd(double[] values, Condition<?>[] conditions) {
    TDoubleArrayList l = new TDoubleArrayList();
    for (int i = 0; i < values.length; i++) {
        boolean result = true;
        for (int j = 0; j < conditions.length && result; j++) {
            result &= conditions[j].eval(values[i]);
        }
        if (result) l.add(values[i]);
    }
    return l.toArray();
}
 
Example 9
Source File: PassThroughEncoder.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
/**
 * Does a bitwise compare of the two bitmaps and returns a fractional
 * value between 0 and 1 of how similar they are.
 * 1 => identical
 * 0 => no overlapping bits
 * IGNORES difference in length (only compares bits of shorter list)  e..g 11 and 1100101010 are "identical"
 * @see org.numenta.nupic.encoders.Encoder#closenessScores(gnu.trove.list.TDoubleList, gnu.trove.list.TDoubleList, boolean)
 */
public gnu.trove.list.TDoubleList closenessScores(gnu.trove.list.TDoubleList expValues, gnu.trove.list.TDoubleList actValues, boolean fractional) {
    TDoubleArrayList result = new TDoubleArrayList();

    double ratio = 1.0d;
    double expectedSum = expValues.sum();
    double actualSum = actValues.sum();

    if (actualSum > expectedSum) {
        double diff = actualSum - expectedSum;
        if (diff < expectedSum)
            ratio = 1 - diff / expectedSum;
        else
            ratio = 1 / diff;
    }

    int[] expectedInts = ArrayUtils.toIntArray(expValues.toArray());
    int[] actualInts = ArrayUtils.toIntArray(actValues.toArray());

    int[] overlap = ArrayUtils.and(expectedInts, actualInts);

    int overlapSum = ArrayUtils.sum(overlap);
    double r = 0.0;
    if (expectedSum != 0)
        r = overlapSum / expectedSum;
    r = r * ratio;

    if(LOGGER.isTraceEnabled()) {
        LOGGER.trace("closenessScores for expValues: {} and actValues: {} is: {}", Arrays.toString(expectedInts), actualInts, r);
    }

    result.add(r);
    return result;
}
 
Example 10
Source File: InteractionAnalysisDetermineDirection.java    From systemsgenetics with GNU General Public License v3.0 4 votes vote down vote up
public EffectDiffResult calculateEffectDifference(String snpId, String geneName, String covariateName, Allele assessedAllele, double fractionOfSamplesPerGroup) {

		if (!variantIdMap.containsKey(snpId)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (!expressionData.containsRow(geneName)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (!covariatesData.containsRow(covariateName)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (fractionOfSamplesPerGroup <= 0 || fractionOfSamplesPerGroup >= 1) {
			throw new RuntimeException("Fraction must be between 0 and 1");
		}

		GeneticVariant variant = variantIdMap.get(snpId);
		Alleles variantAlleles = variant.getVariantAlleles();

		if (!variantAlleles.contains(assessedAllele)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (variantAlleles.getAlleleCount() != 2) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		float[] dosagesAll = variant.getSampleDosages();
		String[] genotypedSamples = genotypeData.getSampleNames();

		LinkedHashSet<String> includedGenotypedSamples = new LinkedHashSet<>();
		TDoubleArrayList dosages = new TDoubleArrayList(dosagesAll.length);

		for (int i = 0; i < dosagesAll.length; ++i) {
			if (dosagesAll[i] >= 0 && gte.containsKey(genotypedSamples[i])) {
				includedGenotypedSamples.add(genotypedSamples[i]);
				dosages.add(dosagesAll[i]);
			}
		}

		System.out.println("Included samples: " + includedGenotypedSamples.size());

		double[] expressionLevels = new double[includedGenotypedSamples.size()];
		double[] covariateLevels = new double[includedGenotypedSamples.size()];

		int s = 0;
		for (String genotypeSample : includedGenotypedSamples) {
			expressionLevels[s] = expressionData.getElement(geneName, gte.get(genotypeSample));
			covariateLevels[s] = covariatesData.getElement(covariateName, gte.get(genotypeSample));
			++s;
		}

		if (assessedAllele != variantAlleles.get(0)) {
			for (int i = 0; i < dosages.size(); ++i) {
				dosages.setQuick(i, dosages.getQuick(i) * -1);
			}
		}

		double[] covariateRanks = COV_RANKER.rank(covariateLevels);

		int samplesPerGroup = (int) Math.floor(covariateRanks.length * fractionOfSamplesPerGroup);

		System.out.println("Samples per group: " + samplesPerGroup);

		double[] dosagesLow = new double[samplesPerGroup];
		double[] expressionLow = new double[samplesPerGroup];

		double[] dosagesHigh = new double[samplesPerGroup];
		double[] expressionHigh = new double[samplesPerGroup];

		for (int i = 0; i < samplesPerGroup; ++i) {
			dosagesLow[i] = dosages.get((int) covariateRanks[i]);
			expressionLow[i] = expressionLevels[(int) covariateRanks[i]];
			dosagesHigh[i] = dosages.get((int) covariateRanks[covariateRanks.length - 1 - i]);
			expressionHigh[i] = expressionLevels[(int) covariateRanks[covariateRanks.length - 1 - i]];
		}

		double rhoLow = spearmanCalculator.correlation(dosagesLow, expressionLow);
		double rhoHigh = spearmanCalculator.correlation(dosagesHigh, expressionHigh);

		System.out.println("rho low:" + rhoLow);
		System.out.println("rho high:" + rhoHigh);

		return new EffectDiffResult(rhoLow, rhoHigh);

	}