






import random
import unittest

import numpy as np

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():
      import PIL

      from transformers import FlavaImageProcessor
      from transformers.image_utils import PILImageResampling
      from transformers.models.flava.image_processing_flava import (
          FLAVA_CODEBOOK_MEAN,
          FLAVA_CODEBOOK_STD,
          FLAVA_IMAGE_MEAN,
          FLAVA_IMAGE_STD,
      )
else:
      __UpperCAmelCase      				=							__UpperCAmelCase      				=							__UpperCAmelCase      				=							__UpperCAmelCase      				=							None
class 					a_(						unittest.TestCase					):



     """simple docstring"""



     def __init__(						self			:					Dict       ,       lowerCAmelCase__			:					List[Any]       ,       lowerCAmelCase__			:					Union[str, Any]=7       ,       lowerCAmelCase__			:					Optional[Any]=3       ,       lowerCAmelCase__			:					str=3_0       ,       lowerCAmelCase__			:					List[str]=4_0_0       ,       lowerCAmelCase__			:					Any=True       ,       lowerCAmelCase__			:					Dict=None       ,       lowerCAmelCase__			:					Dict=True       ,       lowerCAmelCase__			:					Dict=None       ,       lowerCAmelCase__			:					Optional[Any]=None       ,       lowerCAmelCase__			:					List[Any]=True       ,       lowerCAmelCase__			:					Dict=1 / 2_5_5       ,       lowerCAmelCase__			:					Any=True       ,       lowerCAmelCase__			:					str=FLAVA_IMAGE_MEAN       ,       lowerCAmelCase__			:					List[Any]=FLAVA_IMAGE_STD       ,       lowerCAmelCase__			:					List[Any]=1_4       ,       lowerCAmelCase__			:					Union[str, Any]=7_5       ,       lowerCAmelCase__			:					str=None       ,       lowerCAmelCase__			:					List[str]=1_6       ,       lowerCAmelCase__			:					List[str]=0.3       ,       lowerCAmelCase__			:					Dict=None       ,       lowerCAmelCase__			:					List[str]=True       ,       lowerCAmelCase__			:					Any=None       ,       lowerCAmelCase__			:					Dict=None       ,       lowerCAmelCase__			:					List[str]=True       ,       lowerCAmelCase__			:					int=None       ,       lowerCAmelCase__			:					Tuple=True       ,       lowerCAmelCase__			:					List[Any]=True       ,       lowerCAmelCase__			:					Tuple=FLAVA_CODEBOOK_MEAN       ,       lowerCAmelCase__			:					Optional[int]=FLAVA_CODEBOOK_STD       ,       )    ->					Union[str, Any]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
      SCREAMING_SNAKE_CASE							  =						crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
      SCREAMING_SNAKE_CASE							  =						codebook_size if codebook_size is not None else {'height': 1_1_2, 'width': 1_1_2}
      SCREAMING_SNAKE_CASE							  =						codebook_crop_size if codebook_crop_size is not None else {'height': 1_1_2, 'width': 1_1_2}

      SCREAMING_SNAKE_CASE							  =						parent
      SCREAMING_SNAKE_CASE							  =						batch_size
      SCREAMING_SNAKE_CASE							  =						num_channels
      SCREAMING_SNAKE_CASE							  =						do_resize
      SCREAMING_SNAKE_CASE							  =						do_rescale
      SCREAMING_SNAKE_CASE							  =						rescale_factor
      SCREAMING_SNAKE_CASE							  =						min_resolution
      SCREAMING_SNAKE_CASE							  =						max_resolution
      SCREAMING_SNAKE_CASE							  =						size
      SCREAMING_SNAKE_CASE							  =						resample if resample is not None else PILImageResampling.BICUBIC
      SCREAMING_SNAKE_CASE							  =						do_normalize
      SCREAMING_SNAKE_CASE							  =						image_mean
      SCREAMING_SNAKE_CASE							  =						image_std
      SCREAMING_SNAKE_CASE							  =						do_center_crop
      SCREAMING_SNAKE_CASE							  =						crop_size

      SCREAMING_SNAKE_CASE							  =						input_size_patches
      SCREAMING_SNAKE_CASE							  =						total_mask_patches
      SCREAMING_SNAKE_CASE							  =						mask_group_max_patches
      SCREAMING_SNAKE_CASE							  =						mask_group_min_patches
      SCREAMING_SNAKE_CASE							  =						mask_group_min_aspect_ratio
      SCREAMING_SNAKE_CASE							  =						mask_group_max_aspect_ratio

      SCREAMING_SNAKE_CASE							  =						codebook_do_resize
      SCREAMING_SNAKE_CASE							  =						codebook_size
      SCREAMING_SNAKE_CASE							  =						codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
      SCREAMING_SNAKE_CASE							  =						codebook_do_center_crop
      SCREAMING_SNAKE_CASE							  =						codebook_crop_size
      SCREAMING_SNAKE_CASE							  =						codebook_do_map_pixels
      SCREAMING_SNAKE_CASE							  =						codebook_do_normalize
      SCREAMING_SNAKE_CASE							  =						codebook_image_mean
      SCREAMING_SNAKE_CASE							  =						codebook_image_std





     def   __UpperCamelCase		(						self			:					Optional[Any])    ->					Any:

      """simple docstring"""
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
          "resample": self.resample,
          "do_rescale": self.do_rescale,
          "rescale_factor": self.rescale_factor,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
          "input_size_patches": self.input_size_patches,
          "total_mask_patches": self.total_mask_patches,
          "mask_group_max_patches": self.mask_group_max_patches,
          "mask_group_min_patches": self.mask_group_min_patches,
          "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
          "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
          "codebook_do_resize": self.codebook_do_resize,
          "codebook_size": self.codebook_size,
          "codebook_resample": self.codebook_resample,
          "codebook_do_center_crop": self.codebook_do_center_crop,
          "codebook_crop_size": self.codebook_crop_size,
          "codebook_do_map_pixels": self.codebook_do_map_pixels,
          "codebook_do_normalize": self.codebook_do_normalize,
          "codebook_image_mean": self.codebook_image_mean,
          "codebook_image_std": self.codebook_image_std,
      }





     def   __UpperCamelCase		(						self			:					Union[str, Any])    ->					Optional[int]:

      """simple docstring"""
      return (self.size["height"], self.size["width"])





     def   __UpperCamelCase		(						self			:					str)    ->					Any:

      """simple docstring"""
      return (
          (self.input_size_patches, self.input_size_patches)
          if not isinstance(self.input_size_patches       ,       lowerCAmelCase__)
          else self.input_size_patches
      )





     def   __UpperCamelCase		(						self			:					Any)    ->					int:

      """simple docstring"""
      return (self.codebook_size["height"], self.codebook_size["width"])

@require_torch
@require_vision
class 					a_(						lowercase__	,    unittest.TestCase					):



     """simple docstring"""



     __snake_case						:							Optional[Any]       			=FlavaImageProcessor if is_vision_available() else None
     __snake_case						:							Dict       			=None





     def   __UpperCamelCase		(						self			:					List[Any])    ->					int:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						FlavaImageProcessingTester(self)





     @property
     def   __UpperCamelCase		(						self			:					str)    ->					Dict:

      """simple docstring"""
      return self.image_processor_tester.prepare_image_processor_dict()





     def   __UpperCamelCase		(						self			:					str)    ->					Optional[Any]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'image_mean'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'image_std'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'do_normalize'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'do_resize'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'resample'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'crop_size'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'do_center_crop'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'do_rescale'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'rescale_factor'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'masking_generator'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_do_resize'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_size'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_resample'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_do_center_crop'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_crop_size'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_do_map_pixels'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_do_normalize'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_image_mean'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'codebook_image_std'))





     def   __UpperCamelCase		(						self			:					int)    ->					str:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class.from_dict(self.image_processor_dict)
      self.assertEqual(image_processor.size       ,       {'height': 2_2_4, 'width': 2_2_4})
      self.assertEqual(image_processor.crop_size       ,       {'height': 2_2_4, 'width': 2_2_4})
      self.assertEqual(image_processor.codebook_size       ,       {'height': 1_1_2, 'width': 1_1_2})
      self.assertEqual(image_processor.codebook_crop_size       ,       {'height': 1_1_2, 'width': 1_1_2})

      SCREAMING_SNAKE_CASE							  =						self.image_processing_class.from_dict(
          self.image_processor_dict       ,       size=4_2       ,       crop_size=8_4       ,       codebook_size=3_3       ,       codebook_crop_size=6_6)
      self.assertEqual(image_processor.size       ,       {'height': 4_2, 'width': 4_2})
      self.assertEqual(image_processor.crop_size       ,       {'height': 8_4, 'width': 8_4})
      self.assertEqual(image_processor.codebook_size       ,       {'height': 3_3, 'width': 3_3})
      self.assertEqual(image_processor.codebook_crop_size       ,       {'height': 6_6, 'width': 6_6})





     def   __UpperCamelCase		(						self			:					List[str])    ->					int:

      """simple docstring"""
      pass





     def   __UpperCamelCase		(						self			:					Optional[int])    ->					str:

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      # create random PIL images
      SCREAMING_SNAKE_CASE							  =						prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=lowerCAmelCase__)
      for image in image_inputs:
       self.assertIsInstance(lowerCAmelCase__       ,       PIL.Image.Image)

      # Test not batched input
      SCREAMING_SNAKE_CASE							  =						image_processing(image_inputs[0]       ,       return_tensors='pt')

      # Test no bool masked pos
      self.assertFalse('bool_masked_pos' in encoded_images)

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_image_size()

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

      # Test batched
      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       return_tensors='pt')
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_image_size()

      # Test no bool masked pos
      self.assertFalse('bool_masked_pos' in encoded_images)

      self.assertEqual(
          encoded_images.pixel_values.shape       ,       (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )       ,       )





     def   __UpperCamelCase		(						self			:					List[Any]       ,       lowerCAmelCase__			:					Any       ,       lowerCAmelCase__			:					int)    ->					List[str]:

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      # create random tensors
      SCREAMING_SNAKE_CASE							  =						prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=lowerCAmelCase__       ,       **lowerCAmelCase__)
      for image in image_inputs:
       self.assertIsInstance(lowerCAmelCase__       ,       lowerCAmelCase__)

      # Test not batched input
      SCREAMING_SNAKE_CASE							  =						image_processing(image_inputs[0]       ,       return_tensors='pt')

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_image_size()
      self.assertEqual(
          encoded_images.pixel_values.shape       ,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,       )

      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       return_image_mask=lowerCAmelCase__       ,       return_tensors='pt')

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_image_size()
      self.assertEqual(
          encoded_images.pixel_values.shape       ,       (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )       ,       )

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_mask_size()
      self.assertEqual(
          encoded_images.bool_masked_pos.shape       ,       (
              self.image_processor_tester.batch_size,
              expected_height,
              expected_width,
          )       ,       )

      # Test batched
      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       return_tensors='pt').pixel_values

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_image_size()
      self.assertEqual(
          encoded_images.shape       ,       (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )       ,       )

      # Test masking
      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       return_image_mask=lowerCAmelCase__       ,       return_tensors='pt')

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_image_size()
      self.assertEqual(
          encoded_images.pixel_values.shape       ,       (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )       ,       )

      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_mask_size()
      self.assertEqual(
          encoded_images.bool_masked_pos.shape       ,       (
              self.image_processor_tester.batch_size,
              expected_height,
              expected_width,
          )       ,       )





     def   __UpperCamelCase		(						self			:					Dict)    ->					Union[str, Any]:

      """simple docstring"""
      self._test_call_framework(np.ndarray       ,       prepare_kwargs={'numpify': True})





     def   __UpperCamelCase		(						self			:					Optional[int])    ->					str:

      """simple docstring"""
      self._test_call_framework(torch.Tensor       ,       prepare_kwargs={'torchify': True})





     def   __UpperCamelCase		(						self			:					List[Any])    ->					List[Any]:

      """simple docstring"""
      # Initialize image_processing
      random.seed(1_2_3_4)
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      SCREAMING_SNAKE_CASE							  =						prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=lowerCAmelCase__       ,       torchify=lowerCAmelCase__)

      # Test not batched input
      SCREAMING_SNAKE_CASE							  =						image_processing(image_inputs[0]       ,       return_image_mask=lowerCAmelCase__       ,       return_tensors='pt')
      self.assertEqual(encoded_images.bool_masked_pos.sum().item()       ,       7_5)





     def   __UpperCamelCase		(						self			:					Union[str, Any])    ->					Any:

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      # create random PIL images
      SCREAMING_SNAKE_CASE							  =						prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=lowerCAmelCase__)
      for image in image_inputs:
       self.assertIsInstance(lowerCAmelCase__       ,       PIL.Image.Image)

      # Test not batched input
      SCREAMING_SNAKE_CASE							  =						image_processing(image_inputs[0]       ,       return_codebook_pixels=lowerCAmelCase__       ,       return_tensors='pt')
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_codebook_image_size()
      self.assertEqual(
          encoded_images.codebook_pixel_values.shape       ,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,       )

      # Test batched
      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       return_codebook_pixels=lowerCAmelCase__       ,       return_tensors='pt')
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						self.image_processor_tester.get_expected_codebook_image_size()
      self.assertEqual(
          encoded_images.codebook_pixel_values.shape       ,       (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )       ,       )












from datetime import date

import requests

__UpperCAmelCase      				=							"https://api.carbonintensity.org.uk/intensity"





def 		A_			(		)							->str:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						requests.get(lowercase_	).json()['data'][0]
 return last_half_hour["intensity"]["actual"]





def 		A_			(		lowercase_					,       lowercase_	)							->list:







 """simple docstring"""

 return requests.get(f'''{BASE_URL}/{start}/{end}'''	).json()["data"]


if __name__ == "__main__":
      for entry in fetch_from_to(start=date(2_0_2_0, 1_0, 1), end=date(2_0_2_0, 1_0, 3)):
            print("from {from} to {to}: {intensity[actual]}".format(**entry))
      print(f'{fetch_last_half_hour() = }')




