def __init__(self, **kwargs): med2image.__init__(self, **kwargs) self.l_dcmFileNames = sorted(glob.glob('%s/*.dcm' % self._str_inputDir)) self.slices = len(self.l_dcmFileNames) if self._b_convertMiddleSlice: self._sliceToConvert = int(self.slices/2) self._dcm = dicom.read_file(self.l_dcmFileNames[self._sliceToConvert]) self._str_inputFile = self.l_dcmFileNames[self._sliceToConvert] str_outputFile = self.l_dcmFileNames[self._sliceToConvert] if not self._str_outputFileStem.startswith('%'): self._str_outputFileStem, ext = os.path.splitext(self.l_dcmFileNames[self._sliceToConvert]) if not self._b_convertMiddleSlice and self._sliceToConvert != -1: self._dcm = dicom.read_file(self.l_dcmFileNames[self._sliceToConvert]) else: self._dcm = dicom.read_file(self._str_inputFile) if self._sliceToConvert == -1: self._b_3D = True self._dcm = dicom.read_file(self._str_inputFile) image = self._dcm.pixel_array shape2D = image.shape #print(shape2D) self._Vnp_3DVol = np.empty( (shape2D[0], shape2D[1], self.slices) ) i = 0 for img in self.l_dcmFileNames: self._dcm = dicom.read_file(img) image = self._dcm.pixel_array #print('%s: %s\n' % (img, image.shape)) try: self._Vnp_3DVol[:,:,i] = image except Exception, e: error.fatal(self, 'dcmInsertionFail', '\nFor input DICOM file %s\n%s\n' % (img, str(e))) i += 1
def emit_Data(self, inst): if inst.name: self.data(LABEL(name=inst.name)) if inst.type == 'str': self.data(DB(val=util.cstr(inst.data))) else: error.fatal("Don't know how to emit data %r", inst.type)
def f_stage3callback(**kwargs): str_cwd = os.getcwd() lst_subj = [] for key, val in kwargs.iteritems(): if key == 'subj': lst_subj = val if key == 'obj': stage = val for subj in lst_subj: # find the relevant input files in each <subj> dir os.chdir(subj) l_ADC = misc.find(_str_adc) if not l_ADC: error.fatal(pipe_hypothermia, 'noADC') _str_ADCFile = l_ADC[0] _str_outDir = 'outDir' misc.mkdir(_str_outDir) log = stage.log() log('Scheduling masconorm for "%s: ADC"..\n' % (subj)) str_cmd = 'masconorm.py --input %s --mask %s/b0Brain_mask.nii.gz --outStem %s/adc' % ( _str_ADCFile, _str_outDir, _str_outDir) cluster = crun.crun() cluster.echo(False) cluster.echoStdOut(False) cluster.detach(False) cluster(str_cmd, waitForChild=True, stdoutflush=True, stderrflush=True) os.chdir(str_cwd) return True
def execute(self): ''' Run the pipeline, stage by stage. ''' self._log( Colors.CYAN + 'Executing pipeline ' + Colors.PURPLE + '<'+self.name()+'>' + Colors.NO_COLOUR + '...\n') for stage in self._pipeline: if stage.canRun(): self._log(Colors.YELLOW + 'Stage: ' + stage.name() + '\n' + Colors.NO_COLOUR) stage(checkpreconditions=True, runstage=True, checkpostconditions=True) log = stage.log() if self._b_poststdout: log(Colors.LIGHT_GREEN + 'stage specific stdout:\n' + Colors.NO_COLOUR) if not len(stage.stdout()): log(Colors.LIGHT_GREEN + '\t\t(no stage specific stdout)\n' + Colors.NO_COLOUR) else: log('\n' + Colors.LIGHT_GREEN + stage.stdout() + Colors.NO_COLOUR) if self._b_poststderr: log(Colors.LIGHT_RED + 'stage specific stderr:\n' + Colors.NO_COLOUR) if not len(stage.stdout()): log(Colors.LIGHT_RED + '\t\t(no stage specific stderr)\n' + Colors.NO_COLOUR) else: log('\n' + Colors.LIGHT_RED + stage.stderr() + Colors.NO_COLOUR) if stage.exitCode(): error.fatal(self, 'stageError', '%s' % stage.name()) self._log( Colors.CYAN + 'Terminating pipeline ' + Colors.PURPLE + '<'+self.name()+'>' + Colors.NO_COLOUR + '\n')
def f_stage0callback(**kwargs): str_cwd = os.getcwd() lst_subj = [] for key, val in kwargs.iteritems(): if key == 'subj': lst_subj = val if key == 'obj': stage = val for subj in lst_subj: # find the relevant input files in each <subj> dir os.chdir(subj) l_B0 = misc.find(_str_b0) if not l_B0: error.fatal(pipe_hypothermia, 'noB0') _str_B0File = l_B0[0] _str_outDir = '%s/outDir' % os.getcwd() _str_outFile = 'b0Brain.nii' misc.mkdir(_str_outDir) str_prefixCmd = '( cd %s ; ' % (os.getcwd()) log = stage.log() log('Scheduling brain extraction for "%s"...\n' % (subj)) str_cmd = 'bet.py --input %s --output %s/%s' % (_str_B0File, _str_outDir, _str_outFile) #cluster = crun.crun_mosix(cmdPrefix=str_prefixCmd) #cluster = crun.crun_mosix() cluster = crun.crun() #str_ccmd = cluster.echo(True) #log(str_ccmd) cluster.echo(False) cluster.echoStdOut(False) cluster.detach(False) cluster(str_cmd, waitForChild=True, stdoutflush=True, stderrflush=True) if cluster.exitCode(): error.fatal(pipe_hypothermia, 'stageExec', cluster.stderr()) os.chdir(str_cwd) return True
def f_stage0callback(**kwargs): str_cwd = os.getcwd() for key, val in kwargs.iteritems(): if key == 'operand': l_operand = val if key == 'obj': stage = val if key == 'pipe': pipeline = val count = 0 for i in range(1, len(l_operand)): if i == 1: str_previousOutput = l_operand[i-1] str_cumulativeOutput = "%d-%s" % (i, args.output) str_cmd = 'mris_calc -o %s %s %s %s ; cp %s %s' % \ (str_cumulativeOutput, str_previousOutput, args.operation, l_operand[i], str_cumulativeOutput, os.path.basename(str_previousOutput)) str_previousOutput = os.path.basename(str_previousOutput) print str_cmd cluster = crun.crun() cluster.echo(False) cluster.echoStdOut(False) cluster.detach(False) cluster(str_cmd, waitForChild=True, stdoutflush=True, stderrflush=True) if cluster.exitCode(): error.fatal(pipe_mrisCalc, 'stageExec', cluster.stderr()) count += 1 if not count: error.fatal(pipe_mrisCalc, 'stageExec', "Insufficient operands found!") return True
def mods(decl, modifiers): if isinstance(decl, list): return [Declarator.mods(d, modifiers) for d in decl] if not decl: decl = Declarator() modifiers = list(modifiers) function = False for m in modifiers: if decl.returns: Declarator.mods(decl.returns, [m]) elif isinstance(m, Array): decl.type.append('array') decl.array.append(m) elif isinstance(m, Arguments): decl.type.append('function') decl.args.extend(m.args) function = True elif isinstance(m, basestring): decl.type.append(m) elif isinstance(m, CompositeType): decl.type.append(m) elif isinstance(m, AttributeList): decl.type.extend(m.attr) else: error.fatal("Don't know how to handle declmods %r", m) if function: decl.returns = Declarator() return decl
def find(self, symbol): for s in self.stack: val = s.find(symbol) if val: return s, val error.fatal('%s: could not resolve %r', self.name, symbol) return None, None
def sizeof(decl): guess = size['int'] a = 0 asz = 1 tsz = None for t in decl.type: if isinstance(t, C.Struct): tmp = symtab.struct.find(t.name) guess = tmp.sizeof() break if isinstance(t, C.Union): tmp = symtab.union.find(t.name) guess = tmp.sizeof() break if t == 'array': expr = decl.array[a].expr val = iemit.const_eval(expr) asz *= val a += 1 continue tsz = size.get(t) if tsz: guess = tsz[0] break if guess: return guess*asz error.fatal('Cannot determine size of %r', decl.name) return 0
def initialize(self): ''' This method provides some "post-constructor" initialization. It is typically called after the constructor and after other class flags have been set (or reset). ''' # Set the stages self._pipeline.stages_canRun(False) lst_stages = list(self._stageslist) for index in lst_stages: stage = self._pipeline.stage_get(int(index)) stage.canRun(True) # Check for FS env variable self._log('Checking on FREESURFER_HOME', debug=9, lw=self._lw) if not os.environ.get('FREESURFER_HOME'): error.fatal(self, 'noFreeSurferEnv') self._log('[ ok ]\n', debug=9, rw=self._rw, syslog=False) for str_subj in self._l_subject: self._log('Checking on subjectDir <%s>' % str_subj, debug=9, lw=self._lw) if os.path.isdir(str_subj): self._log('[ ok ]\n', debug=9, rw=self._rw, syslog=False) else: self._log('[ not found ]\n', debug=9, rw=self._rw, syslog=False) error.fatal(self, 'subjectDirnotExist')
def modrm(self, sym): # return a mod r/m form for sym sym = self.resolve(sym) if sym.reg: return self.usereg(sym) if sym.offset is not None: return 'dword [ebp%+d]' % self.offset(sym) error.fatal('Symbol %r not in register and has no offset', sym.name) return self.usereg(sym)
def sizeptr(sz, ptr): if sz is None: return ptr if sz == 1: return "byte %s" % ptr if sz == 2: return "word %s" % ptr if sz == 4: return ptr error.fatal("Unknown size %s for %s", sz, ptr)
def allocreg(self, sym, reg=None): # Allocate a register for sym (possibly a specific register) sym = self.resolve(sym) if sym.reg is None: error.info("%40s Allocating reg for %s (count=%d)", "", sym.name, sym.count) self.getreg(reg, sym) error.info("%40s reg=%s (count=%d)", "", sym.reg, sym.count) else: error.fatal('Symbol %r already using register %r', sym.name, sym.reg) return sym.reg
def user_attachUserTree(self, **kwargs): """ Attaches the feed tree of the given user to a convenience member variable, self.userTree. If the DB is dynamic or newly created, then a baseline user feed is generated; otherwise, the user feed from a disk DB is "grafted" to self.userTree. :param kwargs: :return: """ astr_user = '******' """Attach the feed tree for this user""" for key, val in kwargs.iteritems(): if key == 'user': astr_user = val.translate(None, '\'\"') # Get the user's feed tree structure -- we only need to # do this *ONCE* per session/replay. if not self.userTree: if self.b_createNewDB: userTree = user.UserTree_chrisUser( user=astr_user, constructAllFeeds=True, numberOfFeeds=self.b_createNewDB) u = userTree._userTree # and attach it to the stree of this object if self.DB.cd('/users/%s' % astr_user)['status']: if not self.DB.graft(u, '/chris/feeds'): error.fatal(self, 'no_feedTree2Main') if not self.DB.graft(u, '/chris/plugins'): error.fatal(self, 'no_pluginTree2Main') self.userTree = userTree else: error.fatal(self, 'no_userInTree') else: # Read from existing DB # The DB has already been created (and initial access tested with DB_build()... # we just need to add this user's feed and plugin tree to the base structure userFeed hook... self.userTree = user.UserTree(dummyInternalsBuild=False) self.userTree = user.UserTree(dummyInternalsBuild=False) u = self.userTree._userTree if u.cd('/')['status']: if not u.graft(self.DB, '/users/%s' % astr_user): error_fatal(self, 'no_treeRead') if self.userTree.FT._feedTree.cd('/')['status']: if not self.userTree.FT._feedTree.graft( self.DB, '/users/%s/feeds' % astr_user): error.fatal(self, 'no_main2feedTree') if self.userTree.PT._pluginTree.cd('/')['status']: if not self.userTree.PT._pluginTree.graft( self.DB, '/users/%s/plugins' % astr_user): error.fatal(self, 'no_main2pluginTree')
def stageShell_createRemoteInstance(self, astr_remoteHPC, **kwargs): ''' Returns a crun object in the passed stage object that functions as a shell on the remote HPC. ''' for key, val in kwargs.iteritems(): if key == 'stage': stage = val for case in misc.switch(astr_remoteHPC): if case('PICES'): stage.shell(crun.crun_hpc_mosix( remoteUser="******", remoteHost="rc-majesty.tch.harvard.edu") ) stage.shell().emailUser('*****@*****.**') b_jobDetach = True b_disassocaite = True b_waitForChild = False break if case('launchpad'): stage.shell(crun.crun_hpc_launchpad( remoteUser="******", remoteHost="fnndsc:7774") ) b_jobDetach = False b_disassocaite = False b_waitForChild = True break if case('erisone'): stage.shell(crun.crun_hpc_lsf( remoteUser="******", remoteHost="fnndsc:7773") ) stage.shell().scheduleHostOnly( "cmu058 cmu059 cmu061 cmu066 cmu067 cmu071 cmu073 cmu075 cmu077 cmu079 cmu081 cmu087 cmu090 cmu093 cmu094 cmu095 cmu096 cmu102 cmu106 cmu107 cmu108 cmu109 cmu111 cmu112 cmu114 cmu121 cmu123 cmu126 cmu149 cmu154 cmu156 cmu157 " ) b_jobDetach = False b_disassocaite = False b_waitForChild = True break if case(): error.fatal(self, 'noClusterSpec') shell = stage.shell() shell.emailWhenDone(True) if args.b_debug: shell.echo(True) shell.echoStdOut(True) else: shell.echo(False) shell.echoStdOut(False) shell.detach(b_jobDetach) shell.disassociate(b_disassocaite) shell.waitForChild(b_waitForChild)
def DB_build(self): """ This is the main entry point for either constructing a new DB or reading one from disk. Checks on the DB access in the case of reading from disk are performed here, and don't need to be replicated in other parts of the code. Basically, if DB_build() returns successfully, no tests on the str_DBpath etc need to be performed again. :return: """ s = self.DB if self.b_createNewDB: s.cd('/') s.cd('../') s.mkcd('users') s.mkcd('chris') s.mkcd('login') s.touch("userName", "chris") s.touch("fullName", "ChRIS User") s.touch("passwd", "chris1234") # The 'feeds' and 'plugins' directories are attached when the user is authenticated. else: if not len(self.str_DBpath): error.fatal(self, 'no_DBPath') if not os.path.isdir(self.str_DBpath): error.fatal(self, 'no_validDBPath') self.debug('Reading data from DB...') client = ChRIS_DB_client.ChRIS_DB_client( id = "1", request = "GET http://" + self.str_APIcall, saveTo = "", loadFrom = "", stdout = False ) client.start() # Wait for response while not client.b_dataReady: pass self.DB = client.stree # Load the whole tree (including feeds) from disk # self.debug('Reading tree from disk... %s' % self.str_DBpath) # self.DB = C_snode.C_stree.tree_load( # pathDiskRoot = self.str_DBpath, # loadJSON = False, # loadPickle = True) self.b_readDB = True
def DB_build(self): """ This is the main entry point for either constructing a new DB or reading one from disk. Checks on the DB access in the case of reading from disk are performed here, and don't need to be replicated in other parts of the code. Basically, if DB_build() returns successfully, no tests on the str_DBpath etc need to be performed again. :return: """ s = self.DB if self.b_createNewDB: s.cd('/') s.cd('../') s.mkcd('users') s.mkcd('chris') s.mkcd('login') s.touch("userName", "chris") s.touch("fullName", "ChRIS User") s.touch("passwd", "chris1234") # The 'feeds' and 'plugins' directories are attached when the user is authenticated. else: if not len(self.str_DBpath): error.fatal(self, 'no_DBPath') if not os.path.isdir(self.str_DBpath): error.fatal(self, 'no_validDBPath') self.debug('Reading data from DB...') client = ChRIS_DB_client.ChRIS_DB_client(id="1", request="GET http://" + self.str_APIcall, saveTo="", loadFrom="", stdout=False) client.start() # Wait for response while not client.b_dataReady: pass self.DB = client.stree # Load the whole tree (including feeds) from disk # self.debug('Reading tree from disk... %s' % self.str_DBpath) # self.DB = C_snode.C_stree.tree_load( # pathDiskRoot = self.str_DBpath, # loadJSON = False, # loadPickle = True) self.b_readDB = True
def __call__(self, *args, **kwargs): ''' :param args: :param kwargs: :return: ''' # print("In __call__") for key, val in kwargs.iteritems(): if key == 'APIcall': self._str_apiCall = val if self._str_apiCall == "<void>": error.fatal(self, 'no_apiCall') # print(self._str_apiCall) self.readCallHistory() self.parseCurrentCall(authmodule=self.auth) return (self.replayCalls())
def deref(decl): d = copyof(decl) d.name = None d.offset = None for i, v in enumerate(d.type): if v in ('pointer', 'array'): break else: error.fatal('Cannot dereference %r', decl.name) d.type.pop(i) if v == 'array': d.array.pop() return d
def __call__(self, *args, **kwargs): ''' :param args: :param kwargs: :return: ''' # print("In __call__") for key,val in kwargs.iteritems(): if key == 'APIcall': self._str_apiCall = val if self._str_apiCall == "<void>": error.fatal(self, 'no_apiCall') # print(self._str_apiCall) self.readCallHistory() self.parseCurrentCall(authmodule = self.auth) return(self.replayCalls())
def __init__(self, *args, **kwargs): # auth is the per-call authentication module self.auth = None # chris is the chris-object that contains this API self.chris = None self._b_clearStateFile = False self._str_stateFile = "" self._str_apiCall = "" self._l_apiCallHistory = [] self.auth = None self.debug = message.Message(logTo='./debug.log') self.debug._b_syslog = True self._log = message.Message() self._log._b_syslog = True self.__name = "ChRIS_RESTAPI" self.user = "" self.hash = "" self.passwd = "" self.authority = "localhost:5555" # JSON return objects self.d_return = {} self.d_call = {} self.d_cmd = {} self.d_auth = {} self.d_API = {} # The returnStore variables control how json objects are # captured from API calls. Essentially the python exec # stores the return value in _str_returnStore and then # print()s it to stdout. self._b_returnStore = False self._str_returnStore = "" for key, val in kwargs.iteritems(): if key == 'stateFile': self._str_stateFile = val if key == 'auth': self.auth = val if key == 'chris': self.chris = val if key == 'authority': self.authority = val self.serverInfo = serverInfo.serverInfo(authority=self.authority) if not self.chris: error.fatal(self, 'no_chrisModuleSpec') if not len(self._str_stateFile): error.fatal(self, 'no_stateFile')
def __init__(self, *args, **kwargs): # auth is the per-call authentication module self.auth = None # chris is the chris-object that contains this API self.chris = None self._b_clearStateFile = False self._str_stateFile = "" self._str_apiCall = "" self._l_apiCallHistory = [] self.auth = None self.debug = message.Message(logTo = './debug.log') self.debug._b_syslog = True self._log = message.Message() self._log._b_syslog = True self.__name = "ChRIS_RESTAPI" self.user = "" self.hash = "" self.passwd = "" self.authority = "localhost:5555" # JSON return objects self.d_return = {} self.d_call = {} self.d_cmd = {} self.d_auth = {} self.d_API = {} # The returnStore variables control how json objects are # captured from API calls. Essentially the python exec # stores the return value in _str_returnStore and then # print()s it to stdout. self._b_returnStore = False self._str_returnStore = "" for key,val in kwargs.iteritems(): if key == 'stateFile': self._str_stateFile = val if key == 'auth': self.auth = val if key == 'chris': self.chris = val if key == 'authority': self.authority = val self.serverInfo = serverInfo.serverInfo(authority = self.authority) if not self.chris: error.fatal(self, 'no_chrisModuleSpec') if not len(self._str_stateFile): error.fatal(self, 'no_stateFile')
def imageFileName_process(str_imageFile): b_OK = False l_indexAndFile = str_imageFile.split(':') if len(l_indexAndFile) == 1: b_OK = True self.str_outputImageFile = l_indexAndFile[0] if len(l_indexAndFile) == 2: b_OK = True self.str_outputImageFile = l_indexAndFile[1] self.str_imageIndex = l_indexAndFile[0] if not b_OK: self.dp.qprint("Invalid image specifier.", comms='error') error.fatal(self, 'imageFileSpecFail') if len(self.str_outputImageFile): self.b_convertToImg = True
def __call__(self, *args, **kwargs): """Entry point mimicking the external call to the web service """ ChRIS_client.callCounter += 1 try: job = self._shell("%s --RPC --APIcall %s --stateFile %s" % (self._str_executable, args[0], self._str_stateFile)) except: error.fatal(self, 'shellFailure', '\nExecuting:\n\t%s\nstdout:\n-->\t%s\nstderr:\n-->%s' % (self._shell._str_cmd, self._shell.stdout(), self._shell.stderr())) if self._shell._exitCode: error.fatal(self, 'shellFailure', '\nExit code failure:\n\t%s\n%s\n%s' % (self._shell._exitCode, self._shell._str_cmd, self._shell.stderr())) return(json.loads(self.stdout()))
def user_attachUserTree(self, **kwargs): """ Attaches the feed tree of the given user to a convenience member variable, self.userTree. If the DB is dynamic or newly created, then a baseline user feed is generated; otherwise, the user feed from a disk DB is "grafted" to self.userTree. :param kwargs: :return: """ astr_user = '******' """Attach the feed tree for this user""" for key, val in kwargs.iteritems(): if key == 'user': astr_user = val.translate(None, '\'\"') # Get the user's feed tree structure -- we only need to # do this *ONCE* per session/replay. if not self.userTree: if self.b_createNewDB: userTree = user.UserTree_chrisUser(user = astr_user, constructAllFeeds = True, numberOfFeeds = self.b_createNewDB) u = userTree._userTree # and attach it to the stree of this object if self.DB.cd('/users/%s' % astr_user)['status']: if not self.DB.graft(u, '/chris/feeds'): error.fatal(self, 'no_feedTree2Main') if not self.DB.graft(u, '/chris/plugins'): error.fatal(self, 'no_pluginTree2Main') self.userTree = userTree else: error.fatal(self, 'no_userInTree') else: # Read from existing DB # The DB has already been created (and initial access tested with DB_build()... # we just need to add this user's feed and plugin tree to the base structure userFeed hook... self.userTree = user.UserTree(dummyInternalsBuild = False) self.userTree = user.UserTree(dummyInternalsBuild = False) u = self.userTree._userTree if u.cd('/')['status']: if not u.graft(self.DB, '/users/%s' % astr_user): error_fatal(self, 'no_treeRead') if self.userTree.FT._feedTree.cd('/')['status']: if not self.userTree.FT._feedTree.graft(self.DB, '/users/%s/feeds' % astr_user): error.fatal(self, 'no_main2feedTree') if self.userTree.PT._pluginTree.cd('/')['status']: if not self.userTree.PT._pluginTree.graft(self.DB, '/users/%s/plugins' % astr_user): error.fatal(self, 'no_main2pluginTree')
def f_stage0callback(**kwargs): lst_subj = [] for key, val in kwargs.iteritems(): if key == 'subj': lst_subj = val if key == 'obj': stage = val if key == 'pipe': pipeline = val lst_hemi = pipeline.l_hemisphere() lst_surface = pipeline.l_surface() lst_curv = pipeline.l_curv() if int(args.partitions) > _maxPartitions: error.fatal(hbwm, 'Partition') for pipeline._str_subj in lst_subj: for pipeline._str_hemi in lst_hemi: for pipeline._str_surface in lst_surface: for pipeline._str_curv in lst_curv: log = stage.log() log('Processing %s: %s.%s, %s...\n' % \ (pipeline.subj(), pipeline.hemi(), pipeline.surface(), pipeline.curv())) str_hostOnlySpec = '' if len(args.host): str_hostOnlySpec = "--host %s " % args.host log('Locking jobs to only run on host -->%s<--\n' % args.host) str_debug = "" if args.b_debug: str_debug = " --debug " str_queue = "" if args.queue: str_queue = " --queue %s " % args.queue str_cmd = "~/src/scripts/hbwm.py -v 10 -s %s %s -r -m %s -f %s -c %s -p %s --cluster %s %s %s %s" % \ (args.stages, str_hostOnlySpec, pipeline.hemi(), pipeline.surface(), pipeline.curv(), args.partitions, args.cluster, str_debug, str_queue, pipeline.subj()) print str_cmd shell = crun.crun() shell.echo(False) shell.echoStdOut(False) shell.detach(False) shell(str_cmd, waitForChild=True, stdoutflush=True, stderrflush=True) if shell.exitCode(): error.fatal(hbwm, 'stageExec', shell.stderr()) os.chdir(pipeline.startDir()) return True
def __call__(self, f, **kwargs): """Call the object/method This functor actually wraps around the call, and is the main entry point to calling ANYTHING from ChRIS. It is here where the authentication of the caller/hash is verified before executing the actual object.method call Args: f (object.method): The object.method to call. Returns: Whatever is returned by the call is returned back. """ db = self.chris._SMCore._userDB str_APIcall = db.str_APIcall str_VERB = db.str_VERB b_createNewDB = db.b_createNewDB d_ret = db.user_checkAPIcanCall(**kwargs) if not d_ret['status'] and d_ret['code'] == 1: error.fatal(self, 'no_loginFound') ret = f() if b_createNewDB: # If a path DB has been specified (and assuming it's a valid path!) # save the DB tree to that path. if os.path.isdir(self.chris.str_DBpath): shutil.rmtree(self.chris.str_DBpath) db.DB.tree_save(startPath = '/', pathDiskRoot = self.chris.str_DBpath, failOnDirExist = False, saveJSON = False, savePickle = True) if len(self.chris.str_DBpath) and str_VERB != 'GET': client = ChRIS_DB_client.ChRIS_DB_client( id = "1", request = "PUSH http://" + str_APIcall, data = dict(db.DB.snode_root) ) client.start() # Wait for response while not client.b_dataReady: pass return ret
def __init__(self, phase_xml): fails = pygame.init()[1] if fails > 0: error.fatal() try: self.screen = pygame.display.set_mode((0,0), FULLSCREEN|DOUBLEBUF) except pygame.error as e: error.fatal(e) self.set_font() self.fps = 0 self.frames = 0 self.last_second = datetime.datetime.now() self.phase = phase.Phase(self, [ExitListener()], None) self.max_fps = 0
def __call__(self, f, **kwargs): """Call the object/method This functor actually wraps around the call, and is the main entry point to calling ANYTHING from ChRIS. It is here where the authentication of the caller/hash is verified before executing the actual object.method call Args: f (object.method): The object.method to call. Returns: Whatever is returned by the call is returned back. """ db = self.chris._SMCore._userDB str_APIcall = db.str_APIcall str_VERB = db.str_VERB b_createNewDB = db.b_createNewDB d_ret = db.user_checkAPIcanCall(**kwargs) if not d_ret['status'] and d_ret['code'] == 1: error.fatal(self, 'no_loginFound') ret = f() if b_createNewDB: # If a path DB has been specified (and assuming it's a valid path!) # save the DB tree to that path. if os.path.isdir(self.chris.str_DBpath): shutil.rmtree(self.chris.str_DBpath) db.DB.tree_save(startPath='/', pathDiskRoot=self.chris.str_DBpath, failOnDirExist=False, saveJSON=False, savePickle=True) if len(self.chris.str_DBpath) and str_VERB != 'GET': client = ChRIS_DB_client.ChRIS_DB_client( id="1", request="PUSH http://" + str_APIcall, data=dict(db.DB.snode_root)) client.start() # Wait for response while not client.b_dataReady: pass return ret
def stage_get(self, index): ''' Return the stage referenced by <index>. The <index> can be specified in several ways: o an integer offset into the pipeline list. o a string "name" of a particular stage. ''' if type(index) is types.IntType: if index >= len(self._pipeline): error.fatal(self, 'stageNotFound') return self._pipeline[index] if type(index) is types.StringType: for stage in self._pipeline: if stage.name() == index: return stage error.fatal(self, 'stageNotFound')
def f_stageShellExitCode(**kwargs): ''' A simple function that returns a conditional based on the exitCode of the passed stage object. It assumes global access to the <pipeline> object. **kwargs: obj=<stage> The stage to query for exitStatus. ''' stage = None for key, val in kwargs.iteritems(): if key == 'obj': stage = val if not stage: error.fatal(pipeline, "noStagePostConditions") if not stage.callCount(): return True if not stage.exitCode(): return True else: return False
def initialize(self): ''' This method provides some "post-constructor" initialization. It is typically called after the constructor and after other class flags have been set (or reset). ''' # Set the stages self._pipeline.stages_canRun(False) lst_stages = list(self._stageslist) for index in lst_stages: stage = self._pipeline.stage_get(int(index)) stage.canRun(True) # Set absolute dir specs for topDir and outDir log = self.log() log('Setting dir specs...\n') os.chdir(self.topDir()) self.topDir(os.path.abspath(self.topDir())) if os.path.isdir(self.outDir()): log('Existing output directory found. Deleting and recreating...\n') shutil.rmtree(self.outDir()) misc.mkdir(self.outDir()) os.chdir(self.outDir()) self.outDir(os.getcwd()) os.chdir(self.topDir()) # check dirs for each subsample dir # print(os.getcwd()) # print(self._l_subsample) for directory in self._l_subsample: if os.path.isdir(os.path.join(self.topDir(), directory)): os.chdir(directory) self._l_subsampleDir.append(os.path.abspath(os.getcwd())) os.chdir(self.topDir()) else: error.fatal(self, 'subsampleDirNotFound') self._d_subsampleDir = dict(zip(self._l_subsample, self._l_subsampleDir)) # print(self._d_subsampleDir) self.dtree_build() self._dtree.tree_metaData_print(False)
def __call__(self, *args, **kwargs): ''' :param args: :param kwargs: :return: ''' currentFrame = inspect.currentframe() callerFrame = inspect.getouterframes(currentFrame, 2) # self.debug("In REST __call__\n%s\n" % (callerFrame)) for key, val in kwargs.iteritems(): if key == 'APIcall': self._str_apiCall = val if self._str_apiCall == "<void>": error.fatal(self, 'no_apiCall') # print(self._str_apiCall) self.debug('Calling REST %s on "%s"...\n' % (self.str_VERB, self._str_apiCall)) self.d_return = self.parseCurrentCall(authmodule=self.auth) return (self.d_return)
def f_stageShellExitCode(**kwargs): ''' A simple function that returns a conditional based on the exitCode of the passed stage object. It assumes global access to the <pipeline> object. **kwargs: obj=<stage> The stage to query for exitStatus. ''' stage = None for key, val in kwargs.iteritems(): if key == 'obj': stage = val if not stage: error.fatal(pipeline, "noStagePostConditions") if not stage.callCount(): return True if stage.exitCode() == "0": return True else: return False
def __init__(self, *args, **kwargs): # auth is the per-call authentication module self.auth = None # chris is the chris-object that contains this API self.chris = None self._str_apiCall = "" self.debug = message.Message(logTo='./debug.log') self.debug._b_syslog = True self._log = message.Message() self._log._b_syslog = True self.__name = "ChRIS_RESTAPI" self.str_APIaction = "GET" # GET or PUT self.str_VERB = '' self.str_payloadFile = '' self.b_PUSH = False self.user = "" self.hash = "" self.passwd = "" self.authority = "localhost:5555" # JSON return objects self.d_return = {} self.d_call = {} self.d_auth = {} self.d_API = {} for key, val in kwargs.iteritems(): if key == 'auth': self.auth = val if key == 'chris': self.chris = val if key == 'authority': self.authority = val if key == 'VERB': self.str_VERB = val if key == 'payloadFile': self.str_payloadFile = val self.debug('VERB: %s\n' % self.str_VERB) self.serverInfo = serverInfo.serverInfo(authority=self.authority) if not self.chris: error.fatal(self, 'no_chrisModuleSpec')
def enter(self, symbol, value, extra=None): #print "Entering %s into %s (value=%s)" % (symbol, self.name, value) if symbol in self.tab: if self.tab[symbol] is not None: if value is None: return error.fatal('%s %r already defined', self.name, symbol) self.tab[symbol] = value if value is None: return value.extra = extra if not value.name: value.name = symbol if self.type in ('stack', 'struct') and symbol[0] != '%': self.alloc(symbol, value) elif self.type == 'union': self.pos = max(self.pos, typeinfo.sizeof(value))
def f_stage0callback(**kwargs): for key, val in kwargs.iteritems(): if key == 'obj': stage = val if key == 'pipe': pipeline = val log = stage._log os.chdir(pipeline._workingDir) if os.path.isdir(pipeline.outDir()) and not pipeline.clobber(): log('Existing outDir tree found... deleting...\n') shutil.rmtree(pipeline.outDir()) OSshell('mkdir -p %s' % pipeline.outDir()) os.chdir(pipeline.outDir()) pipeline.outDir(os.getcwd()) if OSshell.exitCode() != 0: error.fatal(pipeline, 'outDirNotCreate') d_ret = pipeline.innerLoop(pipeline.outputDirTree_build, log="Building output directory tree...\n") stage.exitCode(0) return True
def initialize(self): ''' This method provides some "post-constructor" initialization. It is typically called after the constructor and after other class flags have been set (or reset). ''' # First, this script should only be run on cluster nodes. lst_clusterNodes = [ 'rc-drno', 'rc-russia', 'rc-thunderball', 'rc-goldfinger', 'rc-twice' ] str_hostname = socket.gethostname() # Set the stages self._pipeline.stages_canRun(False) lst_stages = list(self._stageslist) for index in lst_stages: stage = self._pipeline.stage_get(int(index)) stage.canRun(True) # Check for FS env variable self._log('Checking on FREESURFER_HOME', debug=9, lw=self._lw) if not os.environ.get('FREESURFER_HOME'): error.fatal(self, 'noFreeSurferEnv') self._log('[ ok ]\n', debug=9, rw=self._rw, syslog=False) for str_subj in self._l_subject: self._log('Checking on subjectDir <%s>' % str_subj, debug=9, lw=self._lw) if os.path.isdir(str_subj): self._log('[ ok ]\n', debug=9, rw=self._rw, syslog=False) else: self._log('[ not found ]\n', debug=9, rw=self._rw, syslog=False) error.fatal(self, 'subjectDirnotExist')
def __init__(self, **kwargs): med2image.__init__(self, **kwargs) self.l_dcmFileNames = sorted(glob.glob('%s/*.dcm' % self._str_inputDir)) self.slices = len(self.l_dcmFileNames) if self._b_convertMiddleSlice: self._sliceToConvert = int(self.slices / 2) self._dcm = dicom.read_file( self.l_dcmFileNames[self._sliceToConvert], force=True) self._str_inputFile = self.l_dcmFileNames[self._sliceToConvert] str_outputFile = self.l_dcmFileNames[self._sliceToConvert] if not self._str_outputFileStem.startswith('%'): self._str_outputFileStem, ext = os.path.splitext( self.l_dcmFileNames[self._sliceToConvert]) if not self._b_convertMiddleSlice and self._sliceToConvert != -1: self._dcm = dicom.read_file( self.l_dcmFileNames[self._sliceToConvert], force=True) else: self._dcm = dicom.read_file(self._str_inputFile, force=True) if self._sliceToConvert == -1: self._b_3D = True self._dcm = dicom.read_file(self._str_inputFile, force=True) image = self._dcm.pixel_array shape2D = image.shape #print(shape2D) self._Vnp_3DVol = np.empty((shape2D[0], shape2D[1], self.slices)) i = 0 for img in self.l_dcmFileNames: self._dcm = dicom.read_file(img, force=True) image = self._dcm.pixel_array #print('%s: %s\n' % (img, image.shape)) try: self._Vnp_3DVol[:, :, i] = image except Exception, e: error.fatal( self, 'dcmInsertionFail', '\nFor input DICOM file %s\n%s\n' % (img, str(e))) i += 1
def f_stage0callback(**kwargs): str_cwd = os.getcwd() for key, val in kwargs.iteritems(): if key == 'roi': l_roi = val if key == 'obj': stage = val if key == 'pipe': pipeline = val str_cwd = pipeline.workingDir() log = stage.log() lst_pval = pipeline.l_pval() lst_group = pipeline.l_group() lst_hemi = pipeline.l_hemisphere() lst_surface = pipeline.l_surface() lst_statFunc = pipeline.l_statFunc() lst_roi = pipeline.l_roi() lst_ctype = pipeline.l_curvFunc() lst_ctype = ['thickness', 'curv'] lst_leaf = [ 'pval', 'statFunc', 'pval-only', 'statFunc-only', 'pval_N_statFunc' ] os.chdir(str_cwd) if os.path.isdir(pipeline.outDir()) and not pipeline.clobber(): log('Existing outDir tree found... deleting...\n') shutil.rmtree(pipeline.outDir()) OSshell('mkdir -p %s' % pipeline.outDir()) os.chdir(pipeline.outDir()) pipeline.outDir(os.getcwd()) os.chdir(str_cwd) if OSshell.exitCode() != 0: error.fatal(pipeline, 'outDirNotCreate') for pipeline._str_group in lst_group: for pipeline._str_pval in lst_pval: for pipeline._str_statFunc in lst_statFunc: for pipeline._str_surface in lst_surface: for pipeline._str_hemi in lst_hemi: for ctype in lst_ctype: for leaf in lst_leaf: OSshell('mkdir -p %s/%s/%s' % (pipeline.dirSpec(), ctype, leaf)) stage.exitCode(0) return True
def f_stage2callback(**kwargs): str_cwd = os.getcwd() lst_subj = [] for key, val in kwargs.iteritems(): if key == 'subj': lst_subj = val if key == 'obj': stage = val for subj in lst_subj: # find the relevant input files in each <subj> dir os.chdir(subj) l_B0 = misc.find(_str_b0) l_ASL = misc.find(_str_asl) if not l_B0: error.fatal(pipe_hypothermia, 'noB0') if not l_ASL: error.fatal(pipe_hypothermia, 'noASL') _str_B0File = l_B0[0] _str_ASLFile = l_ASL[0] _str_outDir = 'outDir' _str_outFile = 'asl2b0.nii' misc.mkdir(_str_outDir) log = stage.log() log('Scheduling masconorm for "%s: ASL"..\n' % (subj)) str_cmd = 'masconorm.py --input %s/asl2b0.nii.gz --mask %s/b0Brain_mask.nii.gz --outStem %s/asl' % ( _str_outDir, _str_outDir, _str_outDir) #cluster = crun.crun_mosix() cluster = crun.crun() cluster.echo(False) cluster.echoStdOut(False) cluster.detach(False) cluster(str_cmd, waitForChild=True, stdoutflush=True, stderrflush=True) if cluster.exitCode(): error.fatal(pipe_hypothermia, 'stageExec', cluster.stderr()) os.chdir(str_cwd) return True
def parseCurrentCall(self, **kwargs): """Parse the current REST call""" for key, value in kwargs.iteritems(): if key == 'authmodule': auth = value try: if not len(auth._name): error.fatal(self, 'no_authModuleSpec') except: error.fatal(self, 'no_authModuleSpec') URL_parts = urlparse(self._str_apiCall) l_path = URL_parts.path.split('/')[2:] # skip the /vX/! d_query = parse_qs(URL_parts.query) if not len(l_path): error.fatal(self, 'malformedURL', 'URL: %s' % self._str_apiCall) # print(l_path) # print(d_query) if 'auth' in d_query: str_authSpec = d_query['auth'][0] str_authURL = "?%s" % str_authSpec.replace(',', '&') d_auth = parse_qs(urlparse(str_authURL).query) # print(d_auth.keys()) if 'user' in d_auth.keys(): self.user = d_auth['user'][0] if 'hash' in d_auth.keys(): self.hash = d_auth['hash'][0] if 'passwd' in d_auth.keys(): self.passwd = d_auth['passwd'][0] self.parsePathSpec(l_path) return (self.formatReturnJSON())
def f_stage0callback(**kwargs): for key, val in kwargs.iteritems(): if key == 'obj': stage = val if key == 'pipe': pipeline = val log = stage.log() lst_subj = pipeline.l_subj() if os.path.isdir(pipeline.outDir()): log('Existing outDir tree found... deleting...\n') shutil.rmtree(pipeline.outDir()) log('Creating output directory...\n') OSshell('mkdir -p %s' % pipeline.outDir()) print(OSshell.stdout()) if OSshell.exitCode() != 0: error.fatal(pipeline, 'outDirNotCreate') os.chdir(pipeline.outDir()) pipeline.outDir(os.getcwd()) passcount = 0 failcount = 0 total = 0 for pipeline._str_subj in lst_subj: f_cutoff = random.random() log('thresholding %s...' % pipeline._str_subj) if f_cutoff < pipeline.threshold(): OSshell('ln -s %s/%s .' % (pipeline.topDir(), pipeline._str_subj)) log('[ passed ]\n', rw=20, syslog=False) passcount += 1 else: log('[ failed ]\n', rw=20, syslog=False) failcount += 1 total += 1 f_passPerc = float(passcount) / float(total) * 100 f_failPerc = float(failcount) / float(total) * 100 log('passed: %d (%5.2f%s)\n' % (passcount, f_passPerc, '%')) log('failed: %d (%5.2f%s)\n' % (failcount, f_failPerc, '%')) stage.exitCode(0) return True
def execute(self): ''' Run the pipeline, stage by stage. ''' self._log(Colors.CYAN + 'Executing pipeline ' + Colors.PURPLE + '<' + self.name() + '>' + Colors.NO_COLOUR + '...\n') for stage in self._pipeline: if stage.canRun(): self._log(Colors.YELLOW + 'Stage: ' + stage.name() + '\n' + Colors.NO_COLOUR) stage(checkpreconditions=True, runstage=True, checkpostconditions=True) log = stage.log() if self._b_poststdout: log(Colors.LIGHT_GREEN + 'stage specific stdout:\n' + Colors.NO_COLOUR) if not len(stage.stdout()): log(Colors.LIGHT_GREEN + '\t\t(no stage specific stdout)\n' + Colors.NO_COLOUR) else: log('\n' + Colors.LIGHT_GREEN + stage.stdout() + Colors.NO_COLOUR) if self._b_poststderr: log(Colors.LIGHT_RED + 'stage specific stderr:\n' + Colors.NO_COLOUR) if not len(stage.stdout()): log(Colors.LIGHT_RED + '\t\t(no stage specific stderr)\n' + Colors.NO_COLOUR) else: log('\n' + Colors.LIGHT_RED + stage.stderr() + Colors.NO_COLOUR) if stage.exitCode(): error.fatal(self, 'stageError', '%s' % stage.name()) self._log(Colors.CYAN + 'Terminating pipeline ' + Colors.PURPLE + '<' + self.name() + '>' + Colors.NO_COLOUR + '\n')
def emit_Field(node, ea): ret = IList(result=tmpreg()) if node.op == 'field': code = emit(node.expr, ea=True) else: code = emit(node.expr) _, x = symtab.ident.find(code.result) tp = typeinfo.typeof(deref(code.result)) if isinstance(tp, C.Struct): tab = symtab.struct.find(tp.name) elif isinstance(tp, C.Union): tab = symtab.union.find(tp.name) else: error.fatal('Asked for %r in something not struct or union (%r)', node.field, tp) field = tab.find(node.field) if not field: error.fatal('Cannot find field %r in %r', node.field, tp.name) tmp = tmpreg() symtab.ident.enter(tmp, typeinfo.addrof(field)) if isconst(code): ret.append(Constant(target=tmp, val=(code[0].val + field.offset))) else: ret.extend(code) offset = emit(C.Integer(value=field.offset)) ret.append( offset, Add(target=tmp, src0=code.result, src1=offset.result)) if ea: ret.result = tmp else: tp = copyof(field) symtab.ident.enter(ret.result, tp) ret.append(Load(target=ret.result, addr=tmp, size=typeinfo.sizeof(tp), signed=typeinfo.issigned(tp))) return ret
def f_stage0callback(**kwargs): for key, val in kwargs.iteritems(): if key == 'jobs': jobs = val if key == 'obj': stage = val if key == 'pipe': pipeline = val # Create shell for scheduling/executing on the remote HPC pipeline.stageShell_createRemoteInstance(args.cluster, stage=stage) for job in range(0, int(args.jobs)): log = stage.log() log('Processing job: %d...\n' % job) str_cmd = args.cmd shell = stage.shell() shell.echo(True) shell( str_cmd, waitForChild=shell.waitForChild(), stdoutflush=True, stderrflush=True ) if shell.exitCode(): error.fatal(hpc, 'stageExec', shell.stderr()) os.chdir(pipeline.startDir()) return True
def initialize(self): ''' This method provides some "post-constructor" initialization. It is typically called after the constructor and after other class flags have been set (or reset). ''' # First, this script should only be run on cluster nodes. lst_clusterNodes = ['rc-drno', 'rc-russia', 'rc-thunderball', 'rc-goldfinger', 'rc-twice'] str_hostname = socket.gethostname() #if str_hostname not in lst_clusterNodes: #error.fatal(self, 'notClusterNode', 'Current hostname = %s' % str_hostname) # Set the stages self._pipeline.stages_canRun(False) lst_stages = list(self._stageslist) for index in lst_stages: stage = self._pipeline.stage_get(int(index)) stage.canRun(True) # Check for FS env variable self._log('Checking on FREESURFER_HOME', debug=9, lw=self._lw) if not os.environ.get('FREESURFER_HOME'): error.fatal(self, 'noFreeSurferEnv') self._log('[ ok ]\n', debug=9, rw=self._rw, syslog=False) for str_operand in self._l_operands: self._log('Checking on operand <%s>' % str_operand, debug=9, lw=self._lw) if os.path.isfile(str_operand): self._log('[ ok ]\n', debug=9, rw=self._rw, syslog=False) else: self._log('[ not found ]\n', debug=9, rw=self._rw, syslog=False) error.fatal(self, 'operandFilenotExist')
def parseCurrentCall(self, **kwargs): """Parses the apiCall and updates the stateFile. This method parses the <apiCall>, executes it, and updates the call to the stateFile, also adding the parsed call to the self._l_apiCallHistory list. Also parses the authentication. Returns: state (boolean): True if <apiCall> parsed """ str_auth = "" for key, value in kwargs.iteritems(): if key == 'authmodule': auth = value if not len(auth._name): error.fatal(self, 'no_authModuleSpec') # The main URL components str_auth = auth._name str_ret = "" str_object = "" str_method = "" str_parameters = "" d_component = parse_qs(urlparse(self._str_apiCall).query) if 'clearSessionFile' in d_component: self._b_clearStateFile = int(d_component['clearSessionFile'][0]) else: self._b_clearStateFile = False if 'returnstore' in d_component: str_ret = d_component['returnstore'][0] self._b_returnStore = True self._str_returnStore = str_ret else: self._b_returnStore = False self._str_returnStore = "APIreturn" if 'object' in d_component: str_object = d_component['object'][0] str_method = d_component['method'][0] if 'parameters' in d_component: str_parameters = d_component['parameters'][0] if str_method == 'login' or str_method == 'logout': self.str2URL(str_parameters) if len(str_ret): str_ret = "%s=" % str_ret if len(str_object ) and str_method != 'login' and str_method != 'logout': str_object = "%s(lambda: %s." % (str_auth, str_object) if len(str_object) and str_method == 'login' or str_method == 'logout': str_object = '%s.' % str_object # Parse the "auth" components if 'auth' in d_component: str_authSpec = d_component['auth'][0] self.str2URL(str_authSpec) if len(str_parameters): str_eval = "%s%s%s(%s)" % (str_ret, str_object, str_method, str_parameters) else: str_eval = "%s%s%s()" % (str_ret, str_object, str_method) if len(str_object): if str_method != 'login' and str_method != 'logout': str_eval += ", user=%s, hash=%s)" % (self.user, self.hash) self._l_apiCallHistory.append(str_eval) #print(d_component) str_mode = 'a' # print("clear state file = %d" % self._b_clearStateFile) if self._b_clearStateFile: str_mode = 'w' with open(self._str_stateFile, str_mode) as f: f.write("# %s %s\n" % (datetime.datetime.now(), self._str_apiCall)) f.write("%s\n" % str_eval)