Skip to content

Commit 113a1bf

Browse files
Support zstd compressed files (#132)
* Support reading zstd compressed files Use Match.jl to switch on the filename suffix * Add simple test of file reading * Add stream test files * Remove debugging line * Remove dependency on Match This was a bit OTT for a 3-way choice, just use "if elself elseif else"
1 parent 82f1a80 commit 113a1bf

File tree

8 files changed

+44
-14
lines changed

8 files changed

+44
-14
lines changed

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.4.5"
66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
88
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
9+
CodecZstd = "6b39b394-51ab-5f42-8807-6242bab2b4c2"
910
EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
1011
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1112
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
@@ -25,7 +26,8 @@ JetVisualisation = "Makie"
2526
[compat]
2627
Accessors = "0.1.36"
2728
Aqua = "0.8"
28-
CodecZlib = "0.7.4"
29+
CodecZlib = "0.7"
30+
CodecZstd = "0.8"
2931
EDM4hep = "0.4.0"
3032
EnumX = "1.0.4"
3133
JSON = "0.21"
@@ -41,8 +43,8 @@ julia = "1.9"
4143

4244
[extras]
4345
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
44-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4546
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
47+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4648

4749
[targets]
4850
test = ["Aqua", "Test", "JSON"]

src/Utils.jl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
# Utility functions, which can be used by different top level scripts
22

33
using CodecZlib
4+
using CodecZstd
5+
6+
"""
7+
open_with_stream(fname::AbstractString)
8+
9+
Open a file with a stream decompressor if it is compressed with gzip or zstd,
10+
otherwise as a normal file.
11+
"""
12+
open_with_stream(fname::AbstractString) = begin
13+
if endswith(fname, ".gz")
14+
f = GzipDecompressorStream(open(fname))
15+
elseif endswith(fname, ".zst")
16+
f = ZstdDecompressorStream(open(fname))
17+
else
18+
f = open(fname)
19+
end
20+
f
21+
end
422

523
"""
624
read_final_state_particles(fname; maxevents = -1, skipevents = 0, T=PseudoJet)
@@ -22,12 +40,7 @@ a `LorentzVector` type. Note, if T is not `PseudoJet`, the order of the
2240
arguments in the constructor must be `(t, x, y, z)`.
2341
"""
2442
function read_final_state_particles(fname; maxevents = -1, skipevents = 0, T = PseudoJet)
25-
f = open(fname)
26-
if endswith(fname, ".gz")
27-
@debug "Reading gzipped file $fname"
28-
f = GzipDecompressorStream(f)
29-
end
30-
43+
f = open_with_stream(fname)
3144
events = Vector{T}[]
3245

3346
ipart = 1

test/_common.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ using Logging
1010
using LorentzVectorHEP
1111
using JSON
1212
using Test
13-
using CodecZlib
1413

1514
logger = ConsoleLogger(stdout, Logging.Warn)
1615
global_logger(logger)
@@ -66,11 +65,7 @@ end
6665

6766
"""Read JSON file with fastjet jets in it"""
6867
function read_fastjet_outputs(fname)
69-
f = open(fname)
70-
if endswith(fname, ".gz")
71-
@debug "Reading gzipped file $fname"
72-
f = GzipDecompressorStream(f)
73-
end
68+
f = JetReconstruction.open_with_stream(fname)
7469
JSON.parse(f)
7570
end
7671

test/data/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, JetReconstruction!

test/data/file.txt.gz

55 Bytes
Binary file not shown.

test/data/file.txt.zst

39 Bytes
Binary file not shown.

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ function main()
2929
#
3030
# Now run a few tests with our examples
3131
# include("tests_examples.jl")
32+
33+
# Utility support tests
34+
include("test-utils.jl")
35+
36+
# Substructure tests
3237
include("test-substructure.jl")
3338

3439
# Test with Aqua (https://juliatesting.github.io/Aqua.jl/stable/)

test/test-utils.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tests of utility functions
2+
3+
# Mainly just for "using"
4+
include("common.jl")
5+
6+
const string_target = "Hello, JetReconstruction!"
7+
8+
@testset "File streaming" begin
9+
for fname in ["file.txt", "file.txt.gz", "file.txt.zst"]
10+
fname = joinpath(@__DIR__, "data", fname)
11+
string = readline(JetReconstruction.open_with_stream(fname))
12+
@test string == string_target
13+
end
14+
end

0 commit comments

Comments
 (0)