Repository: StingraySoftware/stingray
Pull request #787 (2023-12-28 13:14:51)

More informative GTI failure
Responding to an issue by G.L. Israel, I substituted a generic exception with a full traceback of what goes wrong when GTIs are not correctly read. 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/StingraySoftware/stingray/pull/787?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware) Report
Attention: `2 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`0046b03`)](https://app.codecov.io/gh/StingraySoftware/stingray/commit/0046b033cf7ee074cccc26666921b06a5b3f739f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware) 96.31% compared to head [(`7c824d2`)](https://app.codecov.io/gh/StingraySoftware/stingray/pull/787?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware) 49.10%.

| [Files](https://app.codecov.io/gh/StingraySoftware/stingray/pull/787?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware) | Patch % | Lines |
|---|---|---|
| [stingray/io.py](https://app.codecov.io/gh/StingraySoftware/stingray/pull/787?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware#diff-c3RpbmdyYXkvaW8ucHk=) | 33.33% | [2 Missing :warning: ](https://app.codecov.io/gh/StingraySoftware/stingray/pull/787?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##             main     #787       +/-   ##
===========================================
- Coverage   96.31%   49.10%   -47.22%     
===========================================
  Files          43       43               
  Lines        8495     8496        +1     
===========================================
- Hits         8182     4172     -4010     
- Misses        313     4324     +4011     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/StingraySoftware/stingray/pull/787?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=StingraySoftware).

pep8speaks: Hello @matteobachetti! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:

* In the file [`stingray/io.py`](https://github.com/StingraySoftware/stingray/blob/05a4d05c10a44637916b21a53a1bd13f36cfedfa/stingray/io.py):

> [Line 662:101](https://github.com/StingraySoftware/stingray/blob/05a4d05c10a44637916b21a53a1bd13f36cfedfa/stingray/io.py#L662): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (113 > 100 characters)





Patch:
diff --git a/docs/changes/787.trivial.rst b/docs/changes/787.trivial.rst
new file mode 100644
index 00000000..a94fd889
--- /dev/null
+++ b/docs/changes/787.trivial.rst
@@ -0,0 +1,1 @@
+More informative GTI messages
diff --git a/setup.cfg b/setup.cfg
index 256ed04c..cfd51c42 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -59,6 +59,7 @@ docs =
     docutils
     sphinx-astropy
     nbsphinx>=0.8.3,!=0.8.8
+    nbconvert<7.14
     pandoc
     ipython
     towncrier<22.12.0
diff --git a/stingray/io.py b/stingray/io.py
index 1cdd7d83..465394a7 100644
--- a/stingray/io.py
+++ b/stingray/io.py
@@ -2,7 +2,8 @@ import logging
 import math
 import copy
 import os
-import pickle
+import sys
+import traceback
 import warnings
 from collections.abc import Iterable
 
@@ -596,7 +597,8 @@ def load_events_and_gtis(
     if modekey is not None and modekey in probe_header:
         mode = probe_header[modekey].strip()
 
-    gtistring = get_key_from_mission_info(db, "gti", "GTI,STDGTI", instr, mode)
+    if gtistring is None:
+        gtistring = get_key_from_mission_info(db, "gti", "GTI,STDGTI", instr, mode)
     if hduname is None:
         hduname = get_key_from_mission_info(db, "events", "EVENTS", instr, mode)
 
@@ -655,10 +657,12 @@ def load_events_and_gtis(
                 accepted_gtistrings=accepted_gtistrings,
                 det_numbers=det_number,
             )
-        except Exception:  # pragma: no cover
+        except Exception as e:  # pragma: no cover
             warnings.warn(
-                "No extensions found with a valid name. "
-                "Please check the `accepted_gtistrings` values.",
+                (
+                    f"No valid GTI extensions found. \nError: {str(e)}\n"
+                    "GTIs will be set to the entire time series."
+                ),
                 AstropyUserWarning,
             )
             gti_list = np.array([[t_start, t_stop]], dtype=np.longdouble)


Test patch:
diff --git a/stingray/tests/test_io.py b/stingray/tests/test_io.py
index b67ae275..86ef6c6e 100644
--- a/stingray/tests/test_io.py
+++ b/stingray/tests/test_io.py
@@ -71,7 +71,7 @@ class TestIO(object):
     def test_event_file_read_and_automatic_sort(self):
         """Test event file reading."""
         fname = os.path.join(datadir, "monol_testA_calib.evt")
-        with pytest.warns(AstropyUserWarning, match="No extensions found with a"):
+        with pytest.warns(AstropyUserWarning, match="No valid GTI extensions"):
             evdata = load_events_and_gtis(fname)
         fname_unsrt = os.path.join(datadir, "monol_testA_calib_unsrt.evt")
         with pytest.warns(UserWarning, match="not sorted. Sorting them for you"):
@@ -93,7 +93,7 @@ class TestIO(object):
     def test_event_file_read_additional_energy_cal(self):
         """Test event file reading."""
         fname = os.path.join(datadir, "monol_testA_calib.evt")
-        with pytest.warns(AstropyUserWarning, match="No extensions found with a"):
+        with pytest.warns(AstropyUserWarning, match="No valid GTI extensions"):
             vals = load_events_and_gtis(fname, additional_columns=["energy"])
         # These energies were calibrated with a different calibration than
         # returned from rough_calibration, on purpose! (notice the +1.)