'''
Author: <NAME>
Created Date: 2021-08-08
Last Modified: 2021-08-31
	content: 
'''

from mmcv.utils import print_log

from .builder import DATASETS
from .dataset_wrappers import ConcatDataset


@DATASETS.register_module()
class MyConcatDataset(ConcatDataset):
    """concatenated dataset adapted from ConcatDataset of MMDetection, only for separate validation

    Args:
        datasets (list[:obj:`Dataset`]): A list of datasets.
        separate_eval (bool): whether to calculate the metrics separately. 
            Default: True
    """

    def __init__(self, datasets, separate_eval=True, ):
        super().__init__(datasets)
        self.separate_eval = separate_eval

        if not separate_eval:
            if len(set([type(ds) for ds in datasets])) != 1:
                raise NotImplementedError(
                    'All the datasets should have same types')

    def evaluate(self, results, logger=None, **kwargs):
        """Evaluate the results.

        Args:
            results (list[list | tuple]): Testing results of the dataset.
            logger (logging.Logger | str | None): Logger used for printing
                related information during evaluation. Default: None.

        Returns:
            dict[str: float]: AP results of the total dataset or each separate
            dataset if `self.separate_eval=True`.
        """
        assert len(results) == self.cumulative_sizes[-1], \
            ('Dataset and results have different sizes: '
             f'{self.cumulative_sizes[-1]} v.s. {len(results)}')

        # Check whether all the datasets support evaluation
        for dataset in self.datasets:
            assert hasattr(dataset, 'evaluate'), \
                    f'{type(dataset)} does not implement evaluate function'

        if self.separate_eval:
            dataset_idx = -1
            total_eval_results = dict()
            for size, dataset in zip(self.cumulative_sizes, self.datasets):
                start_idx = 0 if dataset_idx == -1 else \
                    self.cumulative_sizes[dataset_idx]
                end_idx = self.cumulative_sizes[dataset_idx + 1]

                results_per_dataset = results[start_idx:end_idx]
                print_log(
                    f'\nEvaluateing {dataset.ann_dir} with '
                    f'{len(results_per_dataset)} images now',
                    logger=logger)

                eval_results_per_dataset = dataset.evaluate(
                    results_per_dataset, logger=logger, **kwargs)
                dataset_idx += 1
                for k, v in eval_results_per_dataset.items():
                    total_eval_results.update({f'{dataset_idx}_{k}': v})

            return total_eval_results
        elif len(set([type(ds) for ds in self.datasets])) != 1:
            raise NotImplementedError(
                'All the datasets should have same types')
        else:
            original_data_infos = self.datasets[0].data_infos
            self.datasets[0].data_infos = sum(
                [dataset.data_infos for dataset in self.datasets], [])
            eval_results = self.datasets[0].evaluate(
                results, logger=logger, **kwargs)
            self.datasets[0].data_infos = original_data_infos
            return eval_results

'''
Author: <NAME>
Created Date: 2021-08-08
Last Modified: 2021-08-31
	content: 
'''

from mmcv.utils import print_log

from .builder import DATASETS
from .dataset_wrappers import ConcatDataset


@DATASETS.register_module()
class MyConcatDataset(ConcatDataset):
    """concatenated dataset adapted from ConcatDataset of MMDetection, only for separate validation

    Args:
        datasets (list[:obj:`Dataset`]): A list of datasets.
        separate_eval (bool): whether to calculate the metrics separately. 
            Default: True
    """

    def __init__(self, datasets, separate_eval=True, ):
        super().__init__(datasets)
        self.separate_eval = separate_eval

        if not separate_eval:
            if len(set([type(ds) for ds in datasets])) != 1:
                raise NotImplementedError(
                    'All the datasets should have same types')

    def evaluate(self, results, logger=None, **kwargs):
        """Evaluate the results.

        Args:
            results (list[list | tuple]): Testing results of the dataset.
            logger (logging.Logger | str | None): Logger used for printing
                related information during evaluation. Default: None.

        Returns:
            dict[str: float]: AP results of the total dataset or each separate
            dataset if `self.separate_eval=True`.
        """
        assert len(results) == self.cumulative_sizes[-1], \
            ('Dataset and results have different sizes: '
             f'{self.cumulative_sizes[-1]} v.s. {len(results)}')

        # Check whether all the datasets support evaluation
        for dataset in self.datasets:
            assert hasattr(dataset, 'evaluate'), \
                    f'{type(dataset)} does not implement evaluate function'

        if self.separate_eval:
            dataset_idx = -1
            total_eval_results = dict()
            for size, dataset in zip(self.cumulative_sizes, self.datasets):
                start_idx = 0 if dataset_idx == -1 else \
                    self.cumulative_sizes[dataset_idx]
                end_idx = self.cumulative_sizes[dataset_idx + 1]

                results_per_dataset = results[start_idx:end_idx]
                print_log(
                    f'\nEvaluateing {dataset.ann_dir} with '
                    f'{len(results_per_dataset)} images now',
                    logger=logger)

                eval_results_per_dataset = dataset.evaluate(
                    results_per_dataset, logger=logger, **kwargs)
                dataset_idx += 1
                for k, v in eval_results_per_dataset.items():
                    total_eval_results.update({f'{dataset_idx}_{k}': v})

            return total_eval_results
        elif len(set([type(ds) for ds in self.datasets])) != 1:
            raise NotImplementedError(
                'All the datasets should have same types')
        else:
            original_data_infos = self.datasets[0].data_infos
            self.datasets[0].data_infos = sum(
                [dataset.data_infos for dataset in self.datasets], [])
            eval_results = self.datasets[0].evaluate(
                results, logger=logger, **kwargs)
            self.datasets[0].data_infos = original_data_infos
            return eval_results

Author: <NAME> Created Date: 2021-08-08 Last Modified: 2021-08-31 	content: concatenated dataset adapted from ConcatDataset of MMDetection, only for separate validation      Args:         datasets (list[:obj:`Dataset`]): A list of datasets.         separate_eval (bool): whether to calculate the metrics separately.              Default: True Evaluate the results.          Args:             results (list[list | tuple]): Testing results of the dataset.             logger (logging.Logger | str | None): Logger used for printing                 related information during evaluation. Default: None.          Returns:             dict[str: float]: AP results of the total dataset or each separate             dataset if `self.separate_eval=True`. # Check whether all the datasets support evaluation