示例#1
0
def decodeSQ(raw_attr, level):
    result = DTree(name='Sequence', level=level + 1)
    for i in _branches_:
        result.add_branch(i)
    leng_max = getattr(getattr(raw_attr[0], 'metadata'), 'leng')
    locator = 0
    while locator < leng_max:
        _raw_info_locator_ = _read_raw_info_(raw_attr, locator)
        raw_info = _raw_info_locator_[0]
        locator = _raw_info_locator_[1]
        fin_tag = _fine_metadata_(raw_tag)
        for i in _branches_:
            if i != 'value':
                result.add_leaf(branch=i, value=getattr(fine_metadata, i))
        raw_attr_sub = _read_raw_sub_attr_(raw_attr, raw_info, locator)
        if getattr(raw_tag,
                   'tag') == 0xfffee000:  # To do: add support for itemization
            continue
        if getattr(raw_tag, 'VR') == 'SQ':
            result.append(attribute(fin_tag, decodeSQ(raw_attr_sub)))
            locator = locator + getattr(raw_tag, 'leng')
            continue
        val_sub = readValue(raw_attr_sub, is_sub=True)
        result.append(attribute(fin_tag, val_sub))
        locator = locator + getattr(raw_tag, 'leng')
    if locator != leng_max:
        raise InvalidSQError(
            'The length of the sequence {} is not equal to the length defined in tag'
            .format(str(raw_attr)))
    return result
示例#2
0
def RF(X_train, y_train):
    """
    random forest
    """

    trees = []
    for i in xrange(300):
        d_tree = DTree(0)
        bootstrap_X, bootstrap_y = boot_strap(X_train, y_train)
        d_tree.fit(bootstrap_X, bootstrap_y)
        trees.append(d_tree)
    return trees
示例#3
0
def RF(X_train, y_train):
    """
    random forest
    """

    trees = []
    for i in xrange(300):
        d_tree = DTree(0)
        bootstrap_X, bootstrap_y = boot_strap(X_train, y_train)
        d_tree.fit(bootstrap_X, bootstrap_y)
        trees.append(d_tree)
    return trees
示例#4
0
def read_item_leng(read, **kwargs):
    result = DTree()
    for i in _branches_:
        result.add_branch(i)
    offset = 0
    implicity = False
    littleEndian = True
    encoding = default_encoding
    level = 0
    for key, value in kwargs.items():
        if key == 'implicity':
            implicity = value
        elif key == 'littleEndian':
            littleEndian = value
        elif key == 'encoding':
            encoding = value
        elif key == 'level':
            level = value
        elif key == 'index':
            index = value
    result.set_metainfo(name='item' + str(index + 1),
                        level=level,
                        index=index + 1)
    meta_leng = read_raw_meta_leng(read, **kwargs)
    if getattr(meta_leng[0], 'tag') != 0xfffee000:
        raise ItemNotFound()
    offset = offset + meta_leng[1]
    item_max_leng = getattr(meta_leng[0], 'leng')
    item_offset = 0
    while item_offset < item_max_leng:
        attr_leng = read_attribute_leng(read, **kwargs)
        offset = offset + attr_leng[1]
        item_offset = item_offset + attr_leng[1]
        result.add_attribute(attr_leng[0])
    return (result, offset)
示例#5
0
def read_dataset(read, metainfo):
    result = DTree()
    if not isinstance(metainfo, DTree):
        raise TypeError(
            'The metainfo passed to dataset reader is not of the type DTree')
    for i in _branches_:
        result.add_branch(i)
    imp_le = metainfo.get_value('value', tag=0x00020010)
    implicity = ('Implicit' in imp_le)
    littleEndian = ("Little Endian" in imp_le)
    encoding = default_encoding
    charset = read_attribute_leng(read,
                                  implicity=implicity,
                                  littleEndian=littleEndian,
                                  encoding=encoding)
    if getattr(charset[0], 'tag') != 0x00080005:
        raise InvalidDicomError(
            'Couldn\'t find the expected (0008,0005) attribute')
    encoding = convert_encoding(getattr(charset[0], 'value'), littleEndian)
    result.add_attribute(charset[0])
    try:
        while True:
            attr_leng = read_attribute_leng(read,
                                            implicity=implicity,
                                            littleEndian=littleEndian,
                                            encoding=encoding,
                                            level=0)
            if not attr_leng:
                raise StopIteration
            else:
                result.add_attribute(attr_leng[0])
    except StopIteration:
        pass
    return result
示例#6
0
def dcm_read(filename):
    import os
    result = DTree(name=os.path.basename(filename), level=0, index=0)
    for i in _branches_:
        result.add_branch(i)
    with open(filename, 'rb') as fp:
        read = getattr(fp, 'read')
        if not is_dicom(read):
            raise InvalidDicomError()
        metainfo = read_metainfo(read)
        result.merge(metainfo)
        dataset = read_dataset(read, metainfo)
        result.merge(dataset)
    return result
示例#7
0
def dcmRead(file_name):
    result = DTree(name=file_name.split('.')[0])
    for i in _branches_:
        result.add_branch(i)
    with open(file_name, 'rb') as testFile:
        read = getattr(testFile, "read")
        __validity__ = isDICOM(read)
        if not __validity__:
            raise InvalidDicomError()
        global meta_length
        try:
            while True:
                raw_gen = genRawAttribute(testFile, read)
                raw_attr = next(raw_gen)
                """
                with open("write.test",'ab') as fileWrite:
                    fileWrite.write(str(raw_attr)+'\n')
                    """
                raw_tag = raw_attr[0]
                val = raw_attr[1]
                fine_metadata = _fine_metadata_(raw_attr[1])
                for i in _branches_:
                    if i != 'value':
                        result.add_leaf(branch=i,
                                        value=getattr(fine_metadata, i))
                result.add_leaf(branch=_branches_[4],
                                value=readValue(raw_attr))
                if tag == 0x00020000:
                    meta_length = val + testFile.tell()
                """
                with open("write.test",'ab') as fileWrite:
                    fileWrite.write(str(buf_attr)+'\n')
                    """
        except StopIteration:
            pass
    return result
    ax.set(xlabel='Number of Samples',
           ylabel='Accuracy %',
           title='Accuracy with bagging(Base={})'.format(100 *
                                                         (1 - base_accuracy)))
    ax.legend()
    for xy in zip(num_samples, error_rates):
        ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data')
    fig.savefig('../data/bagging_accuracy_result.png')


if __name__ == '__main__':
    training_data = data_loader.load_diabetes_train_from_file()
    testing_data = data_loader.load_diabetes_test_from_file()

    # Testing 1 DTree
    tree = DTree(sample=False)
    tree.train(training_data)
    tree.test(testing_data)
    print('Error rate for single learner is {}'.format(
        100 * (1 - tree.last_error_rate)))

    # Testing ensemble
    error_rates = []
    for num_sample in [1, 3, 5, 10, 20]:
        for max_depth in [None, 1, 2, 3, 6]:
            bagger = Bagging(num_samples=num_sample, max_depth=max_depth)
            bagger.train_ensemble(training_data)
            error_rate = bagger.test(testing_data)[0]
            if max_depth is None:
                error_rates.append(error_rate)
            print('Num Samples: {} Depth: {} Accuracy: {}'.format(
示例#9
0
 def train_ensemble(self, training_data):
     for i in range(self._num_samples):
         curr_tree = DTree(sample=True, max_depth=self._max_depth)
         curr_tree.train(training_data)
         self._learners.append(curr_tree)
示例#10
0
def read_metainfo(read):
    result = DTree(name='metainfo', level=0)
    for i in _branches_:
        result.add_branch(i)
    implicity = False
    littleEndian = True
    meta_length = 0
    locator = 0
    encoding = default_encoding
    file_meta_leng = read_attribute_leng(read,
                                         implicity=implicity,
                                         littleEndian=littleEndian,
                                         encoding=encoding,
                                         level=0)[0]
    if getattr(file_meta_leng, 'tag') != 0x00020000:
        raise InvalidDicomError(
            'The file is either broken or incomplete. Contact the provider of the file for help'
        )
    else:
        result.add_attribute(file_meta_leng)
        meta_length = getattr(file_meta_leng, 'value')
    while locator < meta_length:
        attr_leng = read_attribute_leng(read)
        result.add_attribute(attr_leng[0])
        locator = locator + attr_leng[1]
    if result.get_value('value', tag=0x00020002) in UID_dictionary:
        result.set_value(UID_dictionary[result.get_value('value',
                                                         tag=0x00020002)][0],
                         'value',
                         tag=0x00020002)
    if result.get_value('value', tag=0x00020010) in UID_dictionary:
        result.set_value(UID_dictionary[result.get_value('value',
                                                         tag=0x00020010)][0],
                         'value',
                         tag=0x00020010)
    return result