code: 
  
  
  
  
  
  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
  
  
  __lowerCAmelCase     =			1_000
  
  
  
  
  class   _UpperCAmelCase	:
  
  
  
  
       def __init__(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase=1.2    ,      __lowerCAmelCase="g"    ,      __lowerCAmelCase=0.5    ,      __lowerCAmelCase="-"    ,      __lowerCAmelCase="test_out.jpg"    ,      __lowerCAmelCase=True    ,      __lowerCAmelCase=False    ,      __lowerCAmelCase=None    ,      __lowerCAmelCase=None    ,      __lowerCAmelCase=0.7    ,      )						->  str:
         if isinstance(__lowerCAmelCase    ,      torch.Tensor       ):
           UpperCamelCase__         =	img.numpy().astype("""np.uint8"""       )
         if isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ):
           UpperCamelCase__         =	img_tensorize(__lowerCAmelCase       )
         assert isinstance(__lowerCAmelCase    ,      np.ndarray       )
  
         UpperCamelCase__  ,			UpperCamelCase__         =	img.shape[1], img.shape[0]
         UpperCamelCase__         =	mplfigure.Figure(frameon=__lowerCAmelCase       )
         UpperCamelCase__         =	fig.get_dpi()
         UpperCamelCase__         =	(width * scale + 1E-2) / dpi
         UpperCamelCase__         =	(height * scale + 1E-2) / dpi
         fig.set_size_inches(__lowerCAmelCase    ,      __lowerCAmelCase       )
         UpperCamelCase__         =	fig.add_axes([0.0, 0.0, 1.0, 1.0]       )
         ax.axis("""off"""       )
         ax.set_xlim(0.0    ,      __lowerCAmelCase       )
         ax.set_ylim(__lowerCAmelCase       )
  
         UpperCamelCase__         =	saveas
         UpperCamelCase__         =	rgb
         UpperCamelCase__         =	pynb
         UpperCamelCase__         =	img
         UpperCamelCase__         =	edgecolor
         UpperCamelCase__         =	0.5
         UpperCamelCase__         =	linestyle
         UpperCamelCase__         =	int(np.sqrt(min(__lowerCAmelCase    ,      __lowerCAmelCase       )       ) * scale // 3       )
         UpperCamelCase__         =	width
         UpperCamelCase__         =	height
         UpperCamelCase__         =	scale
         UpperCamelCase__         =	fig
         UpperCamelCase__         =	ax
         UpperCamelCase__         =	pad
         UpperCamelCase__         =	idaobj
         UpperCamelCase__         =	idaattr
         UpperCamelCase__         =	FigureCanvasAgg(__lowerCAmelCase       )
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase=None       )						->  Tuple:
         if color is None:
           UpperCamelCase__         =	self.edgecolor
         ((UpperCamelCase__)  ,			(UpperCamelCase__)  ,			(UpperCamelCase__)  ,			(UpperCamelCase__))         =	box
         UpperCamelCase__         =	xa - xa
         UpperCamelCase__         =	ya - ya
         self.ax.add_patch(
             mpl.patches.Rectangle(
                 (xa, ya)    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      fill=__lowerCAmelCase    ,      edgecolor=__lowerCAmelCase    ,      linewidth=self.font_size // 3    ,      alpha=self.alpha    ,      linestyle=self.linestyle    ,      )       )
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase=None    ,      __lowerCAmelCase=None    ,      __lowerCAmelCase=None    ,      __lowerCAmelCase=None       )						->  Any:
         if len(boxes.shape       ) > 2:
           UpperCamelCase__         =	boxes[0]
         if len(obj_ids.shape       ) > 1:
           UpperCamelCase__         =	obj_ids[0]
         if len(obj_scores.shape       ) > 1:
           UpperCamelCase__         =	obj_scores[0]
         if len(attr_ids.shape       ) > 1:
           UpperCamelCase__         =	attr_ids[0]
         if len(attr_scores.shape       ) > 1:
           UpperCamelCase__         =	attr_scores[0]
         if isinstance(__lowerCAmelCase    ,      torch.Tensor       ):
           UpperCamelCase__         =	boxes.numpy()
         if isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ):
           UpperCamelCase__         =	np.array(__lowerCAmelCase       )
         assert isinstance(__lowerCAmelCase    ,      np.ndarray       )
         UpperCamelCase__         =	np.prod(boxes[:, 2:] - boxes[:, :2]    ,      axis=1       )
         UpperCamelCase__         =	np.argsort(-areas       ).tolist()
         UpperCamelCase__         =	boxes[sorted_idxs] if boxes is not None else None
         UpperCamelCase__         =	obj_ids[sorted_idxs] if obj_ids is not None else None
         UpperCamelCase__         =	obj_scores[sorted_idxs] if obj_scores is not None else None
         UpperCamelCase__         =	attr_ids[sorted_idxs] if attr_ids is not None else None
         UpperCamelCase__         =	attr_scores[sorted_idxs] if attr_scores is not None else None
  
         UpperCamelCase__         =	[self._random_color(maximum=1       ) for _ in range(len(__lowerCAmelCase       )       )]
         UpperCamelCase__         =	[assigned_colors[idx] for idx in sorted_idxs]
         if obj_ids is not None:
           UpperCamelCase__         =	self._create_text_labels_attr(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase       )
           for i in range(len(__lowerCAmelCase       )       ):
             UpperCamelCase__         =	assigned_colors[i]
             self.add_box(boxes[i]    ,      __lowerCAmelCase       )
             self.draw_labels(labels[i]    ,      boxes[i]    ,      __lowerCAmelCase       )
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase       )						->  Tuple:
         UpperCamelCase__  ,			UpperCamelCase__  ,			UpperCamelCase__  ,			UpperCamelCase__         =	box
         UpperCamelCase__         =	(xa, ya)
         UpperCamelCase__         =	(ya - ya) * (xa - xa)
         UpperCamelCase__         =	_SMALL_OBJ * self.scale
         if instance_area < small or ya - ya < 40 * self.scale:
           if ya >= self.height - 5:
             UpperCamelCase__         =	(xa, ya)
           else:
             UpperCamelCase__         =	(xa, ya)
  
         UpperCamelCase__         =	(ya - ya) / np.sqrt(self.height * self.width       )
         UpperCamelCase__         =	self._change_color_brightness(__lowerCAmelCase    ,      brightness_factor=0.7       )
         UpperCamelCase__         =	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=__lowerCAmelCase    ,      position=__lowerCAmelCase    ,      color=__lowerCAmelCase    ,      )
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase="g"    ,      __lowerCAmelCase="left"    ,      )						->  int:
         UpperCamelCase__         =	0
         UpperCamelCase__         =	self.font_size
         UpperCamelCase__         =	np.maximum(list(mplc.to_rgb(__lowerCAmelCase       )       )    ,      0.2       )
         UpperCamelCase__         =	max(0.8    ,      np.max(__lowerCAmelCase       )       )
         UpperCamelCase__         =	{
             """facecolor""": """black""",
             """alpha""": self.alpha,
             """pad""": self.pad,
             """edgecolor""": """none""",
         }
         UpperCamelCase__  ,			UpperCamelCase__         =	position
         self.ax.text(
             __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      size=font_size * self.scale    ,      family="""sans-serif"""    ,      bbox=__lowerCAmelCase    ,      verticalalignment="""top"""    ,      horizontalalignment=__lowerCAmelCase    ,      color=__lowerCAmelCase    ,      zorder=10    ,      rotation=__lowerCAmelCase    ,      )
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase=None       )						->  Dict:
         if saveas is None:
           UpperCamelCase__         =	self.saveas
         if saveas.lower().endswith(""".jpg"""       ) or saveas.lower().endswith(""".png"""       ):
           cva.imwrite(
               __lowerCAmelCase    ,      self._get_buffer()[:, :, ::-1]    ,      )
         else:
           self.fig.savefig(__lowerCAmelCase       )
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase       )						->  int:
         UpperCamelCase__         =	[self.idaobj[i] for i in classes]
         UpperCamelCase__         =	[self.idaattr[i] for i in attr_classes]
         UpperCamelCase__         =	[
             F"""{label} {score:.2f} {attr} {attr_score:.2f}"""
             for label, score, attr, attr_score in zip(__lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase       )
         ]
         return labels
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase       )						->  Any:
         UpperCamelCase__         =	[self.idaobj[i] for i in classes]
         if scores is not None:
           if labels is None:
             UpperCamelCase__         =	["""{:.0f}%""".format(s * 100       ) for s in scores]
           else:
             UpperCamelCase__         =	["""{} {:.0f}%""".format(__lowerCAmelCase    ,      s * 100       ) for li, s in zip(__lowerCAmelCase    ,      __lowerCAmelCase       )]
         return labels
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase=255       )						->  Any:
         UpperCamelCase__         =	np.random.randint(0    ,      len(_COLORS       )       )
         UpperCamelCase__         =	_COLORS[idx] * maximum
         if not self.rgb:
           UpperCamelCase__         =	ret[::-1]
         return ret
  
  
  
  
       def 	snake_case_		(						self       )						->  Dict:
         if not self.pynb:
           UpperCamelCase__  ,			UpperCamelCase__         =	self.canvas.print_to_buffer()
           if (width, height) != (self.width, self.height):
             UpperCamelCase__         =	cva.resize(self.img    ,      (width, height)       )
           else:
             UpperCamelCase__         =	self.img
         else:
           UpperCamelCase__         =	io.BytesIO()  # works for cairo backend
           self.canvas.print_rgba(__lowerCAmelCase       )
           UpperCamelCase__  ,			UpperCamelCase__         =	self.width, self.height
           UpperCamelCase__         =	buf.getvalue()
           UpperCamelCase__         =	self.img
  
         UpperCamelCase__         =	np.frombuffer(__lowerCAmelCase    ,      dtype="""uint8"""       )
         UpperCamelCase__         =	buffer.reshape(__lowerCAmelCase    ,      __lowerCAmelCase    ,      4       )
         UpperCamelCase__  ,			UpperCamelCase__         =	np.split(__lowerCAmelCase    ,      [3]    ,      axis=2       )
  
         try:
           import numexpr as ne  # fuse them with numexpr
  
           UpperCamelCase__         =	ne.evaluate("""img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)"""       )
         except ImportError:
           UpperCamelCase__         =	alpha.astype("""float32"""       ) / 255.0
           UpperCamelCase__         =	img * (1 - alpha) + rgb * alpha
  
         return visualized_image.astype("""uint8"""       )
  
  
  
  
       def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase       )						->  int:
         assert brightness_factor >= -1.0 and brightness_factor <= 1.0
         UpperCamelCase__         =	mplc.to_rgb(__lowerCAmelCase       )
         UpperCamelCase__         =	colorsys.rgb_to_hls(*mplc.to_rgb(__lowerCAmelCase       )       )
         UpperCamelCase__         =	polygon_color[1] + (brightness_factor * polygon_color[1])
         UpperCamelCase__         =	0.0 if modified_lightness < 0.0 else modified_lightness
         UpperCamelCase__         =	1.0 if modified_lightness > 1.0 else modified_lightness
         UpperCamelCase__         =	colorsys.hls_to_rgb(polygon_color[0]    ,      __lowerCAmelCase    ,      polygon_color[2]       )
         return modified_color
  
  
  
  __lowerCAmelCase     =			(
      np.array(
          [
              0.000,
              0.447,
              0.741,
              0.850,
              0.325,
              0.098,
              0.929,
              0.694,
              0.125,
              0.494,
              0.184,
              0.556,
              0.466,
              0.674,
              0.188,
              0.301,
              0.745,
              0.933,
              0.635,
              0.078,
              0.184,
              0.300,
              0.300,
              0.300,
              0.600,
              0.600,
              0.600,
              1.000,
              0.000,
              0.000,
              1.000,
              0.500,
              0.000,
              0.749,
              0.749,
              0.000,
              0.000,
              1.000,
              0.000,
              0.000,
              0.000,
              1.000,
              0.667,
              0.000,
              1.000,
              0.333,
              0.333,
              0.000,
              0.333,
              0.667,
              0.000,
              0.333,
              1.000,
              0.000,
              0.667,
              0.333,
              0.000,
              0.667,
              0.667,
              0.000,
              0.667,
              1.000,
              0.000,
              1.000,
              0.333,
              0.000,
              1.000,
              0.667,
              0.000,
              1.000,
              1.000,
              0.000,
              0.000,
              0.333,
              0.500,
              0.000,
              0.667,
              0.500,
              0.000,
              1.000,
              0.500,
              0.333,
              0.000,
              0.500,
              0.333,
              0.333,
              0.500,
              0.333,
              0.667,
              0.500,
              0.333,
              1.000,
              0.500,
              0.667,
              0.000,
              0.500,
              0.667,
              0.333,
              0.500,
              0.667,
              0.667,
              0.500,
              0.667,
              1.000,
              0.500,
              1.000,
              0.000,
              0.500,
              1.000,
              0.333,
              0.500,
              1.000,
              0.667,
              0.500,
              1.000,
              1.000,
              0.500,
              0.000,
              0.333,
              1.000,
              0.000,
              0.667,
              1.000,
              0.000,
              1.000,
              1.000,
              0.333,
              0.000,
              1.000,
              0.333,
              0.333,
              1.000,
              0.333,
              0.667,
              1.000,
              0.333,
              1.000,
              1.000,
              0.667,
              0.000,
              1.000,
              0.667,
              0.333,
              1.000,
              0.667,
              0.667,
              1.000,
              0.667,
              1.000,
              1.000,
              1.000,
              0.000,
              1.000,
              1.000,
              0.333,
              1.000,
              1.000,
              0.667,
              1.000,
              0.333,
              0.000,
              0.000,
              0.500,
              0.000,
              0.000,
              0.667,
              0.000,
              0.000,
              0.833,
              0.000,
              0.000,
              1.000,
              0.000,
              0.000,
              0.000,
              0.167,
              0.000,
              0.000,
              0.333,
              0.000,
              0.000,
              0.500,
              0.000,
              0.000,
              0.667,
              0.000,
              0.000,
              0.833,
              0.000,
              0.000,
              1.000,
              0.000,
              0.000,
              0.000,
              0.167,
              0.000,
              0.000,
              0.333,
              0.000,
              0.000,
              0.500,
              0.000,
              0.000,
              0.667,
              0.000,
              0.000,
              0.833,
              0.000,
              0.000,
              1.000,
              0.000,
              0.000,
              0.000,
              0.143,
              0.143,
              0.143,
              0.857,
              0.857,
              0.857,
              1.000,
              1.000,
              1.000,
          ]
      )
      .astype(np.floataa)
      .reshape(-1, 3)
  )
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  __lowerCAmelCase     =			{
      "0": 1,
      "1": 1,
      "2": 2,
      "3": 6,
      "4": 24,
      "5": 120,
      "6": 720,
      "7": 5_040,
      "8": 40_320,
      "9": 362_880,
  }
  
  __lowerCAmelCase     =			{145: 145}
  
  __lowerCAmelCase     =			{
      145: 0,
      169: 3,
      36_301: 3,
      1_454: 3,
      871: 2,
      45_361: 2,
      872: 2,
  }
  
  def     a						(      _lowercase   :     int       )     ->int:
  		if n in CACHE_SUM_DIGIT_FACTORIALS:
  				return CACHE_SUM_DIGIT_FACTORIALS[n]
  		UpperCamelCase__         =	sum(DIGIT_FACTORIALS[let] for let in str(_lowercase       )       )
  		UpperCamelCase__         =	ret
  		return ret
  
  def     a						(      _lowercase   :     int ,						_lowercase   :     set | None = None       )     ->int:
  		UpperCamelCase__         =	previous or set()
  		if n in CHAIN_LENGTH_CACHE:
  				return CHAIN_LENGTH_CACHE[n]
  		UpperCamelCase__         =	sum_digit_factorials(_lowercase       )
  		if next_number in previous:
  				UpperCamelCase__         =	0
  				return 0
  		else:
  				previous.add(_lowercase       )
  				UpperCamelCase__         =	1 + chain_length(_lowercase ,						_lowercase       )
  				UpperCamelCase__         =	ret
  				return ret
  
  def     a						(      _lowercase   :     int = 6_0 ,						_lowercase   :     int = 1_0_0_0_0_0_0       )     ->int:
  		return sum(1 for i in range(1 ,						_lowercase       ) if chain_length(_lowercase       ) == num_terms       )
  
  
  if __name__ == "__main__":
  				print(f"""{solution() = }""")
  
  
  
  
  
  