示例#1
0
    def on_subject(self, params: dict):
        subject_index = params['subject_index']
        properties = params['{}_properties'.format(
            self.category)]  # type: conv.ImageProperties

        self.writer.fill(df.INFO_SHAPE, properties.size,
                         expr.IndexExpression(subject_index))
        self.writer.fill(df.INFO_ORIGIN, properties.origin,
                         expr.IndexExpression(subject_index))
        self.writer.fill(df.INFO_DIRECTION, properties.direction,
                         expr.IndexExpression(subject_index))
        self.writer.fill(df.INFO_SPACING, properties.spacing,
                         expr.IndexExpression(subject_index))
示例#2
0
    def extract(self, reader: miapy_extr.reader.Reader, params: dict,
                extracted: dict) -> None:
        subject_index_expr = miapy_expr.IndexExpression(
            params['subject_index'])

        extracted['centroid_transform'] = reader.read(
            STORE_META_CENTROID_TRANSFORM, subject_index_expr)
示例#3
0
    def on_subject(self, params: dict):
        subject_files = params['subject_files']
        subject_index = params['subject_index']

        subject = subject_files[subject_index].subject
        self.writer.fill(df.SUBJECT, subject,
                         expr.IndexExpression(subject_index))
示例#4
0
    def on_subject(self, params: dict):
        subject_files = params['subject_files']
        subject_index = params['subject_index']
        subject = subject_files[subject_index].subject

        age = 0
        sex = 0
        if self.meta_dict:
            meta = self.meta_dict[subject]
            age = meta['AGE']
            sex = meta['SEX']

        self.writer.fill(STORE_DEMOGRAPHIC_AGE, age,
                         miapy_expr.IndexExpression(subject_index))
        self.writer.fill(STORE_DEMOGRAPHIC_SEX, sex,
                         miapy_expr.IndexExpression(subject_index))
示例#5
0
文件: writer.py 项目: ubern-mia/miapy
    def fill(self, entry: str, data, index: expr.IndexExpression = None):
        # special string handling (in order not to use length limited strings)
        if self.h5[entry].dtype is self.str_type:
            data = np.asarray(data, dtype=object)

        if index is None:
            index = expr.IndexExpression()

        self.h5[entry][index.expression] = data
示例#6
0
    def extract(self, reader: miapy_extr.reader.Reader, params: dict,
                extracted: dict) -> None:
        subject_index_expr = miapy_expr.IndexExpression(
            params['subject_index'])

        extracted[STORE_DEMOGRAPHIC_AGE] = reader.read(STORE_DEMOGRAPHIC_AGE,
                                                       subject_index_expr)
        extracted[STORE_DEMOGRAPHIC_SEX] = reader.read(STORE_DEMOGRAPHIC_SEX,
                                                       subject_index_expr)
示例#7
0
    def extract(self, reader: rd.Reader, params: dict,
                extracted: dict) -> None:
        subject_index_expr = expr.IndexExpression(params['subject_index'])

        shape = reader.read(df.INFO_SHAPE, subject_index_expr)
        if self.numpy_format:
            tmp = shape[0]
            shape[0] = shape[-1]
            shape[-1] = tmp

        extracted['shape'] = tuple(shape.tolist())
示例#8
0
    def __call__(self, shape, offset=0) -> t.List[expr.IndexExpression]:
        if self.shape == shape:
            return self.indexing

        self.shape = shape  # save for later comparison
        shape_without_voxel = shape[:-1]
        indices = np.indices(shape_without_voxel)
        indices = indices.reshape(
            (indices.shape[0], np.prod(indices.shape[1:])))
        indices = indices.transpose()
        self.indexing = [expr.IndexExpression(idx.tolist()) for idx in indices]
        return self.indexing
示例#9
0
    def extract(self, reader: rd.Reader, params: dict,
                extracted: dict) -> None:
        index_expr = params['index_expr']  # type: expr.IndexExpression

        # Make sure all indexing is done with slices (Example: (16,) will be changed to (slice(16, 17, None),) which
        # is equivalent), otherwise the following steps will be wrong; .
        if any(isinstance(s, int) for s in index_expr.expression):
            index_expr.set_indexing([
                slice(s, s + 1) if isinstance(s, int) else s
                for s in index_expr.expression
            ])

        padded_indexing = np.asarray(
            index_expr.get_indexing()) + self.index_diffs
        padded_shape = tuple(
            (padded_indexing[:, 1] - padded_indexing[:, 0]).tolist())

        sub_indexing = padded_indexing.copy()
        sub_indexing[padded_indexing > 0] = 0
        sub_indexing = -sub_indexing

        padded_indexing[
            padded_indexing < 0] = 0  # cannot slice outside the boundary
        padded_index_expr = expr.IndexExpression(padded_indexing.tolist())

        params['index_expr'] = padded_index_expr
        self.extractor.extract(reader, params, extracted)
        params['index_expr'] = index_expr

        for category in self.categories:
            data = extracted[category]

            full_pad_shape = padded_shape + data.shape[len(padded_shape):]
            pad_data = np.zeros(full_pad_shape, dtype=data.dtype)
            sub_indexing[:, 1] = sub_indexing[:, 0] + data.shape[:sub_indexing.
                                                                 shape[0]]
            sub_index_expr = expr.IndexExpression(sub_indexing.tolist())

            pad_data[sub_index_expr.expression] = data
            extracted[category] = pad_data
示例#10
0
    def extract(self, reader: rd.Reader, params: dict,
                extracted: dict) -> None:
        if self.entry_base_names is None:
            entries = reader.get_subject_entries()
            self.entry_base_names = [
                entry.rsplit('/', maxsplit=1)[1] for entry in entries
            ]

        subject_index = params['subject_index']
        index_expr = params['index_expr']  # type: expr.IndexExpression
        padded_indexing = np.asarray(
            index_expr.get_indexing()) + self.index_diffs

        padded_shape = tuple(
            (padded_indexing[:, 1] - padded_indexing[:, 0]).tolist())

        sub_indexing = padded_indexing.copy()
        sub_indexing[padded_indexing > 0] = 0
        sub_indexing = -sub_indexing

        padded_indexing[
            padded_indexing < 0] = 0  # cannot slice outside the boundary
        padded_index_expr = expr.IndexExpression(padded_indexing.tolist())

        base_name = self.entry_base_names[subject_index]
        for category in self.categories:
            data = reader.read(
                '{}/{}'.format(df.DATA_PLACEHOLDER.format(category),
                               base_name), padded_index_expr)

            full_pad_shape = padded_shape + data.shape[len(padded_shape):]
            pad_data = np.zeros(full_pad_shape, dtype=data.dtype)
            sub_indexing[:, 1] = sub_indexing[:, 0] + data.shape[:sub_indexing.
                                                                 shape[0]]
            sub_index_expr = expr.IndexExpression(sub_indexing.tolist())

            pad_data[sub_index_expr.expression] = data
            extracted[category] = pad_data
示例#11
0
    def on_subject(self, params: dict):
        subject_index = params['subject_index']
        subject_files = params['subject_files']

        subject_file = subject_files[subject_index]  # type: subj.SubjectFile

        for category in params['categories']:
            for index, file_name in enumerate(
                    subject_file.categories[category].entries.values()):
                relative_path = os.path.relpath(file_name, self.file_root)
                index_expr = expr.IndexExpression(
                    indexing=[subject_index, index], axis=(0, 1))
                self.writer.fill(df.FILES_PLACEHOLDER.format(category),
                                 relative_path, index_expr)
示例#12
0
    def extract(self, reader: rd.Reader, params: dict,
                extracted: dict) -> None:
        subject_index_expr = expr.IndexExpression(params['subject_index'])

        if not self.cache or self.cached_file_root is None:
            file_root = reader.read(df.FILES_ROOT)
            self.cached_file_root = file_root
        else:
            file_root = self.cached_file_root

        extracted['file_root'] = file_root

        for category in self.categories:
            extracted['{}_files'.format(category)] = reader.read(
                df.FILES_PLACEHOLDER.format(category), subject_index_expr)
示例#13
0
    def extract(self, reader: rd.Reader, params: dict,
                extracted: dict) -> None:
        subject_index_expr = expr.IndexExpression(params['subject_index'])

        shape = reader.read(df.INFO_SHAPE, subject_index_expr).tolist()
        direction = reader.read(df.INFO_DIRECTION, subject_index_expr).tolist()
        spacing = reader.read(df.INFO_SPACING, subject_index_expr).tolist()
        origin = reader.read(df.INFO_ORIGIN, subject_index_expr).tolist()

        # todo: everything in memory?
        image = sitk.Image(shape, sitk.sitkUInt8)
        image.SetDirection(direction)
        image.SetSpacing(spacing)
        image.SetOrigin(origin)
        # todo number_of_components_per_pixel and pixel_id

        img_properties = conv.ImageProperties(image)
        if self.do_pickle:
            # pickle to prevent from problems since own class
            img_properties = pickle.dumps(img_properties)
        extracted['properties'] = img_properties
示例#14
0
    def direct_extract(self,
                       extractor: extr.Extractor,
                       subject_index: int,
                       index_expr: expr.IndexExpression = None,
                       transform: tfm.Transform = None):
        if index_expr is None:
            index_expr = expr.IndexExpression()

        params = {'subject_index': subject_index, 'index_expr': index_expr}
        extracted = {}

        if not self.init_reader_once:
            with rd.get_reader(self.dataset_path) as reader:
                extractor.extract(reader, params, extracted)
        else:
            if self.reader is None:
                self.reader = rd.get_reader(self.dataset_path,
                                            direct_open=True)
            extractor.extract(self.reader, params, extracted)

        if transform:
            extracted = transform(extracted)

        return extracted
示例#15
0
 def __call__(self, shape) -> t.List[expr.IndexExpression]:
     indexing = []
     for axis in self.slice_axis:
         indexing.extend(
             expr.IndexExpression(i, axis) for i in range(shape[axis]))
     return indexing
示例#16
0
 def __call__(self, shape) -> t.List[expr.IndexExpression]:
     return [expr.IndexExpression()]
示例#17
0
 def extract(self, reader: rd.Reader, params: dict,
             extracted: dict) -> None:
     extracted['subject_index'] = params['subject_index']
     subject_index_expr = expr.IndexExpression(params['subject_index'])
     extracted['subject'] = reader.read(df.SUBJECT, subject_index_expr)
示例#18
0
 def on_subject(self, params: dict):
     subject_index = params['subject_index']
     centroid_transform = params['centroid_transform']
     self.writer.fill(STORE_META_CENTROID_TRANSFORM, centroid_transform,
                      miapy_expr.IndexExpression(subject_index))