@@ -433,7 +433,7 @@ def inherit_parameters(Class, *ChildClasses):
433
433
if isinstance (attr ,luigi .Parameter ) and not hasattr (Class , key ):
434
434
setattr (Class ,key , attr )
435
435
436
- def outputfrominput (self , inputformat , stripextension , addextension , outputdirparam = 'outputdir' ):
436
+ def outputfrominput (self , inputformat , stripextension , addextension , replaceinputdirparam = 'replaceinputdir' , outputdirparam = 'outputdir' ):
437
437
"""Derives the output filename from the input filename, removing the input extension and adding the output extension. Supports outputdir parameter."""
438
438
439
439
if not hasattr (self ,'in_' + inputformat ):
@@ -448,15 +448,23 @@ def outputfrominput(self, inputformat, stripextension, addextension, outputdirpa
448
448
if hasattr (self ,outputdirparam ):
449
449
outputdir = getattr (self ,outputdirparam )
450
450
if outputdir and outputdir != '.' :
451
- return TargetInfo (self , os .path .join (outputdir , os .path .basename (replaceextension (inputfilename , stripextension ,addextension ))))
452
- return TargetInfo (self , replaceextension (inputfilename , stripextension ,addextension ))
451
+ if hasattr (self , replaceinputdirparam ):
452
+ replaceinputdir = getattr (self ,replaceinputdirparam )
453
+ if replaceinputdir :
454
+ if inputfilename .startswith (replaceinputdir ):
455
+ return TargetInfo (self , os .path .join (outputdir , os .path .basename (replaceextension (inputfilename [len (replaceinputdir ):], stripextension ,addextension ))))
456
+ else :
457
+ return TargetInfo (self , os .path .join (outputdir , os .path .basename (replaceextension (inputfilename , stripextension ,addextension ))))
458
+ else :
459
+ return TargetInfo (self , replaceextension (inputfilename , stripextension ,addextension ))
453
460
454
461
455
462
class StandardWorkflowComponent (WorkflowComponent ):
456
463
"""A workflow component that takes one inputfile"""
457
464
458
465
inputfile = luigi .Parameter ()
459
466
outputdir = luigi .Parameter (default = "" )
467
+ replaceinputdir = luigi .Parameter (default = "" )
460
468
461
469
class TargetInfo (sciluigi .TargetInfo ):
462
470
pass
@@ -479,6 +487,36 @@ def __init__(self, *args, **kwargs):
479
487
def __hash__ (self ):
480
488
return hash (tuple (sorted (self .items ())))
481
489
490
+ class ParallelBatch (luigi .Task ):
491
+ """Meta workflow"""
492
+ inputfiles = luigi .Parameter ()
493
+ component = luigi .Parameter ()
494
+ passparameters = luigi .Parameter (default = PassParameters ())
495
+
496
+ def requires (self ):
497
+ if isinstance (self .passparameters , str ):
498
+ self .passparameters = PassParameters (json .loads (self .passparameters .replace ("'" ,'"' )))
499
+ elif isinstance (self .passparameters , dict ):
500
+ self .passparameters = PassParameters (self .passparameters )
501
+ elif not isinstance (self .passparameters , PassParameters ):
502
+ raise TypeError ("Keywork argument passparameters must be instance of PassParameters, got " + repr (self .passparameters ))
503
+ tasks = []
504
+ ComponentClass = getcomponentclass (self .component )
505
+ if isinstance (self .inputfiles , str ):
506
+ self .inputfiles = self .inputfiles .split (',' )
507
+ for inputfile in self .inputfiles :
508
+ tasks .append ( ComponentClass (inputfile = inputfile ,** self .passparameters ))
509
+ return tasks
510
+
511
+ def run (self ):
512
+ if isinstance (self .inputfiles , str ):
513
+ self .inputfiles = self .inputfiles .split (',' )
514
+ with self .output ().open ('w' ) as f :
515
+ f .write ("\n " .join (self .inputfiles ))
516
+
517
+ def output (self ):
518
+ return luigi .LocalTarget ('.parallelbatch-' + self .component + '-' + str (hash (self )) + '.done' )
519
+
482
520
class Parallel (sciluigi .WorkflowTask ):
483
521
"""Meta workflow"""
484
522
inputfiles = luigi .Parameter ()
0 commit comments