From 205c0861dd60bdbc3c197bc7e9a1f01691f441e4 Mon Sep 17 00:00:00 2001
From: Tamas Gal <tgal@km3net.de>
Date: Wed, 19 Aug 2020 17:52:45 +0200
Subject: [PATCH] Use km3net_testdata

---
 examples/plot_offline_events.py |  3 ++-
 examples/plot_offline_hits.py   |  7 ++++---
 examples/plot_offline_tracks.py |  7 ++++---
 examples/plot_offline_usr.py    |  3 ++-
 examples/plot_online_example.py |  3 ++-
 requirements-dev.txt            |  1 +
 tests/test_gseagen.py           |  8 ++++----
 tests/test_offline.py           | 20 +++++++++-----------
 tests/test_online.py            |  5 +++--
 tests/test_tools.py             |  7 ++++---
 10 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/examples/plot_offline_events.py b/examples/plot_offline_events.py
index 4ae497d..35ac85d 100644
--- a/examples/plot_offline_events.py
+++ b/examples/plot_offline_events.py
@@ -9,12 +9,13 @@ Note: the offline file used here has MC offline data and was intentionaly reduce
 to 10 events.
 """
 import km3io as ki
+from km3net_testdata import data_path
 
 #####################################################
 # First, pass a filename to the `OfflineReader` class to open the file.
 # Note that only some meta information is read into memory.
 
-r = ki.OfflineReader("samples/numucc.root")
+r = ki.OfflineReader(data_path("offline/numucc.root"))
 
 
 #####################################################
diff --git a/examples/plot_offline_hits.py b/examples/plot_offline_hits.py
index a6f1c38..fab625d 100644
--- a/examples/plot_offline_hits.py
+++ b/examples/plot_offline_hits.py
@@ -9,12 +9,13 @@ Note: the offline file used here has MC offline data and was intentionaly reduce
 to 10 events.
 """
 import km3io as ki
+from km3net_testdata import data_path
 
 #####################################################
 # To access offline hits/mc_hits data:
 
-mc_hits = ki.OfflineReader("samples/numucc.root").events.mc_hits
-hits = ki.OfflineReader("samples/km3net_offline.root").events.hits
+mc_hits = ki.OfflineReader(data_path("offline/numucc.root")).events.mc_hits
+hits = ki.OfflineReader(data_path("offline/km3net_offline.root")).events.hits
 
 
 #####################################################
@@ -111,4 +112,4 @@ print(hits.channel_id[mask])
 #####################################################
 # or:
 
-print(hits.dom_id[mask])
\ No newline at end of file
+print(hits.dom_id[mask])
diff --git a/examples/plot_offline_tracks.py b/examples/plot_offline_tracks.py
index d5bf656..42cf746 100644
--- a/examples/plot_offline_tracks.py
+++ b/examples/plot_offline_tracks.py
@@ -8,12 +8,13 @@ written by aanet software.
 Note: the offline files used here were intentionaly reduced to 10 events.
 """
 import km3io as ki
+from km3net_testdata import data_path
 
 #####################################################
 # To access offline tracks/mc_tracks data:
 
-mc_tracks = ki.OfflineReader("samples/numucc.root").events.mc_tracks
-tracks = ki.OfflineReader("samples/km3net_offline.root").events.tracks
+mc_tracks = ki.OfflineReader(data_path("offline/numucc.root")).events.mc_tracks
+tracks = ki.OfflineReader(data_path("offline/km3net_offline.root")).events.tracks
 
 
 #####################################################
@@ -113,4 +114,4 @@ print(tracks.lik[mask])
 #####################################################
 # or:
 
-print(tracks.dir_z[mask])
\ No newline at end of file
+print(tracks.dir_z[mask])
diff --git a/examples/plot_offline_usr.py b/examples/plot_offline_usr.py
index b74baa9..c335f49 100644
--- a/examples/plot_offline_usr.py
+++ b/examples/plot_offline_usr.py
@@ -8,12 +8,13 @@ interface. The available keys can be accessed either as attributes or via a
 dictionary lookup:
 """
 import km3io as ki
+from km3net_testdata import data_path
 
 #####################################################
 # First, pass a filename to the `OfflineReader` class to open the file.
 # Note that only some meta information is read into memory.
 
-r = ki.OfflineReader("samples/usr-sample.root")
+r = ki.OfflineReader(data_path("offline/usr-sample.root"))
 
 
 #####################################################
diff --git a/examples/plot_online_example.py b/examples/plot_online_example.py
index 2a1db9b..72a38dd 100644
--- a/examples/plot_online_example.py
+++ b/examples/plot_online_example.py
@@ -9,6 +9,7 @@ Such a file is usually called "KM3NET_00000001_00000002.root", where the first
 number is the detector ID and the second the run number.
 """
 import km3io as ki
+from km3net_testdata import data_path
 
 #####################################################
 # Accessing the event tree
@@ -17,7 +18,7 @@ import km3io as ki
 # with:
 
 
-f = ki.OnlineReader("samples/km3net_online.root")
+f = ki.OnlineReader(data_path("online/km3net_online.root"))
 
 #####################################################
 # Note that only some meta information is read into memory.
diff --git a/requirements-dev.txt b/requirements-dev.txt
index 1194f72..813d15a 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -1,3 +1,4 @@
+km3net-testdata>=0.2.6
 matplotlib
 memory_profiler
 numpydoc==0.9.2
diff --git a/tests/test_gseagen.py b/tests/test_gseagen.py
index 9063c5f..842d006 100644
--- a/tests/test_gseagen.py
+++ b/tests/test_gseagen.py
@@ -2,11 +2,11 @@ import os
 import re
 import unittest
 
+from km3net_testdata import data_path
+
 from km3io.gseagen import GSGReader
 
-SAMPLES_DIR = os.path.join(os.path.dirname(__file__), "samples")
-GSG_FILE = os.path.join(SAMPLES_DIR, "gseagen.root")
-GSG_READER = GSGReader(GSG_FILE)
+GSG_READER = GSGReader(data_path("gseagen/gseagen.root"))
 
 
 class TestGSGHeader(unittest.TestCase):
@@ -56,7 +56,7 @@ class TestGSGHeader(unittest.TestCase):
         self.assertListEqual(self.header["NuList"].tolist(), [-14, 14])
 
     def test_unsupported_header(self):
-        f = GSGReader(os.path.join(SAMPLES_DIR, "km3net_online.root"))
+        f = GSGReader(data_path("online/km3net_online.root"))
         with self.assertWarns(UserWarning):
             f.header
 
diff --git a/tests/test_offline.py b/tests/test_offline.py
index 4fb616b..f58e45a 100644
--- a/tests/test_offline.py
+++ b/tests/test_offline.py
@@ -2,17 +2,15 @@ import unittest
 import numpy as np
 from pathlib import Path
 
+from km3net_testdata import data_path
+
 from km3io import OfflineReader
 from km3io.offline import _nested_mapper, Header
 
-SAMPLES_DIR = Path(__file__).parent / 'samples'
-OFFLINE_FILE = OfflineReader(SAMPLES_DIR / 'km3net_offline.root')
-OFFLINE_USR = OfflineReader(SAMPLES_DIR / 'usr-sample.root')
-OFFLINE_MC_TRACK_USR = OfflineReader(
-    SAMPLES_DIR /
-    'mcv5.11r2.gsg_muonCChigherE-CC_50-5000GeV.km3_AAv1.jterbr00004695.jchain.aanet.498.root'
-)
-OFFLINE_NUMUCC = OfflineReader(SAMPLES_DIR / "numucc.root")  # with mc data
+OFFLINE_FILE = OfflineReader(data_path("offline/km3net_offline.root"))
+OFFLINE_USR = OfflineReader(data_path("offline/usr-sample.root"))
+OFFLINE_MC_TRACK_USR = OfflineReader(data_path('offline/mcv5.11r2.gsg_muonCChigherE-CC_50-5000GeV.km3_AAv1.jterbr00004695.jchain.aanet.498.root'))
+OFFLINE_NUMUCC = OfflineReader(data_path("offline/numucc.root"))  # with mc data
 
 
 class TestOfflineReader(unittest.TestCase):
@@ -22,9 +20,9 @@ class TestOfflineReader(unittest.TestCase):
         self.n_events = 10
 
     def test_context_manager(self):
-        filename = SAMPLES_DIR / 'km3net_offline.root'
-        with OfflineReader(filename) as r:
-            assert r._filename == filename
+        filename = OFFLINE_FILE
+        with OfflineReader(data_path("offline/km3net_offline.root")) as r:
+            assert r
 
     def test_number_events(self):
         assert self.n_events == len(self.r.events)
diff --git a/tests/test_online.py b/tests/test_online.py
index 45897b7..5cef172 100644
--- a/tests/test_online.py
+++ b/tests/test_online.py
@@ -2,10 +2,11 @@ import os
 import re
 import unittest
 
+from km3net_testdata import data_path
+
 from km3io.online import OnlineReader, get_rate, has_udp_trailer, get_udp_max_sequence_number, get_channel_flags, get_number_udp_packets
 
-SAMPLES_DIR = os.path.join(os.path.dirname(__file__), "samples")
-ONLINE_FILE = os.path.join(SAMPLES_DIR, "km3net_online.root")
+ONLINE_FILE = data_path("online/km3net_online.root")
 
 
 class TestOnlineReaderContextManager(unittest.TestCase):
diff --git a/tests/test_tools.py b/tests/test_tools.py
index b14f0ab..ab6bbdb 100644
--- a/tests/test_tools.py
+++ b/tests/test_tools.py
@@ -3,16 +3,17 @@
 import unittest
 import awkward1 as ak
 import numpy as np
-
 from pathlib import Path
+
+from km3net_testdata import data_path
+
 from km3io import OfflineReader
 from km3io.tools import (to_num, cached_property, unfold_indices, unique,
                          uniquecount, fitinf, fitparams, count_nested, _find,
                          mask, best_track, rec_types, get_w2list_param,
                          get_multiplicity)
 
-SAMPLES_DIR = Path(__file__).parent / 'samples'
-OFFLINE_FILE = OfflineReader(SAMPLES_DIR / 'km3net_offline.root')
+OFFLINE_FILE = OfflineReader(data_path("offline/km3net_offline.root"))
 
 # class TestGetw2listParam(unittest.TestCase):
 #     def test_get_w2list_param(self):
-- 
GitLab