






import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


UpperCamelCase											=      (3_0, 4_8_7)
UpperCamelCase											=      (3_6, 1_0_2_4)
UpperCamelCase											=      (1_0_0, 1_0_0)
UpperCamelCase											=      1_0_0

UpperCamelCase											=      datasets.Features(
    {'''text''': ArrayaD(SHAPE_TEST_1, dtype='''float32'''), '''image''': ArrayaD(SHAPE_TEST_2, dtype='''float32''')}
)

UpperCamelCase						,  UpperCamelCase											=      os.path.split(__file__)
UpperCamelCase											=      os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))




@get_duration
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      Dict:
 with ArrowWriter(features=lowerCamelCase__ ,			path=os.path.join(lowerCamelCase__ ,			'beta.arrow'				)				) as writer:
  for key, record in dummy_data:
   lowerCamelCase_							:    int							=     my_features.encode_example(lowerCamelCase__				)
   writer.write(lowerCamelCase__				)
  lowerCamelCase_		,      lowerCamelCase_							:    List[Any]							=     writer.finalize()




@get_duration
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      Any:
 lowerCamelCase_							:    Any							=     datasets.Dataset.from_file(
     filename=os.path.join(lowerCamelCase__ ,			'beta.arrow'				) ,			info=datasets.DatasetInfo(features=lowerCamelCase__				)				)
 for _ in dataset:
  pass




@get_duration
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      Dict:
 lowerCamelCase_							:    Optional[int]							=     datasets.Dataset.from_file(
     filename=os.path.join(lowerCamelCase__ ,			'beta.arrow'				) ,			info=datasets.DatasetInfo(features=lowerCamelCase__				)				)
 dataset.set_format('numpy'				)
 for _ in dataset:
  pass




@get_duration
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      List[str]:
 lowerCamelCase_							:    Dict							=     10
 lowerCamelCase_							:    int							=     datasets.Dataset.from_file(
     filename=os.path.join(lowerCamelCase__ ,			'beta.arrow'				) ,			info=datasets.DatasetInfo(features=lowerCamelCase__				)				)
 for i in range(0 ,			len(lowerCamelCase__				) ,			lowerCamelCase__				):
  lowerCamelCase_							:    Tuple							=     dataset[i : i + batch_size]




@get_duration
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      Optional[Any]:
 lowerCamelCase_							:    List[Any]							=     10
 lowerCamelCase_							:    Optional[int]							=     datasets.Dataset.from_file(
     filename=os.path.join(lowerCamelCase__ ,			'beta.arrow'				) ,			info=datasets.DatasetInfo(features=lowerCamelCase__				)				)
 dataset.set_format('numpy'				)
 for i in range(0 ,			len(lowerCamelCase__				) ,			lowerCamelCase__				):
  lowerCamelCase_							:    List[str]							=     dataset[i : i + batch_size]




@get_duration
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      Any:
 lowerCamelCase_							:    Union[str, Any]							=     datasets.Dataset.from_file(
     filename=os.path.join(lowerCamelCase__ ,			'beta.arrow'				) ,			info=datasets.DatasetInfo(features=lowerCamelCase__				)				)
 for col in feats:
  lowerCamelCase_							:    int							=     dataset[col]




@get_duration
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      Optional[Any]:
 lowerCamelCase_							:    Any							=     datasets.Dataset.from_file(
     filename=os.path.join(lowerCamelCase__ ,			'beta.arrow'				) ,			info=datasets.DatasetInfo(features=lowerCamelCase__				)				)
 dataset.set_format('numpy'				)
 for col in feats:
  lowerCamelCase_							:    str							=     dataset[col]




def       _a   (							)  ->      int:
 lowerCamelCase_							:    List[str]							=     {}
 lowerCamelCase_							:    Optional[int]							=     (
     read_unformated,
     read_formatted_as_numpy,
     read_batch_unformated,
     read_batch_formatted_as_numpy,
     read_col_unformated,
     read_col_formatted_as_numpy,
 )
 with tempfile.TemporaryDirectory() as tmp_dir:
  lowerCamelCase_							:    int							=     datasets.Features({'image': ArrayaD(lowerCamelCase__ ,			dtype='float32'				)}				)
  lowerCamelCase_							:    Optional[int]							=     generate_examples(features=lowerCamelCase__ ,			num_examples=lowerCamelCase__				)
  lowerCamelCase_							:    Union[str, Any]							=     write(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
  for read_func in read_functions:
   lowerCamelCase_							:    str							=     read_func(lowerCamelCase__ ,			lowerCamelCase__				)

 with tempfile.TemporaryDirectory() as tmp_dir:
  # don't use fixed length for fair comparison
  # feats = datasets.Features(
  #     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
  # )
  lowerCamelCase_							:    List[Any]							=     datasets.Features({'image': datasets.Sequence(datasets.Sequence(datasets.Value('float32'				)				)				)}				)
  lowerCamelCase_							:    Tuple							=     generate_examples(
      features=lowerCamelCase__ ,			num_examples=lowerCamelCase__ ,			seq_shapes={'image': SPEED_TEST_SHAPE}				)
  lowerCamelCase_							:    Tuple							=     write(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
  for read_func in read_functions:
   lowerCamelCase_							:    Any							=     read_func(lowerCamelCase__ ,			lowerCamelCase__				)

 with tempfile.TemporaryDirectory() as tmp_dir:
  # don't use fixed length for fair comparison
  # feats = datasets.Features(
  #     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
  # )
  lowerCamelCase_							:    Optional[int]							=     datasets.Features({'image': datasets.Sequence(datasets.Value('float32'				)				)}				)
  lowerCamelCase_							:    int							=     generate_examples(
      features=lowerCamelCase__ ,			num_examples=lowerCamelCase__ ,			seq_shapes={'image': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]} ,			)
  lowerCamelCase_							:    str							=     write(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
  for read_func in read_functions:
   lowerCamelCase_							:    str							=     read_func(lowerCamelCase__ ,			lowerCamelCase__				)

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


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








UpperCamelCase											=      [
    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
]




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      bool:
 return 0 <= row < rows and 0 <= col < cols




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      int:
 lowerCamelCase_							:    Tuple							=     len(lowerCamelCase__				)
 lowerCamelCase_							:    Tuple							=     len(mat[0]				)
 if is_safe(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				) and (row, col) not in seen and mat[row][col] == 1:
  seen.add((row, col)				)
  return (
      1
      + depth_first_search(row + 1 ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
      + depth_first_search(row - 1 ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
      + depth_first_search(lowerCamelCase__ ,			col + 1 ,			lowerCamelCase__ ,			lowerCamelCase__				)
      + depth_first_search(lowerCamelCase__ ,			col - 1 ,			lowerCamelCase__ ,			lowerCamelCase__				)
  )
 else:
  return 0




def       _a   (							lowerCamelCase__				)  ->      int:
 lowerCamelCase_							:    set							=     set()

 lowerCamelCase_							:    int							=     0
 for row, line in enumerate(lowerCamelCase__				):
  for col, item in enumerate(lowerCamelCase__				):
   if item == 1 and (row, col) not in seen:
    # Maximizing the area
    lowerCamelCase_							:    str							=     max(lowerCamelCase__ ,			depth_first_search(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)				)
 return max_area


if __name__ == "__main__":
       import doctest

       print(find_max_area(matrix))  # Output -> 6
       doctest.testmod()
