4747package org .knime .knip .io .nodes .imgreader2 .readfrominput ;
4848
4949import java .util .ArrayList ;
50- import java .util .Iterator ;
5150import java .util .List ;
5251import java .util .concurrent .atomic .AtomicBoolean ;
5352
53+ import net .imglib2 .img .ImgFactory ;
54+ import net .imglib2 .img .array .ArrayImgFactory ;
55+ import net .imglib2 .img .cell .CellImgFactory ;
56+ import net .imglib2 .img .planar .PlanarImgFactory ;
57+ import net .imglib2 .type .NativeType ;
58+ import net .imglib2 .type .numeric .RealType ;
59+
5460import org .knime .core .data .DataColumnSpec ;
5561import org .knime .core .data .DataColumnSpecCreator ;
5662import org .knime .core .data .DataRow ;
7985import org .knime .knip .io .nodes .imgreader2 .ColumnCreationMode ;
8086import org .knime .knip .io .nodes .imgreader2 .MetadataMode ;
8187
82- import net .imglib2 .img .ImgFactory ;
83- import net .imglib2 .img .array .ArrayImgFactory ;
84- import net .imglib2 .img .cell .CellImgFactory ;
85- import net .imglib2 .img .planar .PlanarImgFactory ;
86- import net .imglib2 .type .NativeType ;
87- import net .imglib2 .type .numeric .RealType ;
88-
8988/**
9089 * This Node reads images.
9190 *
@@ -127,9 +126,9 @@ public ImgReaderTableNodeModel() {
127126 }
128127
129128 @ Override
130- protected DataTableSpec [] configure (DataTableSpec [] inSpecs ) throws InvalidSettingsException {
129+ protected DataTableSpec [] configure (final DataTableSpec [] inSpecs ) throws InvalidSettingsException {
131130
132- int imgIdx = getPathColIdx (inSpecs [0 ]);
131+ final int imgIdx = getPathColIdx (inSpecs [0 ]);
133132 if (-1 == imgIdx ) {
134133 throw new InvalidSettingsException ("A string column must be selected!" );
135134 }
@@ -138,32 +137,28 @@ protected DataTableSpec[] configure(DataTableSpec[] inSpecs) throws InvalidSetti
138137 }
139138
140139 @ Override
141- protected BufferedDataTable [] execute (BufferedDataTable [] inData , ExecutionContext exec ) throws Exception {
140+ protected BufferedDataTable [] execute (final BufferedDataTable [] inData , final ExecutionContext exec )
141+ throws Exception {
142142
143143 // boolean for exceptions and file format
144144 final AtomicBoolean encounteredExceptions = new AtomicBoolean (false );
145145
146- int imgIdx = getPathColIdx (inData [0 ].getDataTableSpec ());
147- ReadImgTableFunction <T > rifp = createImgTableFunction (exec , inData [0 ].getDataTableSpec (),
146+ final int imgIdx = getPathColIdx (inData [0 ].getDataTableSpec ());
147+ final ReadImgTableFunction <T > rifp = createImgTableFunction (exec , inData [0 ].getDataTableSpec (),
148148 Long .valueOf (inData [0 ].size ()).intValue ());
149149
150- BufferedDataContainer bdc = exec .createDataContainer (getOutspec (inData [0 ].getDataTableSpec (), imgIdx ));
151-
152- Iterator <DataRow > iterator = inData [0 ].iterator ();
153- while (iterator .hasNext ()) {
154- rifp .apply (iterator .next ()).forEachOrdered (dataRow -> {
150+ final BufferedDataContainer bdc = exec .createDataContainer (getOutspec (inData [0 ].getDataTableSpec (), imgIdx ));
155151
152+ for (final DataRow x : inData [0 ]) {
153+ rifp .apply (x ).forEachOrdered (dataRow -> {
156154 if (dataRow .getSecond ().isPresent ()) {
157155 encounteredExceptions .set (true );
158156 LOGGER .warn ("Encountered exception while reading image: " + dataRow .getFirst ().getKey ()
159157 + "! view log for more info." );
160158 LOGGER .debug ("Encountered exception while reading image:" , dataRow .getSecond ().get ());
161159 }
162-
163160 bdc .addRowToTable (dataRow .getFirst ());
164-
165161 });
166-
167162 exec .checkCanceled ();
168163 }
169164
@@ -186,14 +181,15 @@ public StreamableOperator createStreamableOperator(final PartitionInfo partition
186181 final PortObjectSpec [] inSpecs ) throws InvalidSettingsException {
187182 return new StreamableOperator () {
188183 @ Override
189- public void runFinal (PortInput [] inputs , PortOutput [] outputs , ExecutionContext exec ) throws Exception {
190- RowInput in = (RowInput ) inputs [0 ];
191- RowOutput out = (RowOutput ) outputs [0 ];
184+ public void runFinal (final PortInput [] inputs , final PortOutput [] outputs , final ExecutionContext exec )
185+ throws Exception {
186+ final RowInput in = (RowInput ) inputs [0 ];
187+ final RowOutput out = (RowOutput ) outputs [0 ];
192188
193189 // boolean for exceptions and file format
194190 final AtomicBoolean encounteredExceptions = new AtomicBoolean (false );
195191
196- ReadImgTableFunction <T > readImgFunction = createImgTableFunction (exec , in .getDataTableSpec (), 1 );
192+ final ReadImgTableFunction <T > readImgFunction = createImgTableFunction (exec , in .getDataTableSpec (), 1 );
197193
198194 DataRow row ;
199195 while ((row = in .poll ()) != null ) {
@@ -207,7 +203,7 @@ public void runFinal(PortInput[] inputs, PortOutput[] outputs, ExecutionContext
207203
208204 try {
209205 out .push (result .getFirst ());
210- } catch (Exception exc ) {
206+ } catch (final Exception exc ) {
211207 encounteredExceptions .set (true );
212208 LOGGER .warn ("Couldn't push row " + result .getFirst ().getKey () + " into output stream." );
213209 LOGGER .debug ("Encountered exception when trying to push result: " , exc );
@@ -237,21 +233,22 @@ public OutputPortRole[] getOutputPortRoles() {
237233 return new OutputPortRole [] { OutputPortRole .DISTRIBUTED };
238234 }
239235
240- private DataTableSpec getOutspec (DataTableSpec spec , int imgIdx ) {
241- MetadataMode metadataMode = EnumUtils .valueForName (m_metadataModeModel .getStringValue (), MetadataMode .values ());
236+ private DataTableSpec getOutspec (final DataTableSpec spec , final int imgIdx ) {
237+ final MetadataMode metadataMode = EnumUtils .valueForName (m_metadataModeModel .getStringValue (),
238+ MetadataMode .values ());
242239
243- boolean readImage = (metadataMode == MetadataMode .NO_METADATA || metadataMode == MetadataMode . APPEND_METADATA )
244- ? true : false ;
245- boolean readMetadata = (metadataMode == MetadataMode .APPEND_METADATA
240+ final boolean readImage = (metadataMode == MetadataMode .NO_METADATA
241+ || metadataMode == MetadataMode . APPEND_METADATA ) ? true : false ;
242+ final boolean readMetadata = (metadataMode == MetadataMode .APPEND_METADATA
246243 || metadataMode == MetadataMode .METADATA_ONLY ) ? true : false ;
247244
248245 DataTableSpec outSpec ;
249246 // new table
250- ColumnCreationMode columnCreationMode = ColumnCreationMode .fromString (m_colCreationMode .getStringValue ());
247+ final ColumnCreationMode columnCreationMode = ColumnCreationMode .fromString (m_colCreationMode .getStringValue ());
251248 if (columnCreationMode == ColumnCreationMode .NEW_TABLE ) {
252249
253- DataColumnSpec imgSpec = new DataColumnSpecCreator ("Image" , ImgPlusCell .TYPE ).createSpec ();
254- DataColumnSpec omeSpec = new DataColumnSpecCreator ("OME-XML Metadata" , XMLCell .TYPE ).createSpec ();
250+ final DataColumnSpec imgSpec = new DataColumnSpecCreator ("Image" , ImgPlusCell .TYPE ).createSpec ();
251+ final DataColumnSpec omeSpec = new DataColumnSpecCreator ("OME-XML Metadata" , XMLCell .TYPE ).createSpec ();
255252
256253 if (readImage && readMetadata ) {
257254 outSpec = new DataTableSpec (imgSpec , omeSpec );
@@ -265,14 +262,14 @@ private DataTableSpec getOutspec(DataTableSpec spec, int imgIdx) {
265262 // append
266263 else if (columnCreationMode == ColumnCreationMode .APPEND ) {
267264
268- DataColumnSpec imgSpec = new DataColumnSpecCreator (
265+ final DataColumnSpec imgSpec = new DataColumnSpecCreator (
269266 DataTableSpec .getUniqueColumnName (spec , "Image" + m_colSuffix .getStringValue ()), ImgPlusCell .TYPE )
270267 .createSpec ();
271- DataColumnSpec omeSpec = new DataColumnSpecCreator (
268+ final DataColumnSpec omeSpec = new DataColumnSpecCreator (
272269 DataTableSpec .getUniqueColumnName (spec , "OME-XML Metadata" + m_colSuffix .getStringValue ()),
273270 XMLCell .TYPE ).createSpec ();
274271
275- List <DataColumnSpec > list = new ArrayList <>();
272+ final List <DataColumnSpec > list = new ArrayList <>();
276273 for (int i = 0 ; i < spec .getNumColumns (); i ++) {
277274 list .add (spec .getColumnSpec (i ));
278275 }
@@ -290,14 +287,14 @@ else if (columnCreationMode == ColumnCreationMode.APPEND) {
290287 }
291288 // replace
292289 else if (columnCreationMode == ColumnCreationMode .REPLACE ) {
293- DataColumnSpec imgSpec = new DataColumnSpecCreator (
290+ final DataColumnSpec imgSpec = new DataColumnSpecCreator (
294291 DataTableSpec .getUniqueColumnName (spec , "Image" + m_colSuffix .getStringValue ()), ImgPlusCell .TYPE )
295292 .createSpec ();
296- DataColumnSpec omeSpec = new DataColumnSpecCreator (
293+ final DataColumnSpec omeSpec = new DataColumnSpecCreator (
297294 DataTableSpec .getUniqueColumnName (spec , "OME-XML Metadata" + m_colSuffix .getStringValue ()),
298295 XMLCell .TYPE ).createSpec ();
299296
300- List <DataColumnSpec > list = new ArrayList <>();
297+ final List <DataColumnSpec > list = new ArrayList <>();
301298 for (int i = 0 ; i < spec .getNumColumns (); i ++) {
302299 list .add (spec .getColumnSpec (i ));
303300 }
@@ -338,15 +335,16 @@ private int getPathColIdx(final DataTableSpec inSpec) throws InvalidSettingsExce
338335 return imgColIndex ;
339336 }
340337
341- private ReadImgTableFunction <T > createImgTableFunction (ExecutionContext exec , DataTableSpec inSpec , int rowCount )
342- throws InvalidSettingsException {
338+ private ReadImgTableFunction <T > createImgTableFunction (final ExecutionContext exec , final DataTableSpec inSpec ,
339+ final int rowCount ) throws InvalidSettingsException {
343340
344- int imgIdx = getPathColIdx (inSpec );
341+ final int imgIdx = getPathColIdx (inSpec );
345342
346- MetadataMode metadataMode = EnumUtils .valueForName (m_metadataModeModel .getStringValue (), MetadataMode .values ());
347- boolean readImage = (metadataMode == MetadataMode .NO_METADATA || metadataMode == MetadataMode .APPEND_METADATA )
348- ? true : false ;
349- boolean readMetadata = (metadataMode == MetadataMode .APPEND_METADATA
343+ final MetadataMode metadataMode = EnumUtils .valueForName (m_metadataModeModel .getStringValue (),
344+ MetadataMode .values ());
345+ final boolean readImage = (metadataMode == MetadataMode .NO_METADATA
346+ || metadataMode == MetadataMode .APPEND_METADATA ) ? true : false ;
347+ final boolean readMetadata = (metadataMode == MetadataMode .APPEND_METADATA
350348 || metadataMode == MetadataMode .METADATA_ONLY ) ? true : false ;
351349
352350 // create ImgFactory
@@ -372,7 +370,7 @@ private ReadImgTableFunction<T> createImgTableFunction(ExecutionContext exec, Da
372370 }
373371
374372 // create image function
375- ReadImgTableFunction <T > rifp = new ReadImgTableFunction <>(exec , rowCount , m_planeSelect , readImage ,
373+ final ReadImgTableFunction <T > rifp = new ReadImgTableFunction <>(exec , rowCount , m_planeSelect , readImage ,
376374 readMetadata , m_readAllMetaDataModel .getBooleanValue (), m_checkFileFormat .getBooleanValue (),
377375 m_isGroupFiles .getBooleanValue (), seriesSelectionFrom , seriesSelectionTo , imgFac ,
378376 ColumnCreationMode .fromString (m_colCreationMode .getStringValue ()), imgIdx ,
0 commit comments