pacsretrieve/pacsretrieve.py
def run(self, options):
        """
        Define the code to be run by this plugin app.
        """
        # options.inputdir

        # common options between all request types
        # aet
        # aec
        # aet_listener
        # ip
        # port
        pacs_settings = {
            'aet': options.aet,
            'aec': options.aec,
            'aet_listener': options.aet_listener,
            'server_ip': options.server_ip,
            'server_port': options.server_port
        }

        # echo the PACS to make sure we can access it
        pacs_settings['executable'] = '/usr/bin/echoscu'
        echo = pypx.echo(pacs_settings)
        if echo['status'] == 'error':
            with open(os.path.join(options.outputdir, echo['status'] + '.txt'), 'w') as outfile:
                json.dump(echo, outfile, indent=4, sort_keys=True, separators=(',', ':'))
            return

        # create dummy series file with all series
        series_file = os.path.join(options.inputdir, 'success.txt')
        if options.series_file != '':
            series_file = options.series_file

        # uids to be fetched from success.txt
        uids = options.series_uids.split(',')
        uids_set = set(uids)

        # parser series file
        data_file = open(series_file, 'r')
        data = json.load(data_file)
        data_file.close()
        filtered_uids = [
            series for series in data['data'] if str(series['uid']['value']) in uids_set]

        path_dict = []
        data_directory = options.data_location

        # create destination directories and move series
        pacs_settings['executable'] = '/usr/bin/movescu'

        for series in filtered_uids:
            patient_dir = pypx.utils.patientPath(
                '', series['PatientID']['value'],
                series['PatientName']['value'])
            study_dir = pypx.utils.studyPath(
                patient_dir, series['StudyDescription']['value'],
                series['StudyDate']['value'],
                series['StudyInstanceUID']['value'])
            series_dir = pypx.utils.seriesPath(
                study_dir, series['SeriesDescription']['value'],
                series['SeriesDate']['value'],
                series['SeriesInstanceUID']['value'])

            source = os.path.join(data_directory, series_dir)
            series_info = os.path.join(source, 'series.info')
            destination_study = os.path.join(options.outputdir, study_dir)
            destination_series = os.path.join(options.outputdir, series_dir)

            path_dict.append(
                {'source': source,
                 'destination_study': destination_study,
                 'destination_series': destination_series,
                 'info': series_info})

            # move series
            pacs_settings['series_uid'] = series['SeriesInstanceUID']['value']
            output = pypx.move(pacs_settings)


        print('Receiving data...')

        # wait for files to arrive!
        timer = 0

        while timer < 100: # 1h
            for path in path_dict[:]:

                # what if pulling an existing dataset
                # (.info file already there? need extra flag to force re=pull?)
                if os.path.isfile(path['info']):

                    if not os.path.exists(path['destination_study']):
                        os.makedirs(path['destination_study'])
                    else:
                        print(path['destination_study'] + ' already exists.')

                    # copy series to output
                    shutil.copytree(path['source'], path['destination_series'])

                    # create jpgs directory
                    destination_jpgs = os.path.join(path['destination_series'], 'jpgs')
                    if not os.path.exists(destination_jpgs):
                        os.makedirs(destination_jpgs)
                    else:
                        print(destination_jpgs + ' already exists.')

                    # generate jpgs for all dcm files
                    for filename in os.listdir(path['destination_series']):
                        name, extension = os.path.splitext(filename)
                        if extension == '.dcm':
                            basename = os.path.basename(filename)
                            source = os.path.join(path['destination_series'], basename)
                            output = os.path.join(destination_jpgs, basename)
                            exec_location = os.path.dirname(pacs_settings['executable'])
                            executable = os.path.join(exec_location, 'dcmj2pnm')
                            command = executable + ' +oj +Wh 15 +Fa ' + source + ' ' + output + '.jpg';
                            response = subprocess.run(
                                command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

                    # resize all jpgs
                    executable = '/usr/bin/mogrify'
                    options = '-resize 96x96 -background none -gravity center -extent 96x96'
                    source = os.path.join(destination_jpgs, '*')
                    command = executable + ' ' + options + ' ' + source
                    response = subprocess.run(
                        command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

                    # create preview
                    executable = '/usr/bin/convert'
                    options = '-append'
                    output = os.path.join(path['destination_series'], 'preview.jpg')
                    command = executable + ' ' + options + ' ' + source + ' ' + output
                    response = subprocess.run(
                        command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)

                    # remove from dictionnary
                    path_dict.remove(path)

            if len(path_dict) == 0:
                break

            time.sleep(1)
            timer += 1

        print('Done.')

        Define the code to be run by this plugin app.
        
Define the code to be run by this plugin app.
Define the code to be run by this plugin app .
def run(self, options):
        _listener
        pacs_settings = {
            'aet': options.aet,
            'aec': options.aec,
            'aet_listener': options.aet_listener,
            'server_ip': options.server_ip,
            'server_port': options.server_port
        }
        pacs_settings['executable'] = '/usr/bin/echoscu'
        echo = pypx.echo(pacs_settings)
        if echo['status'] == 'error':
            with open(os.path.join(options.outputdir, echo['status'] + '.txt'), 'w') as outfile:
                json.dump(echo, outfile, indent=4, sort_keys=True, separators=(',', ':'))
            return
        series_file = os.path.join(options.inputdir, 'success.txt')
        if options.series_file != '':
            series_file = options.series_file
        uids = options.series_uids.split(',')
        uids_set = set(uids)
        data_file = open(series_file, 'r')
        data = json.load(data_file)
        data_file.close()
        filtered_uids = [
            series for series in data['data'] if str(series['uid']['value']) in uids_set]
        path_dict = []
        data_directory = options.data_location
        pacs_settings['executable'] = '/usr/bin/movescu'
        for series in filtered_uids:
            patient_dir = pypx.utils.patientPath(
                '', series['PatientID']['value'],
                series['PatientName']['value'])
            study_dir = pypx.utils.studyPath(
                patient_dir, series['StudyDescription']['value'],
                series['StudyDate']['value'],
                series['StudyInstanceUID']['value'])
            series_dir = pypx.utils.seriesPath(
                study_dir, series['SeriesDescription']['value'],
                series['SeriesDate']['value'],
                series['SeriesInstanceUID']['value'])
            source = os.path.join(data_directory, series_dir)
            series_info = os.path.join(source, 'series.info')
            destination_study = os.path.join(options.outputdir, study_dir)
            destination_series = os.path.join(options.outputdir, series_dir)
            path_dict.append(
                {'source': source,
                 'destination_study': destination_study,
                 'destination_series': destination_series,
                 'info': series_info})
            pacs_settings['series_uid'] = series['SeriesInstanceUID']['value']
            output = pypx.move(pacs_settings)
        print('Receiving data...')
        timer = 0
        while timer < 100: 
            for path in path_dict[:]:
                if os.path.isfile(path['info']):
                    if not os.path.exists(path['destination_study']):
                        os.makedirs(path['destination_study'])
                    else:
                        print(path['destination_study'] + ' already exists.')
                    shutil.copytree(path['source'], path['destination_series'])
                    destination_jpgs = os.path.join(path['destination_series'], 'jpgs')
                    if not os.path.exists(destination_jpgs):
                        os.makedirs(destination_jpgs)
                    else:
                        print(destination_jpgs + ' already exists.')
                    for filename in os.listdir(path['destination_series']):
                        name, extension = os.path.splitext(filename)
                        if extension == '.dcm':
                            basename = os.path.basename(filename)
                            source = os.path.join(path['destination_series'], basename)
                            output = os.path.join(destination_jpgs, basename)
                            exec_location = os.path.dirname(pacs_settings['executable'])
                            executable = os.path.join(exec_location, 'dcmj2pnm')
                            command = executable + ' +oj +Wh 15 +Fa ' + source + ' ' + output + '.jpg';
                            response = subprocess.run(
                                command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
                    executable = '/usr/bin/mogrify'
                    options = '-resize 96x96 -background none -gravity center -extent 96x96'
                    source = os.path.join(destination_jpgs, '*')
                    command = executable + ' ' + options + ' ' + source
                    response = subprocess.run(
                        command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
                    executable = '/usr/bin/convert'
                    options = '-append'
                    output = os.path.join(path['destination_series'], 'preview.jpg')
                    command = executable + ' ' + options + ' ' + source + ' ' + output
                    response = subprocess.run(
                        command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
                    path_dict.remove(path)
            if len(path_dict) == 0:
                break
            time.sleep(1)
            timer += 1
        print('Done.')
def run ( self , options ) : pacs_settings = { 'aet' : options . aet , 'aec' : options . aec , 'aet_listener' : options . aet_listener , 'server_ip' : options . server_ip , 'server_port' : options . server_port } pacs_settings [ 'executable' ] = '/usr/bin/echoscu' echo = pypx . echo ( pacs_settings ) if echo [ 'status' ] == 'error' : with open ( os . path . join ( options . outputdir , echo [ 'status' ] + '.txt' ) , 'w' ) as outfile : json . dump ( echo , outfile , indent = 4 , sort_keys = True , separators = ( ',' , ':' ) ) return series_file = os . path . join ( options . inputdir , 'success.txt' ) if options . series_file != '' : series_file = options . series_file uids = options . series_uids . split ( ',' ) uids_set = set ( uids ) data_file = open ( series_file , 'r' ) data = json . load ( data_file ) data_file . close ( ) filtered_uids = [ series for series in data [ 'data' ] if str ( series [ 'uid' ] [ 'value' ] ) in uids_set ] path_dict = [ ] data_directory = options . data_location pacs_settings [ 'executable' ] = '/usr/bin/movescu' for series in filtered_uids : patient_dir = pypx . utils . patientPath ( '' , series [ 'PatientID' ] [ 'value' ] , series [ 'PatientName' ] [ 'value' ] ) study_dir = pypx . utils . studyPath ( patient_dir , series [ 'StudyDescription' ] [ 'value' ] , series [ 'StudyDate' ] [ 'value' ] , series [ 'StudyInstanceUID' ] [ 'value' ] ) series_dir = pypx . utils . seriesPath ( study_dir , series [ 'SeriesDescription' ] [ 'value' ] , series [ 'SeriesDate' ] [ 'value' ] , series [ 'SeriesInstanceUID' ] [ 'value' ] ) source = os . path . join ( data_directory , series_dir ) series_info = os . path . join ( source , 'series.info' ) destination_study = os . path . join ( options . outputdir , study_dir ) destination_series = os . path . join ( options . outputdir , series_dir ) path_dict . append ( { 'source' : source , 'destination_study' : destination_study , 'destination_series' : destination_series , 'info' : series_info } ) pacs_settings [ 'series_uid' ] = series [ 'SeriesInstanceUID' ] [ 'value' ] output = pypx . move ( pacs_settings ) print ( 'Receiving data...' ) timer = 0 while timer < 100 : for path in path_dict [ : ] : if os . path . isfile ( path [ 'info' ] ) : if not os . path . exists ( path [ 'destination_study' ] ) : os . makedirs ( path [ 'destination_study' ] ) else : print ( path [ 'destination_study' ] + ' already exists.' ) shutil . copytree ( path [ 'source' ] , path [ 'destination_series' ] ) destination_jpgs = os . path . join ( path [ 'destination_series' ] , 'jpgs' ) if not os . path . exists ( destination_jpgs ) : os . makedirs ( destination_jpgs ) else : print ( destination_jpgs + ' already exists.' ) for filename in os . listdir ( path [ 'destination_series' ] ) : name , extension = os . path . splitext ( filename ) if extension == '.dcm' : basename = os . path . basename ( filename ) source = os . path . join ( path [ 'destination_series' ] , basename ) output = os . path . join ( destination_jpgs , basename ) exec_location = os . path . dirname ( pacs_settings [ 'executable' ] ) executable = os . path . join ( exec_location , 'dcmj2pnm' ) command = executable + ' +oj +Wh 15 +Fa ' + source + ' ' + output + '.jpg' ; response = subprocess . run ( command , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , shell = True ) executable = '/usr/bin/mogrify' options = '-resize 96x96 -background none -gravity center -extent 96x96' source = os . path . join ( destination_jpgs , '*' ) command = executable + ' ' + options + ' ' + source response = subprocess . run ( command , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , shell = True ) executable = '/usr/bin/convert' options = '-append' output = os . path . join ( path [ 'destination_series' ] , 'preview.jpg' ) command = executable + ' ' + options + ' ' + source + ' ' + output response = subprocess . run ( command , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , shell = True ) path_dict . remove ( path ) if len ( path_dict ) == 0 : break time . sleep ( 1 ) timer += 1 print ( 'Done.' )
Define the code to be run by this plugin app.
Define the code to be run by this plugin app .
"""
        Define the code to be run by this plugin app.
        """ # options.inputdir # common options between all request types # aet # aec # aet_listener # ip # port # echo the PACS to make sure we can access it # create dummy series file with all series # uids to be fetched from success.txt # parser series file # create destination directories and move series # move series # wait for files to arrive! # 1h # what if pulling an existing dataset # (.info file already there? need extra flag to force re=pull?) # copy series to output # create jpgs directory # generate jpgs for all dcm files # resize all jpgs # create preview # remove from dictionnary
self
options