Skip to content

Commit

Permalink
added first tests for reading files
Browse files Browse the repository at this point in the history
  • Loading branch information
erikfrojdh committed Mar 27, 2024
1 parent f848c00 commit 6e8cf17
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 10 deletions.
10 changes: 10 additions & 0 deletions core/include/aare/NDView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ template <typename T, ssize_t Ndim=2> class NDView {
T &operator()(ssize_t i) { return buffer_[i]; }
T &operator[](ssize_t i) { return buffer_[i]; }

bool operator==(const NDView &other) const {
if (size_ != other.size_)
return false;
for (ssize_t i = 0; i != size_; ++i) {
if (buffer_[i] != other.buffer_[i])
return false;
}
return true;
}

NDView &operator+=(const T val) { return elemenwise(val, std::plus<T>()); }
NDView &operator-=(const T val) { return elemenwise(val, std::minus<T>()); }
NDView &operator*=(const T val) { return elemenwise(val, std::multiplies<T>()); }
Expand Down
16 changes: 16 additions & 0 deletions core/test/NDView.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,20 @@ TEST_CASE("Retrieve shape"){
REQUIRE(data.shape()[0] == 3);
REQUIRE(data.shape()[1] == 4);

}

TEST_CASE("compare two views"){
std::vector<int> vec1;
for (int i = 0; i != 12; ++i) {
vec1.push_back(i);
}
NDView<int,2> view1(vec1.data(), Shape<2>{3,4});

std::vector<int> vec2;
for (int i = 0; i != 12; ++i) {
vec2.push_back(i);
}
NDView<int,2> view2(vec2.data(), Shape<2>{3,4});

REQUIRE(view1 == view2);
}
Binary file added data/jungfrau/jungfrau_single_0.npy
Binary file not shown.
16 changes: 8 additions & 8 deletions data/jungfrau/read_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
# Read three frames from a jungfrau file with a single interface
rows = 512
cols = 1024
frames = 3
frames = 10

data = np.zeros((frames,rows,cols), dtype = np.uint16)
header = np.zeros(frames, dtype = header_dt)
for file_id in range(4):
file_name = 'jungfrau_single_d0_f{}_0.raw'.format(file_id)
print("Reading file:", file_name)
with open(file_name) as f:
for i in range(frames if file_id != 3 else 1):
header[i] = np.fromfile(f, dtype=header_dt, count = 1)
data[i] = np.fromfile(f, dtype=np.uint16,count = rows*cols).reshape(rows,cols)
for i in range(3 if file_id != 3 else 1):
header[i+file_id*3] = np.fromfile(f, dtype=header_dt, count = 1)
data[i+file_id*3] = np.fromfile(f, dtype=np.uint16,count = rows*cols).reshape(rows,cols)


for i in range(frames if file_id != 3 else 1 ):
print("frame:",i)
print(data[i][0,0],data[i][0,1],data[i][1,0],data[i][rows-1,cols-1])
print("")
# for i in range(frames if file_id != 3 else 1 ):
# print("frame:",i)
# print(data[i][0,0],data[i][0,1],data[i][1,0],data[i][rows-1,cols-1])
# print("")


#fig, ax = plt.subplots()
Expand Down
29 changes: 29 additions & 0 deletions data/scripts/read_first_frame_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
from pathlib import Path

header_dt = np.dtype(
[
("Frame Number", "u8"),
("SubFrame Number/ExpLength", "u4"),
("Packet Number", "u4"),
("Bunch ID", "u8"),
("Timestamp", "u8"),
("Module Id", "u2"),
("Row", "u2"),
("Column", "u2"),
("Reserved", "u2"),
("Debug", "u4"),
("Round Robin Number", "u2"),
("Detector Type", "u1"),
("Header Version", "u1"),
("Packets caught mask", "8u8")
]
)



with open("data/eiger/eiger_500k_16bit_d0_f0_0.raw", "rb") as f:
for i in range(3):
frame_number = np.fromfile(f, dtype=header_dt, count=1)["Frame Number"][0]
print(frame_number)
f.seek(262144,1)
File renamed without changes.
3 changes: 2 additions & 1 deletion file_io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ endif()

if(AARE_TESTS)
set(TestSources
${CMAKE_CURRENT_SOURCE_DIR}/src/NumpyHelpers.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/NumpyHelpers.test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/RawFile.test.cpp
)
target_sources(tests PRIVATE ${TestSources} )
target_link_libraries(tests PRIVATE core file_io)
Expand Down
File renamed without changes.
80 changes: 80 additions & 0 deletions file_io/test/RawFile.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <catch2/catch_test_macros.hpp>
#include <filesystem>
#include "aare/File.hpp"

#include "test_config.hpp"

TEST_CASE("Read number of frames from a jungfrau raw file"){
auto fpath = test_data_path() / "jungfrau" / "jungfrau_single_master_0.json";
REQUIRE(std::filesystem::exists(fpath));

File f(fpath, "r");
REQUIRE(f.total_frames() == 10);
}

TEST_CASE("Read frame numbers from a jungfrau raw file"){
auto fpath = test_data_path() / "jungfrau" / "jungfrau_single_master_0.json";
REQUIRE(std::filesystem::exists(fpath));

File f(fpath, "r");

//we know this file has 10 frames with frame numbers 1 to 10
//f0 1,2,3
//f1 4,5,6
//f2 7,8,9
//f3 10
for (size_t i = 0; i < 10; i++){
CHECK(f.frame_number(i) == i+1);
}
}

TEST_CASE("Read data from a jungfrau 500k single port raw file"){
auto fpath = test_data_path() / "jungfrau" / "jungfrau_single_master_0.json";
REQUIRE(std::filesystem::exists(fpath));

File f(fpath, "r");

//we know this file has 10 frames with pixel 0,0 being: 2123, 2051, 2109, 2117, 2089, 2095, 2072, 2126, 2097, 2102
std::vector<uint16_t> pixel_0_0 = {2123, 2051, 2109, 2117, 2089, 2095, 2072, 2126, 2097, 2102};
for (size_t i = 0; i < 10; i++){
auto frame = f.read();
CHECK(frame.rows() == 512);
CHECK(frame.cols() == 1024);
CHECK(frame.view<uint16_t>()(0,0) == pixel_0_0[i]);

}
}

TEST_CASE("Read frame numbers from a raw file"){
auto fpath = test_data_path() / "eiger" / "eiger_500k_16bit_master_0.json";
REQUIRE(std::filesystem::exists(fpath));

//we know this file has 3 frames with frame numbers 14, 15, 16
std::vector<size_t> frame_numbers = {14, 15, 16};

File f(fpath, "r");
for (size_t i = 0; i < 3; i++){
CHECK(f.frame_number(i) == frame_numbers[i]);
}
}

TEST_CASE("Compare reading from a numpy file with a raw file"){
auto fpath_raw = test_data_path() / "jungfrau" / "jungfrau_single_master_0.json";
REQUIRE(std::filesystem::exists(fpath_raw));

auto fpath_npy = test_data_path() / "jungfrau" / "jungfrau_single_0.npy";
REQUIRE(std::filesystem::exists(fpath_npy));

File raw(fpath_raw, "r");
File npy(fpath_npy, "r");

CHECK(raw.total_frames() == 10);
CHECK(npy.total_frames() == 10);


for (size_t i=0; i<10; ++i){
auto raw_frame = raw.read();
auto npy_frame = npy.read();
CHECK(raw_frame.view<uint16_t>() == npy_frame.view<uint16_t>());
}
}
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ target_sources(tests PRIVATE ${TestSources} )

#Work around to remove, this is not the way to do it =)
# target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}/include/common)
target_link_libraries(tests PRIVATE core aare_compiler_flags)
target_link_libraries(tests PRIVATE core aare_compiler_flags utils)

catch_discover_tests(tests
# WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/data
Expand Down

0 comments on commit 6e8cf17

Please sign in to comment.