Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing imports and undefined variables in the example #48

Open
ayyucedemirbas opened this issue Nov 1, 2024 · 0 comments
Open

Missing imports and undefined variables in the example #48

ayyucedemirbas opened this issue Nov 1, 2024 · 0 comments

Comments

@ayyucedemirbas
Copy link

Hello, while running the CT_Foundation_Demo.ipynb notebook in this repository, I encountered a series of issues with the get_ct_embeddings function and its usage in subsequent cells. Below is a step-by-step description of the problems I faced and the adjustments I made to keep the code running.

I attempted to run the following code block:

from google.api_core.client_options import ClientOptions
from google.api_core.retry import Retry
from google.cloud import aiplatform
from google.api_core import exceptions
_API_ENDPOINT = 'us-west1-aiplatform.googleapis.com'


def create_lidc_series_url(study_instance_uid, series_instance_uid):
  return ('https://healthcare.googleapis.com/v1/projects/hai-cd3-foundations/'
          'locations/us-central1/datasets/ct3d/dicomStores/lidc-idri/dicomWeb/'
          f'studies/{study_instance_uid}/series/{series_instance_uid}')


_RETRIABLE_TYPES = (
    exceptions.TooManyRequests,  # HTTP 429
    exceptions.InternalServerError,  # HTTP 500
    exceptions.BadGateway,  # HTTP 502
    exceptions.ServiceUnavailable,  # HTTP 503
    exceptions.DeadlineExceeded,  # HTTP 504
)


def _is_retryable(exc):
  return isinstance(exc, _RETRIABLE_TYPES)


retry_policy = Retry(predicate=_is_retryable)


def get_ct_embeddings(mytoken: str, dicom_urls: List[str]) -> List[Any]:
  """Calls the API to collect the embeddings from a given volume.

  Args:
    mytoken: The token to access the DICOM volume and API.
    dicom_urls: The list of Series-level DICOM URL to the CT volumes.

  Returns:
    The list of embeddings or errors generated by the service. Differences in
    Vertex end-point configurations may change the return type. The caller is
    responsible for interpreting this value and extracting the requisite
    data.

  """
  api_client = aiplatform.gapic.PredictionServiceClient(
      client_options=ClientOptions(api_endpoint=_API_ENDPOINT)
  )
  endpoint = api_client.endpoint_path(
      project='hai-cd3-foundations', location='us-central1', endpoint=300
  )

  # Create a single instance to send - you can send up to 5.
  instances = []
  for dicom_url in dicom_urls:
    instances.append({"dicom_path": dicom_url, "bearer_token": mytoken})

  response = api_client.predict(
      endpoint=endpoint, instances=[instance], retry=retry_policy, timeout=600
  )
  assert len(response.predictions) == len(dicom_urls)
  assert len(response.predictions[0]) == 3
  # You can get the model version for this prediction
  # response.predictions[0]['model_version']

  # Check for no errors
  responses = []
  for a_prediction in response.predictions:
    if a_prediction['error_response'] is not None:
      responses.append(a_prediction['error_response'])
    else:
      embeddings = np.array(a_prediction['embedding_result']['embedding'],
                            dtype=np.float32)
      assert embeddings.shape == (1408,), 'Unexpected embeddings shape.'
      responses.append(embeddings)

  return embeddings

However, this resulted in the following error:

NameError: name 'List' is not defined

To resolve this, I added from typing import List. Upon running the code again, I then received: NameError: name 'Any' is not defined

I removed the return type hint -> List[Any] to temporarily bypass this, which resolved the error.

Then, while using get_ct_embeddings in the next cell, first, I tried the following lines:

TOKEN_ = !gcloud beta auth application-default print-access-token
TOKEN = TOKEN_[0]

my_url = create_lidc_series_url(study_uids[VOLUME_TO_SHOW],
                                corresponding_series_uids[VOLUME_TO_SHOW])
get_ct_embeddings(mytoken=TOKEN, dicom_url=my_url)

Here, I received this error message: TypeError: get_ct_embeddings() got an unexpected keyword argument 'dicom_url'

So I removed the dicom_url argument while calling the get_ct_embeddings method, and this step caused the following error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-9f9c4d2f19aa> in <cell line: 8>()
      6 my_url = create_lidc_series_url(study_uids[VOLUME_TO_SHOW],
      7                                 corresponding_series_uids[VOLUME_TO_SHOW])
----> 8 get_ct_embeddings(mytoken=TOKEN)

TypeError: get_ct_embeddings() missing 1 required positional argument: 'dicom_urls'

At this point, I realized that the argument name must be dicom_urls, not dicom_url. I then updated the call to:

get_ct_embeddings(mytoken=TOKEN, dicom_urls=my_url)

which resolved the problem. However, I then received this new error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-25-9002dd1d675d> in <cell line: 1>()
----> 1 get_ct_embeddings(mytoken=TOKEN, dicom_urls=my_url)

<ipython-input-18-187d7402fd08> in get_ct_embeddings(mytoken, dicom_urls)
     55 
     56   response = api_client.predict(
---> 57       endpoint=endpoint, instances=[instance], retry=retry_policy, timeout=600
     58   )
     59   assert len(response.predictions) == len(dicom_urls)

NameError: name 'instance' is not defined

This error occurred because in the previous cell response = api_client.predict(endpoint=endpoint, instances=[instance], retry=retry_policy, timeout=600) was referenced, but instance was not defined in the code.

Apologies for the length of this issue. I wanted to provide as much detail as possible to make it clear. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant