def run()

in pathology/dicom_proxy/frame_caching_util.py [0:0]


  def run(self) -> None:
    """Entry point for thread that loads a DICOM frames into the cache."""
    if self._load_display_thread is not None:
      self._load_display_thread.run()  # pytype: disable=attribute-error
    log_struct = {
        DICOM_INSTANCE: self._instance_request.dicom_sop_instance_url,
        CACHE_WHOLE_INSTANCE: False,
        NUMBER_OF_FRAMES_IN_DICOM_INSTANCE: (
            self._instance_request.metadata.number_of_frames
        ),
        'frames_height': self._height_frame_count,
        'frames_width': self._width_frame_count,
        'frames_area': self._height_frame_count * self._width_frame_count,
    }
    try:
      start_time = time.time()
      instance_request = self._instance_request
      uauth = instance_request.user_auth
      try:
        render_params = get_cache_render_params(
            instance_request.dicom_sop_instance_url, instance_request.metadata
        )
      except UnexpectedDicomTransferSyntaxError as exp:
        cloud_logging_client.warning(
            'Can not cache DICOM instance, instance encoded in unsupported'
            ' transfer syntax.',
            {
                'dicomWebInstance': instance_request.dicom_sop_instance_url,
                'transfer_syntax_uid': (
                    instance_request.metadata.dicom_transfer_syntax
                ),
            },
            exp,
        )
        return
      redis = redis_cache.RedisCache()
      # Clip range to edge of region not in block
      frame_list = []
      frame_counter = 0
      for frame_row_index_offset in range(
          self._first_frame_row * self._frames_per_row,
          (self._first_frame_row + self._height_frame_count)
          * self._frames_per_row,
          self._frames_per_row,
      ):
        for frame_number in self._get_frame_number_range_in_row(
            frame_row_index_offset
        ):
          frame_counter += 1
          if not set_cached_frame(
              redis,
              uauth,
              instance_request.dicom_sop_instance_url,
              render_params,
              5,  # Tests if frame defined in cache. If not
              False,  # Try to set cache loading placeholder
              frame_retrieval_util.CACHE_LOADING_FRAME_BYTES,
              frame_number,
          ):  # defined marks frame with temp place holder
            if self._require_first_frame and frame_counter == 1:
              return
            continue  # avoid but not prevent duplicate cache load.
          frame_list.append(frame_number)
      if len(frame_list) < 2:
        return
      log_struct[NUMBER_OF_FRAMES_CACHED] = len(frame_list)
      instance_request.get_raw_dicom_frames(render_params, frame_list)
      elapsed_time = time.time() - start_time
      log_struct[ELAPSED_TIME] = elapsed_time
      cloud_logging_client.info(
          f'Done Frame Block Cache; Loaded: {len(frame_list)}(# of frames);'
          f' Elapsed time: {elapsed_time:.3f}(sec)',
          log_struct,
      )
    except Exception as exp:
      cloud_logging_client.info(
          'Unexpected exception occurred loading frame block cache.',
          log_struct,
          exp,
      )
      raise