Exemplo n.º 1
0
  public static final void main(String args[]) throws IOException {
    String outdir = "/home/johannes/gsv/miv-matrix/qs2013/";
    String matrixFile = "/home/johannes/gsv/miv-matrix/qs2013/matrix.txt";
    String zoneFile = "/home/johannes/gsv/gis/zones/geojson/nuts3.psm.gk3.geojson";
    String inhabFile = "/home/johannes/gsv/miv-matrix/qs2013/inhabitants.csv";

    final NumericMatrix carVol = new NumericMatrix();
    final NumericMatrix railVol = new NumericMatrix();
    final NumericMatrix airVol = new NumericMatrix();

    logger.info("Loading zones...");
    ZoneCollection zones = ZoneGeoJsonIO.readFromGeoJSON(zoneFile, "NO");
    TObjectDoubleHashMap<String> zoneRho = calcDensity(inhabFile, zones);

    logger.info("Reading matrix...");
    MatrixReader mReader = new MatrixReader();
    mReader.read(
        matrixFile,
        new MatrixReader.RowHandler() {
          @Override
          public void handleRow(
              String from,
              String to,
              String purpose,
              String year,
              String mode,
              String direction,
              String day,
              String season,
              double volume) {
            if (mode.equalsIgnoreCase("M")) {
              carVol.add(from, to, volume);
            } else if (mode.equalsIgnoreCase("B")) {
              railVol.add(from, to, volume);
            } else if (mode.equalsIgnoreCase("F")) {
              airVol.add(from, to, volume);
            }
          }
        });

    logger.info("Calculating shares per od-pair...");
    NumericMatrix carShare = new NumericMatrix();
    NumericMatrix railShare = new NumericMatrix();
    NumericMatrix airShare = new NumericMatrix();
    NumericMatrix odCounts = new NumericMatrix();

    Discretizer discr = FixedSampleSizeDiscretizer.create(zoneRho.values(), 1, 20);
    //        Discretizer discr = new DummyDiscretizer();

    Set<String> keys = carVol.keys();
    keys.addAll(railVol.keys());
    keys.addAll(airVol.keys());

    ProgressLogger.init(keys.size(), 2, 10);

    for (String from : keys) {
      for (String to : keys) {
        Double car = carVol.get(from, to);
        if (car == null) car = 0.0;
        Double rail = railVol.get(from, to);
        if (rail == null) rail = 0.0;
        Double air = airVol.get(from, to);
        if (air == null) air = 0.0;

        if (car > 0 && rail > 0 && air > 0) {
          double total = car + rail + air;

          double fromRho = zoneRho.get(from);
          double toRho = zoneRho.get(to);

          fromRho = discr.discretize(fromRho);
          toRho = discr.discretize(toRho);

          String fromRhoStr = String.valueOf(fromRho);
          String toRhoStr = String.valueOf(toRho);
          odCounts.add(fromRhoStr, toRhoStr, 1);
          //                    odCounts.add(fromRhoStr, toRhoStr, total);

          carShare.add(fromRhoStr, toRhoStr, car / total);
          railShare.add(fromRhoStr, toRhoStr, rail / total);
          airShare.add(fromRhoStr, toRhoStr, air / total);
        }
      }
      ProgressLogger.step();
    }
    ProgressLogger.terminate();

    logger.info("Calculating averages...");
    keys = odCounts.keys();
    for (String from : keys) {
      for (String to : keys) {
        Double count = odCounts.get(from, to);
        if (count != null) {
          carShare.multiply(from, to, 1 / count);
          railShare.multiply(from, to, 1 / count);
          airShare.multiply(from, to, 1 / count);
        }
      }
    }

    logger.info("Writing matrices...");
    NumericMatrixTxtIO.write(carShare, String.format("%s/carShare.txt", outdir));
    NumericMatrixTxtIO.write(railShare, String.format("%s/railShare.txt", outdir));
    NumericMatrixTxtIO.write(airShare, String.format("%s/airShare.txt", outdir));
    logger.info("Done.");
  }
Exemplo n.º 2
0
  private void loadSociogramData(Collection<VertexRecord> records, SQLDumpReader sqlData) {
    logger.info("Loading sociogram data...");
    Map<String, VertexRecord> map = sqlData.getFullAlterKeyMappping(records);

    TObjectIntHashMap<Vertex> rawDegrees = new TObjectIntHashMap<Vertex>();
    for (Vertex v : proj.getVertices()) {
      rawDegrees.put(v, v.getNeighbours().size());
    }

    int edgecnt = 0;
    int doublecnt = 0;
    int egoEdge = 0;

    Set<Vertex> notOkVertices = new HashSet<Vertex>();
    Set<Vertex> okVertices = new HashSet<Vertex>();
    DescriptiveStatistics notOkStats = new DescriptiveStatistics();
    DescriptiveStatistics okStats = new DescriptiveStatistics();

    DescriptiveStatistics numDistr = new DescriptiveStatistics();
    DescriptiveStatistics numDistrNoZero = new DescriptiveStatistics();
    DescriptiveStatistics sizeDistr = new DescriptiveStatistics();

    TDoubleArrayList sizeValues = new TDoubleArrayList();
    TDoubleArrayList kSizeValues = new TDoubleArrayList();
    TDoubleArrayList numValues = new TDoubleArrayList();
    TDoubleArrayList numValues2 = new TDoubleArrayList();
    TDoubleArrayList kNumValues = new TDoubleArrayList();

    for (VertexRecord record : records) {
      if (record.isEgo) {
        List<Set<String>> cliques = sqlData.getCliques(record);
        numDistr.addValue(cliques.size());

        Vertex v = idMap.get(record.id);
        numValues.add(cliques.size());
        kNumValues.add(v.getNeighbours().size());

        if (!cliques.isEmpty()) numDistrNoZero.addValue(cliques.size());

        for (Set<String> clique : cliques) {
          sizeDistr.addValue(clique.size());
          sizeValues.add(clique.size());
          kSizeValues.add(rawDegrees.get(projMap.get(v)));
          numValues2.add(cliques.size());
          List<SocialSparseVertex> vertices = new ArrayList<SocialSparseVertex>(clique.size());
          for (String alter : clique) {
            VertexRecord r = map.get(record.egoSQLId + alter);
            if (r != null) {
              SocialSparseVertex vertex = idMap.get(r.id);
              if (vertex != null) {
                vertices.add(vertex);
              } else {
                logger.warn("Vertex not found.");
              }
            } else {
              logger.warn("Record not found.");
            }
          }

          for (int i = 0; i < vertices.size(); i++) {
            for (int j = i + 1; j < vertices.size(); j++) {
              SampledVertexDecorator<SocialSparseVertex> vProj1 = projMap.get(vertices.get(i));
              SampledVertexDecorator<SocialSparseVertex> vProj2 = projMap.get(vertices.get(j));
              if (!vProj1.isSampled() && !vProj2.isSampled()) {

                if (Math.random() < 0.62) {
                  SocialSparseEdge socialEdge =
                      builder.addEdge(graph, vertices.get(i), vertices.get(j));
                  if (socialEdge != null) {
                    projBuilder.addEdge(proj, vProj1, vProj2, socialEdge);
                    edgecnt++;

                    if (vProj1.isSampled() || vProj2.isSampled()) {
                      egoEdge++;
                      if (vProj1.isSampled()) notOkVertices.add(vProj1);
                      else notOkVertices.add(vProj2);
                    }

                  } else {
                    doublecnt++;
                    if (vProj1.isSampled()) okVertices.add(vProj1);
                    else if (vProj2.isSampled()) okVertices.add(vProj2);
                  }
                }
              }
            }
          }
        }
      }
    }

    for (Vertex v : okVertices) okStats.addValue(rawDegrees.get(v));

    for (Vertex v : notOkVertices) notOkStats.addValue(rawDegrees.get(v));
    try {

      TDoubleDoubleHashMap hist =
          Histogram.createHistogram(okStats, new LinearDiscretizer(1), false);
      StatsWriter.writeHistogram(
          hist,
          "k",
          "n",
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/k_ok.txt");

      TDoubleDoubleHashMap hist2 =
          Histogram.createHistogram(notOkStats, new LinearDiscretizer(1), false);
      StatsWriter.writeHistogram(
          hist2,
          "k",
          "n",
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/k_notok.txt");

      TDoubleDoubleHashMap ratio = new TDoubleDoubleHashMap();
      double[] keys = hist.keys();
      for (double k : keys) {
        double val1 = hist2.get(k);
        double val2 = hist.get(k);

        ratio.put(k, val1 / (val2 + val1));
      }
      StatsWriter.writeHistogram(
          ratio,
          "k",
          "p",
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/k_ratio.txt");

      logger.info("Mean num of cliques: " + numDistrNoZero.getMean());
      logger.info("Mean size: " + sizeDistr.getMean());
      logger.info("Median num of cliques: " + StatUtils.percentile(numDistrNoZero.getValues(), 50));
      logger.info("Median size: " + StatUtils.percentile(sizeDistr.getValues(), 50));

      TDoubleDoubleHashMap histNum =
          Histogram.createHistogram(
              numDistrNoZero,
              FixedSampleSizeDiscretizer.create(numDistrNoZero.getValues(), 2, 20),
              true);
      Histogram.normalize(histNum);
      StatsWriter.writeHistogram(
          histNum,
          "num",
          "freq",
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/numCliques.txt");

      TDoubleDoubleHashMap histSize =
          Histogram.createHistogram(
              sizeDistr, FixedSampleSizeDiscretizer.create(sizeDistr.getValues(), 2, 20), true);
      Histogram.normalize(histSize);
      StatsWriter.writeHistogram(
          histSize,
          "size",
          "freq",
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/numPersons.txt");

      Discretizer discretizer =
          FixedSampleSizeDiscretizer.create(kSizeValues.toNativeArray(), 20, 20);
      TDoubleArrayList valuesX = new TDoubleArrayList();
      for (int i = 0; i < kSizeValues.size(); i++) {
        valuesX.add(discretizer.discretize(kSizeValues.get(i)));
      }

      Correlations.writeToFile(
          Correlations.mean(valuesX.toNativeArray(), sizeValues.toNativeArray()),
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/size_k.txt",
          "k",
          "size");

      discretizer = FixedSampleSizeDiscretizer.create(kNumValues.toNativeArray(), 20, 20);
      valuesX = new TDoubleArrayList();
      for (int i = 0; i < kNumValues.size(); i++) {
        valuesX.add(discretizer.discretize(kNumValues.get(i)));
      }

      Correlations.writeToFile(
          Correlations.mean(valuesX.toNativeArray(), numValues.toNativeArray()),
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/num_k.txt",
          "k",
          "n");

      Correlations.writeToFile(
          Correlations.mean(numValues2.toNativeArray(), sizeValues.toNativeArray()),
          "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/size_num.txt",
          "num",
          "size");
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    logger.info(
        String.format("Inserted %1$s edges, %2$s edges already present.", edgecnt, doublecnt));
    logger.info(String.format("Inserted %1$s edges between at least one ego.", egoEdge));
  }