<commit_before>/*
 * GeoSpatialDistribution.java
 *
 * Copyright (c) 2002-2012 <NAME>, <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.geo;

import dr.inference.distribution.MultivariateDistributionLikelihood;
import dr.inference.model.Parameter;
import dr.math.distributions.MultivariateDistribution;
import dr.xml.*;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */

public class GeoSpatialDistribution implements MultivariateDistribution {

    public static final String FLAT_SPATIAL_DISTRIBUTION = "flatGeoSpatialPrior";
    public static final String DATA = "data";
    public static final String TYPE = "geoSpatial";
    public static final String NODE_LABEL = "taxon";
    public static final String KML_FILE = "kmlFileName";
    public static final String INSIDE = "inside";
    public static final String UNION = "union";
    private static final String DEFAULT_LABEL = "";

    public static final int dimPoint = 2; // Assumes 2D points only

    public GeoSpatialDistribution(String label) {
        this.label = label;
    }

    public GeoSpatialDistribution(String label, Polygon2D region, boolean inside) {
        this.label = label;
        this.region = region;
        this.outside = !inside;
    }

    public double logPdf(double[] x) {
        final boolean contains = region.containsPoint2D(new Point2D.Double(x[0], x[1]));
        if (outside ^ contains)
            return 0;
        return Double.NEGATIVE_INFINITY;

    }

    public double[][] getScaleMatrix() {
        return null;
    }

    public double[] getMean() {
        return null;
    }

    public String getType() {
        return TYPE;
    }

    public String getLabel() {
        return label;
    }

    public boolean getOutside() {
        return outside;
    }

    public Polygon2D getRegion() {
        return region;
    }

    protected Polygon2D region;
    protected String label = null;
    private boolean outside = false;

    public static XMLObjectParser FLAT_GEOSPATIAL_PRIOR_PARSER = new AbstractXMLObjectParser() {

        public String getParserName() {
            return FLAT_SPATIAL_DISTRIBUTION;
        }

        public Object parseXMLObject(XMLObject xo) throws XMLParseException {

            String label = xo.getAttribute(NODE_LABEL, DEFAULT_LABEL);

            boolean inside = xo.getAttribute(INSIDE, true);
            boolean union = xo.getAttribute(UNION, false);
            boolean readFromFile = false;

            List<GeoSpatialDistribution> geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();

            if (xo.hasAttribute(KML_FILE)) {
                // read file
                String kmlFileName = xo.getStringAttribute(KML_FILE);
                List<Polygon2D> polygons = Polygon2D.readKMLFile(kmlFileName);
                for (Polygon2D region : polygons)
                    geoSpatialDistributions.add(new GeoSpatialDistribution(label, region, inside));
                readFromFile = true;
            } else {

                for (int i = 0; i < xo.getChildCount(); i++) {
                    if (xo.getChild(i) instanceof Polygon2D) {
                        Polygon2D region = (Polygon2D) xo.getChild(i);
                        geoSpatialDistributions.add(
                                new GeoSpatialDistribution(label, region, inside)
                        );
                    }
                }
            }

            List<Parameter> parameters = new ArrayList<Parameter>();
            XMLObject cxo = xo.getChild(DATA);
            for (int j = 0; j < cxo.getChildCount(); j++) {
                Parameter spatialParameter = (Parameter) cxo.getChild(j);
                parameters.add(spatialParameter);
            }

            if (geoSpatialDistributions.size() == 1) {
                MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(geoSpatialDistributions.get(0));
                for (Parameter spatialParameter : parameters) {
                    if (spatialParameter.getDimension() != dimPoint)
                        throw new XMLParseException("Spatial priors currently only work in " + dimPoint + "D");
                    likelihood.addData(spatialParameter);
                }
                return likelihood;
            }

            if (geoSpatialDistributions.size() == 0) {
                throw new XMLParseException("Error constructing geo spatial distributions in " + xo.getId());
            }

            if (parameters.size() == 1) {
                Parameter parameter = parameters.get(0);
                if (parameter.getDimension() % dimPoint != 0)
                    throw new XMLParseException("Spatial priors currently only work in " + dimPoint + "D");

                if (!label.equals(DEFAULT_LABEL)) {  // For a tip-taxon
                    Logger.getLogger("dr.geo").info(
                            "\nConstructing a multiple-region spatial prior:\n" +
                                    "\tTaxon: " + label + "\n" +
                                    "\tNumber of regions: " + geoSpatialDistributions.size() + "\n\n");
                    MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(
                            new MultiRegionGeoSpatialDistribution(label, geoSpatialDistributions, union));<commit_msg>Fixing bug in GeoSpatialDistribution parser<commit_after>/*
 * GeoSpatialDistribution.java
 *
 * Copyright (c) 2002-2012 <NAME>, <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.geo;

import dr.inference.distribution.MultivariateDistributionLikelihood;
import dr.inference.model.Parameter;
import dr.math.distributions.MultivariateDistribution;
import dr.xml.*;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */

public class GeoSpatialDistribution implements MultivariateDistribution {

    public static final String FLAT_SPATIAL_DISTRIBUTION = "flatGeoSpatialPrior";
    public static final String DATA = "data";
    public static final String TYPE = "geoSpatial";
    public static final String NODE_LABEL = "taxon";
    public static final String KML_FILE = "kmlFileName";
    public static final String INSIDE = "inside";
    public static final String UNION = "union";
    private static final String DEFAULT_LABEL = "";

    public static final int dimPoint = 2; // Assumes 2D points only

    public GeoSpatialDistribution(String label) {
        this.label = label;
    }

    public GeoSpatialDistribution(String label, Polygon2D region, boolean inside) {
        this.label = label;
        this.region = region;
        this.outside = !inside;
    }

    public double logPdf(double[] x) {
        final boolean contains = region.containsPoint2D(new Point2D.Double(x[0], x[1]));
        if (outside ^ contains)
            return 0;
        return Double.NEGATIVE_INFINITY;

    }

    public double[][] getScaleMatrix() {
        return null;
    }

    public double[] getMean() {
        return null;
    }

    public String getType() {
        return TYPE;
    }

    public String getLabel() {
        return label;
    }

    public boolean getOutside() {
        return outside;
    }

    public Polygon2D getRegion() {
        return region;
    }

    protected Polygon2D region;
    protected String label = null;
    private boolean outside = false;

    public static XMLObjectParser FLAT_GEOSPATIAL_PRIOR_PARSER = new AbstractXMLObjectParser() {

        public String getParserName() {
            return FLAT_SPATIAL_DISTRIBUTION;
        }

        public Object parseXMLObject(XMLObject xo) throws XMLParseException {

            String label = xo.getAttribute(NODE_LABEL, DEFAULT_LABEL);

            boolean inside = xo.getAttribute(INSIDE, true);
            boolean union = xo.getAttribute(UNION, false);
            boolean readFromFile = false;

            List<GeoSpatialDistribution> geoSpatialDistributions = new ArrayList<GeoSpatialDistribution>();

            if (xo.hasAttribute(KML_FILE)) {
                // read file
                String kmlFileName = xo.getStringAttribute(KML_FILE);
                List<Polygon2D> polygons = Polygon2D.readKMLFile(kmlFileName);
                for (Polygon2D region : polygons)
                    geoSpatialDistributions.add(new GeoSpatialDistribution(label, region, inside));
                readFromFile = true;
            } else {

                for (int i = 0; i < xo.getChildCount(); i++) {
                    if (xo.getChild(i) instanceof Polygon2D) {
                        Polygon2D region = (Polygon2D) xo.getChild(i);
                        geoSpatialDistributions.add(
                                new GeoSpatialDistribution(label, region, inside)
                        );
                    }
                }
            }

            List<Parameter> parameters = new ArrayList<Parameter>();
            XMLObject cxo = xo.getChild(DATA);
            for (int j = 0; j < cxo.getChildCount(); j++) {
                Parameter spatialParameter = (Parameter) cxo.getChild(j);
                parameters.add(spatialParameter);
            }

            if (geoSpatialDistributions.size() == 1) {
                MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(geoSpatialDistributions.get(0));
                for (Parameter spatialParameter : parameters) {
                    if (spatialParameter.getDimension() % dimPoint != 0)
                        throw new XMLParseException("Spatial priors currently only work in " + dimPoint + "D");

                    if (!label.equals(DEFAULT_LABEL)) {  // For a tip-taxon
                        likelihood.addData(spatialParameter);
                        return likelihood;
                    } else {
                        Logger.getLogger("dr.geo").info(
                                "\nConstructing a GeoSpatialCollectionModel:\n" +
                                        "\tParameter: " + spatialParameter.getId() + "\n" +
                                        "\tNumber of regions: " + geoSpatialDistributions.size() + "\n\n");

                        return new GeoSpatialCollectionModel(xo.getId(), spatialParameter, geoSpatialDistributions, !union);
                    }
                }
            }

            if (geoSpatialDistributions.size() == 0) {
                throw new XMLParseException("Error constructing geo spatial distributions in " + xo.getId());
            }

            if (parameters.size() == 1) {
                Parameter parameter = parameters.get(0);
                if (parameter.getDimension() % dimPoint != 0)
                    throw new XMLParseException("Spatial priors currently only work in " + dimPoint + "D");

                if (!label.equals(DEFAULT_LABEL)) {  // For a tip-taxon
                    Logger.getLogger("dr.geo").info(
                            "\nConstructing a multiple-region spatial prior:\n" +
                                    "\tTaxon: " + label + "\n" +
                                    "\tNumber of regions: " + geoSpatialDistributions.size() + "\n\n");
                    MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(
                            new MultiRegionGeoSpatialDistribution(label, geoSpatialDistributions, union));
{'fraction_non_alphanumeric': 0.08520888219796763, 'fraction_numerical': 0.0068498306360557015, 'mean_word_length': 2.1769488283118124, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3384692', 'n_tokens_mistral': 3647, 'n_tokens_neox': 3368, 'n_words': 1004}