Changeset 6533


Ignore:
Timestamp:
06/17/10 17:33:55 (9 years ago)
Author:
bdezonia
Message:

more refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/loci-plugins/test/loci/plugins/in/ImporterTest.java

    r6532 r6533  
    4848//   concat and split (returns wrong number of images per imp) 
    4949//   record does not work 
    50  
    51 // broken 
    52 //   comboCropAndAutoscale for INT32. I think its a limitation of Fake. The values of the cropped image are less 
    53 //     than the minimum representable value of an int as a float. So when we make a FloatProcessor on the int[] data 
    54 //     the huge negative values get clamped to the lowest representable point and thus max and min are not set correctly 
    55 //     by IJ. I have verified that the pixel data that is sent to FloatProcessor() is correct. Limitation we'll live 
    56 //     with I guess. 
    5750 
    5851// testable code according to my notes 
     
    326319 
    327320  // TODO : this code written to pass tests - looks wrong on a number of pixel types 
     321  /** returns the maximum pixel value for a given pixel type */ 
    328322  private long maxPixelValue(int pixType) 
    329323  { 
     
    345339  } 
    346340   
     341  /** returns the minimum pixel value for a given pixel type */ 
    347342  private long minPixelValue(int pixType) 
    348343  { 
     
    365360  } 
    366361 
    367   // note - this code relies on setPosition(c,z,t) being called previously. Otherwise regular ImagePlus case won't work. 
    368   //   In general this method not designed for general use but actually just for use by getPixelValue(). 
    369    
     362  private long expectedMin(int pixType, boolean wantAutoscale) 
     363  { 
     364    long min; 
     365     
     366    if (wantAutoscale || (FormatTools.isFloatingPoint(pixType))) 
     367    { 
     368      min = minPixelValue(pixType); 
     369    } 
     370    else // not autoscaling - get min/max of pixel type 
     371    { 
     372      min = 0; 
     373    } 
     374 
     375    if (pixType == FormatTools.INT16)  // hack : clamp like IJ does 
     376      if (min < 0) 
     377        min = 0; 
     378 
     379    return min; 
     380  } 
     381   
     382  private long expectedMax(int pixType, boolean wantAutoscale, long maxPixVal, long maxIndex) 
     383  { 
     384    long max; 
     385     
     386    if (wantAutoscale || (FormatTools.isFloatingPoint(pixType))) 
     387    { 
     388      max = Math.max( maxPixVal, maxIndex); 
     389    } 
     390    else // not autoscaling - get min/max of pixel type 
     391    { 
     392      max = maxPixelValue(pixType); 
     393    } 
     394 
     395    if (pixType == FormatTools.INT16)  // hack : clamp like IJ does 
     396      if (max > 65535) 
     397        max = 65535; 
     398     
     399    return max; 
     400  } 
     401 
     402  /** set an ImagePlus' position relative to CZT ordering (matches imp.setPosition()) */ 
     403  private void setCztPosition(ImagePlus imp, int z, int c, int t) 
     404  { 
     405    // czt order : should match the .setPosition(c,z,t) code 
     406    int sliceNumber = c + (z*imp.getNChannels()) + (t*imp.getNSlices()*imp.getNChannels()); 
     407    imp.setSlice(sliceNumber+1); 
     408  } 
     409   
     410  /** set an ImagePlus' position relative to ZCT ordering (rather than default CZT) */ 
     411  private void setZctPosition(ImagePlus imp, int z, int c, int t) 
     412  { 
     413    // zct order 
     414    int sliceNumber = z + (c*imp.getNSlices()) + (t*imp.getNSlices()*imp.getNChannels()); 
     415    imp.setSlice(sliceNumber+1); 
     416  } 
     417   
     418  // note - the following code relies on setZctPosition() being called previously. Otherwise regular ImagePlus case 
     419  //   won't work. In general this method not designed for general use but actually just for use by getPixelValue(). 
     420   
     421  /** gets the color table from any kind of ImagePlus (composite or not) - not for general use */ 
    370422  private LUT getColorTable(ImagePlus imp, int channel) 
    371423  { 
     
    386438     
    387439    return new LUT(reds,greens,blues); 
    388   } 
    389    
    390   private void setCztPosition(ImagePlus imp, int z, int c, int t) 
    391   { 
    392     // czt order : should match the .setPosition(c,z,t) code 
    393     int sliceNumber = c + (z*imp.getNChannels()) + (t*imp.getNSlices()*imp.getNChannels()); 
    394     imp.setSlice(sliceNumber+1); 
    395   } 
    396    
    397   /** set an ImagePlus' position relative to ZCT ordering (rather than default CZT) */ 
    398   private void setZctPosition(ImagePlus imp, int z, int c, int t) 
    399   { 
    400     // zct order 
    401     int sliceNumber = z + (c*imp.getNSlices()) + (t*imp.getNSlices()*imp.getNChannels()); 
    402     imp.setSlice(sliceNumber+1); 
    403440  } 
    404441   
     
    448485    assertNotNull(imps); 
    449486    assertEquals(numExpected,imps.length); 
     487  } 
     488   
     489  /** tests that the stack of an ImagePlus is of a given size */ 
     490  private void stackTest(ImagePlus imp, int expectedSize) 
     491  { 
     492    assertNotNull(imp); 
     493    assertEquals(expectedSize,imp.getStack().getSize()); 
    450494  } 
    451495   
     
    527571  private void stackInZctOrderTest(ImagePlus imp, int maxZ, int maxC, int maxT, boolean indexed) 
    528572  { 
    529     assertEquals(imp.getStack().getSize(),(maxZ * maxC * maxT)); 
     573    stackTest(imp,(maxZ * maxC * maxT)); 
    530574 
    531575    int iIndex = 0; 
     
    549593  private void multipleSeriesInZtcOrderTest(ImagePlus imp, int numSeries, int maxZ, int maxC, int maxT) 
    550594  { 
     595    // make sure the number of slices in stack is a sum of all series 
     596    stackTest(imp,(numSeries*maxZ*maxC*maxT)); 
     597 
    551598    ImageStack st = imp.getStack(); 
    552599 
    553     // make sure the number of slices in stack is a sum of all series 
    554     assertEquals(st.getSize(), (numSeries*maxZ*maxC*maxT)); 
    555      
    556600    int slice = 0; 
    557601    for (int sIndex = 0; sIndex < numSeries; sIndex++) { 
     
    586630  } 
    587631   
     632  /** tests that multiple file groups are pulled into one dataset */ 
    588633  private void groupedFilesTest(ImagePlus imp, int numDatasets, int numImagesPerDataset) 
    589634  { 
     635    stackTest(imp,numDatasets*numImagesPerDataset); 
     636     
    590637    ImageStack st = imp.getStack(); 
    591      
    592     assertEquals(st.getSize(),numDatasets*numImagesPerDataset); 
    593638     
    594639    int slice = 0; 
     
    608653  } 
    609654   
     655  /** tests that a dataset has had its Z & T dimensions swapped */ 
    610656  private void swappedZtTest(ImagePlus imp, int originalZ, int originalC, int originalT) 
    611657  { 
     658    stackTest(imp,(originalZ*originalC*originalT)); 
     659 
    612660    ImageStack st = imp.getStack(); 
    613     int numSlices = st.getSize(); 
    614     assertEquals(numSlices,(originalZ*originalC*originalT)); 
    615661 
    616662    // verify that the dimensional extents were swapped 
     
    648694    int ts = numInSeries(tFrom,tTo,tBy); 
    649695    
     696    stackTest(imp,(zs * cs * ts)); 
     697     
    650698    ImageStack st = imp.getStack(); 
    651      
    652     assertTrue((zs * cs * ts) == st.getSize()); 
    653699     
    654700    for (int t = 0; t < ts; t++) 
     
    670716  } 
    671717 
     718  /** for signed integral data tests that the Calibration of an ImagePlus is correct */ 
     719  private void calibrationTest(ImagePlus imp, int pixType) 
     720  { 
     721    if (FormatTools.isSigned(pixType) && !FormatTools.isFloatingPoint(pixType)) 
     722    { 
     723      Calibration cal = imp.getCalibration(); 
     724      assertEquals(Calibration.STRAIGHT_LINE,cal.getFunction()); 
     725      double[] coeffs = cal.getCoefficients(); 
     726      int bitsPerPix = FormatTools.getBytesPerPixel(pixType) * 8; 
     727      assertEquals(-(Math.pow(2, (bitsPerPix-1))),coeffs[0],0); 
     728      assertEquals(1,coeffs[1],0); 
     729    } 
     730  } 
     731   
    672732  // ******** specific testers  ********************************** 
    673733   
     
    722782    xyzctTest(imp,x,y,z,c,t); 
    723783 
     784    stackTest(imp,z*c*t); 
     785     
    724786    ImageStack st = imp.getStack(); 
    725     int numSlices = st.getSize(); 
    726  
    727     assertEquals(z*c*t,numSlices); 
    728787 
    729788    int slice = 0; 
     
    780839  } 
    781840 
    782   private void datasetOpenAllSeriesTester(int x, int y, int z, int c, int t, int s) 
    783   { 
     841  /** open a fakefile series either as separate ImagePluses or as one ImagePlus depending on input flag allOfThem */ 
     842  private ImagePlus[] openSeriesTest(String fakeFileName, boolean allOfThem) 
     843  { 
     844    ImagePlus[] imps = null; 
     845     
     846    try { 
     847      ImporterOptions options = new ImporterOptions(); 
     848      options.setId(fakeFileName); 
     849      options.setOpenAllSeries(allOfThem); 
     850      imps = BF.openImagePlus(options); 
     851    } 
     852    catch (IOException e) { 
     853      fail(e.getMessage()); 
     854    } 
     855    catch (FormatException e) { 
     856      fail(e.getMessage()); 
     857    } 
     858     
     859    return imps; 
     860  } 
     861   
     862  private void datasetOpenAllSeriesTester(boolean allOfThem) 
     863  { 
     864    int x = 55, y = 20, z = 2, c = 3, t = 4, s = 5; 
     865     
    784866    String path = constructFakeFilename("openAllSeries", FormatTools.UINT32, x, y, z, c, t, s, false, -1, false, -1); 
    785867     
    786     // try it when false 
    787      
    788     ImagePlus[] imps = null; 
    789     try { 
    790       ImporterOptions options = new ImporterOptions(); 
    791       options.setId(path); 
    792       options.setOpenAllSeries(false); 
    793       imps = BF.openImagePlus(options); 
    794     } 
    795     catch (IOException e) { 
    796       fail(e.getMessage()); 
    797     } 
    798     catch (FormatException e) { 
    799       fail(e.getMessage()); 
    800     } 
    801      
    802     // test results 
    803      
    804     impsCountTest(imps,1); 
    805     xyzctTest(imps[0],x,y,z,c,t); 
    806     assertEquals(z*c*t, imps[0].getStack().getSize()); 
    807      
    808     // try it when true 
    809      
    810     try { 
    811       ImporterOptions options = new ImporterOptions(); 
    812       options.setId(path); 
    813       options.setOpenAllSeries(true); 
    814       imps = BF.openImagePlus(options); 
    815     } 
    816     catch (IOException e) { 
    817       fail(e.getMessage()); 
    818     } 
    819     catch (FormatException e) { 
    820       fail(e.getMessage()); 
    821     } 
    822  
    823     // test results 
    824      
    825     assertEquals(s,imps.length); 
    826     for (int i = 0; i < s; i++) 
    827     { 
    828       assertEquals(x,imps[i].getWidth()); 
    829       assertEquals(y,imps[i].getHeight()); 
    830       assertEquals(z*c*t, imps[i].getStack().getSize()); 
    831     } 
     868    int expectedNumSeries = 1; 
     869    if (allOfThem) 
     870      expectedNumSeries = s; 
     871     
     872    ImagePlus[] imps = openSeriesTest(path,allOfThem); 
     873    impsCountTest(imps,expectedNumSeries); 
     874    for (int i = 0; i < expectedNumSeries; i++) 
     875      xyzctTest(imps[i],x,y,z,c,t); 
    832876  } 
    833877   
     
    887931 
    888932    xyzctTest(imp,sizeX,sizeY,sizeZ,sizeC,sizeT); 
    889  
     933     
     934    stackTest(imp,sizeZ*sizeC*sizeT); 
     935 
     936    calibrationTest(imp,pixType); 
     937     
     938    long maxPixVal = minPixelValue(pixType)+sizeX-1; 
     939    long maxIndex = sizeZ*sizeC*sizeT - 1; 
     940     
     941    long expectedMax = expectedMax(pixType,wantAutoscale,maxPixVal,maxIndex); 
     942    long expectedMin = expectedMin(pixType,wantAutoscale); 
     943     
    890944    ImageStack st = imp.getStack(); 
    891945    int numSlices = st.getSize(); 
    892  
    893     long expectedMax,expectedMin; 
    894      
    895     if (wantAutoscale || (FormatTools.isFloatingPoint(pixType))) 
    896     { 
    897       expectedMax = Math.max( minPixelValue(pixType)+sizeX-1, sizeZ*sizeC*sizeT - 1 );  // series size always 1 so ignore 
    898       expectedMin = minPixelValue(pixType); 
    899     } 
    900     else // not autoscaling - get min/max of pixel type 
    901     { 
    902       expectedMax = maxPixelValue(pixType); 
    903       expectedMin = 0; 
    904     } 
    905  
    906     // if signed make sure Calibration is setup correctly 
    907     if (FormatTools.isSigned(pixType) && !FormatTools.isFloatingPoint(pixType)) 
    908     { 
    909       Calibration cal = imp.getCalibration(); 
    910       assertEquals(Calibration.STRAIGHT_LINE,cal.getFunction()); 
    911       double[] coeffs = cal.getCoefficients(); 
    912       int bitsPerPix = FormatTools.getBytesPerPixel(pixType) * 8; 
    913       assertEquals(-(Math.pow(2, (bitsPerPix-1))),coeffs[0],0); 
    914       assertEquals(1,coeffs[1],0); 
    915        
    916       // note - IJ clamps min and max to a range for ShortProcessor (unlike all other processors) 
    917       if (pixType == FormatTools.INT16)  // hack : clamp like IJ does 
    918       { 
    919         if (expectedMin < 0) 
    920           expectedMin = 0; 
    921         if (expectedMax > 65535) 
    922           expectedMax = 65535; 
    923       } 
    924     } 
    925      
    926946    for (int i = 0; i < numSlices; i++) 
    927947    { 
     
    9861006    ci.reset();  // force the channel processors to get initialized, otherwise nullptr  - TODO : does this point out a IJ bug? 
    9871007     
     1008    /* 
    9881009    int maxZ = ci.getNSlices(); 
    9891010    int maxC = ci.getNChannels(); 
     
    9931014    //System.out.println("maxes z c t = "+maxZ+" "+maxC+" "+maxT); 
    9941015     
    995     /* 
    9961016    // check that each image in the overall series has the correct iIndex value 
    9971017    int index = 0; 
     
    13451365    xyzctTest(imps[0],sizeCrop,sizeCrop,sizeZ,sizeC,sizeT); 
    13461366 
    1347     ImageStack st = imp.getStack(); 
    1348     int numSlices = st.getSize(); 
    1349      
    1350     assertEquals(sizeZ*sizeC*sizeT,numSlices); 
    1351  
     1367    stackTest(imp,(sizeZ*sizeC*sizeT)); 
     1368     
     1369    calibrationTest(imp,pixType); 
     1370     
    13521371    long expectedMax = minPixelValue(pixType) + originCropX + sizeCrop - 1; 
    13531372    long expectedMin = minPixelValue(pixType) + originCropX; 
    13541373 
    1355     // make sure Calibration is set correctly 
    1356     if (FormatTools.isSigned(pixType) && !FormatTools.isFloatingPoint(pixType)) 
     1374    if (pixType == FormatTools.INT16)  // hack : clamp like IJ does 
    13571375    { 
    1358       Calibration cal = imp.getCalibration(); 
    1359       assertEquals(Calibration.STRAIGHT_LINE,cal.getFunction()); 
    1360       double[] coeffs = cal.getCoefficients(); 
    1361       int bitsPerPix = FormatTools.getBytesPerPixel(pixType) * 8; 
    1362       assertEquals(-(Math.pow(2, (bitsPerPix-1))),coeffs[0],0); 
    1363       assertEquals(1,coeffs[1],0); 
    1364        
    1365       // note - IJ clamps min and max to a range for ShortProcessor (unlike all other processors) 
    1366       if (pixType == FormatTools.INT16)  // hack : clamp like IJ does 
    1367       { 
    1368         if (expectedMin < 0) 
    1369           expectedMin = 0; 
    1370         if (expectedMax > 65535) 
    1371           expectedMax = 65535; 
    1372       } 
     1376      if (expectedMin < 0) 
     1377        expectedMin = 0; 
     1378      if (expectedMax > 65535) 
     1379        expectedMax = 65535; 
    13731380    } 
    13741381 
     
    13791386    */ 
    13801387     
     1388    ImageStack st = imp.getStack(); 
     1389    int numSlices = st.getSize(); 
    13811390    for (int i = 0; i < numSlices; i++) 
    13821391    { 
     
    14201429      ImagePlus imp = imps[z]; 
    14211430      xyzctTest(imp,sizeX,sizeY,1,sizeC,sizeT); 
     1431      stackTest(imp,series*sizeC*sizeT); 
    14221432      ImageStack st = imp.getStack(); 
    1423       assertEquals(series*sizeC*sizeT,st.getSize()); 
    14241433      for (int s = 0; s < series; s++) { 
    14251434        int slice = s*sizeC*sizeT; 
     
    14751484      ImagePlus imp = imps[c]; 
    14761485      xyzctTest(imp,sizeX,sizeY,sizeZ,1,sizeT); 
     1486      stackTest(imp,series*sizeZ*sizeT); 
    14771487      ImageStack st = imp.getStack(); 
    1478       assertEquals(series*sizeZ*sizeT,st.getSize()); 
    14791488      for (int s = 0; s < series; s++) { 
    14801489        int slice = s*sizeZ*sizeT; 
     
    15291538      ImagePlus imp = imps[t]; 
    15301539      xyzctTest(imp,sizeX,sizeY,sizeZ,sizeC,1); 
     1540      stackTest(imp,series*sizeZ*sizeC); 
    15311541      ImageStack st = imp.getStack(); 
    1532       assertEquals(series*sizeZ*sizeC,st.getSize()); 
    15331542      for (int s = 0; s < series; s++) { 
    15341543        int slice = s*sizeZ*sizeC; 
     
    15501559  } 
    15511560   
    1552  
    15531561  private void compositeTester(int sizeC, boolean indexed) 
    15541562  { 
     
    16731681     
    16741682    impsCountTest(imps,1); 
    1675     assertEquals(16,imps[0].getStack().getSize()); // one loaded as one set with 16 slices 
     1683    stackTest(imps[0],16); // one loaded as one set with 16 slices 
    16761684     
    16771685    // try grouped 
     
    16931701     
    16941702    impsCountTest(imps,1); 
    1695     assertEquals(32,imps[0].getStack().getSize()); // both loaded as one set of 32 slices 
     1703    stackTest(imps[0],32); // both loaded as one set of 32 slices 
    16961704  } 
    16971705 
     
    17161724  public void testDatasetOpenAllSeries() 
    17171725  { 
    1718     datasetOpenAllSeriesTester(73,107,1,1,1,1);  // one series 
    1719     datasetOpenAllSeriesTester(73,107,1,1,1,2);  // two series 
    1720     datasetOpenAllSeriesTester(73,107,5,3,4,4);  // multiple series with Z,C,T larger than 1 
     1726    datasetOpenAllSeriesTester(false); 
     1727    datasetOpenAllSeriesTester(true); 
    17211728  } 
    17221729 
     
    20892096      ImagePlus imp = imps[c]; 
    20902097      xyzctTest(imp,sizeX,sizeY,sizeZ,1,sizeT); 
     2098      stackTest(imp,sizeZ * sizeT); 
    20912099      ImageStack st = imp.getStack(); 
    2092       assertEquals(sizeZ * sizeT,st.getSize()); 
    20932100      int slice = 0; 
    20942101      for (int t = 0; t < sizeT; t++) { 
     
    21332140      ImagePlus imp = imps[z]; 
    21342141      xyzctTest(imp,sizeX,sizeY,1,sizeC,sizeT); 
     2142      stackTest(imp,sizeC * sizeT); 
    21352143      ImageStack st = imp.getStack(); 
    2136       assertEquals(sizeC * sizeT,st.getSize()); 
    21372144      int slice = 0; 
    21382145      for (int t = 0; t < sizeT; t++) { 
     
    21772184      ImagePlus imp = imps[t]; 
    21782185      xyzctTest(imp,sizeX,sizeY,sizeZ,sizeC,1); 
     2186      stackTest(imp,sizeZ * sizeC); 
    21792187      ImageStack st = imp.getStack(); 
    2180       assertEquals(sizeZ * sizeC,st.getSize()); 
    21812188      int slice = 0; 
    21822189      for (int c = 0; c < sizeC; c++) { 
     
    22112218    for (int pixType : PixelTypes) 
    22122219      comboCropAndAutoscaleTester(pixType,240,240,2,2,2,225,225,10); 
     2220 
     2221    // broken 
     2222    //  comboCropAndAutoscale for INT32. I think its a limitation of Fake. The values of the cropped image are less 
     2223    //    than the minimum representable value of an int as a float. So when we make a FloatProcessor on the int[] data 
     2224    //    the huge negative values get clamped to the lowest representable point and thus max and min are not set correctly 
     2225    //    by IJ. I have verified that the pixel data that is sent to FloatProcessor() is correct. Limitation we'll live 
     2226    //    with I guess. 
     2227 
    22132228  } 
    22142229   
     
    22902305      xyzctTest(imp,cropSizeX,cropSizeY,1,sizeZ,numC); // all dims changed 
    22912306   
     2307      stackTest(imp,sizeZ*numC);  // sizeZ = C, numC = T 
     2308       
    22922309      ImageStack st = imp.getStack(); 
    2293       assertEquals(sizeZ*numC,st.getSize());  // sizeZ = C, numC = T 
    22942310       
    22952311      int slice = 0; 
Note: See TracChangeset for help on using the changeset viewer.