






import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam






class 						lowercase_   (      datasets.BeamBasedBuilder  ):






      '''simple docstring'''




      def  __lowerCAmelCase   ( self :		str    )       ->str:




       """simple docstring"""


       return datasets.DatasetInfo(
           features=datasets.Features({'''content''': datasets.Value('''string'''    )}    )					,   supervised_keys=__UpperCAmelCase					,   )







      def  __lowerCAmelCase   ( self :		Tuple					,   __UpperCAmelCase :		Optional[Any]					,   __UpperCAmelCase :		Dict    )       ->Optional[Any]:




       """simple docstring"""


       return [datasets.SplitGenerator(name=datasets.Split.TRAIN					,   gen_kwargs={'''examples''': get_test_dummy_examples()}    )]







      def  __lowerCAmelCase   ( self :		List[str]					,   __UpperCAmelCase :		Union[str, Any]					,   __UpperCAmelCase :		Optional[int]    )       ->Any:




       """simple docstring"""


       import apache_beam as beam

       return pipeline | "Load Examples" >> beam.Create(__UpperCAmelCase    )






class 						lowercase_   (      datasets.BeamBasedBuilder  ):






      '''simple docstring'''




      def  __lowerCAmelCase   ( self :		Optional[Any]    )       ->int:




       """simple docstring"""


       return datasets.DatasetInfo(
           features=datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string'''    )}    )}    )					,   supervised_keys=__UpperCAmelCase					,   )







      def  __lowerCAmelCase   ( self :		Optional[int]					,   __UpperCAmelCase :		List[Any]					,   __UpperCAmelCase :		Union[str, Any]    )       ->Optional[int]:




       """simple docstring"""


       return [
           datasets.SplitGenerator(name=datasets.Split.TRAIN					,   gen_kwargs={'''examples''': get_test_nested_examples()}    )
       ]







      def  __lowerCAmelCase   ( self :		int					,   __UpperCAmelCase :		Tuple					,   __UpperCAmelCase :		Optional[int]    )       ->Optional[Any]:




       """simple docstring"""


       import apache_beam as beam

       return pipeline | "Load Examples" >> beam.Create(__UpperCAmelCase    )
def 		_a       ( )				->     List[Any]:
 return [(i, {"content": content}) for i, content in enumerate(['''foo''', '''bar''', '''foobar''']					)]
def 		_a       ( )				->     Optional[int]:
 return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['''foo''', '''bar''', '''foobar''']					)]






class 						lowercase_   (      lowercase  ):






      '''simple docstring'''




      @require_beam
      def  __lowerCAmelCase   ( self :		List[Any]    )       ->List[Any]:




       """simple docstring"""


       a						    = len(get_test_dummy_examples()    )
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
        a						    = DummyBeamDataset(cache_dir=__UpperCAmelCase					,   beam_runner='''DirectRunner'''    )
        builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(__UpperCAmelCase					,   builder.name					,   '''default'''					,   '''0.0.0'''					,   F"""{builder.name}-train.arrow"""    )    )    )
        self.assertDictEqual(builder.info.features					,   datasets.Features({'''content''': datasets.Value('''string'''    )}    )    )
        a						    = builder.as_dataset()
        self.assertEqual(dset['''train'''].num_rows					,   __UpperCAmelCase    )
        self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples					,   __UpperCAmelCase    )
        self.assertDictEqual(dset['''train'''][0]					,   get_test_dummy_examples()[0][1]    )
        self.assertDictEqual(
            dset['''train'''][expected_num_examples - 1]					,   get_test_dummy_examples()[expected_num_examples - 1][1]    )
        self.assertTrue(
            os.path.exists(os.path.join(__UpperCAmelCase					,   builder.name					,   '''default'''					,   '''0.0.0'''					,   '''dataset_info.json'''    )    )    )
        del dset







      @require_beam
      def  __lowerCAmelCase   ( self :		Dict    )       ->int:




       """simple docstring"""


       import apache_beam as beam

       a						    = beam.io.parquetio.WriteToParquet

       a						    = len(get_test_dummy_examples()    )
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
        a						    = DummyBeamDataset(cache_dir=__UpperCAmelCase					,   beam_runner='''DirectRunner'''    )
        with patch('''apache_beam.io.parquetio.WriteToParquet'''    ) as write_parquet_mock:
         a						    = partial(__UpperCAmelCase					,   num_shards=2    )
         builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(
                    __UpperCAmelCase					,   builder.name					,   '''default'''					,   '''0.0.0'''					,   F"""{builder.name}-train-00000-of-00002.arrow"""    )    )    )
        self.assertTrue(
            os.path.exists(
                os.path.join(
                    __UpperCAmelCase					,   builder.name					,   '''default'''					,   '''0.0.0'''					,   F"""{builder.name}-train-00000-of-00002.arrow"""    )    )    )
        self.assertDictEqual(builder.info.features					,   datasets.Features({'''content''': datasets.Value('''string'''    )}    )    )
        a						    = builder.as_dataset()
        self.assertEqual(dset['''train'''].num_rows					,   __UpperCAmelCase    )
        self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples					,   __UpperCAmelCase    )
        # Order is not preserved when sharding, so we just check that all the elements are there
        self.assertListEqual(sorted(dset['''train''']['''content''']    )					,   sorted(['''foo''', '''bar''', '''foobar''']    )    )
        self.assertTrue(
            os.path.exists(os.path.join(__UpperCAmelCase					,   builder.name					,   '''default'''					,   '''0.0.0'''					,   '''dataset_info.json'''    )    )    )
        del dset







      @require_beam
      def  __lowerCAmelCase   ( self :		Tuple    )       ->str:




       """simple docstring"""


       with tempfile.TemporaryDirectory() as tmp_cache_dir:
        a						    = DummyBeamDataset(cache_dir=__UpperCAmelCase    )
        self.assertRaises(datasets.builder.MissingBeamOptions					,   builder.download_and_prepare    )







      @require_beam
      def  __lowerCAmelCase   ( self :		Dict    )       ->List[str]:




       """simple docstring"""


       a						    = len(get_test_nested_examples()    )
       with tempfile.TemporaryDirectory() as tmp_cache_dir:
        a						    = NestedBeamDataset(cache_dir=__UpperCAmelCase					,   beam_runner='''DirectRunner'''    )
        builder.download_and_prepare()
        self.assertTrue(
            os.path.exists(
                os.path.join(__UpperCAmelCase					,   builder.name					,   '''default'''					,   '''0.0.0'''					,   F"""{builder.name}-train.arrow"""    )    )    )
        self.assertDictEqual(
            builder.info.features					,   datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string'''    )}    )}    )    )
        a						    = builder.as_dataset()
        self.assertEqual(dset['''train'''].num_rows					,   __UpperCAmelCase    )
        self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples					,   __UpperCAmelCase    )
        self.assertDictEqual(dset['''train'''][0]					,   get_test_nested_examples()[0][1]    )
        self.assertDictEqual(
            dset['''train'''][expected_num_examples - 1]					,   get_test_nested_examples()[expected_num_examples - 1][1]    )
        self.assertTrue(
            os.path.exists(os.path.join(__UpperCAmelCase					,   builder.name					,   '''default'''					,   '''0.0.0'''					,   '''dataset_info.json'''    )    )    )
        del dset










from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map
def 		_a       ( a					:Dict[str, torch.Tensor]					)				->     Dict[str, torch.Tensor]:
 a						    = []
 a						    = []
 a						    = []

 for rt in rc.restypes:
  a						    = rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
  restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names]					)
  a						    = {name: i for i, name in enumerate(a					)}
  restype_atomaa_to_atomaa_list.append(
      [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types]					)

  restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names]					)

 # Add dummy mapping for restype 'UNK'
 restype_atomaa_to_atomaa_list.append([0] * 14					)
 restype_atomaa_to_atomaa_list.append([0] * 37					)
 restype_atomaa_mask_list.append([0.0] * 14					)

 a						    = torch.tensor(
     a      ,				dtype=torch.intaa      ,				device=protein['''aatype'''].device      ,				)
 a						    = torch.tensor(
     a      ,				dtype=torch.intaa      ,				device=protein['''aatype'''].device      ,				)
 a						    = torch.tensor(
     a      ,				dtype=torch.floataa      ,				device=protein['''aatype'''].device      ,				)
 a						    = protein['''aatype'''].to(torch.long					)

 # create the mapping for (residx, atom14) --> atom37, i.e. an array
 # with shape (num_res, 14) containing the atom37 indices for this protein
 a						    = restype_atomaa_to_atomaa[protein_aatype]
 a						    = restype_atomaa_mask[protein_aatype]

 a						    = residx_atomaa_mask
 a						    = residx_atomaa_to_atomaa.long()

 # create the gather indices for mapping back
 a						    = restype_atomaa_to_atomaa[protein_aatype]
 a						    = residx_atomaa_to_atomaa.long()

 # create the corresponding mask
 a						    = torch.zeros([21, 37]      ,				dtype=torch.floataa      ,				device=protein['''aatype'''].device					)
 for restype, restype_letter in enumerate(rc.restypes					):
  a						    = rc.restype_atoa[restype_letter]
  a						    = rc.residue_atoms[restype_name]
  for atom_name in atom_names:
   a						    = rc.atom_order[atom_name]
   a						    = 1

 a						    = restype_atomaa_mask[protein_aatype]
 a						    = residx_atomaa_mask

 return protein

def 		_a       ( a					:Dict[str, torch.Tensor]					)				->     Dict[str, np.ndarray]:
 a						    = tree_map(lambda a					: torch.tensor(a      ,				device=batch['''aatype'''].device					)      ,				a      ,				np.ndarray					)
 a						    = tensor_tree_map(lambda a					: np.array(a					)      ,				make_atomaa_masks(a					)					)
 return out


