hyperspy / exspy

EDS and EELS data analysis with HyperSpy
https://hyperspy.org/exspy
GNU General Public License v3.0
8 stars 9 forks source link

eelsdb queries #64

Open PauTorru opened 4 days ago

PauTorru commented 4 days ago

The following code:


from exspy.misc.eels import eelsdb
results = eelsdb.eelsdb(spectrum_type="coreloss",element="Cu",edge="L2,3")

Produces this error

JSONDecodeError                           Traceback (most recent call last)
File ~\AppData\Local\anaconda3\Lib\site-packages\requests\models.py:971, in Response.json(self, **kwargs)
    970 try:
--> 971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File ~\AppData\Local\anaconda3\Lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File ~\AppData\Local\anaconda3\Lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File ~\AppData\Local\anaconda3\Lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

JSONDecodeError                           Traceback (most recent call last)
Cell In[2], line 1
----> 1 results = eelsdb.eelsdb(spectrum_type="coreloss",element="Cu",edge="L2,3")

File ~\AppData\Local\anaconda3\Lib\site-packages\exspy\misc\eels\eelsdb.py:255, in eelsdb(spectrum_type, title, author, element, formula, edge, min_energy, max_energy, resolution, min_energy_compare, max_energy_compare, resolution_compare, max_n, monochromated, order, order_direction, verify_certificate, show_progressbar)
    251 request = requests.get(
    252     "https://api.eelsdb.eu/spectra", params=params, verify=verify_certificate
    253 )
    254 spectra = []
--> 255 jsons = request.json()
    256 if "message" in jsons:
    257     # Invalid query, EELSdb raises error.
    258     raise IOError(
    259         "Please report the following error to the eXSpy developers: "
    260         f"{jsons['message']}."
    261     )

File ~\AppData\Local\anaconda3\Lib\site-packages\requests\models.py:975, in Response.json(self, **kwargs)
    971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

upon further investigation, the eels db rejects the conection. I manually fixed the issue by:

headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"}
request = requests.get(
    "https://api.eelsdb.eu/spectra", params={"type":"coreloss","element":"Cu","edge":"L2,3"}, verify=True,headers=headers
)
ericpre commented 4 days ago

I have seen it and did notice that the json was empty but thought that it was transient as it happens once in a while.

I don't know why this would be necessary now but specifying the "user-agent" shouldn't hurt - for example, we do this to check for broken extremal links when building the hyperspy documentation to publisher and setting the user-agent helps to get some of them to work.

Do you want to make a pull request? The relevant code should be https://github.com/hyperspy/exspy/blob/e63bd212491494f4a1ee6659772295bc09fa7ac1/exspy/misc/eels/eelsdb.py#L251-L255

@llajaunie, any idea why it would be necessary now?

PauTorru commented 3 days ago

I just made the pull request.