Пример #1
0
    def __init__(self):
        AE.__init__(self,config.DICOMSERVER_AET, config.DICOMSERVER_PORT, [], [StorageSOPClass, VerificationSOPClass])
        self.StorePath = config.DICOMSERVER_DIR

        # start AE
        rl.info('DICOM server starting ...')
        self.start()
        rl.info('DICOM server started')
        self.QuitOnKeyboardInterrupt()
Пример #2
0
def StoreSCU(aec, remotehost, remoteport, dicomfile, aet='PYNETDICOM'):

    # find random free port on localhost
    tmpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tmpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    tmpsocket.bind(('', 0))
    freelocalport = tmpsocket.getsockname()[1]
    tmpsocket.close()

    # create application entity
    MyAE = AE(aet, freelocalport, [StorageSOPClass, VerificationSOPClass], [],
              ts)

    MyAE.OnAssociateResponse = OnAssociateResponse

    # remote application entity
    print(remotehost, remoteport, aec)
    RemoteAE = dict(Address=remotehost, Port=remoteport, AET=aec)

    # create association with remote AE
    print "Request association"
    assoc = MyAE.RequestAssociation(RemoteAE)

    if not assoc:
        print "Could not establish association"
        sys.exit(1)

    # perform a DICOM ECHO, just to make sure remote AE is listening
    print "DICOM Echo ... ",
    st = assoc.VerificationSOPClass.SCU(1)
    print 'done with status "%s"' % st

    # create some dataset
    print "DICOM StoreSCU ... ",
    try:
        st = assoc.SCU(dicomfile, 1)
        print 'done with status "%s"' % st
    except:
        print "problem", dicomfile.SOPClassUID
    print "Release association"
    assoc.Release(0)

    # done
    MyAE.Quit()
Пример #3
0
def StoreSCU(aec, remotehost, remoteport, dicomfile, aet="PYNETDICOM"):

    # find random free port on localhost
    tmpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tmpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    tmpsocket.bind(("", 0))
    freelocalport = tmpsocket.getsockname()[1]
    tmpsocket.close()

    # create application entity
    MyAE = AE(aet, freelocalport, [StorageSOPClass, VerificationSOPClass], [], ts)

    MyAE.OnAssociateResponse = OnAssociateResponse

    # remote application entity
    print (remotehost, remoteport, aec)
    RemoteAE = dict(Address=remotehost, Port=remoteport, AET=aec)

    # create association with remote AE
    print "Request association"
    assoc = MyAE.RequestAssociation(RemoteAE)

    if not assoc:
        print "Could not establish association"
        sys.exit(1)

    # perform a DICOM ECHO, just to make sure remote AE is listening
    print "DICOM Echo ... ",
    st = assoc.VerificationSOPClass.SCU(1)
    print 'done with status "%s"' % st

    # create some dataset
    print "DICOM StoreSCU ... ",
    try:
        st = assoc.SCU(dicomfile, 1)
        print 'done with status "%s"' % st
    except:
        print "problem", dicomfile.SOPClassUID
    print "Release association"
    assoc.Release(0)

    # done
    MyAE.Quit()
Пример #4
0
def StoreSCU(aec, remotehost, remoteport, dicomfile, aet="PYNETDICOM"):

    # create application entity
    MyAE = AE(aet, 9999, [StorageSOPClass, VerificationSOPClass], [], ts)

    MyAE.OnAssociateResponse = OnAssociateResponse

    # remote application entity
    print (remotehost, remoteport, aec)
    RemoteAE = dict(Address=remotehost, Port=remoteport, AET=aec)

    # create association with remote AE
    print "Request association"
    assoc = MyAE.RequestAssociation(RemoteAE)

    if not assoc:
        print "Could not establish association"
        sys.exit(1)

    # perform a DICOM ECHO, just to make sure remote AE is listening
    print "DICOM Echo ... ",
    st = assoc.VerificationSOPClass.SCU(1)
    print 'done with status "%s"' % st

    # create some dataset
    print "DICOM StoreSCU ... ",
    try:
        st = assoc.SCU(dicomfile, 1)
        print 'done with status "%s"' % st
    except:
        print "problem", dicomfile.SOPClassUID
    print "Release association"
    assoc.Release(0)

    # done
    MyAE.Quit()
Пример #5
0
def OnReceiveStore(SOPClass, DS):
    #print "Received C-STORE"
    # do something with dataset. For instance, store it on disk.
    file_meta = Dataset()
    file_meta.MediaStorageSOPClassUID = DS.SOPClassUID
    file_meta.MediaStorageSOPInstanceUID = "1.2.3"  # !! Need valid UID here
    file_meta.ImplementationClassUID = "1.2.3.4"  # !!! Need valid UIDs here
    filename = '%s/%s.dcm' % (tempfile.gettempdir(), DS.SOPInstanceUID)
    ds = FileDataset(filename, {}, file_meta=file_meta, preamble="\0" * 128)
    ds.update(DS)
    ds.is_little_endian = True
    ds.is_implicit_VR = True
    ds.save_as(filename)
    #print "File %s written" % filename
    # must return appropriate status
    return SOPClass.Success


# setup AE
MyAE = AE(args.aet, args.port, [], [StorageSOPClass, VerificationSOPClass])
MyAE.OnAssociateRequest = OnAssociateRequest
MyAE.OnAssociateResponse = OnAssociateResponse
MyAE.OnReceiveStore = OnReceiveStore
MyAE.OnReceiveEcho = OnReceiveEcho

# start AE
print "starting AE ... ",
MyAE.start()
print "done"
MyAE.QuitOnKeyboardInterrupt()
Пример #6
0
    ts = [ExplicitVRLittleEndian]
else:
    ts = [
        ExplicitVRLittleEndian,
        ImplicitVRLittleEndian,
        ExplicitVRBigEndian
    ]

# call back


def OnAssociateResponse(association):
    print "Association response received"

# create application entity
MyAE = AE(args.aet, 9999, [StorageSOPClass,  VerificationSOPClass], [], ts)
MyAE.OnAssociateResponse = OnAssociateResponse

# remote application entity
RemoteAE = dict(Address=args.remotehost, Port=args.remoteport, AET=args.aec)

# create association with remote AE
print "Request association"
assoc = MyAE.RequestAssociation(RemoteAE)

if not assoc:
    print "Could not establish association"
    sys.exit(1)
# perform a DICOM ECHO, just to make sure remote AE is listening
print "DICOM Echo ... ",
st = assoc.VerificationSOPClass.SCU(1)
Пример #7
0
def OnReceiveStore(SOPClass, DS):
    #print "Received C-STORE"
    # do something with dataset. For instance, store it on disk.
    file_meta = Dataset()
    file_meta.MediaStorageSOPClassUID = DS.SOPClassUID
    file_meta.MediaStorageSOPInstanceUID = "1.2.3"  # !! Need valid UID here
    file_meta.ImplementationClassUID = "1.2.3.4"  # !!! Need valid UIDs here
    filename = '%s/%s.dcm' % (tempfile.gettempdir(), DS.SOPInstanceUID)
    ds = FileDataset(filename, {}, file_meta=file_meta, preamble="\0" * 128)
    ds.update(DS)
    ds.is_little_endian = True
    ds.is_implicit_VR = True
    ds.save_as(filename)
    #print "File %s written" % filename
    # must return appropriate status
    return SOPClass.Success


# setup AE
MyAE = AE(args.aet, args.port, [], [StorageSOPClass, VerificationSOPClass])
MyAE.OnAssociateRequest = OnAssociateRequest
MyAE.OnAssociateResponse = OnAssociateResponse
MyAE.OnReceiveStore = OnReceiveStore
MyAE.OnReceiveEcho = OnReceiveEcho

# start AE
print "starting AE ... ",
MyAE.start()
print "done"
MyAE.QuitOnKeyboardInterrupt()
Пример #8
0
if args.implicit:
    ts = [ImplicitVRLittleEndian]
elif args.explicit:
    ts = [ExplicitVRLittleEndian]
else:
    ts = [ExplicitVRLittleEndian, ImplicitVRLittleEndian, ExplicitVRBigEndian]

# call back


def OnAssociateResponse(association):
    print "Association response received"


# create application entity
MyAE = AE(args.aet, 0, [StorageSOPClass, VerificationSOPClass], [], ts)
MyAE.OnAssociateResponse = OnAssociateResponse

# remote application entity
RemoteAE = dict(Address=args.remotehost, Port=args.remoteport, AET=args.aec)

# create association with remote AE
print "Request association"
assoc = MyAE.RequestAssociation(RemoteAE)

if not assoc:
    print "Could not establish association"
    sys.exit(1)
# perform a DICOM ECHO, just to make sure remote AE is listening
print "DICOM Echo ... ",
st = assoc.VerificationSOPClass.SCU(1)
Пример #9
0
def send_dicom(calledAET, callingAET, remotehost, remoteport, filename):
    """ Function sends a dicom file to specified host.
        Returns (status, msg):
          Status:
            True  : success
            False : error occurred (added to logfile)
          msg: string or None
    """
    verbose = True

    ts = [
        dicom.UID.ExplicitVRLittleEndian, dicom.UID.ImplicitVRLittleEndian,
        dicom.UID.ExplicitVRBigEndian
    ]

    # call back
    def OnAssociateResponse(association):
        if verbose: print "[Input2DCM] Association response received"
        pass

    # find random free port on localhost
    tmpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tmpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    tmpsocket.bind(('', 0))
    freelocalport = tmpsocket.getsockname()[1]
    tmpsocket.close()

    # create application entity
    MyAE = AE(callingAET, freelocalport,
              [StorageSOPClass, VerificationSOPClass], [], ts)
    MyAE.OnAssociateResponse = OnAssociateResponse

    # remote application entity
    RemoteAE = dict(Address=remotehost, Port=remoteport, AET=calledAET)

    # create association with remote AE
    if verbose: print '[Input2DCM] Request association'
    assoc = MyAE.RequestAssociation(RemoteAE)

    if not assoc:
        msg = 'Could not establish association'
        print '[Input2DCM] %s' % msg
        return (False, msg)

    # perform a DICOM ECHO, just to make sure remote AE is listening
    try:
        status = assoc.VerificationSOPClass.SCU(1)
        if verbose:
            print '[Input2DCM] DICOM Echo ... done with status "%s"' % status
    except:
        msg = 'DICOM Echo ... problem occurred'
        print '[Input2DCM] %s' % msg
        return (False, msg)

    dcm = dicom.read_file(filename)
    try:
        status = assoc.SCU(dcm, 1)
        if verbose:
            print '[Input2DCM] DICOM StoreSCU ... done with status "%s"' % status
    except:
        msg = 'DICOM StoreSCU ... problem occurred'
        print '[Input2DCM] %s' % msg
        return (False, msg)

    if verbose: print 'Release association'
    assoc.Release(0)
    os.remove(filename)

    # done
    MyAE.Quit()

    return (True, None)
    def ExportDataToDicomNetwork(self,
                                 aec="HIFU_CONSOLE",
                                 aet="SCANNER",
                                 ip="127.0.0.1",
                                 port=4001):
        from netdicom import AE
        from netdicom.SOPclass import StorageSOPClass, MRImageStorageSOPClass, VerificationSOPClass
        from dicom.UID import ExplicitVRLittleEndian, ImplicitVRLittleEndian, \
            ExplicitVRBigEndian

        def OnAssociateResponse(association):
            print("Association response received")

        # create application entity
        ts = [
            ExplicitVRLittleEndian, ImplicitVRLittleEndian, ExplicitVRBigEndian
        ]
        MyAE = AE(aet, 9999, [MRImageStorageSOPClass, VerificationSOPClass],
                  [], ts)
        MyAE.OnAssociateResponse = OnAssociateResponse
        # remote application entity
        RemoteAE = dict(Address=ip, Port=port, AET=aec)
        # create association with remote AE

        print("Request association")
        assoc = MyAE.RequestAssociation(RemoteAE)
        if not assoc:
            raise ValueError("Could not establish association")

        # perform a DICOM ECHO, just to make sure remote AE is listening
        print("DICOM Echo ... ")
        st = assoc.VerificationSOPClass.SCU(1)
        print('done with status "%s"' % st)

        NewSeriesInstanceUID = dicom.uid.generate_uid(
            prefix=self.Images[0].InstanceCreatorUID)
        NewSeriesNumber = int(
            self.Images[0].SeriesNumber) + np.random.randint(10000)
        time.sleep(0.01)

        for image in self.Images:
            image.SeriesDescription = image.SeriesDescription + ' offset (LR,AP,AH)= ' + np.array2string(
                self.Offset, formatter={'float_kind': '{0:2.1f}'.format})
            image.SeriesInstanceUID = NewSeriesInstanceUID
            image.SOPInstanceUID = dicom.uid.generate_uid(
                prefix=image.InstanceCreatorUID)
            image.SeriesNumber = NewSeriesNumber
            time.sleep(0.01)

            try:
                print('sending image...')
                st = assoc.SCU(image, 1)
                print('done with status "%s"' % st)
            except:
                raise
                print("problem", d.SOPClassUID)

        print("Release association")

        assoc.Release(0)

        # done

        MyAE.Quit()
Пример #11
0
def send_dicom(calledAET,callingAET,remotehost,remoteport,filename):
    """ Function sends a dicom file to specified host.
        Returns (status, msg):
          Status:
            True  : success
            False : error occurred (added to logfile)
          msg: string or None
    """
    verbose = True

    ts = [
        dicom.UID.ExplicitVRLittleEndian, 
        dicom.UID.ImplicitVRLittleEndian, 
        dicom.UID.ExplicitVRBigEndian
    ]

    # call back
    def OnAssociateResponse(association):
        if verbose: print "[Input2DCM] Association response received"
        pass

    # find random free port on localhost
    tmpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tmpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
    tmpsocket.bind(('', 0))
    freelocalport = tmpsocket.getsockname()[1]
    tmpsocket.close()

    # create application entity
    MyAE = AE(callingAET, freelocalport, [StorageSOPClass,  VerificationSOPClass], [], ts)
    MyAE.OnAssociateResponse = OnAssociateResponse

    # remote application entity
    RemoteAE = dict(Address=remotehost, Port=remoteport, AET=calledAET)

    # create association with remote AE
    if verbose: print '[Input2DCM] Request association'
    assoc = MyAE.RequestAssociation(RemoteAE)

    if not assoc:
        msg = 'Could not establish association'
        print '[Input2DCM] %s'%msg
        return (False,msg)

    # perform a DICOM ECHO, just to make sure remote AE is listening
    try:
        status = assoc.VerificationSOPClass.SCU(1)
        if verbose: print '[Input2DCM] DICOM Echo ... done with status "%s"' % status
    except:
        msg = 'DICOM Echo ... problem occurred'
        print '[Input2DCM] %s'%msg
        return (False,msg)

    dcm = dicom.read_file(filename)
    try:
        status = assoc.SCU(dcm, 1)
        if verbose: print '[Input2DCM] DICOM StoreSCU ... done with status "%s"' % status
    except:
        msg = 'DICOM StoreSCU ... problem occurred'
        print '[Input2DCM] %s'%msg
        return (False,msg)

    if verbose: print 'Release association'
    assoc.Release(0)
    os.remove(filename)

    # done
    MyAE.Quit()

    return (True,None)