


import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


lowerCAmelCase__  :Optional[Any]             =							5_0_0_0_0_0

lowerCAmelCase__  :Optional[Any]             =							os.path.split(__file__)
lowerCAmelCase__  :Union[str, Any]             =							os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))

@get_duration
def 	lowerCAmelCase__			(      a__:		Optional[int]      ,  **a__:		Tuple	)  ->		Dict:


							'''simple docstring'''





							_UpperCAmelCase					=      dataset.map(**__snake_case	)

@get_duration
def 	lowerCAmelCase__			(      a__:		Optional[Any]      ,  **a__:		Dict	)  ->		int:


							'''simple docstring'''





							_UpperCAmelCase					=      dataset.filter(**__snake_case	)

def 	lowerCAmelCase__			(      )  ->		List[str]:


							'''simple docstring'''





							_UpperCAmelCase					=      {"num examples": SPEED_TEST_N_EXAMPLES}
							with tempfile.TemporaryDirectory() as tmp_dir:
														_UpperCAmelCase					=      datasets.Features({'text': datasets.Value('string'	), 'numbers': datasets.Value('float32'	)}	)
														_UpperCAmelCase					=      generate_example_dataset(
														    os.path.join(__snake_case      ,  'dataset.arrow'	)      ,  __snake_case      ,  num_examples=__snake_case	)

														_UpperCAmelCase					=      transformers.AutoTokenizer.from_pretrained('bert-base-cased'      ,  use_fast=__snake_case	)

														def tokenize(a__:		List[Any]	):
																					return tokenizer(examples['text']	)

														_UpperCAmelCase					=      map(__snake_case	)

														_UpperCAmelCase					=      map(__snake_case      ,  batched=__snake_case	)

														_UpperCAmelCase					=      map(__snake_case      ,  function=lambda a__	: None      ,  batched=__snake_case	)

														with dataset.formatted_as(type='numpy'	):
																					_UpperCAmelCase					=      map(__snake_case      ,  function=lambda a__	: None      ,  batched=__snake_case	)

														with dataset.formatted_as(type='pandas'	):
																					_UpperCAmelCase					=      map(__snake_case      ,  function=lambda a__	: None      ,  batched=__snake_case	)

														with dataset.formatted_as(type='torch'      ,  columns='numbers'	):
																					_UpperCAmelCase					=      map(__snake_case      ,  function=lambda a__	: None      ,  batched=__snake_case	)

														with dataset.formatted_as(type='tensorflow'      ,  columns='numbers'	):
																					_UpperCAmelCase					=      map(__snake_case      ,  function=lambda a__	: None      ,  batched=__snake_case	)

														_UpperCAmelCase					=      map(__snake_case      ,  function=__snake_case      ,  batched=__snake_case	)

														_UpperCAmelCase					=      filter(__snake_case	)

														# Activate later when tokenizer support batched inputs
														# with dataset.formatted_as(type='numpy'):
														#     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

							with open(__snake_case      ,  'wb'	) as f:
														f.write(json.dumps(__snake_case	).encode('utf-8'	)	)


if __name__ == "__main__":  # useful to run the profiler
		benchmark_map_filter()



from __future__ import annotations

from PIL import Image

# Define glider example
lowerCAmelCase__  :str             =							[
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
lowerCAmelCase__  :Any             =							[[0, 1, 0], [0, 1, 0], [0, 1, 0]]

def 	lowerCAmelCase__			(      a__:		list[list[int]]	)  ->		list[list[int]]:


							'''simple docstring'''





							_UpperCAmelCase					=      []
							for i in range(len(a__	)	):
														_UpperCAmelCase					=      []
														for j in range(len(cells[i]	)	):
																					# Get the number of live neighbours
																					_UpperCAmelCase					=      0
																					if i > 0 and j > 0:
																												neighbour_count += cells[i - 1][j - 1]
																					if i > 0:
																												neighbour_count += cells[i - 1][j]
																					if i > 0 and j < len(cells[i]	) - 1:
																												neighbour_count += cells[i - 1][j + 1]
																					if j > 0:
																												neighbour_count += cells[i][j - 1]
																					if j < len(cells[i]	) - 1:
																												neighbour_count += cells[i][j + 1]
																					if i < len(a__	) - 1 and j > 0:
																												neighbour_count += cells[i + 1][j - 1]
																					if i < len(a__	) - 1:
																												neighbour_count += cells[i + 1][j]
																					if i < len(a__	) - 1 and j < len(cells[i]	) - 1:
																												neighbour_count += cells[i + 1][j + 1]

																					# Rules of the game of life (excerpt from Wikipedia):
																					# 1. Any live cell with two or three live neighbours survives.
																					# 2. Any dead cell with three live neighbours becomes a live cell.
																					# 3. All other live cells die in the next generation.
																					#    Similarly, all other dead cells stay dead.
																					_UpperCAmelCase					=      cells[i][j] == 1
																					if (
																					    (alive and 2 <= neighbour_count <= 3)
																					    or not alive
																					    and neighbour_count == 3
																					):
																												next_generation_row.append(1	)
																					else:
																												next_generation_row.append(0	)

														next_generation.append(a__	)
							return next_generation

def 	lowerCAmelCase__			(      a__:		list[list[int]]      ,  a__:		int	)  ->		list[Image.Image]:


							'''simple docstring'''





							_UpperCAmelCase					=      []
							for _ in range(a__	):
														# Create output image
														_UpperCAmelCase					=      Image.new('RGB'      ,  (len(cells[0]	), len(a__	))	)
														_UpperCAmelCase					=      img.load()

														# Save cells to image
														for x in range(len(a__	)	):
																					for y in range(len(cells[0]	)	):
																												_UpperCAmelCase					=      2_5_5 - cells[y][x] * 2_5_5
																												_UpperCAmelCase					=      (colour, colour, colour)

        # Save image
														images.append(a__	)
														_UpperCAmelCase					=      new_generation(a__	)
							return images


if __name__ == "__main__":
		lowerCAmelCase__  :Tuple             =							generate_images(GLIDER, 1_6)
		images[0].save('''out.gif''', save_all=True, append_images=images[1:])
