Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions documentation/release_6.6.htm
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ <h2> Summary for end users (also to be read by developers)</h2>
<h3>Bug fixes</h3>
<ul>
<li>

SAFIR list-mode parameter files: relative filenames are now relative to the directory of the parameter file,
as for Interfile headers. Previously, reading only worked when run from the directory of the parameter file.
<br>
<a </a>.
<a href=https://github.com/UCL/STIR/pull/1799>PR #1799</a>, fixing <a href=https://github.com/UCL/STIR/issues/1680>issue #1680</a>.
</li>

</ul>

<h3>New functionality</h3>
Expand Down Expand Up @@ -126,6 +126,11 @@ <h3>Test changes</h3>
<br>
<a href=https://github.com/UCL/STIR/pull/1687>PR #1687</a>.
</li>
<li>
<code>test_SAFIRCListmodeInputFileFormat</code> reads SAFIR list-mode data via a parameter file in another directory.
<br>
<a href=https://github.com/UCL/STIR/pull/1799>PR #1799</a>, fixing <a href=https://github.com/UCL/STIR/issues/1680>issue #1680</a>.
</li>
</ul>
</body>

Expand Down
21 changes: 21 additions & 0 deletions src/include/stir/IO/SAFIRCListmodeInputFileFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Copyright 2015 ETH Zurich, Institute of Particle Physics
Copyright 2020 Positrigo AG, Zurich
Copyright 2026 Tejaswi Naidu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +43,7 @@
#include "stir/info.h"
#include "stir/error.h"
#include "stir/utilities.h"
#include "stir/FilePath.h"
#include "stir/ParsingObject.h"

#include "stir/listmode/CListRecordSAFIR.h"
Expand Down Expand Up @@ -72,6 +74,8 @@ each event.
END CListModeDataSAFIR Parameters:=
\endcode

Relative filenames are relative to the directory of the parameter file, as for Interfile headers.

The first 32 bytes of the binary file are interpreted as file signature and matched against the strings "MUPET CListModeData\0",
"SAFIR CListModeData\0" and "NeuroLF CListModeData\0". If either is successfull, the class claims it can read the file format. The
rest of the file is read as records as specified as template parameter, e.g. CListRecordSAFIR.
Expand Down Expand Up @@ -152,6 +156,8 @@ class SAFIRCListmodeInputFileFormat : public InputFileFormat<ListModeData>, publ
mutable std::string crystal_map_filename;
mutable std::string template_proj_data_filename;
mutable double lor_randomization_sigma;
//! directory of the parameter file being parsed, prepended to relative filenames
mutable std::string par_file_directory;

bool actual_can_read(const FileSignature& signature, std::istream& input) const override
{
Expand Down Expand Up @@ -179,6 +185,7 @@ class SAFIRCListmodeInputFileFormat : public InputFileFormat<ListModeData>, publ

bool actual_do_parsing(const std::string& filename) const
{
par_file_directory = get_directory_name(filename);
// Ugly const_casts here, but I don't see an other nice way to use the parser
if (const_cast<SAFIRCListmodeInputFileFormat<EventDataType>*>(this)->parse(filename.c_str()))
{
Expand All @@ -191,6 +198,10 @@ class SAFIRCListmodeInputFileFormat : public InputFileFormat<ListModeData>, publ

bool post_processing() override
{
listmode_filename = prepend_par_file_directory(listmode_filename);
template_proj_data_filename = prepend_par_file_directory(template_proj_data_filename);
crystal_map_filename = prepend_par_file_directory(crystal_map_filename);

if (!file_exists(listmode_filename))
return true;
else if (!file_exists(template_proj_data_filename))
Expand All @@ -203,6 +214,16 @@ class SAFIRCListmodeInputFileFormat : public InputFileFormat<ListModeData>, publ
}

private:
//! prepend par_file_directory to \a filename, unless it is empty or an absolute path
std::string prepend_par_file_directory(const std::string& filename) const
{
if (filename.empty())
return filename;
FilePath file_path(filename, false);
file_path.prepend_directory_name(par_file_directory);
return file_path.get_as_string();
}

bool file_exists(const std::string& filename)
{
std::ifstream infile(filename.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(${dir_SIMPLE_TEST_EXE_SOURCES}
test_ScatterSimulation.cxx
test_ML_norm.cxx
test_proj_data_info_subsets_pet.cxx
test_SAFIRCListmodeInputFileFormat.cxx
)

set(${dir_SIMPLE_TEST_EXE_SOURCES_NO_REGISTRIES}
Expand Down
137 changes: 137 additions & 0 deletions src/test/test_SAFIRCListmodeInputFileFormat.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*!

\file
\ingroup test

\brief Test program for stir::SAFIRCListmodeInputFileFormat

Checks that relative filenames in the SAFIR parameter file are found relative
to the directory of that parameter file, as for Interfile headers.

\author Tejaswi Naidu

*/
/* Copyright (C) 2026, Tejaswi Naidu
This file is part of STIR.

SPDX-License-Identifier: Apache-2.0

See STIR/LICENSE.txt for details
*/

#include "stir/RunTests.h"
#include "stir/FilePath.h"
#include "stir/ExamInfo.h"
#include "stir/ProjDataInfo.h"
#include "stir/ProjDataInterfile.h"
#include "stir/Scanner.h"
#include "stir/listmode/ListModeData.h"
#include "stir/IO/read_from_file.h"
#include "stir/is_null_ptr.h"
#include <fstream>
#include <string>

START_NAMESPACE_STIR

/*!
\ingroup test
\brief Test class for reading SAFIR list-mode data via its parameter file
*/
class SAFIRCListmodeInputFileFormatTests : public RunTests
{
public:
void run_tests() override;

private:
//! Writes a template, a list-mode file and a parameter file into \a directory
void
write_files(const std::string& directory, const std::string& listmode_name_in_par, const std::string& template_name_in_par);
//! Returns true if read_from_file<ListModeData> succeeds for \a par_filename
bool can_read_listmode(const std::string& par_filename);
};

void
SAFIRCListmodeInputFileFormatTests::write_files(const std::string& directory,
const std::string& listmode_name_in_par,
const std::string& template_name_in_par)
{
shared_ptr<Scanner> scanner_sptr(new Scanner(Scanner::SAFIRDualRingPrototype));
scanner_sptr->set_up();
shared_ptr<ProjDataInfo> proj_data_info_sptr(
ProjDataInfo::construct_proj_data_info(scanner_sptr,
/*span*/ 1,
scanner_sptr->get_num_rings() - 1,
/*views*/ scanner_sptr->get_num_detectors_per_ring() / 2,
/*tang_pos*/ 64,
/*arc_corrected*/ false));
shared_ptr<ExamInfo> exam_info_sptr(new ExamInfo(ImagingModality::PT));
{
ProjDataInterfile template_proj_data(
exam_info_sptr, proj_data_info_sptr, directory + "template.hs", std::ios::out | std::ios::trunc);
}

{
// 32-byte header with the SAFIR signature, followed by one (empty) 8-byte event
std::ofstream listmode_file((directory + "listmode.safir").c_str(), std::ios::binary);
char header[32] = "SAFIR CListModeData";
listmode_file.write(header, sizeof(header));
const char event[8] = { 0 };
listmode_file.write(event, sizeof(event));
}

std::ofstream par_file((directory + "listmode.par").c_str());
par_file << "CListModeDataSAFIR Parameters:=\n"
<< " listmode data filename:= " << listmode_name_in_par << "\n"
<< " template projection data filename:= " << template_name_in_par << "\n"
<< "END CListModeDataSAFIR Parameters:=\n";
}

bool
SAFIRCListmodeInputFileFormatTests::can_read_listmode(const std::string& par_filename)
{
try
{
shared_ptr<ListModeData> lm_data_sptr(read_from_file<ListModeData>(par_filename));
return !is_null_ptr(lm_data_sptr) && lm_data_sptr->get_scanner().get_type() == Scanner::SAFIRDualRingPrototype;
}
catch (...)
{
return false;
}
}

void
SAFIRCListmodeInputFileFormatTests::run_tests()
{
std::cerr << "\n-------- Testing SAFIRCListmodeInputFileFormat --------\n";

FilePath working_directory(FilePath::get_current_working_directory());
const std::string directory_name = "test_SAFIR_listmode_dir";
FilePath directory = working_directory.append(directory_name);
std::string directory_with_separator = directory.get_as_string();
FilePath::append_separator(directory_with_separator);

{
std::cerr << "Relative filenames, parameter file in another directory\n";
write_files(directory_with_separator, "listmode.safir", "template.hs");
check(can_read_listmode(directory_name + "/listmode.par"),
"reading SAFIR list-mode data with filenames relative to the parameter file");
}
{
std::cerr << "Absolute filenames\n";
write_files(directory_with_separator, directory_with_separator + "listmode.safir", directory_with_separator + "template.hs");
check(can_read_listmode(directory_name + "/listmode.par"), "reading SAFIR list-mode data with absolute filenames");
}
}

END_NAMESPACE_STIR

USING_NAMESPACE_STIR

int
main()
{
SAFIRCListmodeInputFileFormatTests tests;
tests.run_tests();
return tests.main_return_value();
}
Loading