modules/dataset.py
def _kfold_split(self, folds: int, seed: int = 0) -> List[List[bool]]:
        '''
        Performs a k-fold split of self._data_train.
        A boolean filter list must be created for each split.
        A list of all these filters must be returned.
        I.e.,
            [[True, True, False, False],
             [False, False, True, True]]
        For a 2-fold split of a dataset of size 4

        The return value must be a list of :param:folds lists.
        Where each list must be precisely len(self._data_train) items long.
        There should be roughly the same number of elements in each split.
        Each index must be True exatly once (I.g., each data point is contained
          in exactly one split).
        It should be taken into account that all images of an individual
          patient are in the same split.
        The splits should be randomized but it must return identical results
          when the same seed is passed.
        '''

        # Currently just splits into :param:folds strides
        # I.e., [[True, True, False, False, False, False],
        #        [False, False, True, True, False, False],
        #        [False, False, False, False, True, True]]
        # Where the last first fold-1 items contains the same amout of dataset,
        # and the last item contains slighly more or less, depeneding on how
        # well len(self._data_train) is divisable by :param:folds
        items_per_fold = int(len(self._data_train)/folds)
        items_used = [False]*len(self._data_train)

        _filters = [[False]*len(self._data_train) for x in range(folds)]

        group_iterator = self._data_train.groupby(['patient'], as_index=False)
        group_list = list(group_iterator.groups.values())

        # makes sure that all images of a single patient are in the same fold
        random.seed(seed)
        random.shuffle(group_list)

        curr_group = 0
        curr_group_cnt = 0
        for v in group_list:
            if curr_group_cnt > items_per_fold:
                curr_group += 1
                curr_group_cnt = 0
            curr_group_cnt += len(v)
            for idx in v:
                _filters[curr_group][idx] = True


        # Do not delete the following
        # makes sure all lists are of the same length
        _it = iter(_filters)
        _len = len(next(_it))
        if not all(len(l) == _len for l in _it):
            raise ValueError('not all lists have same length!')

        # makes sure each element is in at least one fold
        if not all(reduce(np.logical_or, _filters)):
            raise ValueError('some items are not in any fold')

        # makes sure each element is in exactly one fold
        if not all([sum(x)==1 for x in zip(*_filters)]):
            raise ValueError('some items are in more than one fold')

        return _filters

        Performs a k-fold split of self._data_train.
        A boolean filter list must be created for each split.
        A list of all these filters must be returned.
        I.e.,
            [[True, True, False, False],
             [False, False, True, True]]
        For a 2-fold split of a dataset of size 4

        The return value must be a list of :param:folds lists.
        Where each list must be precisely len(self._data_train) items long.
        There should be roughly the same number of elements in each split.
        Each index must be True exatly once (I.g., each data point is contained
          in exactly one split).
        It should be taken into account that all images of an individual
          patient are in the same split.
        The splits should be randomized but it must return identical results
          when the same seed is passed.
        
Performs a k-fold split of self._data_train.
A boolean filter list must be created for each split.
A list of all these filters must be returned.
I.e.,
[[True, True, False, False],
[False, False, True, True]]
For a 2-fold split of a dataset of size 4

The return value must be a list of :param:folds lists.
Where each list must be precisely len(self._data_train) items long.
There should be roughly the same number of elements in each split.
Each index must be True exatly once (I.g., each data point is contained
in exactly one split).
It should be taken into account that all images of an individual
patient are in the same split.
The splits should be randomized but it must return identical results
when the same seed is passed.
Performs a k - fold split of self . _data_train . A boolean filter list must be created for each split . A list of all these filters must be returned . I . e . [[ True True False False ] [ False False True True ]] For a 2 - fold split of a dataset of size 4 The return value must be a list of : param : folds lists . Where each list must be precisely len ( self . _data_train ) items long . There should be roughly the same number of elements in each split . Each index must be True exatly once ( I . g . each data point is contained in exactly one split ) . It should be taken into account that all images of an individual patient are in the same split . The splits should be randomized but it must return identical results when the same seed is passed .
def _kfold_split(self, folds: int, seed: int = 0) -> List[List[bool]]:
        items_per_fold = int(len(self._data_train)/folds)
        items_used = [False]*len(self._data_train)
        _filters = [[False]*len(self._data_train) for x in range(folds)]
        group_iterator = self._data_train.groupby(['patient'], as_index=False)
        group_list = list(group_iterator.groups.values())
        random.seed(seed)
        random.shuffle(group_list)
        curr_group = 0
        curr_group_cnt = 0
        for v in group_list:
            if curr_group_cnt > items_per_fold:
                curr_group += 1
                curr_group_cnt = 0
            curr_group_cnt += len(v)
            for idx in v:
                _filters[curr_group][idx] = True
        _it = iter(_filters)
        _len = len(next(_it))
        if not all(len(l) == _len for l in _it):
            raise ValueError('not all lists have same length!')
        if not all(reduce(np.logical_or, _filters)):
            raise ValueError('some items are not in any fold')
        if not all([sum(x)==1 for x in zip(*_filters)]):
            raise ValueError('some items are in more than one fold')
        return _filters
def _kfold_split ( self , folds : int , seed : int = 0 ) -> List [ List [ bool ] ] : items_per_fold = int ( len ( self . _data_train ) / folds ) items_used = [ False ] * len ( self . _data_train ) _filters = [ [ False ] * len ( self . _data_train ) for x in range ( folds ) ] group_iterator = self . _data_train . groupby ( [ 'patient' ] , as_index = False ) group_list = list ( group_iterator . groups . values ( ) ) random . seed ( seed ) random . shuffle ( group_list ) curr_group = 0 curr_group_cnt = 0 for v in group_list : if curr_group_cnt > items_per_fold : curr_group += 1 curr_group_cnt = 0 curr_group_cnt += len ( v ) for idx in v : _filters [ curr_group ] [ idx ] = True _it = iter ( _filters ) _len = len ( next ( _it ) ) if not all ( len ( l ) == _len for l in _it ) : raise ValueError ( 'not all lists have same length!' ) if not all ( reduce ( np . logical_or , _filters ) ) : raise ValueError ( 'some items are not in any fold' ) if not all ( [ sum ( x ) == 1 for x in zip ( * _filters ) ] ) : raise ValueError ( 'some items are in more than one fold' ) return _filters
Performs a k-fold split of self._data_train.
Performs a k - fold split of self . _data_train .
'''
        Performs a k-fold split of self._data_train.
        A boolean filter list must be created for each split.
        A list of all these filters must be returned.
        I.e.,
            [[True, True, False, False],
             [False, False, True, True]]
        For a 2-fold split of a dataset of size 4

        The return value must be a list of :param:folds lists.
        Where each list must be precisely len(self._data_train) items long.
        There should be roughly the same number of elements in each split.
        Each index must be True exatly once (I.g., each data point is contained
          in exactly one split).
        It should be taken into account that all images of an individual
          patient are in the same split.
        The splits should be randomized but it must return identical results
          when the same seed is passed.
        ''' # Currently just splits into :param:folds strides # I.e., [[True, True, False, False, False, False], #        [False, False, True, True, False, False], #        [False, False, False, False, True, True]] # Where the last first fold-1 items contains the same amout of dataset, # and the last item contains slighly more or less, depeneding on how # well len(self._data_train) is divisable by :param:folds # makes sure that all images of a single patient are in the same fold # Do not delete the following # makes sure all lists are of the same length # makes sure each element is in at least one fold # makes sure each element is in exactly one fold
self
folds
int
seed
int