Skip to content

Commit

Permalink
delete unnecessary methods
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Oct 25, 2023
1 parent de8872a commit c537907
Showing 1 changed file with 31 additions and 141 deletions.
172 changes: 31 additions & 141 deletions src/main/java/io/bioimage/modelrunner/tiling/TileGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
*/
package io.bioimage.modelrunner.tiling;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import org.bioimageanalysis.icy.deepicy.tools.ImgLib2Utils;

import io.bioimage.modelrunner.utils.IndexingUtils;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
Expand All @@ -41,181 +40,72 @@
public class TileGrid
{
/**
* Size of the input patch. Following "xyczb" axes order
* Size of the input patch. Following the tensor axes order
*/
private int[] patchInputSize;
private int[] tileSize;
/**
* Size of the number of patches per axis. Following "xyczb" axes order
* Size of roi of each tile, following the tensor axes order
*/
private int[] patchGridSize;
private int[] roiSize;

private int[][] corner;
/**
* Name of the tensor to whom these specs correspond
*/
private String tensorName;

private List<long[]> tilePostionsInImage = new ArrayList<long[]>();

private List<long[]> roiPositionsInTile = new ArrayList<long[]>();

private List<long[]> roiPositionsInImage = new ArrayList<long[]>();


private TileGrid()
{
}

/**
*/
public static TileGrid create(PatchSpec tileSpecs, long[] imSize, int[] gridSize)
public static TileGrid create(PatchSpec tileSpecs, int[] gridSize)
{
TileGrid ps = new TileGrid();
ps.tileSize = tileSpecs.getPatchInputSize();
int tileCount = Arrays.stream(gridSize).reduce(1, (a, b) -> a * b);

for (int j = 0; j < tileCount; j ++) {
int[] patchIndex = IndexingUtils.flatIntoMultidimensionalIndex(j, gridSize);
int[] patchSize = tileSpecs.getPatchInputSize();
int[][] padSize = tileSpecs.getPatchPaddingSize();
int[][] padSizeSeparated = new int[padSize.length][padSize[0].length];
int[] roiSize = IntStream.range(0, patchIndex.length)
.map(i -> patchSize[i] - padSizeSeparated[0][i] - padSizeSeparated[1][i]).toArray();
int[] patchStart = IntStream.range(0, patchIndex.length)
.map(i -> roiSize[i] * patchIndex[i] - padSizeSeparated[0][i]).toArray();
.map(i -> patchSize[i] - padSize[0][i] - padSize[1][i]).toArray();
ps.roiSize = roiSize;
ps.roiPositionsInTile.add(IntStream.range(0, padSize[0].length).mapToLong(i -> (long) padSize[0][i]).toArray());
long[] roiStart = IntStream.range(0, patchIndex.length)
.map(i -> roiSize[i] * patchIndex[i]).mapToLong(i -> (long) i).toArray();
ps.roiPositionsInImage.add(roiStart);
long[] patchStart = IntStream.range(0, patchIndex.length)
.map(i -> roiSize[i] * patchIndex[i] - padSize[0][i]).mapToLong(i -> (long) i).toArray();
ps.tilePostionsInImage.add(patchStart);
}




String patchAxesOrder = "xyczb";
int[] patchSize = tileSpecs.getPatchInputSize();
int[] patchSizeTensorAxes = PatchGridCalculator.arrayToWantedAxesOrderAddOnes(patchSize, patchAxesOrder, tensor.getAxesOrder());
int[] patchIndexTensorAxes = PatchGridCalculator.arrayToWantedAxesOrderAddOnes(patchIndex, patchAxesOrder, tensor.getAxesOrder());
int[][] padSize = tileSpecs.getPatchPaddingSize();
int[][] padSizeAxesTensorAxes = new int[padSize.length][padSize[0].length];
padSizeAxesTensorAxes[0] = PatchGridCalculator.arrayToWantedAxesOrderAddOnes(padSize[0], patchAxesOrder, tensor.getAxesOrder());
padSizeAxesTensorAxes[1] = PatchGridCalculator.arrayToWantedAxesOrderAddOnes(padSize[1], patchAxesOrder, tensor.getAxesOrder());

int[] roiSizeAxesOrder = IntStream.range(0, patchIndexTensorAxes.length)
.map(i -> patchSizeTensorAxes[i] - padSizeAxesTensorAxes[0][i] - padSizeAxesTensorAxes[1][i]).toArray();
int[] patchStartAxesOrder = IntStream.range(0, patchIndexTensorAxes.length)
.map(i -> roiSizeAxesOrder[i] * patchIndexTensorAxes[i] - padSizeAxesTensorAxes[0][i]).toArray();

Img< T > patchSequence = new ArrayImgFactory((NativeType) Util.getTypeFromInterval(inputNDArray))
.create( patchSizeTensorAxes);
ImgLib2Utils.copyRaiData(inputNDArray, patchSequence, patchStartAxesOrder, padSizeAxesTensorAxes[1]);
ImgLib2Utils.addMirrorToPatchRai(inputNDArray, patchSequence, patchStartAxesOrder, padSizeAxesTensorAxes);

return ps;
}

private TileGrid()
{
}

/**
* TODO this method should be per image, not in total??
* TODO this method should be per image, not in total??
* TODO this method should be per image, not in total??
* TODO this method should be per image, not in total??
* Obtain the number of patches in each axes for a list of input patch specs.
* When tiling is allowed, only one patch grid is permitted. If among the tensors
* there are one or more that do not allow tiling, then two patch sizes are allowed,
* the one for the tensors that allow tiling and the one for the ones that not (that will
* just be 1s in every axes).
* In the case there exist tensors that allow tiling, the grid size for those will be the
* one returned
* @param patches
* list of specs for the tiling strategy of a list of tensors
* @return the number of patches in each axes
*/
public static int[] getGridSize(List<TileGrid> patches) {
// The minimum possible grid is just one patch in every direction. This is the
// grid if no tiling is allowed
int[] grid = new int[]{1, 1, 1, 1, 1};
// If there is any different grid, that will be the absolute one
for (TileGrid pp : patches) {
if (!PatchGridCalculator.compareTwoArrays(grid, pp.getPatchGridSize()))
return pp.getPatchGridSize();
}
return grid;
}

/**
* TODO this method should be per image, not in total??
* TODO this method should be per image, not in total??
* TODO this method should be per image, not in total??
* TODO this method should be per image, not in total??
* Obtain the number of patches in each axes for a list of input patch specs.
* When tiling is allowed, only one patch grid is permitted. If among the tensors
* there are one or more that do not allow tiling, then two patch sizes are allowed,
* the one for the tensors that allow tiling and the one for the ones that not (that will
* just be 1s in every axes).
* In the case there exist tensors that allow tiling, the grid size for those will be the
* one returned
* @param patches
* map containing tiling specs per tensor
* @return the number of patches in each axes
*/
public static int[] getGridSize(Map<String, TileGrid> patches) {
// The minimum possible grid is just one patch in every direction. This is the
// grid if no tiling is allowed
int[] grid = new int[]{1, 1, 1, 1, 1};
// If there is any different grid, that will be the absolute one
for (TileGrid pp : patches.values()) {
if (!PatchGridCalculator.compareTwoArrays(grid, pp.getPatchGridSize()))
return pp.getPatchGridSize();
}
return grid;
}

/**
* Return the PatchSpec corresponding to the tensor called by the name defined
* @param specs
* list of patch specs
* @param name
* name of the tensor of interest
* @return the patch specs of the tensor if interest
*/
public static TileGrid getPatchSpecFromListByName(List<TileGrid> specs, String name) {
return specs.stream().filter(pp -> pp.getTensorName().equals(name)).findAny().orElse(null);
}

/**
* GEt the name of the tensor
* @return the name of the tensor
*/
public String getTensorName() {
return tensorName;
}

/**
* @return Input patch size. The patch taken from the input sequence including the halo.
*/
public int[] getPatchInputSize()
{
return patchInputSize;
}

/**
* @return The patch grid size. The number of patches in each axis used to cover the entire sequence. It should be computed from the output patch and input
* sequence sizes.
*/
public int[] getPatchGridSize()
{
return patchGridSize;
}

/**
* @return The padding size used on each patch.
*/
public int[][] getPatchPaddingSize()
{
return patchPaddingSize;
}

@Override
public String toString()
{
return "";
/*
String[] paddingStrArr = new String[patchPaddingSize[0].length];
for (int i = 0; i < paddingStrArr.length; i ++)
paddingStrArr[i] = patchPaddingSize[0][i] + "," + patchPaddingSize[1][i];
StringBuilder builder = new StringBuilder();
builder.append("PatchSpec of '" + tensorName + "'"
+ "[patchInputSize=").append(Arrays.toString(patchInputSize))
+ "[patchInputSize=").append(Arrays.toString(this.tileSize))
.append(", patchGridSize=").append(Arrays.toString(patchGridSize))
.append(", patchPaddingSize=").append(Arrays.toString(paddingStrArr))
.append("]");
return builder.toString();
*/
}

}

0 comments on commit c537907

Please sign in to comment.