





from scipy.stats import spearmanr

import datasets


_A :   str  			=  '\nThe Spearman rank-order correlation coefficient is a measure of the\nrelationship between two datasets. Like other correlation coefficients,\nthis one varies between -1 and +1 with 0 implying no correlation.\nPositive correlations imply that as data in dataset x increases, so\ndoes data in dataset y. Negative correlations imply that as x increases,\ny decreases. Correlations of -1 or +1 imply an exact monotonic relationship.\n\nUnlike the Pearson correlation, the Spearman correlation does not\nassume that both datasets are normally distributed.\n\nThe p-value roughly indicates the probability of an uncorrelated system\nproducing datasets that have a Spearman correlation at least as extreme\nas the one computed from these datasets. The p-values are not entirely\nreliable but are probably reasonable for datasets larger than 500 or so.\n'

_A :   Any  			=  '\nArgs:\n    predictions (`List[float]`): Predicted labels, as returned by a model.\n    references (`List[float]`): Ground truth labels.\n    return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns\n            only the spearmanr score. Defaults to `False`.\nReturns:\n    spearmanr (`float`): Spearman correlation coefficient.\n    p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input.\nExamples:\n    Example 1:\n        >>> spearmanr_metric = datasets.load_metric("spearmanr")\n        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4])\n        >>> print(results)\n        {\'spearmanr\': -0.7}\n\n    Example 2:\n        >>> spearmanr_metric = datasets.load_metric("spearmanr")\n        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5],\n        ...                                     predictions=[10, 9, 2.5, 6, 4],\n        ...                                     return_pvalue=True)\n        >>> print(results[\'spearmanr\'])\n        -0.7\n        >>> print(round(results[\'spearmanr_pvalue\'], 2))\n        0.19\n'

_A :   str  			=  r'\\n@book{kokoska2000crc,\n  title={CRC standard probability and statistics tables and formulae},\n  author={Kokoska, Stephen and Zwillinger, Daniel},\n  year={2000},\n  publisher={Crc Press}\n}\n@article{2020SciPy-NMeth,\n  author  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n            Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n            Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n            Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n            Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n            Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n            Kern, Robert and Larson, Eric and Carey, C J and\n            Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and\n            {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n            Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n            Harris, Charles R. and Archibald, Anne M. and\n            Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and\n            {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\n  title   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n            Computing in Python}},\n  journal = {Nature Methods},\n  year    = {2020},\n  volume  = {17},\n  pages   = {261--272},\n  adsurl  = {https://rdcu.be/b08Wh},\n  doi     = {10.1038/s41592-019-0686-2},\n}\n'


@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION					,_KWARGS_DESCRIPTION )
class        __SCREAMING_SNAKE_CASE      (			datasets.Metric ):







       def 						__lowerCamelCase   (				self					:     Optional[Any]      )       ->str:
             return datasets.MetricInfo(
                 description=_DESCRIPTION		, citation=_CITATION		, inputs_description=_KWARGS_DESCRIPTION		, features=datasets.Features(
                     {
                         '''predictions''': datasets.Value('''float'''      ),
                         '''references''': datasets.Value('''float'''      ),
                     }      )		, reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html''']		, )







       def 						__lowerCamelCase   (				self					:     Any		, A					:     str		, A					:     Any		, A					:     List[Any]=False      )       ->Tuple:
             lowerCamelCase__       :						str        =							spearmanr(A		, A      )
             if return_pvalue:
                   return {"spearmanr": results[0], "spearmanr_pvalue": results[1]}
             else:
                   return {"spearmanr": results[0]}










import json
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
       import torch

       if is_vision_available():
              from transformers import OneFormerImageProcessor
              from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle
              from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput

if is_vision_available():
       from PIL import Image



def 	_a						(   UpperCAmelCase      ,  UpperCAmelCase="shi-labs/oneformer_demo"					)    ->    Union[str, Any]:







   """simple docstring"""
   with open(hf_hub_download(UpperCAmelCase      ,  UpperCAmelCase      ,  repo_type='''dataset'''					)      ,  '''r'''					) as f:
      lowerCamelCase__       :						List[Any]        =							json.load(UpperCAmelCase					)
   lowerCamelCase__       :						Dict        =							{}
   lowerCamelCase__       :						str        =							[]
   lowerCamelCase__       :						Optional[Any]        =							[]
   for key, info in class_info.items():
      lowerCamelCase__       :						Union[str, Any]        =							info['''name''']
      class_names.append(info['''name''']					)
      if info["isthing"]:
         thing_ids.append(int(UpperCAmelCase					)					)
   lowerCamelCase__       :						Optional[int]        =							thing_ids
   lowerCamelCase__       :						Dict        =							class_names
   return metadata


class        __SCREAMING_SNAKE_CASE      (			unittest.TestCase ):







       def __init__(				self					:     Tuple		, A					:     Tuple		, A					:     List[Any]=7		, A					:     str=3		, A					:     List[str]=3_0		, A					:     Optional[int]=4_0_0		, A					:     int=None		, A					:     Tuple=True		, A					:     Dict=True		, A					:     Dict=[0.5, 0.5, 0.5]		, A					:     Tuple=[0.5, 0.5, 0.5]		, A					:     int=1_0		, A					:     List[str]=False		, A					:     Optional[Any]=2_5_5		, A					:     Union[str, Any]="shi-labs/oneformer_demo"		, A					:     Optional[Any]="ade20k_panoptic.json"		, A					:     str=1_0		, )       ->Dict:
          lowerCamelCase__       :						Dict        =							parent
          lowerCamelCase__       :						List[Any]        =							batch_size
          lowerCamelCase__       :						Dict        =							num_channels
          lowerCamelCase__       :						List[str]        =							min_resolution
          lowerCamelCase__       :						str        =							max_resolution
          lowerCamelCase__       :						Optional[Any]        =							do_resize
          lowerCamelCase__       :						Any        =							{'''shortest_edge''': 3_2, '''longest_edge''': 1_3_3_3} if size is None else size
          lowerCamelCase__       :						str        =							do_normalize
          lowerCamelCase__       :						List[str]        =							image_mean
          lowerCamelCase__       :						List[str]        =							image_std
          lowerCamelCase__       :						Optional[int]        =							class_info_file
          lowerCamelCase__       :						Any        =							prepare_metadata(A		, A      )
          lowerCamelCase__       :						str        =							num_text
          lowerCamelCase__       :						Dict        =							repo_path

          # for the post_process_functions
          lowerCamelCase__       :						str        =							2
          lowerCamelCase__       :						Union[str, Any]        =							1_0
          lowerCamelCase__       :						List[Any]        =							1_0
          lowerCamelCase__       :						List[Any]        =							3
          lowerCamelCase__       :						Tuple        =							4
          lowerCamelCase__       :						List[Any]        =							num_labels
          lowerCamelCase__       :						List[Any]        =							do_reduce_labels
          lowerCamelCase__       :						List[Any]        =							ignore_index







       def 						__lowerCamelCase   (				self					:     str      )       ->Dict:
          return {
              "do_resize": self.do_resize,
              "size": self.size,
              "do_normalize": self.do_normalize,
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "num_labels": self.num_labels,
              "do_reduce_labels": self.do_reduce_labels,
              "ignore_index": self.ignore_index,
              "class_info_file": self.class_info_file,
              "metadata": self.metadata,
              "num_text": self.num_text,
          }







       def 						__lowerCamelCase   (				self					:     List[str]		, A					:     List[Any]		, A					:     Tuple=False      )       ->int:
          if not batched:
             lowerCamelCase__       :						List[Any]        =							image_inputs[0]
             if isinstance(A		, Image.Image      ):
                lowerCamelCase__       ,							lowerCamelCase__       :						str        =							image.size
             else:
                lowerCamelCase__       ,							lowerCamelCase__       :						str        =							image.shape[1], image.shape[2]
             if w < h:
                lowerCamelCase__       :						Any        =							int(self.size['''shortest_edge'''] * h / w      )
                lowerCamelCase__       :						Tuple        =							self.size['''shortest_edge''']
             elif w > h:
                lowerCamelCase__       :						Union[str, Any]        =							self.size['''shortest_edge''']
                lowerCamelCase__       :						Dict        =							int(self.size['''shortest_edge'''] * w / h      )
             else:
                lowerCamelCase__       :						Union[str, Any]        =							self.size['''shortest_edge''']
                lowerCamelCase__       :						Optional[Any]        =							self.size['''shortest_edge''']

          else:
             lowerCamelCase__       :						Any        =							[]
             for image in image_inputs:
                lowerCamelCase__       ,							lowerCamelCase__       :						List[str]        =							self.get_expected_values([image]      )
                expected_values.append((expected_height, expected_width)      )
             lowerCamelCase__       :						Optional[Any]        =							max(A		, key=lambda A      : item[0]      )[0]
             lowerCamelCase__       :						List[str]        =							max(A		, key=lambda A      : item[1]      )[1]

          return expected_height, expected_width







       def 						__lowerCamelCase   (				self					:     Optional[int]      )       ->List[str]:
          return OneFormerForUniversalSegmentationOutput(
              # +1 for null class
              class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1)      )		, masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width)      )		, )


@require_torch
@require_vision
class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_					,unittest.TestCase ):
       _UpperCAmelCase				:  Optional[int]			   =   OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None
       # only for test_image_processing_common.test_image_proc_to_json_string
       _UpperCAmelCase				:  Dict			   =   image_processing_class







       def 						__lowerCamelCase   (				self					:     Optional[int]      )       ->str:
          lowerCamelCase__       :						Optional[int]        =							OneFormerImageProcessorTester(self      )







       @property
       def 						__lowerCamelCase   (				self					:     List[str]      )       ->Any:
          return self.image_processing_tester.prepare_image_processor_dict()







       def 						__lowerCamelCase   (				self					:     int      )       ->Tuple:
          lowerCamelCase__       :						List[str]        =							self.image_processing_class(**self.image_processor_dict      )
          self.assertTrue(hasattr(A		, '''image_mean'''      )      )
          self.assertTrue(hasattr(A		, '''image_std'''      )      )
          self.assertTrue(hasattr(A		, '''do_normalize'''      )      )
          self.assertTrue(hasattr(A		, '''do_resize'''      )      )
          self.assertTrue(hasattr(A		, '''size'''      )      )
          self.assertTrue(hasattr(A		, '''ignore_index'''      )      )
          self.assertTrue(hasattr(A		, '''class_info_file'''      )      )
          self.assertTrue(hasattr(A		, '''num_text'''      )      )
          self.assertTrue(hasattr(A		, '''repo_path'''      )      )
          self.assertTrue(hasattr(A		, '''metadata'''      )      )
          self.assertTrue(hasattr(A		, '''do_reduce_labels'''      )      )







       def 						__lowerCamelCase   (				self					:     Any      )       ->Tuple:
          pass







       def 						__lowerCamelCase   (				self					:     Optional[Any]      )       ->Optional[int]:
          # Initialize image_processor
          lowerCamelCase__       :						Union[str, Any]        =							self.image_processing_class(**self.image_processor_dict      )
          # create random PIL images
          lowerCamelCase__       :						List[Any]        =							prepare_image_inputs(self.image_processing_tester		, equal_resolution=A      )
          for image in image_inputs:
             self.assertIsInstance(A		, Image.Image      )

          # Test not batched input
          lowerCamelCase__       :						Any        =							image_processor(image_inputs[0]		, ['''semantic''']		, return_tensors='''pt'''      ).pixel_values

          lowerCamelCase__       ,							lowerCamelCase__       :						Tuple        =							self.image_processing_tester.get_expected_values(A      )

          self.assertEqual(
              encoded_images.shape		, (1, self.image_processing_tester.num_channels, expected_height, expected_width)		, )

          # Test batched
          lowerCamelCase__       ,							lowerCamelCase__       :						Tuple        =							self.image_processing_tester.get_expected_values(A		, batched=A      )

          lowerCamelCase__       :						List[str]        =							image_processor(
              A		, ['''semantic'''] * len(A      )		, return_tensors='''pt'''      ).pixel_values
          self.assertEqual(
              encoded_images.shape		, (
                  self.image_processing_tester.batch_size,
                  self.image_processing_tester.num_channels,
                  expected_height,
                  expected_width,
              )		, )







       def 						__lowerCamelCase   (				self					:     Tuple      )       ->Tuple:
          # Initialize image_processor
          lowerCamelCase__       :						Union[str, Any]        =							self.image_processing_class(**self.image_processor_dict      )
          # create random numpy tensors
          lowerCamelCase__       :						List[str]        =							prepare_image_inputs(self.image_processing_tester		, equal_resolution=A		, numpify=A      )
          for image in image_inputs:
             self.assertIsInstance(A		, np.ndarray      )

          # Test not batched input
          lowerCamelCase__       :						List[str]        =							image_processor(image_inputs[0]		, ['''semantic''']		, return_tensors='''pt'''      ).pixel_values

          lowerCamelCase__       ,							lowerCamelCase__       :						Dict        =							self.image_processing_tester.get_expected_values(A      )

          self.assertEqual(
              encoded_images.shape		, (1, self.image_processing_tester.num_channels, expected_height, expected_width)		, )

          # Test batched
          lowerCamelCase__       ,							lowerCamelCase__       :						str        =							self.image_processing_tester.get_expected_values(A		, batched=A      )

          lowerCamelCase__       :						int        =							image_processor(
              A		, ['''semantic'''] * len(A      )		, return_tensors='''pt'''      ).pixel_values
          self.assertEqual(
              encoded_images.shape		, (
                  self.image_processing_tester.batch_size,
                  self.image_processing_tester.num_channels,
                  expected_height,
                  expected_width,
              )		, )







       def 						__lowerCamelCase   (				self					:     int      )       ->str:
          # Initialize image_processor
          lowerCamelCase__       :						Any        =							self.image_processing_class(**self.image_processor_dict      )
          # create random PyTorch tensors
          lowerCamelCase__       :						int        =							prepare_image_inputs(self.image_processing_tester		, equal_resolution=A		, torchify=A      )
          for image in image_inputs:
             self.assertIsInstance(A		, torch.Tensor      )

          # Test not batched input
          lowerCamelCase__       :						int        =							image_processor(image_inputs[0]		, ['''semantic''']		, return_tensors='''pt'''      ).pixel_values

          lowerCamelCase__       ,							lowerCamelCase__       :						int        =							self.image_processing_tester.get_expected_values(A      )

          self.assertEqual(
              encoded_images.shape		, (1, self.image_processing_tester.num_channels, expected_height, expected_width)		, )

          # Test batched
          lowerCamelCase__       ,							lowerCamelCase__       :						Tuple        =							self.image_processing_tester.get_expected_values(A		, batched=A      )

          lowerCamelCase__       :						Any        =							image_processor(
              A		, ['''semantic'''] * len(A      )		, return_tensors='''pt'''      ).pixel_values
          self.assertEqual(
              encoded_images.shape		, (
                  self.image_processing_tester.batch_size,
                  self.image_processing_tester.num_channels,
                  expected_height,
                  expected_width,
              )		, )







       def 						__lowerCamelCase   (				self					:     Dict		, A					:     Tuple=False		, A					:     Dict=False		, A					:     Optional[Any]="np"      )       ->List[str]:
          lowerCamelCase__       :						Optional[Any]        =							self.image_processing_class(**self.image_processor_dict      )
          # prepare image and target
          lowerCamelCase__       :						Any        =							self.image_processing_tester.num_labels
          lowerCamelCase__       :						List[str]        =							None
          lowerCamelCase__       :						Any        =							None
          lowerCamelCase__       :						Union[str, Any]        =							prepare_image_inputs(self.image_processing_tester		, equal_resolution=A      )
          if with_segmentation_maps:
             lowerCamelCase__       :						int        =							num_labels
             if is_instance_map:
                lowerCamelCase__       :						str        =							list(range(A      )      ) * 2
                lowerCamelCase__       :						Union[str, Any]        =							dict(enumerate(A      )      )
             lowerCamelCase__       :						int        =							[
                 np.random.randint(0		, high * 2		, (img.size[1], img.size[0])      ).astype(np.uinta      ) for img in image_inputs
             ]
             if segmentation_type == "pil":
                lowerCamelCase__       :						Any        =							[Image.fromarray(A      ) for annotation in annotations]

          lowerCamelCase__       :						int        =							image_processor(
              A		, ['''semantic'''] * len(A      )		, A		, return_tensors='''pt'''		, instance_id_to_semantic_id=A		, pad_and_return_pixel_mask=A		, )

          return inputs







       def 						__lowerCamelCase   (				self					:     Dict      )       ->Optional[Any]:
          pass







       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Any:
          def common(A					:     Dict=False		, A					:     Tuple=None      ):
             lowerCamelCase__       :						str        =							self.comm_get_image_processor_inputs(
                 with_segmentation_maps=A		, is_instance_map=A		, segmentation_type=A      )

             lowerCamelCase__       :						Union[str, Any]        =							inputs['''mask_labels''']
             lowerCamelCase__       :						List[Any]        =							inputs['''class_labels''']
             lowerCamelCase__       :						List[str]        =							inputs['''pixel_values''']
             lowerCamelCase__       :						Union[str, Any]        =							inputs['''text_inputs''']

             # check the batch_size
             for mask_label, class_label, text_input in zip(A		, A		, A      ):
                self.assertEqual(mask_label.shape[0]		, class_label.shape[0]      )
                # this ensure padding has happened
                self.assertEqual(mask_label.shape[1:]		, pixel_values.shape[2:]      )
                self.assertEqual(len(A      )		, self.image_processing_tester.num_text      )

          common()
          common(is_instance_map=A      )
          common(is_instance_map=A		, segmentation_type='''pil'''      )
          common(is_instance_map=A		, segmentation_type='''pil'''      )







       def 						__lowerCamelCase   (				self					:     Any      )       ->Optional[int]:
          lowerCamelCase__       :						List[Any]        =							np.zeros((2_0, 5_0)      )
          lowerCamelCase__       :						Any        =							1
          lowerCamelCase__       :						Union[str, Any]        =							1
          lowerCamelCase__       :						List[str]        =							1

          lowerCamelCase__       :						str        =							binary_mask_to_rle(A      )
          self.assertEqual(len(A      )		, 4      )
          self.assertEqual(rle[0]		, 2_1      )
          self.assertEqual(rle[1]		, 4_5      )







       def 						__lowerCamelCase   (				self					:     int      )       ->Dict:
          lowerCamelCase__       :						int        =							self.image_processing_class(
              num_labels=self.image_processing_tester.num_classes		, max_seq_length=7_7		, task_seq_length=7_7		, class_info_file='''ade20k_panoptic.json'''		, num_text=self.image_processing_tester.num_text		, repo_path='''shi-labs/oneformer_demo'''		, )
          lowerCamelCase__       :						str        =							self.image_processing_tester.get_fake_oneformer_outputs()

          lowerCamelCase__       :						List[Any]        =							fature_extractor.post_process_semantic_segmentation(A      )

          self.assertEqual(len(A      )		, self.image_processing_tester.batch_size      )
          self.assertEqual(
              segmentation[0].shape		, (
                  self.image_processing_tester.height,
                  self.image_processing_tester.width,
              )		, )

          lowerCamelCase__       :						Any        =							[(1, 4) for i in range(self.image_processing_tester.batch_size      )]
          lowerCamelCase__       :						Optional[Any]        =							fature_extractor.post_process_semantic_segmentation(A		, target_sizes=A      )

          self.assertEqual(segmentation[0].shape		, target_sizes[0]      )







       def 						__lowerCamelCase   (				self					:     Tuple      )       ->Tuple:
          lowerCamelCase__       :						Optional[Any]        =							self.image_processing_class(
              num_labels=self.image_processing_tester.num_classes		, max_seq_length=7_7		, task_seq_length=7_7		, class_info_file='''ade20k_panoptic.json'''		, num_text=self.image_processing_tester.num_text		, repo_path='''shi-labs/oneformer_demo'''		, )
          lowerCamelCase__       :						Optional[int]        =							self.image_processing_tester.get_fake_oneformer_outputs()
          lowerCamelCase__       :						Union[str, Any]        =							image_processor.post_process_instance_segmentation(A		, threshold=0      )

          self.assertTrue(len(A      ) == self.image_processing_tester.batch_size      )
          for el in segmentation:
             self.assertTrue('''segmentation''' in el      )
             self.assertTrue('''segments_info''' in el      )
             self.assertEqual(type(el['''segments_info''']      )		, A      )
             self.assertEqual(
                 el['''segmentation'''].shape		, (self.image_processing_tester.height, self.image_processing_tester.width)      )







       def 						__lowerCamelCase   (				self					:     str      )       ->Dict:
          lowerCamelCase__       :						Union[str, Any]        =							self.image_processing_class(
              num_labels=self.image_processing_tester.num_classes		, max_seq_length=7_7		, task_seq_length=7_7		, class_info_file='''ade20k_panoptic.json'''		, num_text=self.image_processing_tester.num_text		, repo_path='''shi-labs/oneformer_demo'''		, )
          lowerCamelCase__       :						Tuple        =							self.image_processing_tester.get_fake_oneformer_outputs()
          lowerCamelCase__       :						List[str]        =							image_processor.post_process_panoptic_segmentation(A		, threshold=0      )

          self.assertTrue(len(A      ) == self.image_processing_tester.batch_size      )
          for el in segmentation:
             self.assertTrue('''segmentation''' in el      )
             self.assertTrue('''segments_info''' in el      )
             self.assertEqual(type(el['''segments_info''']      )		, A      )
             self.assertEqual(
                 el['''segmentation'''].shape		, (self.image_processing_tester.height, self.image_processing_tester.width)      )



