public static IDataset interpolate(IDataset oldx, IDataset oldy, IDataset newx) {

    // TODO more sanity checks on inputs

    DoubleDataset dx = (DoubleDataset) DatasetUtils.cast(oldx, Dataset.FLOAT64);
    DoubleDataset dy = (DoubleDataset) DatasetUtils.cast(oldy, Dataset.FLOAT64);

    boolean sorted = true;
    double maxtest = oldx.getDouble(0);
    for (int i = 1; i < ((Dataset) oldx).count(); i++) {
      if (maxtest > oldx.getDouble(i)) {
        sorted = false;
        break;
      }
      maxtest = dx.getDouble(i);
    }

    double[] sortedx = null;
    double[] sortedy = null;

    if (!sorted) {
      IntegerDataset sIdx = getIndiciesOfSorted(dx);
      sortedx = new double[dx.getData().length];
      sortedy = new double[dy.getData().length];

      for (int i = 0; i < sIdx.getSize(); i++) {
        sortedx[i] = dx.getDouble(sIdx.get(i));
        sortedy[i] = dy.getDouble(sIdx.get(i));
      }
    } else {
      sortedx = dx.getData();
      sortedy = dy.getData();
    }

    SplineInterpolator si = new SplineInterpolator();
    PolynomialSplineFunction poly = si.interpolate(sortedx, sortedy);

    IDataset newy = newx.clone();
    newy.setName(oldy.getName() + "_interpolated");

    for (int i = 0; i < ((Dataset) newx).count(); i++) {
      newy.set(poly.value(newx.getDouble(i)), i);
    }

    return newy;
  }
  @Test
  public void testFunction() {
    AFunction f = new StraightLine();
    Assert.assertEquals(2, f.getNoOfParameters());
    f.setParameterValues(23., -10.);
    Assert.assertArrayEquals(new double[] {23., -10.}, f.getParameterValues(), ABS_TOL);
    Assert.assertEquals(23. - 10., f.val(1), ABS_TOL);

    DoubleDataset xd = new DoubleDataset(new double[] {-1, 0, 2});
    DoubleDataset dx;
    dx = f.calculateValues(xd);
    Assert.assertArrayEquals(new double[] {-23. - 10, -10, 23. * 2 - 10.}, dx.getData(), ABS_TOL);

    dx = f.calculatePartialDerivativeValues(f.getParameter(0), xd);
    Assert.assertArrayEquals(new double[] {-1, 0, 2}, dx.getData(), ABS_TOL);

    dx = f.calculatePartialDerivativeValues(f.getParameter(1), xd);
    Assert.assertArrayEquals(new double[] {1, 1, 1}, dx.getData(), ABS_TOL);

    Assert.assertEquals(-1, f.partialDeriv(f.getParameter(0), -1), ABS_TOL);
    Assert.assertEquals(1, f.partialDeriv(f.getParameter(1), -1), ABS_TOL);
  }
  public double absorptionCorrectionCommon(boolean isSampleScatterer, boolean isSampleAttenuator) {
    // Make the two components as ComponentGeometries
    XPDFComponentGeometry sample = new XPDFComponentCylinder();
    XPDFComponentGeometry capillary = new XPDFComponentCylinder();

    sample.setDistances(0.0, 0.15);
    sample.setStreamality(true, true);

    capillary.setDistances(0.15, 0.16);
    capillary.setStreamality(true, true);

    XPDFComponentGeometry scatterer = (isSampleScatterer) ? sample : capillary;
    XPDFComponentGeometry attenuator = (isSampleAttenuator) ? sample : capillary;

    // These will be calculated by the Composition class
    double quartzAttenuationCoefficient = 0.0529783195963;
    double ceriaAttenuationCoefficient = 1.86349597825;
    double attenuationCoefficient =
        (isSampleAttenuator) ? ceriaAttenuationCoefficient : quartzAttenuationCoefficient;

    XPDFBeamData beamData = new XPDFBeamData();
    beamData.setBeamEnergy(76.6);
    beamData.setBeamHeight(0.07);
    beamData.setBeamWidth(0.07);

    // Read the target data, and also the angles (in radians) to run
    String dataPath = "/home/rkl37156/ceria_dean_data/";
    IDataHolder dh = null;
    String scattererName, attenuatorName;
    scattererName = (isSampleScatterer) ? "sample" : "container";
    attenuatorName = (isSampleAttenuator) ? "sample" : "container";
    try {
      dh = LoaderFactory.getData(dataPath + scattererName + "_" + attenuatorName + "_abs.xy");
    } catch (Exception e) {
    }
    Dataset delta1D = DatasetUtils.convertToDataset(dh.getLazyDataset("Column_1").getSlice());
    Dataset delta = new DoubleDataset(delta1D.getSize(), 1);
    for (int i = 0; i < delta1D.getSize(); i++) delta.set(delta1D.getDouble(i), i, 0);
    Dataset gamma = DoubleDataset.zeros(delta);
    Dataset expected = DatasetUtils.convertToDataset(dh.getLazyDataset("Column_2").getSlice());
    expected.setShape(expected.getSize(), 1);

    Dataset calculated =
        scatterer.calculateAbsorptionCorrections(
            gamma, delta, attenuator, attenuationCoefficient, beamData, true, true);

    // calculated.squeeze();

    Dataset difference = Maths.subtract(calculated, expected);
    return Math.sqrt((Double) Maths.square(difference).mean());
  }
  public void testGetUpstreamPathLength() {
    XPDFComponentCylinder cap = new XPDFComponentCylinder();
    cap.setDistances(0.15, 0.16);
    cap.setStreamality(true, true);

    final int xSize = 64, ySize = 64;
    Dataset pathLengthExp = new DoubleDataset(xSize, ySize);
    Dataset pathLength = new DoubleDataset(pathLengthExp);
    Dataset r = new DoubleDataset(pathLengthExp);
    Dataset xi = new DoubleDataset(pathLengthExp);

    // Read the data from file
    String fileDirectory = "/home/rkl37156/ceria_dean_data/";
    Dataset[] datasets = {pathLengthExp, r, xi};
    String[] files = {"incoming", "sample_r", "sample_xi"};

    for (int k = 0; k < 3; k++) {

      Path filePath = new File(fileDirectory + files[k] + ".txt").toPath();
      List<String> filelines = new ArrayList<String>();
      try {
        filelines = Files.readAllLines(filePath);
      } catch (Exception e) {
        fail("Error reading " + filePath.toString());
      }
      for (int i = 0; i < ySize; i++) {
        String[] splitline = filelines.get(i).split(" ");
        for (int j = 0; j < xSize; j++) {
          double assignee = Double.parseDouble(splitline[j]);
          datasets[k].set(assignee, i, j);
        }
      }
    }
    Dataset x = Maths.multiply(r, Maths.sin(xi));
    Dataset z = Maths.multiply(r, Maths.cos(xi));
    Dataset y = DoubleDataset.zeros(x);
    pathLength = cap.getUpstreamPathLength(x, y, z);

    double rmsError =
        Math.sqrt((Double) Maths.square(Maths.subtract(pathLength, pathLengthExp)).mean());
    assertTrue("Error in upstream path length too large", rmsError < 1e-6);
    // fail("Not yet implemented");
  }
Esempio n. 5
0
  protected void updateChannelGraphs() {
    if (histograms != null && histograms.size() > 1) {
      // remove red channel graph
      histograms.remove(0);
      // remove green channel graph
      histograms.remove(0);
      // remove blue channel graph
      histograms.remove(0);
      // remove alpha channel graph
      histograms.remove(0);
    }
    AbstractMapFunction redFunc = GlobalColourMaps.mappingFunctions.get(curRedSelect);
    AbstractMapFunction greenFunc = GlobalColourMaps.mappingFunctions.get(curGreenSelect);
    AbstractMapFunction blueFunc = GlobalColourMaps.mappingFunctions.get(curBlueSelect);
    AbstractMapFunction alphaFunc = GlobalColourMaps.mappingFunctions.get(curAlphaSelect);

    double maxValue = histograms.get(0).max().doubleValue();
    int currentSize = histograms.get(0).getSize();
    DoubleDataset redChannel = new DoubleDataset(currentSize);
    DoubleDataset greenChannel = new DoubleDataset(currentSize);
    DoubleDataset blueChannel = new DoubleDataset(currentSize);
    DoubleDataset alphaChannel = new DoubleDataset(currentSize);
    for (int i = 0; i < currentSize; i++) {
      double value = i / (double) currentSize;
      double redvalue = redFunc.getPoint(value);

      if (curRedInverse) redvalue = 1.0 - redvalue;

      redvalue *= maxValue;

      double greenvalue = greenFunc.getPoint(value);

      if (curGreenInverse) greenvalue = 1.0 - greenvalue;

      greenvalue *= maxValue;

      double bluevalue = blueFunc.getPoint(value);

      if (curBlueInverse) bluevalue = 1.0 - bluevalue;

      bluevalue *= maxValue;

      double alphavalue = alphaFunc.getPoint(value);

      if (curAlphaInverse) alphavalue = 1.0 - alphavalue;
      alphavalue *= maxValue;

      redChannel.set(Math.max(redvalue, 0.0), i);
      greenChannel.set(Math.max(greenvalue, 0.0), i);
      blueChannel.set(Math.max(bluevalue, 0.0), i);
      alphaChannel.set(Math.max(alphavalue, 0.0), i);
    }
    histograms.add(0, alphaChannel);
    histograms.add(0, blueChannel);
    histograms.add(0, greenChannel);
    histograms.add(0, redChannel);

    // Some logging to check on the histograms size
    logger.debug("number of histograms stored is {}", histograms.size());
    if (histograms.size() > 10) {
      logger.warn(
          "Number of stored histograms is over expected levels, now at {}", histograms.size());
    }

    try {
      histogramPlotter.replaceAllPlots(histograms);
    } catch (PlotException e) {
      e.printStackTrace();
    }
  }
  public void testGetDownstreamPathLength() {
    XPDFComponentCylinder cap = new XPDFComponentCylinder();
    cap.setDistances(0.15, 0.16);
    cap.setStreamality(true, true);

    final int xSize = 64, ySize = 64;
    Dataset pathLengthExp = new DoubleDataset(xSize, ySize);
    Dataset pathLength = new DoubleDataset(pathLengthExp);
    Dataset r = new DoubleDataset(xSize, ySize);
    Dataset xi = new DoubleDataset(r);

    String[] angles = {"0.0", "15.0", "28.5", "29.5", "47.0"};
    int nAngles = angles.length;

    for (String angle : angles) {

      // Read the data from file
      String fileDirectory = "/home/rkl37156/ceria_dean_data/";
      Dataset[] datasets = {pathLengthExp, r, xi};
      String[] files = {"outgoing" + angle, "sample_r", "sample_xi"};

      for (int k = 0; k < 3; k++) {

        Path filePath = new File(fileDirectory + files[k] + ".txt").toPath();
        List<String> filelines = new ArrayList<String>();
        try {
          filelines = Files.readAllLines(filePath);
        } catch (Exception e) {
          fail("Error reading " + filePath.toString());
        }
        for (int i = 0; i < ySize; i++) {
          String[] splitline = filelines.get(i).split(" ");
          for (int j = 0; j < xSize; j++) {
            double assignee = Double.parseDouble(splitline[j]);
            datasets[k].set(assignee, i, j);
          }
        }
      }

      Dataset x = Maths.multiply(r, Maths.sin(xi));
      Dataset z = Maths.multiply(r, Maths.cos(xi));
      Dataset y = DoubleDataset.zeros(x);
      pathLength =
          cap.getDownstreamPathLength(x, y, z, 0.0, Math.toRadians(Double.parseDouble(angle)));

      // TODO: remove temporary write command
      try (Writer fileWriter =
          new BufferedWriter(
              new OutputStreamWriter(
                  new FileOutputStream(
                      "/home/rkl37156/ceria_dean_data/outgoing" + angle + ".java.txt"),
                  "utf-8"))) {
        for (int i = 0; i < ySize; i++) {
          for (int j = 0; j < xSize; j++) {
            fileWriter.write(((Double) pathLength.getDouble(i, j)).toString() + " ");
          }
          fileWriter.write(13); // Carriage return
        }
      } catch (Exception e) {;
      }

      double rmsError =
          Math.sqrt((Double) Maths.square(Maths.subtract(pathLength, pathLengthExp)).mean());
      assertTrue(
          "Error in downstream path length at " + angle + " too large: " + rmsError,
          rmsError < 1e-6);
    }
  }