|
33 | 33 | __doc__ = """ |
34 | 34 | Module to calculate a set of ROIs on a stack of data. |
35 | 35 | """ |
| 36 | + |
36 | 37 | import os |
| 38 | +import sys |
| 39 | +import logging |
| 40 | + |
37 | 41 | import numpy |
| 42 | + |
38 | 43 | from PyMca5.PyMcaIO import ConfigDict |
39 | | -import time |
40 | | -import logging |
| 44 | +from PyMca5.PyMca import EDFStack |
| 45 | +from PyMca5.PyMca import ArraySave |
| 46 | +from PyMca5.PyMcaMisc import CliUtils |
41 | 47 |
|
42 | 48 | _logger = logging.getLogger(__name__) |
43 | 49 |
|
@@ -274,113 +280,111 @@ def getFileListFromPattern(pattern, begin, end, increment=None): |
274 | 280 | raise ValueError("Cannot handle more than three indices.") |
275 | 281 | return fileList |
276 | 282 |
|
277 | | -if __name__ == "__main__": |
278 | | - import glob |
279 | | - import sys |
280 | | - from PyMca5.PyMca import EDFStack |
281 | | - from PyMca5.PyMca import ArraySave |
282 | | - import getopt |
283 | | - _logger.setLevel(logging.DEBUG) |
284 | | - options = '' |
285 | | - longoptions = ['cfg=', 'outdir=', |
286 | | - 'tif=', #'listfile=', |
287 | | - 'filepattern=', 'begin=', 'end=', 'increment=', |
288 | | - "outfileroot="] |
289 | | - try: |
290 | | - opts, args = getopt.getopt( |
291 | | - sys.argv[1:], |
292 | | - options, |
293 | | - longoptions) |
294 | | - except Exception: |
295 | | - _logger.error(sys.exc_info()[1]) |
296 | | - sys.exit(1) |
297 | | - fileRoot = "" |
298 | | - outputDir = None |
299 | | - fileindex = 0 |
300 | | - filepattern=None |
301 | | - begin = None |
302 | | - end = None |
303 | | - increment=None |
304 | | - tif=0 |
305 | | - for opt, arg in opts: |
306 | | - if opt in ('--cfg'): |
307 | | - configurationFile = arg |
308 | | - elif opt in '--begin': |
309 | | - if "," in arg: |
310 | | - begin = [int(x) for x in arg.split(",")] |
311 | | - else: |
312 | | - begin = [int(arg)] |
313 | | - elif opt in '--end': |
314 | | - if "," in arg: |
315 | | - end = [int(x) for x in arg.split(",")] |
316 | | - else: |
317 | | - end = int(arg) |
318 | | - elif opt in '--increment': |
319 | | - if "," in arg: |
320 | | - increment = [int(x) for x in arg.split(",")] |
321 | | - else: |
322 | | - increment = int(arg) |
323 | | - elif opt in '--filepattern': |
324 | | - filepattern = arg.replace('"', '') |
325 | | - filepattern = filepattern.replace("'", "") |
326 | | - elif opt in '--outdir': |
327 | | - outputDir = arg |
328 | | - elif opt in '--outfileroot': |
329 | | - fileRoot = arg |
330 | | - elif opt in ['--tif', '--tiff']: |
331 | | - tif = int(arg) |
332 | | - if filepattern is not None: |
333 | | - if (begin is None) or (end is None): |
334 | | - raise ValueError(\ |
335 | | - "A file pattern needs at least a set of begin and end indices") |
336 | | - if filepattern is not None: |
337 | | - fileList = getFileListFromPattern(filepattern, begin, end, increment=increment) |
338 | | - else: |
339 | | - fileList = args |
340 | | - if len(fileList): |
341 | | - dataStack = EDFStack.EDFStack(fileList, dtype=numpy.float32) |
| 283 | + |
| 284 | +def main(args): |
| 285 | + if args.filepattern is not None: |
| 286 | + if args.begin is None or args.end is None: |
| 287 | + raise ValueError( |
| 288 | + "A file pattern needs at least a set of begin and end indices" |
| 289 | + ) |
| 290 | + fileList = getFileListFromPattern( |
| 291 | + args.filepattern, |
| 292 | + args.begin, |
| 293 | + args.end, |
| 294 | + increment=args.increment, |
| 295 | + ) |
342 | 296 | else: |
343 | | - print("OPTIONS:", longoptions) |
344 | | - sys.exit(0) |
345 | | - if outputDir is None: |
| 297 | + fileList = args.files |
| 298 | + |
| 299 | + if not fileList: |
| 300 | + print("No input files provided") |
| 301 | + return 0 |
| 302 | + |
| 303 | + dataStack = EDFStack.EDFStack(fileList, dtype=numpy.float32) |
| 304 | + |
| 305 | + if args.outdir is None: |
346 | 306 | print("RESULTS WILL NOT BE SAVED: No output directory specified") |
347 | | - t0 = time.time() |
| 307 | + |
348 | 308 | worker = StackROIBatch() |
349 | | - worker.setConfigurationFile(configurationFile) |
| 309 | + worker.setConfigurationFile(args.configurationFile) |
350 | 310 | result = worker.batchROIMultipleSpectra(y=dataStack) |
351 | | - if outputDir is not None: |
352 | | - imageNames = result['names'] |
353 | | - images = result['images'] |
354 | | - nImages = images.shape[0] |
355 | | - |
356 | | - if fileRoot in [None, ""]: |
357 | | - fileRoot = "images" |
358 | | - if not os.path.exists(outputDir): |
359 | | - os.mkdir(outputDir) |
360 | | - imagesDir = os.path.join(outputDir, "IMAGES") |
361 | | - if not os.path.exists(imagesDir): |
362 | | - os.mkdir(imagesDir) |
363 | | - imageList = [None] * (nImages) |
364 | | - fileImageNames = [None] * (nImages) |
365 | | - j = 0 |
366 | | - for i in range(nImages): |
367 | | - name = imageNames[i].replace(" ", "-") |
368 | | - fileImageNames[j] = name |
369 | | - imageList[j] = images[i] |
370 | | - j += 1 |
371 | | - fileName = os.path.join(imagesDir, fileRoot+".edf") |
372 | | - ArraySave.save2DArrayListAsEDF(imageList, fileName, |
373 | | - labels=fileImageNames) |
374 | | - fileName = os.path.join(imagesDir, fileRoot+".csv") |
375 | | - ArraySave.save2DArrayListAsASCII(imageList, fileName, csv=True, |
376 | | - labels=fileImageNames) |
377 | | - if tif: |
378 | | - i = 0 |
379 | | - for i in range(len(fileImageNames)): |
380 | | - label = fileImageNames[i] |
381 | | - fileName = os.path.join(imagesDir, |
382 | | - fileRoot + fileImageNames[i] + ".tif") |
383 | | - ArraySave.save2DArrayListAsMonochromaticTiff([imageList[i]], |
384 | | - fileName, |
385 | | - labels=[label], |
386 | | - dtype=numpy.float32) |
| 311 | + |
| 312 | + if args.outdir is None: |
| 313 | + print("No output directory provided") |
| 314 | + return 0 |
| 315 | + |
| 316 | + imageNames = result["names"] |
| 317 | + images = result["images"] |
| 318 | + nImages = images.shape[0] |
| 319 | + |
| 320 | + if not fileRoot: |
| 321 | + fileRoot = "images" |
| 322 | + |
| 323 | + os.makedirs(args.outdir, exist_ok=True) |
| 324 | + imagesDir = os.path.join(args.outdir, "IMAGES") |
| 325 | + os.makedirs(imagesDir, exist_ok=True) |
| 326 | + |
| 327 | + imageList = [] |
| 328 | + fileImageNames = [] |
| 329 | + |
| 330 | + for i in range(nImages): |
| 331 | + name = imageNames[i].replace(" ", "-") |
| 332 | + fileImageNames.append(name) |
| 333 | + imageList.append(images[i]) |
| 334 | + |
| 335 | + # Save EDF |
| 336 | + fileName = os.path.join(imagesDir, fileRoot + ".edf") |
| 337 | + ArraySave.save2DArrayListAsEDF( |
| 338 | + imageList, |
| 339 | + fileName, |
| 340 | + labels=fileImageNames, |
| 341 | + ) |
| 342 | + |
| 343 | + # Save CSV |
| 344 | + fileName = os.path.join(imagesDir, fileRoot + ".csv") |
| 345 | + ArraySave.save2DArrayListAsASCII( |
| 346 | + imageList, |
| 347 | + fileName, |
| 348 | + csv=True, |
| 349 | + labels=fileImageNames, |
| 350 | + ) |
| 351 | + |
| 352 | + # Optional TIFF |
| 353 | + if args.tif: |
| 354 | + for i, label in enumerate(fileImageNames): |
| 355 | + fileName = os.path.join( |
| 356 | + imagesDir, |
| 357 | + fileRoot + label + ".tif" |
| 358 | + ) |
| 359 | + ArraySave.save2DArrayListAsMonochromaticTiff( |
| 360 | + [imageList[i]], |
| 361 | + fileName, |
| 362 | + labels=[label], |
| 363 | + dtype=numpy.float32, |
| 364 | + ) |
| 365 | + |
| 366 | + return 0 |
| 367 | + |
| 368 | + |
| 369 | +def build_parser(): |
| 370 | + parser = CliUtils.create_parser(description="Stack ROI Batch Processing") |
| 371 | + |
| 372 | + parser.add_argument("--cfg", type=str, dest="configurationFile") |
| 373 | + parser.add_argument("--outdir", type=str, default=None) |
| 374 | + parser.add_argument("--outfileroot", type=str, dest="fileRoot", default=None) |
| 375 | + |
| 376 | + parser.add_argument("--filepattern", type=str, default=None, help="File pattern") |
| 377 | + parser.add_argument("--begin", type=CliUtils.int_or_list, default=None, help="Begin index/indices, comma-separated") |
| 378 | + parser.add_argument("--end", type=CliUtils.int_or_list, default=None, help="End index/indices, comma-separated") |
| 379 | + parser.add_argument("--increment", type=CliUtils.int_or_list, default=None, help="Increment(s), comma-separated") |
| 380 | + |
| 381 | + parser.add_argument("--tif", type=int, default=0) |
| 382 | + |
| 383 | + parser.add_argument("files", nargs="*") |
| 384 | + |
| 385 | + return parser |
| 386 | + |
| 387 | + |
| 388 | +if __name__ == "__main__": |
| 389 | + exit_code = CliUtils.cli_main(main, build_parser(), loggers=(_logger,)) |
| 390 | + sys.exit(exit_code) |
0 commit comments