コード例 #1
0
ファイル: pftree.py プロジェクト: FNNDSC/pftree
    def outputSaveCallback(self, at_data, **kwargs):
        """
        Test method for outputSaveCallback

        Simply writes a file in the output tree corresponding
        to the number of files in the input tree.
        """
        path = at_data[0]
        d_outputInfo = at_data[1]
        other.mkdir(self.str_outputDir)
        filesSaved = 0
        other.mkdir(path)
        if not self.testType:
            str_outfile = '%s/file-ls.txt' % path
        else:
            str_outfile = '%s/file-count.txt' % path

        with open(str_outfile, 'w') as f:
            self.dp.qprint("saving: %s" % (str_outfile), level=5)
            if not self.testType:
                f.write('%s`' % self.pp.pformat(d_outputInfo['l_file']))
            else:
                f.write('%d\n' % d_outputInfo['filesAnalyzed'])
        filesSaved += 1

        return {
            'status': True,
            'outputFile': str_outfile,
            'filesSaved': filesSaved
        }
コード例 #2
0
ファイル: pfdo_run.py プロジェクト: FNNDSC/pfdo_run
    def exec(self) -> dict:
        """
        The main entry point for connecting methods of this class
        to the appropriate callbacks of the `pftree.tree_process()`.
        Note that the return json of each callback is available to
        the next callback in the queue as the second tuple value in
        the first argument passed to the callback.
        """
        d_exec     : dict    = {}

        other.mkdir(self.args['outputDir'])
        d_exec     = self.pf_tree.tree_process(
                            inputReadCallback       = self.inputReadCallback,
                            analysisCallback        = self.inputAnalyzeCallback,
                            outputWriteCallback     = None,
                            persistAnalysisResults  = False
        )
        return d_exec
コード例 #3
0
    def outputSaveCallback(self, *args, **kwags) -> dict:
        """
        Callback stub for saving outputs. Here, we simply
        "touch" each file in the analyzed list to the output
        tree.

        This dummy method is mostly for illustration.
        """

        str_outputPath: str = ""
        d_inputAnalyzeCallback: dict = {}
        filesSaved: int = 0
        b_status: bool = False
        str_fileToSave: str = ""

        if len(args):
            at_data = args[0]
            str_outputPath = at_data[0]
            d_inputAnalyzeCallback = at_data[1]

        if 'l_fileAnalyzed' in d_inputAnalyzeCallback.keys() and \
        len(str_outputPath):
            other.mkdir(self.args['outputDir'])
            other.mkdir(str_outputPath)
            for f in d_inputAnalyzeCallback['l_fileAnalyzed']:
                str_fileToSave = os.path.join(str_outputPath, f)
                if os.path.exists(str_fileToSave):
                    if self.args['overwrite']: os.remove(str_fileToSave)
                    else:
                        error.warn(self, 'outputFileExists', drawBox=True)
                        b_status = False
                        break
                os.mknod('%s/%s' % (str_outputPath, f))
                b_status = True
                self.dp.qprint("saving: %s/%s" % (str_outputPath, f), level=5)
                filesSaved += 1

        return {
            'status': b_status,
            'filesSaved': filesSaved,
            'overwrite': self.args['overwrite']
        }
コード例 #4
0
ファイル: pfdo_run.py プロジェクト: FNNDSC/pfdo_run
    def inputReadCallback(self, *args, **kwargs):
        """
        This method does not actually read in any files, but
        exists to preserve the list of files associated with a
        given input directory.

        By preserving and returning this file list, the next
        callback function in this pipeline is able to receive an
        input path and a list of files in that path.
        """
        str_path            : str       = ''
        l_fileProbed        : list      = []
        b_status            : bool      = True
        filesProbed         : int       = 0
        str_outputWorkingDir: str       = ""

        if len(args):
            at_data         = args[0]
            str_path        = at_data[0]
            l_fileProbed    = at_data[1]

        # Need to create the output dir appropriately here!
        str_outputWorkingDir    = str_path.replace(
                                        self.args['inputDir'],
                                        self.args['outputDir']
        )
        self.dp.qprint("mkdir %s" % str_outputWorkingDir,
                        level = 3)
        other.mkdir(str_outputWorkingDir)

        if not len(l_fileProbed): b_status = False
        l_fileProbed.sort()

        return {
            'status':           b_status,
            'l_fileProbed':     l_fileProbed,
            'str_path':         str_path,
            'filesProbed':      filesProbed
        }
コード例 #5
0
    def outputSaveCallback(self, at_data, **kwags):
        """
        Callback for saving outputs.

        In order to be thread-safe, all directory/file
        descriptors must be *absolute* and no chdir()'s
        must ever be called!
        """

        path = at_data[0]
        d_outputInfo = at_data[1]
        str_cwd = os.getcwd()
        other.mkdir(self.str_outputDir)
        filesSaved = 0
        other.mkdir(path)

        for f, ds in zip(d_outputInfo['l_file'], d_outputInfo['l_dcm']):
            ds.save_as('%s/%s' % (path, f))
            self.dp.qprint("saving: %s/%s" % (path, f), level=5)
            filesSaved += 1

        return {'status': True, 'filesSaved': filesSaved}
コード例 #6
0
    def outputSaveCallback(self, at_data, **kwargs):
        """

        Callback for saving outputs.

        In order to be thread-safe, all directory/file
        descriptors must be *absolute* and no chdir()'s
        must ever be called!

        The input 'data' is the return dictionary from the
        inputAnalyzeCallback method.

        """
        def html_make(str_inputFile, str_rawContent, *args):
            str_img = ""
            if self.b_convertToImg:
                str_img = '''
                    <div style="text-align: center;"><img src='%s'></div>
                ''' % args[0]

            str_headContent = '''
                    <title>DCM tags: %s</title>
                    <style>
                    .perSeriesOverview {
                        color: white;
                        background-color: #1d1f21;
                    }
                    </style>
            ''' % str_inputFile

            str_bodyContent = '''
            <div class="perSeriesOverview">
                %s
                <div style="text-align:left">
                    <pre>
                %s
                    </pre>
                </div>
            </div>
            ''' % (str_img, "\n" + str_rawContent)

            htmlPage = '''
                <!DOCTYPE html>
                <html>
                <head>
                %s
                </head>
                <body>
                %s
                </body>
                </html> ''' % (str_headContent, str_bodyContent)
            return htmlPage, str_bodyContent

        path = at_data[0]
        d_outputInfo = at_data[1]
        str_outputImageFile = ""
        d_convertToImg = {}
        str_cwd = os.getcwd()
        other.mkdir(self.str_outputDir)
        filesSaved = 0
        other.mkdir(path)

        # pudb.set_trace()

        if self.b_printToScreen:
            print(d_outputInfo['dstr_result']['raw'])

        if self.b_convertToImg:
            d_convertToImg      = self.img_create(d_outputInfo\
                                                    ['d_DCMfileRead']
                                                    ['d_DICOM'], path)
            str_outputImageFile = d_convertToImg['str_outputImageFile']
        for str_outputFormat in self.l_outputFileType:
            str_fileStem = d_outputInfo['d_DCMfileRead']['outputFileStem']
            if len(str_fileStem):
                if str_outputFormat == 'json':
                    str_fileName = str_fileStem + '.json'
                    with open('%s/%s' % (path, str_fileName), 'w') as f:
                        f.write(d_outputInfo['dstr_result']['json'])
                    self.dp.qprint('Saved report file: %s' % str_fileName,
                                   level=5)
                    filesSaved += 1
                if str_outputFormat == 'dict':
                    str_fileName = str_fileStem + '-dict.txt'
                    with open('%s/%s' % (path, str_fileName), 'w') as f:
                        f.write(d_outputInfo['dstr_result']['dict'])
                    self.dp.qprint('Saved report file: %s' % str_fileName,
                                   level=5)
                    filesSaved += 1
                if str_outputFormat == 'col':
                    str_fileName = str_fileStem + '-col.txt'
                    with open('%s/%s' % (path, str_fileName), 'w') as f:
                        f.write(d_outputInfo['dstr_result']['col'])
                    self.dp.qprint('Saved report file: %s' % str_fileName,
                                   level=5)
                    filesSaved += 1
                if str_outputFormat == 'raw':
                    str_fileName = str_fileStem + '-raw.txt'
                    with open('%s/%s' % (path, str_fileName), 'w') as f:
                        f.write(d_outputInfo['dstr_result']['raw'])
                    self.dp.qprint('Saved report file: %s' % str_fileName,
                                   level=5)
                    filesSaved += 1
                if str_outputFormat == 'html':
                    str_fileName = str_fileStem + '.html'
                    str_bodyName = str_fileStem + '-body.html'
                    if self.b_useIndexhtml:
                        str_fileName = 'index.html'
                    str_htmlContent, str_bodyOnly = \
                        html_make(  d_outputInfo['d_DCMfileRead']['inputFilename'],
                                    d_outputInfo['dstr_result']['raw'],
                                    str_outputImageFile)
                    with open('%s/%s' % (path, str_fileName), 'w') as f:
                        f.write(str_htmlContent)
                        self.dp.qprint('Saved report file: %s' % str_fileName,
                                       level=5)
                    with open('%s/%s' % (path, str_bodyName), 'w') as f:
                        f.write(str_bodyOnly)
                        self.dp.qprint('Saved report file: %s' % str_bodyOnly,
                                       level=5)
                    filesSaved += 2
                if str_outputFormat == 'csv':
                    str_fileName = str_fileStem + '-csv.txt'
                    with open('%s/%s' % (path, str_fileName), 'w') as f:
                        w = csv.DictWriter(
                            f, d_outputInfo['d_DCMfileRead']['d_DICOM']
                            ['d_json'].keys())
                        w.writeheader()
                        w.writerow(
                            d_outputInfo['d_DCMfileRead']['d_DICOM']['d_json'])
                    self.dp.qprint('Saved report file: %s' % str_fileName,
                                   level=5)
                    filesSaved += 1
        return {'status': True, 'filesSaved': filesSaved}
コード例 #7
0
ファイル: tsmake.py プロジェクト: FNNDSC/tslide
    def run(self, *args, **kwargs):
        """
        DESC
            The run method is the main entry point to the operational
            behaviour of the script.

        INPUT
            [timerStart      = True|False]

        RETURN
        {
            'status':       True|False
        }
        """

        b_status = True
        d_env = self.env_check()
        b_timerStart = False
        d_inputFile = {}
        d_slides = {}
        d_html = {}
        d_assemble = {}
        d_ret = {}
        numSlides = 0

        self.dp.qprint("Starting tsmake run... ", level=1)

        for k, v in kwargs.items():
            if k == 'timerStart': b_timerStart = bool(v)

        if b_timerStart:
            other.tic()

        # Process an optional input file to split into slides
        d_inputFile = self.slidesFile_break()

        if d_inputFile['status']:
            # read input slides
            d_slides = self.slide_filesRead(directory=self.str_inputDir)
            numSlides = d_slides['numSlides']
            # read html components
            if d_slides['status']:
                d_html = self.htmlSnippets_read()

                # assemble the HTML page
                if d_html['status']:
                    d_assemble = self.htmlPage_assemble()

                    # now create the output dir
                    other.mkdir(self.str_outputDir)

                    # write the index.html file
                    with open('%s/index.html' % self.str_outputDir, "w") as fp:
                        fp.write(d_assemble['pageHTML'])

                    # and copy necessary dirs
                    l_supportDirs = [
                        './css', './fortunes', './images', './js', './logos'
                    ]
                    l_userDirs = []
                    if len(self.str_additionalDirList):
                        l_userDirs = self.str_additionalDirList.split(',')
                    for str_dir in l_supportDirs + l_userDirs:
                        self.dp.qprint("Copying dir %s... to %s/%s" % \
                            (
                                str_dir,
                                self.str_outputDir,
                                os.path.basename(str_dir)
                            ),
                            level = 2)
                        copy_tree(
                            '%s' % str_dir, '%s/%s' %
                            (self.str_outputDir, os.path.basename(str_dir)))

        d_ret = {
            'status': b_status,
            'd_env': d_env,
            'd_inputFile': d_inputFile,
            'd_slides': d_slides,
            'd_html': d_html,
            'd_assemble': d_assemble,
            'numSlides': numSlides,
            'runTime': other.toc()
        }

        self.dp.qprint('Returning from tslide run...', level=1)
        return d_ret