Skip to content

Commit

Permalink
bounds checking on subfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
erikfrojdh committed Jan 10, 2025
1 parent 7ce0200 commit d0f435a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 63 deletions.
130 changes: 67 additions & 63 deletions python/examples/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,99 +14,103 @@

f = File(base/'Moench03new/cu_half_speed_master_4.json')

for i, frame in enumerate(f):
print(f'{i}', end='\r')
print()


from aare._aare import ClusterFinderMT, ClusterCollector, ClusterFileSink


cf = ClusterFinderMT((400,400), (3,3), n_threads = 3)
# collector = ClusterCollector(cf)
out_file = ClusterFileSink(cf, "test.clust")
# cf = ClusterFinderMT((400,400), (3,3), n_threads = 3)
# # collector = ClusterCollector(cf)
# out_file = ClusterFileSink(cf, "test.clust")

for i in range(1000):
img = f.read_frame()
cf.push_pedestal_frame(img)
print('Pedestal done')
cf.sync()
# for i in range(1000):
# img = f.read_frame()
# cf.push_pedestal_frame(img)
# print('Pedestal done')
# cf.sync()

for i in range(100):
img = f.read_frame()
cf.find_clusters(img)
# for i in range(100):
# img = f.read_frame()
# cf.find_clusters(img)


# # time.sleep(1)
# cf.stop()
# time.sleep(1)
cf.stop()
time.sleep(1)
print('Second run')
cf.start()
for i in range(100):
img = f.read_frame()
cf.find_clusters(img)
# print('Second run')
# cf.start()
# for i in range(100):
# img = f.read_frame()
# cf.find_clusters(img)

cf.stop()
print('Third run')
cf.start()
for i in range(129):
img = f.read_frame()
cf.find_clusters(img)
# cf.stop()
# print('Third run')
# cf.start()
# for i in range(129):
# img = f.read_frame()
# cf.find_clusters(img)

cf.stop()
out_file.stop()
print('Done')
# cf.stop()
# out_file.stop()
# print('Done')


cfile = ClusterFile("test.clust")
i = 0
while True:
try:
cv = cfile.read_frame()
i+=1
except RuntimeError:
break
print(f'Read {i} frames')
# cfile = ClusterFile("test.clust")
# i = 0
# while True:
# try:
# cv = cfile.read_frame()
# i+=1
# except RuntimeError:
# break
# print(f'Read {i} frames')




# cf = ClusterFinder((400,400), (3,3))
# for i in range(1000):
# cf.push_pedestal_frame(f.read_frame())
# # cf = ClusterFinder((400,400), (3,3))
# # for i in range(1000):
# # cf.push_pedestal_frame(f.read_frame())

# fig, ax = plt.subplots()
# im = ax.imshow(cf.pedestal())
# cf.pedestal()
# cf.noise()
# # fig, ax = plt.subplots()
# # im = ax.imshow(cf.pedestal())
# # cf.pedestal()
# # cf.noise()



# N = 500
# t0 = time.perf_counter()
# hist1 = bh.Histogram(bh.axis.Regular(40, -2, 4000))
# f.seek(0)
# # N = 500
# # t0 = time.perf_counter()
# # hist1 = bh.Histogram(bh.axis.Regular(40, -2, 4000))
# # f.seek(0)

# t0 = time.perf_counter()
# data = f.read_n(N)
# t_elapsed = time.perf_counter()-t0
# # t0 = time.perf_counter()
# # data = f.read_n(N)
# # t_elapsed = time.perf_counter()-t0


# n_bytes = data.itemsize*data.size
# # n_bytes = data.itemsize*data.size

# print(f'Reading {N} frames took {t_elapsed:.3f}s {N/t_elapsed:.0f} FPS, {n_bytes/1024**2:.4f} GB/s')
# # print(f'Reading {N} frames took {t_elapsed:.3f}s {N/t_elapsed:.0f} FPS, {n_bytes/1024**2:.4f} GB/s')


# for frame in data:
# a = cf.find_clusters(frame)
# # for frame in data:
# # a = cf.find_clusters(frame)

# clusters = cf.steal_clusters()
# # clusters = cf.steal_clusters()

# t_elapsed = time.perf_counter()-t0
# print(f'Clustering {N} frames took {t_elapsed:.2f}s {N/t_elapsed:.0f} FPS')
# # t_elapsed = time.perf_counter()-t0
# # print(f'Clustering {N} frames took {t_elapsed:.2f}s {N/t_elapsed:.0f} FPS')


# t0 = time.perf_counter()
# total_clusters = clusters.size
# # t0 = time.perf_counter()
# # total_clusters = clusters.size

# hist1.fill(clusters.sum())
# # hist1.fill(clusters.sum())

# t_elapsed = time.perf_counter()-t0
# print(f'Filling histogram with the sum of {total_clusters} clusters took: {t_elapsed:.3f}s, {total_clusters/t_elapsed:.3g} clust/s')
# print(f'Average number of clusters per frame {total_clusters/N:.3f}')
# # t_elapsed = time.perf_counter()-t0
# # print(f'Filling histogram with the sum of {total_clusters} clusters took: {t_elapsed:.3f}s, {total_clusters/t_elapsed:.3g} clust/s')
# # print(f'Average number of clusters per frame {total_clusters/N:.3f}')
12 changes: 12 additions & 0 deletions src/RawFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect
if (n_subfile_parts != 1) {
for (size_t part_idx = 0; part_idx != n_subfile_parts; ++part_idx) {
auto subfile_id = frame_index / m_master.max_frames_per_file();
if (subfile_id >= subfiles.size()) {
throw std::runtime_error(LOCATION +
" Subfile out of range. Possible missing data.");
}
frame_numbers[part_idx] =
subfiles[subfile_id][part_idx]->frame_number(
frame_index % m_master.max_frames_per_file());
Expand Down Expand Up @@ -311,6 +315,10 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect
for (size_t part_idx = 0; part_idx != n_subfile_parts; ++part_idx) {
auto corrected_idx = frame_indices[part_idx];
auto subfile_id = corrected_idx / m_master.max_frames_per_file();
if (subfile_id >= subfiles.size()) {
throw std::runtime_error(LOCATION +
" Subfile out of range. Possible missing data.");
}

// This is where we start writing
auto offset = (m_module_pixel_0[part_idx].y * m_cols +
Expand Down Expand Up @@ -343,6 +351,10 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect
auto pos = m_module_pixel_0[part_idx];
auto corrected_idx = frame_indices[part_idx];
auto subfile_id = corrected_idx / m_master.max_frames_per_file();
if (subfile_id >= subfiles.size()) {
throw std::runtime_error(LOCATION +
" Subfile out of range. Possible missing data.");
}

subfiles[subfile_id][part_idx]->seek(corrected_idx % m_master.max_frames_per_file());
subfiles[subfile_id][part_idx]->read_into(part_buffer, header);
Expand Down

0 comments on commit d0f435a

Please sign in to comment.