






"""simple docstring"""




import colorsys
import io

import cva
import matplotlib as mpl
import matplotlib.colors as mplc
import matplotlib.figure as mplfigure
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg

from utils import img_tensorize


_snake_case					   =     1000






class  UpperCamelCase    :


 def __init__(  self	:		Optional[Any]							,		UpperCAmelCase__	:		Union[str, Any]							,		UpperCAmelCase__	:		Dict=1.2							,		UpperCAmelCase__	:		Optional[Any]="g"							,		UpperCAmelCase__	:		str=0.5							,		UpperCAmelCase__	:		str="-"							,		UpperCAmelCase__	:		List[Any]="test_out.jpg"							,		UpperCAmelCase__	:		Dict=True							,		UpperCAmelCase__	:		List[str]=False							,		UpperCAmelCase__	:		int=None							,		UpperCAmelCase__	:		Optional[Any]=None							,		UpperCAmelCase__	:		List[Any]=0.7							,		)      -> Optional[int]:
   if isinstance(UpperCAmelCase__							,		torch.Tensor							):
     _a						: int    =			img.numpy().astype("""np.uint8"""							)
   if isinstance(UpperCAmelCase__							,		UpperCAmelCase__							):
     _a						: List[Any]    =			img_tensorize(UpperCAmelCase__							)
   assert isinstance(UpperCAmelCase__							,		np.ndarray							)

   _a   ,						_a						: Tuple    =			img.shape[1], img.shape[0]
   _a						: str    =			mplfigure.Figure(frameon=UpperCAmelCase__							)
   _a						: int    =			fig.get_dpi()
   _a						: List[str]    =			(width * scale + 1E-2) / dpi
   _a						: Any    =			(height * scale + 1E-2) / dpi
   fig.set_size_inches(UpperCAmelCase__							,		UpperCAmelCase__							)
   _a						: Union[str, Any]    =			fig.add_axes([0.0, 0.0, 1.0, 1.0]							)
   ax.axis("""off"""							)
   ax.set_xlim(0.0							,		UpperCAmelCase__							)
   ax.set_ylim(UpperCAmelCase__							)

   _a						: List[str]    =			saveas
   _a						: List[str]    =			rgb
   _a						: Optional[Any]    =			pynb
   _a						: Tuple    =			img
   _a						: List[Any]    =			edgecolor
   _a						: List[Any]    =			0.5
   _a						: str    =			linestyle
   _a						: List[str]    =			int(np.sqrt(min(UpperCAmelCase__							,		UpperCAmelCase__							)							) * scale // 3							)
   _a						: str    =			width
   _a						: Optional[int]    =			height
   _a						: List[Any]    =			scale
   _a						: str    =			fig
   _a						: Optional[Any]    =			ax
   _a						: Any    =			pad
   _a						: Union[str, Any]    =			idaobj
   _a						: List[str]    =			idaattr
   _a						: Union[str, Any]    =			FigureCanvasAgg(UpperCAmelCase__							)


 def _lowercase			(  self	:		Tuple							,		UpperCAmelCase__	:		int							,		UpperCAmelCase__	:		Optional[Any]=None							)      -> List[Any]:
   if color is None:
     _a						: Optional[Any]    =			self.edgecolor
   ((_a)   ,						(_a)   ,						(_a)   ,						(_a))						: List[Any]    =			box
   _a						: Any    =			xa - xa
   _a						: Optional[Any]    =			ya - ya
   self.ax.add_patch(
       mpl.patches.Rectangle(
           (xa, ya)							,		UpperCAmelCase__							,		UpperCAmelCase__							,		fill=UpperCAmelCase__							,		edgecolor=UpperCAmelCase__							,		linewidth=self.font_size // 3							,		alpha=self.alpha							,		linestyle=self.linestyle							,		)							)


 def _lowercase			(  self	:		List[Any]							,		UpperCAmelCase__	:		List[str]							,		UpperCAmelCase__	:		Union[str, Any]=None							,		UpperCAmelCase__	:		List[str]=None							,		UpperCAmelCase__	:		Optional[Any]=None							,		UpperCAmelCase__	:		int=None							)      -> List[Any]:
   if len(boxes.shape							) > 2:
     _a						: Tuple    =			boxes[0]
   if len(obj_ids.shape							) > 1:
     _a						: str    =			obj_ids[0]
   if len(obj_scores.shape							) > 1:
     _a						: str    =			obj_scores[0]
   if len(attr_ids.shape							) > 1:
     _a						: Optional[Any]    =			attr_ids[0]
   if len(attr_scores.shape							) > 1:
     _a						: Union[str, Any]    =			attr_scores[0]
   if isinstance(UpperCAmelCase__							,		torch.Tensor							):
     _a						: Optional[Any]    =			boxes.numpy()
   if isinstance(UpperCAmelCase__							,		UpperCAmelCase__							):
     _a						: Tuple    =			np.array(UpperCAmelCase__							)
   assert isinstance(UpperCAmelCase__							,		np.ndarray							)
   _a						: int    =			np.prod(boxes[:, 2:] - boxes[:, :2]							,		axis=1							)
   _a						: Tuple    =			np.argsort(-areas							).tolist()
   _a						: Tuple    =			boxes[sorted_idxs] if boxes is not None else None
   _a						: int    =			obj_ids[sorted_idxs] if obj_ids is not None else None
   _a						: Dict    =			obj_scores[sorted_idxs] if obj_scores is not None else None
   _a						: Any    =			attr_ids[sorted_idxs] if attr_ids is not None else None
   _a						: int    =			attr_scores[sorted_idxs] if attr_scores is not None else None

   _a						: str    =			[self._random_color(maximum=1							) for _ in range(len(UpperCAmelCase__							)							)]
   _a						: Union[str, Any]    =			[assigned_colors[idx] for idx in sorted_idxs]
   if obj_ids is not None:
     _a						: int    =			self._create_text_labels_attr(UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							)
     for i in range(len(UpperCAmelCase__							)							):
       _a						: Optional[int]    =			assigned_colors[i]
       self.add_box(boxes[i]							,		UpperCAmelCase__							)
       self.draw_labels(labels[i]							,		boxes[i]							,		UpperCAmelCase__							)


 def _lowercase			(  self	:		Optional[int]							,		UpperCAmelCase__	:		Any							,		UpperCAmelCase__	:		Optional[Any]							,		UpperCAmelCase__	:		Dict							)      -> Optional[int]:
   _a   ,						_a   ,						_a   ,						_a						: Tuple    =			box
   _a						: Union[str, Any]    =			(xa, ya)
   _a						: Optional[int]    =			(ya - ya) * (xa - xa)
   _a						: Any    =			_SMALL_OBJ * self.scale
   if instance_area < small or ya - ya < 40 * self.scale:
     if ya >= self.height - 5:
       _a						: Optional[Any]    =			(xa, ya)
     else:
       _a						: Optional[int]    =			(xa, ya)

   _a						: Any    =			(ya - ya) / np.sqrt(self.height * self.width							)
   _a						: List[str]    =			self._change_color_brightness(UpperCAmelCase__							,		brightness_factor=0.7							)
   _a						: str    =			np.clip((height_ratio - 0.0_2) / 0.0_8 + 1							,		1.2							,		2							)
   font_size *= 0.7_5 * self.font_size

   self.draw_text(
       text=UpperCAmelCase__							,		position=UpperCAmelCase__							,		color=UpperCAmelCase__							,		)


 def _lowercase			(  self	:		Optional[Any]							,		UpperCAmelCase__	:		Any							,		UpperCAmelCase__	:		Dict							,		UpperCAmelCase__	:		Dict="g"							,		UpperCAmelCase__	:		List[Any]="left"							,		)      -> List[str]:
   _a						: Tuple    =			0
   _a						: Dict    =			self.font_size
   _a						: List[Any]    =			np.maximum(list(mplc.to_rgb(UpperCAmelCase__							)							)							,		0.2							)
   _a						: Tuple    =			max(0.8							,		np.max(UpperCAmelCase__							)							)
   _a						: Union[str, Any]    =			{
       """facecolor""": """black""",
       """alpha""": self.alpha,
       """pad""": self.pad,
       """edgecolor""": """none""",
   }
   _a   ,						_a						: Union[str, Any]    =			position
   self.ax.text(
       UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		size=font_size * self.scale							,		family="""sans-serif"""							,		bbox=UpperCAmelCase__							,		verticalalignment="""top"""							,		horizontalalignment=UpperCAmelCase__							,		color=UpperCAmelCase__							,		zorder=10							,		rotation=UpperCAmelCase__							,		)


 def _lowercase			(  self	:		str							,		UpperCAmelCase__	:		Tuple=None							)      -> int:
   if saveas is None:
     _a						: int    =			self.saveas
   if saveas.lower().endswith(""".jpg"""							) or saveas.lower().endswith(""".png"""							):
     cva.imwrite(
         UpperCAmelCase__							,		self._get_buffer()[:, :, ::-1]							,		)
   else:
     self.fig.savefig(UpperCAmelCase__							)


 def _lowercase			(  self	:		Union[str, Any]							,		UpperCAmelCase__	:		str							,		UpperCAmelCase__	:		Optional[int]							,		UpperCAmelCase__	:		Any							,		UpperCAmelCase__	:		int							)      -> Any:
   _a						: List[str]    =			[self.idaobj[i] for i in classes]
   _a						: str    =			[self.idaattr[i] for i in attr_classes]
   _a						: Dict    =			[
       f"""{label} {score:.2f} {attr} {attr_score:.2f}"""
       for label, score, attr, attr_score in zip(UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							)
   ]
   return labels


 def _lowercase			(  self	:		List[Any]							,		UpperCAmelCase__	:		Optional[Any]							,		UpperCAmelCase__	:		Optional[Any]							)      -> List[Any]:
   _a						: List[str]    =			[self.idaobj[i] for i in classes]
   if scores is not None:
     if labels is None:
       _a						: Union[str, Any]    =			["""{:.0f}%""".format(s * 100							) for s in scores]
     else:
       _a						: List[Any]    =			["""{} {:.0f}%""".format(UpperCAmelCase__							,		s * 100							) for li, s in zip(UpperCAmelCase__							,		UpperCAmelCase__							)]
   return labels


 def _lowercase			(  self	:		int							,		UpperCAmelCase__	:		Optional[Any]=255							)      -> Tuple:
   _a						: List[str]    =			np.random.randint(0							,		len(_COLORS							)							)
   _a						: Tuple    =			_COLORS[idx] * maximum
   if not self.rgb:
     _a						: Any    =			ret[::-1]
   return ret


 def _lowercase			(  self	:		List[Any]							)      -> str:
   if not self.pynb:
     _a   ,						_a						: Union[str, Any]    =			self.canvas.print_to_buffer()
     if (width, height) != (self.width, self.height):
       _a						: str    =			cva.resize(self.img							,		(width, height)							)
     else:
       _a						: Tuple    =			self.img
   else:
     _a						: str    =			io.BytesIO()  # works for cairo backend
     self.canvas.print_rgba(UpperCAmelCase__							)
     _a   ,						_a						: Union[str, Any]    =			self.width, self.height
     _a						: Tuple    =			buf.getvalue()
     _a						: Optional[Any]    =			self.img

   _a						: Tuple    =			np.frombuffer(UpperCAmelCase__							,		dtype="""uint8"""							)
   _a						: List[str]    =			buffer.reshape(UpperCAmelCase__							,		UpperCAmelCase__							,		4							)
   _a   ,						_a						: Any    =			np.split(UpperCAmelCase__							,		[3]							,		axis=2							)

   try:
     import numexpr as ne  # fuse them with numexpr

     _a						: Any    =			ne.evaluate("""img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)"""							)
   except ImportError:
     _a						: str    =			alpha.astype("""float32"""							) / 2_5_5.0
     _a						: Any    =			img * (1 - alpha) + rgb * alpha

   return visualized_image.astype("""uint8"""							)







 def _lowercase			(  self	:		List[Any]							,		UpperCAmelCase__	:		List[Any]							,		UpperCAmelCase__	:		Dict							)      -> List[str]:
   assert brightness_factor >= -1.0 and brightness_factor <= 1.0
   _a						: Union[str, Any]    =			mplc.to_rgb(UpperCAmelCase__							)
   _a						: Any    =			colorsys.rgb_to_hls(*mplc.to_rgb(UpperCAmelCase__							)							)
   _a						: str    =			polygon_color[1] + (brightness_factor * polygon_color[1])
   _a						: Optional[int]    =			0.0 if modified_lightness < 0.0 else modified_lightness
   _a						: Any    =			1.0 if modified_lightness > 1.0 else modified_lightness
   _a						: Union[str, Any]    =			colorsys.hls_to_rgb(polygon_color[0]							,		UpperCAmelCase__							,		polygon_color[2]							)
   return modified_color







_snake_case					   =     (
    np.array(
        [
            0.0_00,
            0.4_47,
            0.7_41,
            0.8_50,
            0.3_25,
            0.0_98,
            0.9_29,
            0.6_94,
            0.1_25,
            0.4_94,
            0.1_84,
            0.5_56,
            0.4_66,
            0.6_74,
            0.1_88,
            0.3_01,
            0.7_45,
            0.9_33,
            0.6_35,
            0.0_78,
            0.1_84,
            0.3_00,
            0.3_00,
            0.3_00,
            0.6_00,
            0.6_00,
            0.6_00,
            1.0_00,
            0.0_00,
            0.0_00,
            1.0_00,
            0.5_00,
            0.0_00,
            0.7_49,
            0.7_49,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            1.0_00,
            0.6_67,
            0.0_00,
            1.0_00,
            0.3_33,
            0.3_33,
            0.0_00,
            0.3_33,
            0.6_67,
            0.0_00,
            0.3_33,
            1.0_00,
            0.0_00,
            0.6_67,
            0.3_33,
            0.0_00,
            0.6_67,
            0.6_67,
            0.0_00,
            0.6_67,
            1.0_00,
            0.0_00,
            1.0_00,
            0.3_33,
            0.0_00,
            1.0_00,
            0.6_67,
            0.0_00,
            1.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.3_33,
            0.5_00,
            0.0_00,
            0.6_67,
            0.5_00,
            0.0_00,
            1.0_00,
            0.5_00,
            0.3_33,
            0.0_00,
            0.5_00,
            0.3_33,
            0.3_33,
            0.5_00,
            0.3_33,
            0.6_67,
            0.5_00,
            0.3_33,
            1.0_00,
            0.5_00,
            0.6_67,
            0.0_00,
            0.5_00,
            0.6_67,
            0.3_33,
            0.5_00,
            0.6_67,
            0.6_67,
            0.5_00,
            0.6_67,
            1.0_00,
            0.5_00,
            1.0_00,
            0.0_00,
            0.5_00,
            1.0_00,
            0.3_33,
            0.5_00,
            1.0_00,
            0.6_67,
            0.5_00,
            1.0_00,
            1.0_00,
            0.5_00,
            0.0_00,
            0.3_33,
            1.0_00,
            0.0_00,
            0.6_67,
            1.0_00,
            0.0_00,
            1.0_00,
            1.0_00,
            0.3_33,
            0.0_00,
            1.0_00,
            0.3_33,
            0.3_33,
            1.0_00,
            0.3_33,
            0.6_67,
            1.0_00,
            0.3_33,
            1.0_00,
            1.0_00,
            0.6_67,
            0.0_00,
            1.0_00,
            0.6_67,
            0.3_33,
            1.0_00,
            0.6_67,
            0.6_67,
            1.0_00,
            0.6_67,
            1.0_00,
            1.0_00,
            1.0_00,
            0.0_00,
            1.0_00,
            1.0_00,
            0.3_33,
            1.0_00,
            1.0_00,
            0.6_67,
            1.0_00,
            0.3_33,
            0.0_00,
            0.0_00,
            0.5_00,
            0.0_00,
            0.0_00,
            0.6_67,
            0.0_00,
            0.0_00,
            0.8_33,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            0.1_67,
            0.0_00,
            0.0_00,
            0.3_33,
            0.0_00,
            0.0_00,
            0.5_00,
            0.0_00,
            0.0_00,
            0.6_67,
            0.0_00,
            0.0_00,
            0.8_33,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            0.1_67,
            0.0_00,
            0.0_00,
            0.3_33,
            0.0_00,
            0.0_00,
            0.5_00,
            0.0_00,
            0.0_00,
            0.6_67,
            0.0_00,
            0.0_00,
            0.8_33,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            0.1_43,
            0.1_43,
            0.1_43,
            0.8_57,
            0.8_57,
            0.8_57,
            1.0_00,
            1.0_00,
            1.0_00,
        ]
    )
    .astype(np.floataa)
    .reshape(-1, 3)
)









"""simple docstring"""




import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


_snake_case					   =     1000_0000_0000
_snake_case					   =     1_0000

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

def     lowerCAmelCase__  (  UpperCamelCase__  ,     UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Union[str, Any]    =			pa.Table.from_pydict({"""col""": [0] * chunk_size}       )
  _a						: Dict    =			pa.concat_tables([table] * (num_examples // chunk_size)       )
  return datasets.Dataset(UpperCamelCase__  ,     fingerprint="""table_100B"""       )






@dataclass
class  UpperCamelCase    :
 UpperCamelCase      : int
 UpperCamelCase      : int
 UpperCamelCase      : int
 UpperCamelCase      : int


 def _lowercase			(  self	:		Optional[int]							)      -> Dict:
   _a						: Tuple    =			np.random.default_rng(self.seed							)
   _a						: Dict    =			rng.integers(low=self.low							,		high=self.high							,		size=self.size							).tolist()


 def __iter__(  self	:		List[Any]							)      -> Optional[Any]:
   return iter(self._sampled_values							)







 def __len__(  self	:		Dict							)      -> Optional[Any]:
   return self.size







@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Union[str, Any]    =			dataset[0]

@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Optional[Any]    =			dataset[-1]

@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: str    =			dataset[range(len(UpperCamelCase__       ) // 2  ,     len(UpperCamelCase__       ) // 2 + 1_0_2_4       )]

@get_duration
def     lowerCAmelCase__  (  UpperCamelCase__       ):



  '''simple docstring'''
  _a						: Tuple    =			dataset[RandIter(0  ,     len(UpperCamelCase__       )  ,     1_0_2_4  ,     seed=4_2       )]

def     lowerCAmelCase__  (  ):



  '''simple docstring'''
  _a						: str    =			{"""num examples""": SPEED_TEST_N_EXAMPLES}
  _a						: Dict    =			(get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
  print("""generating dataset"""       )
  _a						: List[str]    =			generate_100B_dataset(num_examples=UpperCamelCase__  ,     chunk_size=UpperCamelCase__       )
  print("""Functions"""       )
  for func in functions:
    print(func.__name__       )
    _a						: Optional[Any]    =			func(UpperCamelCase__       )

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


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

