



'''simple docstring'''





import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

 if is_vision_available():
  from transformers import MaskFormerImageProcessor

if is_vision_available():
 from PIL import Image


class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=2  ,UpperCAmelCase_=True  ,UpperCAmelCase_=False  ,UpperCAmelCase_=10  ,UpperCAmelCase_=3  ,UpperCAmelCase_=32 * 4  ,UpperCAmelCase_=32 * 6  ,UpperCAmelCase_=4  ,UpperCAmelCase_=32  ,)	->				List[str]:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    is_training
           lowercase__									=    use_auxiliary_loss
           lowercase__									=    num_queries
           lowercase__									=    num_channels
           lowercase__									=    min_size
           lowercase__									=    max_size
           lowercase__									=    num_labels
           lowercase__									=    mask_feature_size



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]   ).to(
               UpperCAmelCase_   )

           lowercase__									=    torch.ones([self.batch_size, self.min_size, self.max_size]  ,device=UpperCAmelCase_   )

           lowercase__									=    (
               torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]  ,device=UpperCAmelCase_   ) > 0.5
           ).float()
           lowercase__									=    (torch.rand((self.batch_size, self.num_labels)  ,device=UpperCAmelCase_   ) > 0.5).long()

           lowercase__									=    self.get_config()
           return config, pixel_values, pixel_mask, mask_labels, class_labels



       def 				_a   (					self   )	->				List[Any]:
           return MaskFormerConfig.from_backbone_and_decoder_configs(
               backbone_config=SwinConfig(
                   depths=[1, 1, 1, 1]  ,)  ,decoder_config=DetrConfig(
                   decoder_ffn_dim=128  ,num_queries=self.num_queries  ,decoder_attention_heads=2  ,d_model=self.mask_feature_size  ,)  ,mask_feature_size=self.mask_feature_size  ,fpn_feature_size=self.mask_feature_size  ,num_channels=self.num_channels  ,num_labels=self.num_labels  ,)



       def 				_a   (					self   )	->				str:
           lowercase__							,							lowercase__							,							lowercase__							,							lowercase__							,							lowercase__									=    self.prepare_config_and_inputs()
           lowercase__									=    {"pixel_values": pixel_values, "pixel_mask": pixel_mask}
           return config, inputs_dict



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Any:
           lowercase__									=    output.encoder_hidden_states
           lowercase__									=    output.pixel_decoder_hidden_states
           lowercase__									=    output.transformer_decoder_hidden_states

           self.parent.assertTrue(len(UpperCAmelCase_   )  ,len(config.backbone_config.depths   )   )
           self.parent.assertTrue(len(UpperCAmelCase_   )  ,len(config.backbone_config.depths   )   )
           self.parent.assertTrue(len(UpperCAmelCase_   )  ,config.decoder_config.decoder_layers   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_=False   )	->				str:
           with torch.no_grad():
               lowercase__									=    MaskFormerModel(config=UpperCAmelCase_   )
               model.to(UpperCAmelCase_   )
               model.eval()

               lowercase__									=    model(pixel_values=UpperCAmelCase_  ,pixel_mask=UpperCAmelCase_   )
               lowercase__									=    model(UpperCAmelCase_  ,output_hidden_states=UpperCAmelCase_   )
           # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
           # encoder and pixel decoder
           self.parent.assertEqual(
               output.transformer_decoder_last_hidden_state.shape  ,(self.batch_size, self.num_queries, self.mask_feature_size)  ,)
           # let's ensure the other two hidden state exists
           self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None   )
           self.parent.assertTrue(output.encoder_last_hidden_state is not None   )

           if output_hidden_states:
               self.check_output_hidden_state(UpperCAmelCase_  ,UpperCAmelCase_   )







       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				List[Any]:
           lowercase__									=    MaskFormerForInstanceSegmentation(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           def comm_check_on_output(UpperCAmelCase_   ):
               # let's still check that all the required stuff is there
               self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None   )
               self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None   )
               self.parent.assertTrue(result.encoder_last_hidden_state is not None   )
               # okay, now we need to check the logits shape
               # due to the encoder compression, masks have a //4 spatial size
               self.parent.assertEqual(
                   result.masks_queries_logits.shape  ,(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)  ,)
               # + 1 for null class
               self.parent.assertEqual(
                   result.class_queries_logits.shape  ,(self.batch_size, self.num_queries, self.num_labels + 1)   )

           with torch.no_grad():
               lowercase__									=    model(pixel_values=UpperCAmelCase_  ,pixel_mask=UpperCAmelCase_   )
               lowercase__									=    model(UpperCAmelCase_   )

               comm_check_on_output(UpperCAmelCase_   )

               lowercase__									=    model(
                   pixel_values=UpperCAmelCase_  ,pixel_mask=UpperCAmelCase_  ,mask_labels=UpperCAmelCase_  ,class_labels=UpperCAmelCase_   )

           comm_check_on_output(UpperCAmelCase_   )

           self.parent.assertTrue(result.loss is not None   )
           self.parent.assertEqual(result.loss.shape  ,torch.Size([1]   )   )





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Tuple     =       (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
       lowerCAmelCase__	:str     =       (
           {"feature-extraction": MaskFormerModel, "image-segmentation": MaskFormerForInstanceSegmentation}
           if is_torch_available()
           else {}
       )

       lowerCAmelCase__	:Union[str, Any]     =       False
       lowerCAmelCase__	:Dict     =       False
       lowerCAmelCase__	:Any     =       False
       lowerCAmelCase__	:Union[str, Any]     =       False



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    MaskFormerModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,has_text_modality=UpperCAmelCase_   )



       def 				_a   (					self   )	->				Any:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskformer_model(UpperCAmelCase_  ,**UpperCAmelCase_  ,output_hidden_states=UpperCAmelCase_   )



       def 				_a   (					self   )	->				str:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*UpperCAmelCase_   )



       @unittest.skip(reason="MaskFormer does not use inputs_embeds"   )
       def 				_a   (					self   )	->				Optional[int]:
           pass



       @unittest.skip(reason="MaskFormer does not have a get_input_embeddings method"   )
       def 				_a   (					self   )	->				Any:
           pass



       @unittest.skip(reason="MaskFormer is not a generative model"   )
       def 				_a   (					self   )	->				List[str]:
           pass



       @unittest.skip(reason="MaskFormer does not use token embeddings"   )
       def 				_a   (					self   )	->				Dict:
           pass



       @require_torch_multi_gpu
       @unittest.skip(
           reason="MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`"   )
       def 				_a   (					self   )	->				Union[str, Any]:
           pass



       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."   )
       def 				_a   (					self   )	->				int:
           pass



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowercase__									=    model_class(UpperCAmelCase_   )
               lowercase__									=    inspect.signature(model.forward   )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               lowercase__									=    [*signature.parameters.keys()]

               lowercase__									=    ["pixel_values"]
               self.assertListEqual(arg_names[:1]  ,UpperCAmelCase_   )



       @slow
       def 				_a   (					self   )	->				List[Any]:
           for model_name in ["facebook/maskformer-swin-small-coco"]:
               lowercase__									=    MaskFormerModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    (self.model_tester.min_size,) * 2
           lowercase__									=    {
               "pixel_values": torch.randn((2, 3, *size)  ,device=UpperCAmelCase_   ),
               "mask_labels": torch.randn((2, 10, *size)  ,device=UpperCAmelCase_   ),
               "class_labels": torch.zeros(2  ,10  ,device=UpperCAmelCase_   ).long(),
           }

           lowercase__									=    MaskFormerForInstanceSegmentation(MaskFormerConfig()   ).to(UpperCAmelCase_   )
           lowercase__									=    model(**UpperCAmelCase_   )
           self.assertTrue(outputs.loss is not None   )



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.create_and_check_maskformer_model(UpperCAmelCase_  ,**UpperCAmelCase_  ,output_hidden_states=UpperCAmelCase_   )



       def 				_a   (					self   )	->				Dict:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowercase__									=    model_class(UpperCAmelCase_   ).to(UpperCAmelCase_   )
               lowercase__									=    model(**UpperCAmelCase_  ,output_attentions=UpperCAmelCase_   )
               self.assertTrue(outputs.attentions is not None   )



       def 				_a   (					self   )	->				Dict:
           if not self.model_tester.is_training:
               return
           # only MaskFormerForInstanceSegmentation has the loss
           lowercase__									=    self.all_model_classes[1]
           lowercase__							,							lowercase__							,							lowercase__							,							lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs()

           lowercase__									=    model_class(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.train()

           lowercase__									=    model(UpperCAmelCase_  ,mask_labels=UpperCAmelCase_  ,class_labels=UpperCAmelCase_   ).loss
           loss.backward()







       def 				_a   (					self   )	->				Any:
           # only MaskFormerForInstanceSegmentation has the loss
           lowercase__									=    self.all_model_classes[1]
           lowercase__							,							lowercase__							,							lowercase__							,							lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs()
           lowercase__									=    True
           lowercase__									=    True

           lowercase__									=    model_class(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.train()

           lowercase__									=    model(UpperCAmelCase_  ,mask_labels=UpperCAmelCase_  ,class_labels=UpperCAmelCase_   )

           lowercase__									=    outputs.encoder_hidden_states[0]
           encoder_hidden_states.retain_grad()

           lowercase__									=    outputs.pixel_decoder_hidden_states[0]
           pixel_decoder_hidden_states.retain_grad()
           # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
           lowercase__									=    outputs.transformer_decoder_hidden_states[0]
           transformer_decoder_hidden_states.retain_grad()

           lowercase__									=    outputs.attentions[0]
           attentions.retain_grad()

           outputs.loss.backward(retain_graph=UpperCAmelCase_   )

           self.assertIsNotNone(encoder_hidden_states.grad   )
           self.assertIsNotNone(pixel_decoder_hidden_states.grad   )
           self.assertIsNotNone(transformer_decoder_hidden_states.grad   )
           self.assertIsNotNone(attentions.grad   )





SCREAMING_SNAKE_CASE__          =  1e-4




def 							lowerCamelCase	(					):





    '''simple docstring'''


    lowercase__									=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
    return image



@require_vision
@slow
class 					snake_case			(unittest.TestCase ):



       @cached_property
       def 				_a   (					self   )	->				int:
           return (
               MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco"   )
               if is_vision_available()
               else None
           )



       def 				_a   (					self   )	->				int:
           lowercase__									=    MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco"   ).to(UpperCAmelCase_   )
           lowercase__									=    self.default_image_processor
           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(UpperCAmelCase_  ,return_tensors="pt"   ).to(UpperCAmelCase_   )
           lowercase__									=    inputs["pixel_values"].shape
           # check size is divisible by 32
           self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0   )
           # check size
           self.assertEqual(UpperCAmelCase_  ,(1, 3, 800, 1_088)   )

           with torch.no_grad():
               lowercase__									=    model(**UpperCAmelCase_   )

           lowercase__									=    torch.tensor(
               [[-0.04_82, 0.92_28, 0.49_51], [-0.25_47, 0.80_17, 0.85_27], [-0.00_69, 0.33_85, -0.00_89]]   ).to(UpperCAmelCase_   )
           self.assertTrue(
               torch.allclose(
                   outputs.encoder_last_hidden_state[0, 0, :3, :3]  ,UpperCAmelCase_  ,atol=UpperCAmelCase_   )   )

           lowercase__									=    torch.tensor(
               [[-0.84_22, -0.84_34, -0.97_18], [-1.01_44, -0.55_65, -0.41_95], [-1.00_38, -0.44_84, -0.19_61]]   ).to(UpperCAmelCase_   )
           self.assertTrue(
               torch.allclose(
                   outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]  ,UpperCAmelCase_  ,atol=UpperCAmelCase_   )   )

           lowercase__									=    torch.tensor(
               [[0.28_52, -0.01_59, 0.97_35], [0.62_54, 0.18_58, 0.85_29], [-0.06_80, -0.41_16, 1.84_13]]   ).to(UpperCAmelCase_   )
           self.assertTrue(
               torch.allclose(
                   outputs.transformer_decoder_last_hidden_state[0, :3, :3]  ,UpperCAmelCase_  ,atol=UpperCAmelCase_   )   )



       def 				_a   (					self   )	->				Dict:
           lowercase__									=    (
               MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"   )
               .to(UpperCAmelCase_   )
               .eval()
           )
           lowercase__									=    self.default_image_processor
           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(UpperCAmelCase_  ,return_tensors="pt"   ).to(UpperCAmelCase_   )
           lowercase__									=    inputs["pixel_values"].shape
           # check size is divisible by 32
           self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0   )
           # check size
           self.assertEqual(UpperCAmelCase_  ,(1, 3, 800, 1_088)   )

           with torch.no_grad():
               lowercase__									=    model(**UpperCAmelCase_   )
           # masks_queries_logits
           lowercase__									=    outputs.masks_queries_logits
           self.assertEqual(
               masks_queries_logits.shape  ,(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)  ,)
           lowercase__									=    [
               [-1.3_73_71_24, -1.7_72_49_37, -1.9_36_42_33],
               [-1.5_97_72_81, -1.9_86_79_39, -2.1_52_36_95],
               [-1.5_79_53_98, -1.9_26_98_32, -2.09_39_42],
           ]
           lowercase__									=    torch.tensor(UpperCAmelCase_   ).to(UpperCAmelCase_   )
           self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]  ,UpperCAmelCase_  ,atol=UpperCAmelCase_   )   )
           # class_queries_logits
           lowercase__									=    outputs.class_queries_logits
           self.assertEqual(
               class_queries_logits.shape  ,(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)   )
           lowercase__									=    torch.tensor(
               [
                   [1.6_5_1_2E0_0, -5.2_5_7_2E0_0, -3.3_5_1_9E0_0],
                   [3.6_1_6_9E-0_2, -5.9_0_2_5E0_0, -2.9_3_1_3E0_0],
                   [1.0_7_6_6E-0_4, -7.7_6_3_0E0_0, -5.1_2_6_3E0_0],
               ]   ).to(UpperCAmelCase_   )
           self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]  ,UpperCAmelCase_  ,atol=UpperCAmelCase_   )   )



       def 				_a   (					self   )	->				Dict:
           lowercase__									=    (
               MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff"   )
               .to(UpperCAmelCase_   )
               .eval()
           )
           lowercase__									=    self.default_image_processor
           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(UpperCAmelCase_  ,return_tensors="pt"   ).to(UpperCAmelCase_   )
           lowercase__									=    inputs["pixel_values"].shape
           # check size is divisible by 32
           self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0   )
           # check size
           self.assertEqual(UpperCAmelCase_  ,(1, 3, 800, 1_088)   )

           with torch.no_grad():
               lowercase__									=    model(**UpperCAmelCase_   )
           # masks_queries_logits
           lowercase__									=    outputs.masks_queries_logits
           self.assertEqual(
               masks_queries_logits.shape  ,(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)  ,)
           lowercase__									=    [[-0.90_46, -2.63_66, -4.60_62], [-3.41_79, -5.78_90, -8.80_57], [-4.91_79, -7.65_60, -10.77_11]]
           lowercase__									=    torch.tensor(UpperCAmelCase_   ).to(UpperCAmelCase_   )
           self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]  ,UpperCAmelCase_  ,atol=UpperCAmelCase_   )   )
           # class_queries_logits
           lowercase__									=    outputs.class_queries_logits
           self.assertEqual(
               class_queries_logits.shape  ,(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)   )
           lowercase__									=    torch.tensor(
               [[4.71_88, -3.25_85, -2.88_57], [6.68_71, -2.91_81, -1.24_87], [7.24_49, -2.27_64, -2.18_74]]   ).to(UpperCAmelCase_   )
           self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]  ,UpperCAmelCase_  ,atol=UpperCAmelCase_   )   )







       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    (
               MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco"   )
               .to(UpperCAmelCase_   )
               .eval()
           )
           lowercase__									=    self.default_image_processor

           lowercase__									=    image_processor(
               [np.zeros((3, 800, 1_333)   ), np.zeros((3, 800, 1_333)   )]  ,segmentation_maps=[np.zeros((384, 384)   ).astype(np.floataa   ), np.zeros((384, 384)   ).astype(np.floataa   )]  ,return_tensors="pt"  ,)

           lowercase__									=    inputs["pixel_values"].to(UpperCAmelCase_   )
           lowercase__									=    [el.to(UpperCAmelCase_   ) for el in inputs["mask_labels"]]
           lowercase__									=    [el.to(UpperCAmelCase_   ) for el in inputs["class_labels"]]

           with torch.no_grad():
               lowercase__									=    model(**UpperCAmelCase_   )

           self.assertTrue(outputs.loss is not None   )









'''simple docstring'''





from __future__ import annotations

import time

import numpy as np

SCREAMING_SNAKE_CASE__          =  [8, 5, 9, 7]
SCREAMING_SNAKE_CASE__          =  [
    [2, 0, 1, 1],
    [0, 1, 2, 1],
    [4, 0, 0, 3],
    [0, 2, 1, 0],
    [1, 0, 3, 0],
]
SCREAMING_SNAKE_CASE__          =  [
    [3, 2, 1, 4],
    [0, 2, 5, 2],
    [5, 1, 0, 5],
    [1, 5, 3, 0],
    [3, 0, 3, 3],
]


class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,)	->				None:
           lowercase__									=    claim_vector
           lowercase__									=    allocated_resources_table
           lowercase__									=    maximum_claim_table



       def 				_a   (					self   )	->				list[int]:
           return [
               sum(p_item[i] for p_item in self.__allocated_resources_table   )
               for i in range(len(self.__allocated_resources_table[0]   )   )
           ]



       def 				_a   (					self   )	->				list[int]:
           return np.array(self.__claim_vector   ) - np.array(
               self.__processes_resource_summation()   )



       def 				_a   (					self   )	->				list[list[int]]:
           return [
               list(np.array(self.__maximum_claim_table[i]   ) - np.array(UpperCAmelCase_   )   )
               for i, allocated_resource in enumerate(self.__allocated_resources_table   )
           ]



       def 				_a   (					self   )	->				dict[int, list[int]]:
           return {self.__need().index(UpperCAmelCase_   ): i for i in self.__need()}



       def 				_a   (					self  ,**UpperCAmelCase_   )	->				None:
           lowercase__									=    self.__need()
           lowercase__									=    self.__allocated_resources_table
           lowercase__									=    self.__available_resources()
           lowercase__									=    self.__need_index_manager()
           for kw, val in kwargs.items():
               if kw and val is True:
                   self.__pretty_data()
           print("_" * 50 + "\n"   )
           while need_list:
               lowercase__									=    False
               for each_need in need_list:
                   lowercase__									=    True
                   for index, need in enumerate(UpperCAmelCase_   ):
                       if need > available_resources[index]:
                           lowercase__									=    False
                           break
                   if execution:
                       lowercase__									=    True
                       # get the original index of the process from ind_ctrl db
                       for original_need_index, need_clone in need_index_manager.items():
                           if each_need == need_clone:
                               lowercase__									=    original_need_index
                       print(F'''Process {process_number + 1} is executing.'''   )
                       # remove the process run from stack
                       need_list.remove(UpperCAmelCase_   )
                       # update available/freed resources stack
                       lowercase__									=    np.array(UpperCAmelCase_   ) + np.array(
                           alloc_resources_table[process_number]   )
                       print(
                           "Updated available resource stack for processes: "
                           + " ".join([str(UpperCAmelCase_   ) for x in available_resources]   )   )
                       break
               if safe:
                   print("The process is in a safe state.\n"   )
               else:
                   print("System in unsafe state. Aborting...\n"   )
                   break







       def 				_a   (					self   )	->				List[Any]:
           print(" " * 9 + "Allocated Resource Table"   )
           for item in self.__allocated_resources_table:
               print(
                   F'''P{self.__allocated_resources_table.index(UpperCAmelCase_   ) + 1}'''
                   + " ".join(F'''{it:>8}''' for it in item   )
                   + "\n"   )
           print(" " * 9 + "System Resource Table"   )
           for item in self.__maximum_claim_table:
               print(
                   F'''P{self.__maximum_claim_table.index(UpperCAmelCase_   ) + 1}'''
                   + " ".join(F'''{it:>8}''' for it in item   )
                   + "\n"   )
           print(
               "Current Usage by Active Processes: "
               + " ".join(str(UpperCAmelCase_   ) for x in self.__claim_vector   )   )
           print(
               "Initial Available Resources:       "
               + " ".join(str(UpperCAmelCase_   ) for x in self.__available_resources()   )   )
           time.sleep(1   )


if __name__ == "__main__":
 import doctest

 doctest.testmod()




