



from sklearn.metrics import matthews_corrcoef

import datasets


a_			=					"""
Compute the Matthews correlation coefficient (MCC)

The Matthews correlation coefficient is used in machine learning as a
measure of the quality of binary and multiclass classifications. It takes
into account true and false positives and negatives and is generally
regarded as a balanced measure which can be used even if the classes are of
very different sizes. The MCC is in essence a correlation coefficient value
between -1 and +1. A coefficient of +1 represents a perfect prediction, 0
an average random prediction and -1 an inverse prediction.  The statistic
is also known as the phi coefficient. [source: Wikipedia]
"""

a_			=					"""
Args:
    predictions (list of int): Predicted labels, as returned by a model.
    references (list of int): Ground truth labels.
    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.
Returns:
    matthews_correlation (dict containing float): Matthews correlation.
Examples:
    Example 1, a basic example with only predictions and references as inputs:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3])
        >>> print(round(results['matthews_correlation'], 2))
        0.54

    Example 2, the same example as above, but also including sample weights:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])
        >>> print(round(results['matthews_correlation'], 2))
        0.1

    Example 3, the same example as above, but with sample weights that cause a negative correlation:
        >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])
        >>> print(round(results['matthews_correlation'], 2))
        -0.25
"""

a_			=					"""\
@article{scikit-learn,
  title={Scikit-learn: Machine Learning in {P}ython},
  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
  journal={Journal of Machine Learning Research},
  volume={12},
  pages={2825--2830},
  year={2011}
}
"""



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




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    return datasets.MetricInfo(
        description=_DESCRIPTION	,				citation=_CITATION	,				inputs_description=_KWARGS_DESCRIPTION	,				features=datasets.Features(
            {
                '''predictions''': datasets.Value('''int32'''				),
                '''references''': datasets.Value('''int32'''				),
            }				)	,				reference_urls=[
            '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html'''
        ]	,				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase=None				):



    '''simple docstring'''
    return {
        "matthews_correlation": float(matthews_corrcoef(__UpperCAmelCase	,				__UpperCAmelCase	,				sample_weight=__UpperCAmelCase				)				),
    }






import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


a_			=					False



class  __lowerCAmelCase     (	unittest.TestCase   ):
 pass



@nightly
@require_torch_gpu
class  __lowerCAmelCase     (	unittest.TestCase   ):




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    # clean up the VRAM after each test
    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''	,				torch_dtype=torch.floataa				)
    pipe.to(__UpperCAmelCase				)
    pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

    __lowerCamelCase													=    load_image(
        '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''				)

    __lowerCamelCase													=    torch.manual_seed(0				)
    __lowerCamelCase													=    pipe.dual_guided(
        prompt='''first prompt'''	,				image=__UpperCAmelCase	,				text_to_image_strength=0.75	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=2	,				output_type='''numpy'''	,				).images

    with tempfile.TemporaryDirectory() as tmpdirname:
       pipe.save_pretrained(__UpperCAmelCase				)
       __lowerCamelCase													=    VersatileDiffusionPipeline.from_pretrained(__UpperCAmelCase	,				torch_dtype=torch.floataa				)
    pipe.to(__UpperCAmelCase				)
    pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

    __lowerCamelCase													=    generator.manual_seed(0				)
    __lowerCamelCase													=    pipe.dual_guided(
        prompt='''first prompt'''	,				image=__UpperCAmelCase	,				text_to_image_strength=0.75	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=2	,				output_type='''numpy'''	,				).images

    assert np.abs(image - new_image				).sum() < 1E-5, "Models don't have the same forward pass"




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''	,				torch_dtype=torch.floataa				)
    pipe.to(__UpperCAmelCase				)
    pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

    __lowerCamelCase													=    '''cyberpunk 2077'''
    __lowerCamelCase													=    load_image(
        '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''				)
    __lowerCamelCase													=    torch.manual_seed(0				)
    __lowerCamelCase													=    pipe.dual_guided(
        prompt=__UpperCAmelCase	,				image=__UpperCAmelCase	,				text_to_image_strength=0.75	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=50	,				output_type='''numpy'''	,				).images

    __lowerCamelCase													=    image[0, 253:256, 253:256, -1]

    assert image.shape == (1, 512, 512, 3)
    __lowerCamelCase													=    np.array([0.1_448, 0.1_619, 0.1_741, 0.1_086, 0.1_147, 0.1_128, 0.1_199, 0.1_165, 0.1_001]				)

    assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1

    __lowerCamelCase													=    '''A painting of a squirrel eating a burger '''
    __lowerCamelCase													=    torch.manual_seed(0				)
    __lowerCamelCase													=    pipe.text_to_image(
        prompt=__UpperCAmelCase	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=50	,				output_type='''numpy'''				).images

    __lowerCamelCase													=    image[0, 253:256, 253:256, -1]

    assert image.shape == (1, 512, 512, 3)
    __lowerCamelCase													=    np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778]				)

    assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1

    __lowerCamelCase													=    pipe.image_variation(__UpperCAmelCase	,				generator=__UpperCAmelCase	,				output_type='''numpy'''				).images

    __lowerCamelCase													=    image[0, 253:256, 253:256, -1]

    assert image.shape == (1, 512, 512, 3)
    __lowerCamelCase													=    np.array([0.3_076, 0.3_123, 0.3_284, 0.3_782, 0.3_770, 0.3_894, 0.4_297, 0.4_331, 0.4_456]				)

    assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1






