Esempio n. 1
0
    def read(self, filename=None, bad=None):
        """read GSE MCA file"""
        self.filename = filename
        if bad is None:
            bad = self.bad
        fh = open(filename)
        lines = fh.readlines()
        fh.close()
        nrow = 0
        data_mode = 'HEADER'
        counts = []
        rois = []
        environ = []
        head = self.header = GSEMCA_Header()
        for l in lines:
            l = l.strip()
            if len(l) < 1: continue
            if data_mode == 'DATA':
                counts.append(str2ints(l))
            else:
                pos = l.find(' ')
                if (pos == -1): pos = len(l)
                tag = l[0:pos].strip().lower()
                if tag.endswith(':'):
                    tag = tag[:-1]
                val = l[pos:len(l)].strip()
                if tag in ('version', 'date'):
                    setattr(head, tag, val)
                elif tag in ('elements', 'channels'):
                    setattr(head, tag, int(val))
                elif tag in ('real_time', 'live_time', 'cal_offset',
                             'cal_slope', 'cal_quad'):
                    setattr(head, tag, str2floats(val))
                elif tag == 'rois':
                    head.rois = str2ints(val)
                    self.nrois = max(head.rois)
                elif tag == 'data':
                    data_mode = 'DATA'
                elif tag == 'environment':
                    addr, val = val.split('="')
                    val, desc = val.split('"')
                    val = val.strip()
                    desc = desc.strip()
                    if desc.startswith('(') and desc.endswith(')'):
                        desc = desc[1:-1]
                    environ.append((desc, val, addr))
                elif tag[0:4] == 'roi_':
                    iroi, item = tag[4:].split('_')
                    iroi = int(iroi)
                    if iroi >= len(rois):
                        for ir in range(1 + iroi - len(rois)):
                            rois.append({'label': [], 'right': [], 'left': []})
                    if item == "label":
                        rois[iroi]['label'] = str2str(val, delim='&')
                    elif item == "left":
                        rois[iroi]['left'] = str2ints(val)
                    elif item == "right":
                        rois[iroi]['right'] = str2ints(val)
                else:
                    pass  # print(" Warning: " , tag, " is not supported here!")

        #
        counts = np.array(counts)
        ## Data has been read, now store in MCA objects
        sum_mca = None

        for tag in ('real_time', 'live_time', 'cal_offset', 'cal_slope',
                    'cal_quad'):
            val = getattr(head, tag)
            # print( ' Attr ', tag, val)
            if len(val) == 1 and head.elements > 1:
                val = [val[0]] * head.elements
                setattr(head, tag, val)
        for imca in range(head.elements):
            thismca = MCA(name='mca%i' % (imca + 1),
                          nchans=head.channels,
                          counts=counts[:, imca],
                          start_time=head.date,
                          offset=head.cal_offset[imca],
                          slope=head.cal_slope[imca],
                          quad=head.cal_quad[imca],
                          real_time=head.real_time[imca],
                          live_time=head.live_time[imca])

            for desc, val, addr in environ:
                thismca.add_environ(desc=desc, val=val, addr=addr)

            for roi in rois:
                left = roi['left'][imca]
                right = roi['right'][imca]
                label = roi['label'][imca]
                if right > 1 and len(label) > 1:
                    thismca.add_roi(name=label,
                                    left=left,
                                    right=right,
                                    sort=False,
                                    counts=counts[:, imca])
            thismca.rois.sort()
            self.mcas.append(thismca)

        mca0 = self.__get_mca0()
        self.counts = self.get_counts()
        self.raw = self.get_counts(dt_correct=False)
        self.name = 'mcasum'
        self.energy = mca0.energy[:]
        self.environ = mca0.environ
        self.real_time = mca0.real_time
        self.live_time = mca0.live_time
        self.offset = mca0.offset
        self.slope = mca0.slope
        self.quad = mca0.quad
        self.rois = []
        for roi in mca0.rois:
            self.add_roi(name=roi.name,
                         left=roi.left,
                         right=roi.right,
                         sort=False,
                         counts=counts,
                         to_mcas=False)
        self.rois.sort()
        return
Esempio n. 2
0
    def read(self, filename=None, bad=None):
        """read GSE MCA file"""
        self.filename = filename
        if bad is None:
            bad = self.bad
        fh    = open(filename)
        lines = fh.readlines()
        fh.close()
        nrow       = 0
        data_mode  = 'HEADER'
        counts     = []
        rois       = []
        environ    = []
        head = self.header = GSEMCA_Header()
        for l in lines:
            l  = l.strip()
            if len(l) < 1: continue
            if data_mode == 'DATA':
                counts.append(str2ints(l))
            else:
                pos = l.find(' ')
                if (pos == -1): pos = len(l)
                tag = l[0:pos].strip().lower()
                if tag.endswith(':'):
                    tag = tag[:-1]
                val = l[pos:len(l)].strip()
                if tag in ('version', 'date'):
                    setattr(head, tag, val)
                elif tag in ('elements', 'channels'):
                    setattr(head, tag, int(val))
                elif tag in ('real_time', 'live_time', 'cal_offset',
                             'cal_slope', 'cal_quad'):
                    setattr(head, tag, str2floats(val))
                elif tag == 'rois':
                    head.rois = str2ints(val)
                    self.nrois = max(head.rois)
                elif tag == 'data':
                    data_mode = 'DATA'
                elif tag == 'environment':
                    addr, val = val.split('="')
                    val, desc = val.split('"')
                    val = val.strip()
                    desc = desc.strip()
                    if desc.startswith('(') and desc.endswith(')'):
                        desc = desc[1:-1]
                    environ.append((desc, val, addr))
                elif tag[0:4] == 'roi_':
                    iroi, item = tag[4:].split('_')
                    iroi = int(iroi)
                    if iroi >= len(rois):
                        for ir in range(1  + iroi - len(rois)):
                            rois.append({'label':[], 'right':[], 'left':[]})
                    if item == "label":
                        rois[iroi]['label'] = str2str(val, delim='&')
                    elif item == "left":
                        rois[iroi]['left']  = str2ints(val)
                    elif item == "right":
                        rois[iroi]['right'] = str2ints(val)
                else:
                    pass # print(" Warning: " , tag, " is not supported here!")

        #
        counts =  np.array(counts)
        ## Data has been read, now store in MCA objects
        sum_mca = None

        for tag in ('real_time', 'live_time', 'cal_offset',
                    'cal_slope', 'cal_quad'):
            val = getattr(head, tag)
            # print( ' Attr ', tag, val)
            if len(val) == 1 and head.elements > 1:
                val = [val[0]]*head.elements
                setattr(head, tag, val)
        for imca in range(head.elements):
            thismca = MCA(name='mca%i' % (imca+1),
                          nchans=head.channels,
                          counts=counts[:,imca],
                          start_time=head.date,
                          offset=head.cal_offset[imca],
                          slope=head.cal_slope[imca],
                          quad=head.cal_quad[imca],
                          real_time=head.real_time[imca],
                          live_time=head.live_time[imca])

            for desc, val, addr in environ:
                thismca.add_environ(desc=desc, val=val, addr=addr)

            for roi in rois:
                left = roi['left'][imca]
                right = roi['right'][imca]
                label = roi['label'][imca]
                if right > 1 and len(label) > 1:
                    thismca.add_roi(name=label, left=left, right=right,
                                    sort=False, counts=counts[:,imca])
            thismca.rois.sort()
            self.mcas.append(thismca)

        mca0 = self.__get_mca0()
        self.counts = self.get_counts()
        self.raw    = self.get_counts(dt_correct=False)
        self.name   = 'mcasum'
        self.energy = mca0.energy[:]
        self.environ = mca0.environ
        self.real_time = mca0.real_time
        self.live_time = mca0.live_time
        self.offset = mca0.offset
        self.slope  = mca0.slope
        self.quad   = mca0.quad
        self.rois = []
        for roi in mca0.rois:
            self.add_roi(name=roi.name, left=roi.left,
                         right=roi.right, sort=False,
                         counts=counts, to_mcas=False)
        self.rois.sort()
        return