Get number of Processing Units from StatisticalAPI with python

Hi, when sending requests via Postman we get in the response header X-ProcessingUnits-Spent.

Is it possible to get the same information when using StatisticalAPI with python?

Here is a snippet of my code:

sd = ‘2022-03-01’ # start date
ed = ‘2022-05-22’ # end date
sn2_ndvi_request = SentinelHubStatistical(
aggregation=SentinelHubStatistical.aggregation(
evalscript=sn2_ndvi_evalscript,
time_interval=(sd, ed),
aggregation_interval=“P1D”,
resolution=(0.0001, 0.0001)
),
input_data=[SentinelHubStatistical.input_data(DataCollection.SENTINEL2_L2A)],
geometry=poly,
config=config,
)

Can I get X-ProcessingUnits-Spent from that?

Hi @org_accounts,

Unfortunately in the current version of sentinelhub-py package it is not possible to obtain response headers. But we are planning to add some support for this in the next version, which we plan to release in the next couple of weeks.

Great, can’t wait :slight_smile:

Can I get the PU from the user dashboard in a detailed way? meaning, that if I execute 5 different StatisticalAPI one after the other and then I would like to know how much PU each costs.

It seems that the dashboard has some kind of delay, it does not update automatically, how long does it suppose to take until I see the API call in the dashboard.
Also, does each API call has a unique ID that I can later identify (via Python)?

The Dashboard doesn’t offer a per-request view into PU usage, only per OAuth client and per type of service. Therefore, the expected way of testing would be that a user creates a new OAuth client, runs 1 or a few requests and checks the statistics for that client.

There might be some delay in the statistics shown by Dashboards, however I don’t know exactly how much. Refreshing the stats is also required.

Good news, in the newly released sentinelhub-py version 3.7.0 it is now possible to obtain response headers and and this way obtain info how many processing units a request costed. Here is how you can do it in your example:

...
sn2_ndvi_request = SentinelHubStatistical(
    ...
)

response = sn2_ndvi_request.get_data(decode_data=False)
print(response)  # It is an instance of DownloadResponse class

print("Processing units spent:", response.headers["X-ProcessingUnits-Spent"])

data = response.decode()
print(data)
4 Likes

Hello Matej,

print(“Processing units spent:”, response.headers[“X-ProcessingUnits-Spent”])

is providing the error:

AttributeError: ‘list’ object has no attribute ‘headers’

You certain the attribute is headers or something else?

Thank you!

Hi, just a few clarifications/corrections.

  1. Make sure you use decode_data=False, otherwise the headers are discarded. You’ll need to call the .decode() method on the response later to get the data out of it.
  2. The get_data method actually returns a list of DownloadResponse objects, so the original suggestion was slightly wrong. It’s very likely a list with a single member, so you could do response[0].headers to get the headers.
  3. I checked one such response and it seems like the key should be all lowercase (i.e. 'x-processingunits-spent') but i suggest you first print all of the headers and the fine-tune to the field you need.

Python lists cannot be divided into separate lists based on characters that appear in the values of a list. This is unlike strings which values can be separated into a list. The AttributeError is an exception thrown when an object does not have the attribute you tried to access. The ‘list’ object has no attribute ‘split’ and you’re trying to call python split() function on the whole list of lines, and you can’t split a list of strings, only a string. So, you need to split each line, not the whole thing.

To solve the above problem, you need to iterate over the strings in the list to get individual strings; then, you can call the split() function.