Skip to content

Commit 180ed00

Browse files
committed
ImgMath: printing hierarchy of OFunction, which may have been simplified
(pruned of null ops)
1 parent 8636c15 commit 180ed00

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/main/java/net/imglib2/algorithm/math/Compute.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ public < O extends RealType< O > > RandomAccessibleInterval< O > into(
177177
return into( target, inConverter, target.randomAccess().get().createVariable(), null );
178178
}
179179

180+
public < O extends RealType< O >, C extends RealType< C > > RandomAccessibleInterval< O > into(
181+
final RandomAccessibleInterval< O > target,
182+
Converter< RealType< ? >, C > inConverter,
183+
final C computingType,
184+
Converter< C, O > outConverter
185+
)
186+
{
187+
return into( target, inConverter, computingType, outConverter, false );
188+
}
189+
180190
/**
181191
* Execute the mathematical operations and store the result into the given {@code RandomAccessibleInterval}.
182192
* Takes into account whether all images involved in the computation are iterable in a compatible way.
@@ -197,6 +207,8 @@ public < O extends RealType< O > > RandomAccessibleInterval< O > into(
197207
* when the {@code computingType} equal the {@code Type} of the {@code target}, and a generic
198208
* {@code RealType} converter that uses floating-point values (with {@code RealType#setReal(double)})
199209
* created with {@code Util#genericRealTypeConverter()} is used.
210+
*
211+
* @param printHierarchy Emits to stdout the hierarchy of {@code OFunction} types.
200212
*
201213
* @return The {@code target}.
202214
*/
@@ -205,7 +217,8 @@ public < O extends RealType< O >, C extends RealType< C > > RandomAccessibleInte
205217
final RandomAccessibleInterval< O > target,
206218
Converter< RealType< ? >, C > inConverter,
207219
final C computingType,
208-
Converter< C, O > outConverter
220+
Converter< C, O > outConverter,
221+
final boolean printHierarchy
209222
)
210223
{
211224
if ( null == inConverter )
@@ -222,6 +235,9 @@ public < O extends RealType< O >, C extends RealType< C > > RandomAccessibleInte
222235
new HashMap< String, LetBinding< C > >(),
223236
inConverter, null );
224237

238+
if ( printHierarchy )
239+
System.out.println( Util.hierarchy( f ) );
240+
225241
if ( are_same_type )
226242
{
227243
// Skip outputConverter: same type

src/main/java/net/imglib2/algorithm/math/abstractions/Util.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public static final String hierarchy( final IFunction f )
296296
// Iterate into the nested operations, depth-first
297297
while ( ! ops.isEmpty() )
298298
{
299-
final IFunction op = ops.removeFirst();
299+
final IFunction op = ops.removeFirst();
300300

301301
String indent = indents.removeFirst();
302302
String pre = "";
@@ -334,4 +334,32 @@ else if ( op instanceof Var )
334334

335335
return hierarchy.toString();
336336
}
337+
338+
public static final String hierarchy( final OFunction< ? > f )
339+
{
340+
final StringBuilder hierarchy = new StringBuilder();
341+
final LinkedList< String > indents = new LinkedList<>();
342+
indents.add("");
343+
344+
final LinkedList< OFunction< ? > > ops = new LinkedList<>();
345+
ops.addLast( f );
346+
347+
// Iterate into the nested operations
348+
while ( ! ops.isEmpty() )
349+
{
350+
final OFunction< ? > op = ops.removeFirst();
351+
352+
final String indent = indents.removeFirst();
353+
354+
for ( final OFunction< ? > cf : op.children() )
355+
{
356+
ops.addFirst( cf );
357+
indents.addFirst( indent + " " );
358+
}
359+
360+
hierarchy.append( indent + op.getClass().getSimpleName() + "\n");
361+
}
362+
363+
return hierarchy.toString();
364+
}
337365
}

0 commit comments

Comments
 (0)