






"""simple docstring"""







import gc
import unittest

import numpy as np
import torch

from diffusers import DDPMScheduler, MidiProcessor, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder
from diffusers.utils import require_torch_gpu, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_note_seq, require_onnxruntime

from ..pipeline_params import TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS, TOKENS_TO_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


lowerCAmelCase_		:			Any				   =					'''./tests/fixtures/elise_format0.mid'''





@unittest.skip('The note-seq package currently throws an error on import'     )
class      UpperCamelCase_       (				a_      ,    unittest.TestCase     ):
  _A				:    Optional[int] =		SpectrogramDiffusionPipeline
  _A				:    Tuple =		PipelineTesterMixin.required_optional_params - {
      'callback',
      'latents',
      'callback_steps',
      'output_type',
      'num_images_per_prompt',
  }
  _A				:    Dict =		False
  _A				:    str =		False
  _A				:    Dict =		TOKENS_TO_AUDIO_GENERATION_PARAMS
  _A				:    Dict =		TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS



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



         """simple docstring"""





         torch.manual_seed(0 )
         UpperCAmelCase					=	SpectrogramNotesEncoder(
             max_length=20_48       ,						vocab_size=15_36       ,						d_model=7_68       ,						dropout_rate=0.1       ,						num_layers=1       ,						num_heads=1       ,						d_kv=4       ,						d_ff=20_48       ,						feed_forward_proj="""gated-gelu"""       ,						)

         UpperCAmelCase					=	SpectrogramContEncoder(
             input_dims=1_28       ,						targets_context_length=2_56       ,						d_model=7_68       ,						dropout_rate=0.1       ,						num_layers=1       ,						num_heads=1       ,						d_kv=4       ,						d_ff=20_48       ,						feed_forward_proj="""gated-gelu"""       ,						)

         UpperCAmelCase					=	TaFilmDecoder(
             input_dims=1_28       ,						targets_length=2_56       ,						max_decoder_noise_time=20_000.0       ,						d_model=7_68       ,						num_layers=1       ,						num_heads=1       ,						d_kv=4       ,						d_ff=20_48       ,						dropout_rate=0.1       ,						)

         UpperCAmelCase					=	DDPMScheduler()

         UpperCAmelCase					=	{
             """notes_encoder""": notes_encoder.eval(),
             """continuous_encoder""": continuous_encoder.eval(),
             """decoder""": decoder.eval(),
             """scheduler""": scheduler,
             """melgan""": None,
         }
         return components



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__=0 )	->	Optional[Any]:



         """simple docstring"""





         if str(snake_case__ ).startswith("""mps""" ):
                UpperCAmelCase					=	torch.manual_seed(snake_case__ )
         else:
                UpperCAmelCase					=	torch.Generator(device=snake_case__ ).manual_seed(snake_case__ )
         UpperCAmelCase					=	{
             """input_tokens""": [
                 [11_34, 90, 11_35, 11_33, 10_80, 1_12, 11_32, 10_80, 11_33, 10_79, 1_33, 11_32, 10_79, 11_33, 1] + [0] * 20_33
             ],
             """generator""": generator,
             """num_inference_steps""": 4,
             """output_type""": """mel""",
         }
         return inputs



  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





         UpperCAmelCase					=	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
         UpperCAmelCase					=	self.get_dummy_components()
         UpperCAmelCase					=	SpectrogramDiffusionPipeline(**snake_case__ )
         UpperCAmelCase					=	pipe.to(snake_case__ )
         pipe.set_progress_bar_config(disable=snake_case__ )

         UpperCAmelCase					=	self.get_dummy_inputs(snake_case__ )
         UpperCAmelCase					=	pipe(**snake_case__ )
         UpperCAmelCase					=	output.audios

         UpperCAmelCase					=	mel[0, -3:, -3:]

         assert mel_slice.shape == (3, 3)
         UpperCAmelCase					=	np.array(
             [-11.512_925, -4.788_215, -0.46_172_905, -2.051_715, -10.539_147, -10.970_963, -9.091_634, 4.0, 4.0] )
         assert np.abs(mel_slice.flatten() - expected_slice ).max() < 1e-2



  @skip_mps
  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         return super().test_save_load_local()



  @skip_mps
  def 						UpperCamelCase_ (					self )	->	int:



         """simple docstring"""





         return super().test_dict_tuple_outputs_equivalent()



  @skip_mps
  def 						UpperCamelCase_ (					self )	->	Union[str, Any]:



         """simple docstring"""





         return super().test_save_load_optional_components()



  @skip_mps
  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         return super().test_attention_slicing_forward_pass()



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



         """simple docstring"""





         pass



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



         """simple docstring"""





         pass



  @skip_mps
  def 						UpperCamelCase_ (					self )	->	List[str]:



         """simple docstring"""





         return super().test_progress_bar()





@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class      UpperCamelCase_       (				unittest.TestCase     ):



  def 						UpperCamelCase_ (					self )	->	List[str]:



         """simple docstring"""





         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()



  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         UpperCAmelCase					=	torch_device

         UpperCAmelCase					=	SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion""" )
         UpperCAmelCase					=	pipe.melgan
         UpperCAmelCase					=	None

         UpperCAmelCase					=	pipe.to(snake_case__ )
         pipe.set_progress_bar_config(disable=snake_case__ )

         def callback(snake_case__       ,						snake_case__ ):
                # decode mel to audio
                UpperCAmelCase					=	melgan(input_features=mel_output.astype(np.floataa ) )[0]
                assert len(audio[0] ) == 8_19_20 * (step + 1)
                # simulate that audio is played
                return audio

         UpperCAmelCase					=	MidiProcessor()
         UpperCAmelCase					=	processor(snake_case__ )

         UpperCAmelCase					=	input_tokens[:3]
         UpperCAmelCase					=	torch.manual_seed(0 )
         pipe(snake_case__       ,						num_inference_steps=5       ,						generator=snake_case__       ,						callback=snake_case__       ,						output_type="""mel""" )



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



         """simple docstring"""





         UpperCAmelCase					=	torch_device

         UpperCAmelCase					=	SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion""" )
         UpperCAmelCase					=	pipe.to(snake_case__ )
         pipe.set_progress_bar_config(disable=snake_case__ )
         UpperCAmelCase					=	MidiProcessor()

         UpperCAmelCase					=	processor(snake_case__ )
         # just run two denoising loops
         UpperCAmelCase					=	input_tokens[:2]

         UpperCAmelCase					=	torch.manual_seed(0 )
         UpperCAmelCase					=	pipe(snake_case__       ,						num_inference_steps=2       ,						generator=snake_case__ )

         UpperCAmelCase					=	output.audios[0]

         assert abs(np.abs(snake_case__ ).sum() - 3_612.841 ) < 1e-1



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	torch_device

         UpperCAmelCase					=	SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion""" )
         UpperCAmelCase					=	pipe.to(snake_case__ )
         pipe.set_progress_bar_config(disable=snake_case__ )

         UpperCAmelCase					=	MidiProcessor()

         UpperCAmelCase					=	processor(snake_case__ )

         # just run 4 denoising loops
         UpperCAmelCase					=	input_tokens[:4]

         UpperCAmelCase					=	torch.manual_seed(0 )
         UpperCAmelCase					=	pipe(snake_case__       ,						num_inference_steps=1_00       ,						generator=snake_case__ )

         UpperCAmelCase					=	output.audios[0]
         assert abs(np.abs(snake_case__ ).sum() - 9_389.1_111 ) < 5e-2








"""simple docstring"""







import cva
import numpy as np

from digital_image_processing.filters.convolve import img_convolve
from digital_image_processing.filters.sobel_filter import sobel_filter

lowerCAmelCase_		:			List[Any]				   =					1_8_0






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	k_size // 2
       UpperCAmelCase			,				UpperCAmelCase					=	np.mgrid[0 - center : k_size - center, 0 - center : k_size - center]
       UpperCAmelCase					=	(
           1
           / (2 * np.pi * sigma)
           * np.exp(-(np.square(lowerCAmelCase						) + np.square(lowerCAmelCase						)) / (2 * np.square(lowerCAmelCase						))						)
       )
       return g






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	np.zeros(lowerCAmelCase						)

       for row in range(1		,					image_shape[0] - 1						):
              for col in range(1		,					image_shape[1] - 1						):
                     UpperCAmelCase					=	gradient_direction[row, col]

                     if (
                         0 <= direction < PI / 8
                         or 15 * PI / 8 <= direction <= 2 * PI
                         or 7 * PI / 8 <= direction <= 9 * PI / 8
                     ):
                            UpperCAmelCase					=	sobel_grad[row, col - 1]
                            UpperCAmelCase					=	sobel_grad[row, col + 1]
                            if sobel_grad[row, col] >= w and sobel_grad[row, col] >= e:
                                   UpperCAmelCase					=	sobel_grad[row, col]

                     elif (
                         PI / 8 <= direction < 3 * PI / 8
                         or 9 * PI / 8 <= direction < 11 * PI / 8
                     ):
                            UpperCAmelCase					=	sobel_grad[row + 1, col - 1]
                            UpperCAmelCase					=	sobel_grad[row - 1, col + 1]
                            if sobel_grad[row, col] >= sw and sobel_grad[row, col] >= ne:
                                   UpperCAmelCase					=	sobel_grad[row, col]

                     elif (
                         3 * PI / 8 <= direction < 5 * PI / 8
                         or 11 * PI / 8 <= direction < 13 * PI / 8
                     ):
                            UpperCAmelCase					=	sobel_grad[row - 1, col]
                            UpperCAmelCase					=	sobel_grad[row + 1, col]
                            if sobel_grad[row, col] >= n and sobel_grad[row, col] >= s:
                                   UpperCAmelCase					=	sobel_grad[row, col]

                     elif (
                         5 * PI / 8 <= direction < 7 * PI / 8
                         or 13 * PI / 8 <= direction < 15 * PI / 8
                     ):
                            UpperCAmelCase					=	sobel_grad[row - 1, col - 1]
                            UpperCAmelCase					=	sobel_grad[row + 1, col + 1]
                            if sobel_grad[row, col] >= nw and sobel_grad[row, col] >= se:
                                   UpperCAmelCase					=	sobel_grad[row, col]

       return destination






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       for row in range(1		,					image_shape[0] - 1						):
              for col in range(1		,					image_shape[1] - 1						):
                     if destination[row, col] >= threshold_high:
                            UpperCAmelCase					=	strong
                     elif destination[row, col] <= threshold_low:
                            UpperCAmelCase					=	0
                     else:
                            UpperCAmelCase					=	weak






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       for row in range(1		,					image_shape[0]						):
              for col in range(1		,					image_shape[1]						):
                     if destination[row, col] == weak:
                            if 255 in (
                                destination[row, col + 1],
                                destination[row, col - 1],
                                destination[row - 1, col],
                                destination[row + 1, col],
                                destination[row - 1, col - 1],
                                destination[row + 1, col - 1],
                                destination[row - 1, col + 1],
                                destination[row + 1, col + 1],
                            ):
                                   UpperCAmelCase					=	strong
                            else:
                                   UpperCAmelCase					=	0






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase=15		,					lowerCAmelCase=30		,					lowerCAmelCase=128		,					lowerCAmelCase=255						):







       '''simple docstring'''
       # gaussian_filter
       UpperCAmelCase					=	img_convolve(lowerCAmelCase		,					gen_gaussian_kernel(9		,					sigma=1.4						)						)
       # get the gradient and degree by sobel_filter
       UpperCAmelCase			,				UpperCAmelCase					=	sobel_filter(lowerCAmelCase						)
       UpperCAmelCase					=	PI + np.radadeg(lowerCAmelCase						)

       UpperCAmelCase					=	suppress_non_maximum(image.shape		,					lowerCAmelCase		,					lowerCAmelCase						)

       detect_high_low_threshold(
           image.shape		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase						)

       track_edge(image.shape		,					lowerCAmelCase		,					lowerCAmelCase		,					lowerCAmelCase						)

       return destination


if __name__ == "__main__":
 # read original image in gray mode
 lowerCAmelCase_		:			Optional[Any]				   =					cva.imread(R'''../image_data/lena.jpg''', 0)
 # canny edge detection
 lowerCAmelCase_		:			Dict				   =					canny(lena)
 cva.imshow('''canny''', canny_destination)
 cva.waitKey(0)
