use of net.imglib2.util.Pair in project repseqio by repseqio.
the class MarkovInsertModel method create.
@Override
public InsertGenerator create(RandomGenerator random, final boolean v, List<VDJCGene> vGenes, List<VDJCGene> dGenes, List<VDJCGene> jGenes, List<VDJCGene> cGenes) {
Map<Byte, List<Pair<Byte, Double>>> distParams = new HashMap<>();
for (Map.Entry<String, Double> s : distribution.entrySet()) {
String[] split = s.getKey().split(">");
if (split.length != 2 || split[0].length() != 1 || split[1].length() != 1)
throw new IllegalArgumentException("Illegal distribution key: " + s.getKey() + ". " + "Expected something like \"A>C\"");
byte codeFrom = NucleotideSequence.ALPHABET.symbolToCode(split[0].charAt(0));
byte codeTo = NucleotideSequence.ALPHABET.symbolToCode(split[1].charAt(0));
if (codeFrom == -1 || codeTo == -1)
throw new IllegalArgumentException("Illegal nucleotide in: " + s.getKey() + ".");
List<Pair<Byte, Double>> pairs = distParams.get(codeFrom);
if (pairs == null)
distParams.put(codeFrom, pairs = new ArrayList<>());
pairs.add(new Pair<>(codeTo, s.getValue()));
}
final Map<Byte, EnumeratedDistribution<Byte>> dists = new HashMap<>();
for (byte from = 0; from < NucleotideSequence.ALPHABET.basicSize(); from++) {
List<Pair<Byte, Double>> d = distParams.get(from);
if (d == null)
throw new IllegalArgumentException("No distribution for letter: " + NucleotideSequence.ALPHABET.codeToSymbol(from));
dists.put(from, new EnumeratedDistribution<>(random, d));
}
final IndependentIntGenerator lengthDist = lengthDistribution.create(random);
return new InsertGenerator() {
@Override
public NucleotideSequence generate(GGene gene) {
ReferencePoint point = beginPoint(fromLeft, v);
int pointPosition = gene.getPartitioning().getPosition(point);
if (pointPosition == -1)
throw new RuntimeException("Point " + point + " is not available for gene " + gene);
byte letter = gene.getSequence(new Range(pointPosition, pointPosition + 1)).codeAt(0);
int length = lengthDist.sample();
byte[] array = new byte[length];
for (int i = 0; i < length; i++) {
byte cLetter = dists.get(letter).sample();
array[i] = cLetter;
letter = cLetter;
}
if (!fromLeft)
ArraysUtils.reverse(array);
return NucleotideSequence.ALPHABET.createBuilder().ensureCapacity(length).append(array).createAndDestroy();
}
};
}
use of net.imglib2.util.Pair in project androidApp by InspectorIncognito.
the class GetBusesRequest method processResult.
@NonNull
@Override
public Pair<ArrayList<MapBus>, ArrayList<Event>> processResult(@NonNull String result) {
ArrayList<MapBus> busResponse = new ArrayList<>();
ArrayList<Event> eventResponse = new ArrayList<>();
try {
JSONObject response = new JSONObject(result);
JSONArray buses = response.getJSONArray("servicios");
JSONArray events = response.getJSONArray("eventos");
Log.d("Callback events", events.toString());
for (int i = 0; i < buses.length(); i++) {
try {
MapBus bus = new MapBus(buses.getJSONObject(i));
busResponse.add(bus);
} catch (JSONException ignored) {
}
}
for (int i = 0; i < events.length(); i++) {
// Event event = new Event(events.getJSONObject(i));
// eventResponse.add(event);
}
return new Pair<>(busResponse, eventResponse);
} catch (JSONException e) {
e.printStackTrace();
Log.e("GetBusesRequest", result);
}
return new Pair<>(busResponse, eventResponse);
}
use of net.imglib2.util.Pair in project TrakEM2 by trakem2.
the class ExportARGB method makeFlatImageARGBFromOriginals.
/**
* Limited to 2GB arrays for the requested image.
*
* @param patches
* @param roi
* @param backgroundValue
* @param scale
* @return
*/
public static final Pair<ColorProcessor, ByteProcessor> makeFlatImageARGBFromOriginals(final List<Patch> patches, final Rectangle roi, final double backgroundValue, final double scale) {
final ColorProcessor target = new ColorProcessor((int) (roi.width * scale), (int) (roi.height * scale));
target.setInterpolationMethod(ImageProcessor.BILINEAR);
final ByteProcessor targetMask = new ByteProcessor(target.getWidth(), target.getHeight());
targetMask.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
for (final Patch patch : patches) {
final Patch.PatchImage pai = patch.createTransformedImage();
final ColorProcessor fp = (ColorProcessor) pai.target.convertToRGB();
final ByteProcessor alpha;
System.out.println("IMAGE:" + patch.getTitle());
System.out.println("mask: " + pai.mask);
System.out.println("outside: " + pai.outside);
if (null == pai.mask) {
if (null == pai.outside) {
alpha = new ByteProcessor(fp.getWidth(), fp.getHeight());
// fully opaque
Arrays.fill((byte[]) alpha.getPixels(), (byte) 255);
} else {
alpha = pai.outside;
}
} else {
alpha = pai.mask;
}
// The affine to apply
final AffineTransform atc = new AffineTransform();
atc.scale(scale, scale);
atc.translate(-roi.x, -roi.y);
final AffineTransform at = new AffineTransform();
at.preConcatenate(atc);
at.concatenate(patch.getAffineTransform());
final AffineModel2D aff = new AffineModel2D();
aff.set(at);
final CoordinateTransformMesh mesh = new CoordinateTransformMesh(aff, patch.getMeshResolution(), fp.getWidth(), fp.getHeight());
final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(mesh);
fp.setInterpolationMethod(ImageProcessor.BILINEAR);
// no interpolation
alpha.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
mapping.map(fp, alpha, target, targetMask);
}
return new Pair<ColorProcessor, ByteProcessor>(target, targetMask);
}
use of net.imglib2.util.Pair in project TrakEM2 by trakem2.
the class ExportBestFlatImage method makeFlatColorImage.
public Pair<ColorProcessor, ByteProcessor> makeFlatColorImage() {
printInfo();
if (canUseAWTImage()) {
// less than 0.5 GB array size
final ColorProcessor cp = new ColorProcessor(createAWTImage(ImagePlus.COLOR_RGB));
final ByteProcessor alpha = new ByteProcessor(cp.getWidth(), cp.getHeight(), cp.getChannel(4));
return new Pair<ColorProcessor, ByteProcessor>(cp, alpha);
}
if (!isSmallerThan2GB()) {
Utils.log("Cannot create an image larger than 2 GB.");
return null;
}
if (loader.isMipMapsRegenerationEnabled()) {
return ExportARGB.makeFlatImageARGBFromMipMaps(patches, finalBox, 0, scale);
}
// No mipmaps: create an image as large as possible, then downsample it
final Pair<ColorProcessor, ByteProcessor> pair = ExportARGB.makeFlatImageARGBFromOriginals(patches, finalBox, 0, scaleUP);
final double sigma = computeSigma(pair.a.getWidth(), pair.a.getHeight());
new GaussianBlur().blurGaussian(pair.a, sigma, sigma, 0.0002);
new GaussianBlur().blurGaussian(pair.b, sigma, sigma, 0.0002);
return pair;
}
use of net.imglib2.util.Pair in project TrakEM2 by trakem2.
the class ExportBestFlatImage method makeFlatFloatGrayImageAndAlpha.
/**
* While the data is in the 8-bit range, the format is as a FloatProcessor.
*/
public Pair<FloatProcessor, FloatProcessor> makeFlatFloatGrayImageAndAlpha() {
printInfo();
if (canUseAWTImage()) {
// In color to preserve the alpha channel present in mipmaps
final Image img = createAWTImage(ImagePlus.COLOR_RGB);
final int width = img.getWidth(null);
final int height = img.getHeight(null);
final int[] pixels = new int[width * height];
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
;
final float[] grey = new float[pixels.length];
final float[] alpha = new float[pixels.length];
for (int i = 0; i < pixels.length; ++i) {
final int p = pixels[i];
alpha[i] = ((p & 0xff000000) >> 24);
grey[i] = (((p & 0x00ff0000) >> 16) + ((p & 0x0000ff00) >> 8) + (p & 0x000000ff)) / 3f;
}
return new Pair<FloatProcessor, FloatProcessor>(new FloatProcessor(width, height, grey, null), new FloatProcessor(width, height, alpha, null));
}
if (!isSmallerThan2GB()) {
Utils.log("Cannot create an image larger than 2 GB.");
return null;
}
if (loader.isMipMapsRegenerationEnabled()) {
// Use mipmaps directly: they are already Gaussian-downsampled
final Pair<ByteProcessor, ByteProcessor> pair = ExportUnsignedByte.makeFlatImageFromMipMaps(patches, finalBox, 0, scale);
return new Pair<FloatProcessor, FloatProcessor>(pair.a.convertToFloatProcessor(), pair.b.convertToFloatProcessor());
}
// Else: no mipmaps
loader.releaseAll();
// Use originals and Gaussian-downsample them, then map them onto the target image
final Pair<ByteProcessor, ByteProcessor> pair = ExportUnsignedByte.makeFlatImageFromOriginals(patches, finalBox, 0, scale);
return new Pair<FloatProcessor, FloatProcessor>(pair.a.convertToFloatProcessor(), pair.b.convertToFloatProcessor());
}
Aggregations