



import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers.models.esm.modeling_esmfold import EsmForProteinFolding

class       _SCREAMING_SNAKE_CASE	:





     def __init__(       self :							Union[str, Any]     ,  UpperCamelCase :							int     ,  UpperCamelCase :							Dict=1_3     ,  UpperCamelCase :							List[str]=7     ,  UpperCamelCase :							List[str]=False     ,  UpperCamelCase :							Optional[Any]=True     ,  UpperCamelCase :							Tuple=False     ,  UpperCamelCase :							str=False     ,  UpperCamelCase :							Optional[int]=1_9     ,  UpperCamelCase :							Dict=3_2     ,  UpperCamelCase :							Any=5     ,  UpperCamelCase :							Union[str, Any]=4     ,  UpperCamelCase :							List[str]=3_7     ,  UpperCamelCase :							Tuple="gelu"     ,  UpperCamelCase :							Tuple=0.1     ,  UpperCamelCase :							Union[str, Any]=0.1     ,  UpperCamelCase :							Any=5_1_2     ,  UpperCamelCase :							Dict=1_6     ,  UpperCamelCase :							Tuple=2     ,  UpperCamelCase :							Optional[int]=0.0_2     ,  UpperCamelCase :							int=3     ,  UpperCamelCase :							Any=4     ,  UpperCamelCase :							Tuple=None     ,  )->List[Any]:
      __SCREAMING_SNAKE_CASE       :    Any       =	parent
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	batch_size
      __SCREAMING_SNAKE_CASE       :    List[str]       =	seq_length
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	is_training
      __SCREAMING_SNAKE_CASE       :    int       =	use_input_mask
      __SCREAMING_SNAKE_CASE       :    List[str]       =	use_token_type_ids
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	use_labels
      __SCREAMING_SNAKE_CASE       :    List[str]       =	vocab_size
      __SCREAMING_SNAKE_CASE       :    Dict       =	hidden_size
      __SCREAMING_SNAKE_CASE       :    Dict       =	num_hidden_layers
      __SCREAMING_SNAKE_CASE       :    int       =	num_attention_heads
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	intermediate_size
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	hidden_act
      __SCREAMING_SNAKE_CASE       :    str       =	hidden_dropout_prob
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	attention_probs_dropout_prob
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	max_position_embeddings
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	type_vocab_size
      __SCREAMING_SNAKE_CASE       :    str       =	type_sequence_label_size
      __SCREAMING_SNAKE_CASE       :    Tuple       =	initializer_range
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	num_labels
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	num_choices
      __SCREAMING_SNAKE_CASE       :    Dict       =	scope





     def 	__snake_case    (       self :							str				)->List[Any]:
      __SCREAMING_SNAKE_CASE       :    List[str]       =	ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size				)

      __SCREAMING_SNAKE_CASE       :    List[Any]       =	None
      if self.use_input_mask:
       __SCREAMING_SNAKE_CASE       :    Tuple       =	random_attention_mask([self.batch_size, self.seq_length]				)

      __SCREAMING_SNAKE_CASE       :    str       =	None
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	None
      __SCREAMING_SNAKE_CASE       :    int       =	None
      if self.use_labels:
       __SCREAMING_SNAKE_CASE       :    Tuple       =	ids_tensor([self.batch_size]     ,  self.type_sequence_label_size				)
       __SCREAMING_SNAKE_CASE       :    List[str]       =	ids_tensor([self.batch_size, self.seq_length]     ,  self.num_labels				)
       __SCREAMING_SNAKE_CASE       :    str       =	ids_tensor([self.batch_size]     ,  self.num_choices				)

      __SCREAMING_SNAKE_CASE       :    Tuple       =	self.get_config()

      return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels





     def 	__snake_case    (       self :							Dict				)->Any:
      __SCREAMING_SNAKE_CASE       :    int       =	EsmConfig(
          vocab_size=3_3     ,  hidden_size=self.hidden_size     ,  pad_token_id=1     ,  num_hidden_layers=self.num_hidden_layers     ,  num_attention_heads=self.num_attention_heads     ,  intermediate_size=self.intermediate_size     ,  hidden_act=self.hidden_act     ,  hidden_dropout_prob=self.hidden_dropout_prob     ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,  max_position_embeddings=self.max_position_embeddings     ,  type_vocab_size=self.type_vocab_size     ,  initializer_range=self.initializer_range     ,  is_folding_model=UpperCamelCase     ,  esmfold_config={"trunk": {"num_blocks": 2}, "fp16_esm": False}     ,  )
      return config





     def 	__snake_case    (       self :							Union[str, Any]     ,  UpperCamelCase :							Dict     ,  UpperCamelCase :							str     ,  UpperCamelCase :							Tuple     ,  UpperCamelCase :							Any     ,  UpperCamelCase :							Dict     ,  UpperCamelCase :							Union[str, Any]				)->Optional[Any]:
      __SCREAMING_SNAKE_CASE       :    int       =	EsmForProteinFolding(config=UpperCamelCase				).float()
      model.to(UpperCamelCase				)
      model.eval()
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	model(UpperCamelCase     ,  attention_mask=UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    List[str]       =	model(UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    Dict       =	model(UpperCamelCase				)

      self.parent.assertEqual(result.positions.shape     ,  (8, self.batch_size, self.seq_length, 1_4, 3)				)
      self.parent.assertEqual(result.angles.shape     ,  (8, self.batch_size, self.seq_length, 7, 2)				)






     def 	__snake_case    (       self :							str				)->Optional[int]:
      __SCREAMING_SNAKE_CASE       :    Any       =	self.prepare_config_and_inputs()
      (
          (
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		
      )       :    List[Any]       =	config_and_inputs
      __SCREAMING_SNAKE_CASE       :    Any       =	{"input_ids": input_ids, "attention_mask": input_mask}
      return config, inputs_dict






@require_torch
class       _SCREAMING_SNAKE_CASE	(UpperCamelCase			,    UpperCamelCase			,    unittest.TestCase ):
     lowerCAmelCase					=			False

     lowerCAmelCase					=			(EsmForProteinFolding,) if is_torch_available() else ()
     lowerCAmelCase					=			()
     lowerCAmelCase					=			{} if is_torch_available() else {}
     lowerCAmelCase					=			False





     def 	__snake_case    (       self :							List[str]				)->Dict:
      __SCREAMING_SNAKE_CASE       :    str       =	EsmFoldModelTester(self				)
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	ConfigTester(self     ,  config_class=UpperCamelCase     ,  hidden_size=3_7				)





     def 	__snake_case    (       self :							Optional[Any]				)->Union[str, Any]:
      self.config_tester.run_common_tests()





     def 	__snake_case    (       self :							Dict				)->int:
      __SCREAMING_SNAKE_CASE       :    str       =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCamelCase				)





     @unittest.skip("Does not support attention outputs"				)
     def 	__snake_case    (       self :							Tuple				)->List[str]:
      pass





     @unittest.skip
     def 	__snake_case    (       self :							str				)->List[str]:
      pass





     @unittest.skip("Esm does not support embedding resizing"				)
     def 	__snake_case    (       self :							Optional[Any]				)->Union[str, Any]:
      pass





     @unittest.skip("Esm does not support embedding resizing"				)
     def 	__snake_case    (       self :							List[str]				)->Optional[Any]:
      pass





     @unittest.skip("ESMFold does not support passing input embeds!"				)
     def 	__snake_case    (       self :							List[str]				)->str:
      pass





     @unittest.skip("ESMFold does not support head pruning."				)
     def 	__snake_case    (       self :							List[Any]				)->List[Any]:
      pass





     @unittest.skip("ESMFold does not support head pruning."				)
     def 	__snake_case    (       self :							List[Any]				)->Dict:
      pass





     @unittest.skip("ESMFold does not support head pruning."				)
     def 	__snake_case    (       self :							Any				)->List[str]:
      pass





     @unittest.skip("ESMFold does not support head pruning."				)
     def 	__snake_case    (       self :							Optional[int]				)->int:
      pass





     @unittest.skip("ESMFold does not support head pruning."				)
     def 	__snake_case    (       self :							Optional[Any]				)->Union[str, Any]:
      pass





     @unittest.skip("ESMFold does not output hidden states in the normal way."				)
     def 	__snake_case    (       self :							List[str]				)->List[Any]:
      pass





     @unittest.skip("ESMfold does not output hidden states in the normal way."				)
     def 	__snake_case    (       self :							List[str]				)->Any:
      pass





     @unittest.skip("ESMFold only has one output format."				)
     def 	__snake_case    (       self :							List[str]				)->str:
      pass





     @unittest.skip("This test doesn't work for ESMFold and doesn't test core functionality"				)
     def 	__snake_case    (       self :							str				)->str:
      pass





     @unittest.skip("ESMFold does not support input chunking."				)
     def 	__snake_case    (       self :							List[str]				)->Dict:
      pass





     @unittest.skip("ESMFold doesn't respect you and it certainly doesn't respect your initialization arguments."				)
     def 	__snake_case    (       self :							Any				)->str:
      pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."				)
     def 	__snake_case    (       self :							Union[str, Any]				)->List[str]:
      pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."				)
     def 	__snake_case    (       self :							int				)->Any:
      pass





     @unittest.skip("ESMFold doesn't support torchscript compilation."				)
     def 	__snake_case    (       self :							Union[str, Any]				)->Tuple:
      pass





     @unittest.skip("ESMFold doesn't support data parallel."				)
     def 	__snake_case    (       self :							Optional[int]				)->Union[str, Any]:
      pass






     @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."				)
     def 	__snake_case    (       self :							Dict				)->str:
      pass






@require_torch
class       _SCREAMING_SNAKE_CASE	(UpperCamelCase ):





     @slow
     def 	__snake_case    (       self :							str				)->str:
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	EsmForProteinFolding.from_pretrained("facebook/esmfold_v1"				).float()
      model.eval()
      __SCREAMING_SNAKE_CASE       :    Tuple       =	torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]				)
      __SCREAMING_SNAKE_CASE       :    Dict       =	model(UpperCamelCase				)["positions"]
      __SCREAMING_SNAKE_CASE       :    str       =	torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4]     ,  dtype=torch.floataa				)
      self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]     ,  UpperCamelCase     ,  atol=1E-4				)				)











from __future__ import annotations

import math
from collections import Counter
from string import ascii_lowercase






def 	_lowerCAmelCase						(				__lowerCamelCase   :     str     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       :    Tuple       =	analyze_text(__lowerCamelCase     )
 __SCREAMING_SNAKE_CASE       :    List[Any]       =	list(" " + ascii_lowercase     )
 # what is our total sum of probabilities.
 __SCREAMING_SNAKE_CASE       :    int       =	sum(single_char_strings.values()     )

 # one length string
 __SCREAMING_SNAKE_CASE       :    str       =	0
 # for each alpha we go in our dict and if it is in it we calculate entropy
 for ch in my_alphas:
  if ch in single_char_strings:
   __SCREAMING_SNAKE_CASE       :    List[Any]       =	single_char_strings[ch]
   __SCREAMING_SNAKE_CASE       :    str       =	my_str / all_sum
   my_fir_sum += prob * math.loga(__lowerCamelCase     )  # entropy formula.

    # print entropy
 print(F"""{round(-1 * my_fir_sum     ):.1f}"""     )

 # two len string
 __SCREAMING_SNAKE_CASE       :    Optional[int]       =	sum(two_char_strings.values()     )
 __SCREAMING_SNAKE_CASE       :    int       =	0
 # for each alpha (two in size) calculate entropy.
 for cha in my_alphas:
  for cha in my_alphas:
   __SCREAMING_SNAKE_CASE       :    int       =	cha + cha
   if sequence in two_char_strings:
    __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	two_char_strings[sequence]
    __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	int(__lowerCamelCase     ) / all_sum
    my_sec_sum += prob * math.loga(__lowerCamelCase     )

    # print second entropy
 print(F"""{round(-1 * my_sec_sum     ):.1f}"""     )

 # print the difference between them
 print(F"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum)     ):.1f}"""     )






def 	_lowerCAmelCase						(				__lowerCamelCase   :     str     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE       :    Any       =	Counter()  # type: ignore
 __SCREAMING_SNAKE_CASE       :    int       =	Counter()  # type: ignore
 single_char_strings[text[-1]] += 1

 # first case when we have space at start.
 two_char_strings[" " + text[0]] += 1
 for i in range(0		,	len(__lowerCamelCase     ) - 1     ):
  single_char_strings[text[i]] += 1
  two_char_strings[text[i : i + 2]] += 1
 return single_char_strings, two_char_strings






def 	_lowerCAmelCase						(				):



 """simple docstring"""





 import doctest

 doctest.testmod()
 # text = (
 #     "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark "
 #     "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest "
 #     "jointure saw horrible. He private he on be imagine suppose. Fertile "
 #     "beloved evident through no service elderly is. Blind there if every no so "
 #     "at. Own neglected you preferred way sincerity delivered his attempted. To "
 #     "of message cottage windows do besides against uncivil.  Delightful "
 #     "unreserved impossible few estimating men favourable see entreaties. She "
 #     "propriety immediate was improving. He or entrance humoured likewise "
 #     "moderate. Much nor game son say feel. Fat make met can must form into "
 #     "gate. Me we offending prevailed discovery. "
 # )

 # calculate_prob(text)


if __name__ == "__main__":
      main()






