Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWIG: expose more LOR and listmode classes #1288

Merged
merged 6 commits into from
Nov 16, 2023
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
43 changes: 43 additions & 0 deletions examples/python/listmode_loop_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Demo how to use STIR from Python to access events in a list-mode file

# Copyright 2023 - University College London
# This file is part of STIR.
#
# SPDX-License-Identifier: Apache-2.0
#
# See STIR/LICENSE.txt for details

#%% Initial imports
import stir

#%% read file
filename = '20170809_NEMA_60min_UCL.l.hdr'
# example using the hroot file in recon_test_pack, which needs some env variables to be set
#import os
#os.environ["INPUT_ROOT_FILE"]="test_PET_GATE.root"
#os.environ["EXCLUDE_SCATTERED"] = "1"
#os.environ["EXCLUDE_RANDOM"] = "0"
#filename = "root_header.hroot"

lm=stir.ListModeData.read_from_file(filename)
# could print some exam info
# print(lm.get_exam_info().parameter_info())
proj_data_info = lm.get_proj_data_info()
# could print some geometric info
#print(proj_data_info.parameter_info())

#%% loop over first few events and print some information
# create some variables for re-use in the loop
record = lm.get_empty_record()
b = stir.Bin()
for i in range(50):
lm.get_next_record(record)
if (record.is_time()):
print(f"Time: {record.time().get_time_in_millisecs()}")
if (record.is_event()):
event = record.event()
lor = event.get_LOR()
event.get_bin(b, proj_data_info);
# TODO We can will be able to simply print bin once STIR_TOF is on
print(f"Event: p/d: {event.is_prompt()} LOR: {[lor.p1(), lor.p2()]}, ",
f"bin: s:{b.segment_num}, a: {b.axial_pos_num}, v: {b.view_num}, t:{b.tangential_pos_num}")
57 changes: 27 additions & 30 deletions src/include/stir/LORCoordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ class LORCylindricalCoordinates_z_and_radius
template <class coordT>
class LORInCylinderCoordinates : public LOR<coordT>
{
#ifdef SWIG
// SWIG needs this typedef to be public
public:
#endif
typedef LORInCylinderCoordinates<coordT> self_type;
private:
void check_state() const
{
assert(_radius>0);
Expand Down Expand Up @@ -217,16 +222,14 @@ class LORInCylinderCoordinates : public LOR<coordT>
inline
LORInCylinderCoordinates(const LORInAxialAndSinogramCoordinates<coordT>&);

#if 0
// disabled as would need radius argument as well, but currently not needed
inline
LORInCylinderCoordinates(const LORAs2Points<coordT>&);
#endif

virtual
#ifndef STIR_NO_COVARIANT_RETURN_TYPES
self_type*
#else
LOR<coordT>*
#endif
clone() const { return new self_type(*this); }
self_type* clone() const override
{ return new self_type(*this); }

virtual
Succeeded
Expand Down Expand Up @@ -259,6 +262,10 @@ class LORInCylinderCoordinates : public LOR<coordT>
template <class coordT>
class LORAs2Points : public LOR<coordT>
{
#ifdef SWIG
// SWIG needs this typedef to be public
public:
#endif
typedef LORAs2Points<coordT> self_type;
public:
const CartesianCoordinate3D<coordT>& p1() const { return _p1; }
Expand All @@ -282,13 +289,8 @@ class LORAs2Points : public LOR<coordT>
inline
LORAs2Points(const LORInAxialAndNoArcCorrSinogramCoordinates<coordT>&);

virtual
#ifndef STIR_NO_COVARIANT_RETURN_TYPES
self_type*
#else
LOR<coordT>*
#endif
clone() const { return new self_type(*this); }
self_type* clone() const override
{ return new self_type(*this); }

virtual
Succeeded
Expand Down Expand Up @@ -326,7 +328,12 @@ class LORInAxialAndSinogramCoordinates
: public LOR<coordT>, private LORCylindricalCoordinates_z_and_radius<coordT>
{
private:
#ifdef SWIG
// SWIG needs this typedef to be public
public:
#endif
typedef LORInAxialAndSinogramCoordinates<coordT> self_type;
private:
typedef LORCylindricalCoordinates_z_and_radius<coordT> private_base_type;
// sorry: has to be first to give the compiler a better chance of inlining
void check_state() const
Expand Down Expand Up @@ -371,18 +378,13 @@ class LORInAxialAndSinogramCoordinates
inline
LORInAxialAndSinogramCoordinates(const LORInAxialAndNoArcCorrSinogramCoordinates<coordT>&);

#if __cplusplus>= 201103L
#if 0
inline
LORInAxialAndSinogramCoordinates(const LORAs2Points<coordT>&);
#endif

virtual
#ifndef STIR_NO_COVARIANT_RETURN_TYPES
self_type*
#else
LOR<coordT>*
#endif
clone() const { return new self_type(*this); }
self_type* clone() const override
{ return new self_type(*this); }

void reset(coordT radius=1)
{
Expand Down Expand Up @@ -506,18 +508,13 @@ class LORInAxialAndNoArcCorrSinogramCoordinates
inline
LORInAxialAndNoArcCorrSinogramCoordinates(const LORInAxialAndSinogramCoordinates<coordT>&);

#if __cplusplus>= 201103L
#if 0
inline
LORInAxialAndNoArcCorrSinogramCoordinates(const LORAs2Points<coordT>&);
#endif

virtual
#ifndef STIR_NO_COVARIANT_RETURN_TYPES
self_type*
#else
LOR<coordT>*
#endif
clone() const { return new self_type(*this); }
self_type* clone() const override
{ return new self_type(*this); }

virtual
Succeeded
Expand Down
2 changes: 1 addition & 1 deletion src/include/stir/LORCoordinates.inl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ LORInAxialAndNoArcCorrSinogramCoordinates(const LORInAxialAndSinogramCoordinates
check_state();
}

#if __cplusplus>= 201103L
#if 0
template <class coordT>
LORInAxialAndSinogramCoordinates<coordT>::
LORInAxialAndSinogramCoordinates(const LORAs2Points<coordT>& coords)
Expand Down
2 changes: 1 addition & 1 deletion src/include/stir/listmode/CListRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef __stir_listmode_CListRecord_H__
#define __stir_listmode_CListRecord_H__

#include "ListRecord.h"
#include "stir/listmode/ListRecord.h"

START_NAMESPACE_STIR
class Bin;
Expand Down
2 changes: 0 additions & 2 deletions src/include/stir/listmode/ListEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef __stir_listmode_ListEvent_H__
#define __stir_listmode_ListEvent_H__

#include "stir/round.h"
#include "stir/Succeeded.h"
#include "stir/Bin.h"
#include "stir/ProjDataInfo.h"
#include "stir/CartesianCoordinate3D.h"
Expand Down
5 changes: 2 additions & 3 deletions src/include/stir/listmode/ListRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
#ifndef __stir_listmode_ListRecord_H__
#define __stir_listmode_ListRecord_H__

#include "ListEvent.h"
#include "ListTime.h"
#include "stir/Succeeded.h"
#include "stir/listmode/ListEvent.h"
#include "stir/listmode/ListTime.h"

START_NAMESPACE_STIR

Expand Down
1 change: 1 addition & 0 deletions src/swig/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ set(swig_stir_dependencies
stir_objectivefunctions.i
stir_priors.i
stir_projdata.i
stir_listmode.i
stir_projectors.i
stir_reconstruction.i
stir_shapes.i
Expand Down
45 changes: 7 additions & 38 deletions src/swig/stir.i
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@
#include "stir/recon_buildblock/BinNormalisationFromProjData.h"
#include "stir/recon_buildblock/BinNormalisationFromAttenuationImage.h"
#include "stir/recon_buildblock/TrivialBinNormalisation.h"
#include "stir/listmode/LmToProjData.h"
#include "stir/listmode/ListRecord.h"
#include "stir/listmode/ListEvent.h"
#include "stir/listmode/CListRecord.h"
#include "stir/listmode/ListModeData.h"
#include "stir/listmode/CListModeData.h"
#include "stir/listmode/LmToProjData.h"

#include "stir/CartesianCoordinate2D.h"
#include "stir/CartesianCoordinate3D.h"
Expand Down Expand Up @@ -908,9 +912,6 @@ namespace std {
%shared_ptr(stir::ParsingObject);

%shared_ptr(stir::Verbosity);
%shared_ptr(stir::LORAs2Points<float>);
%shared_ptr(stir::LOR<float>);
%shared_ptr(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>);

// William S Fulton trick for passing templates (with commas) through macro arguments
// (already defined in swgmacros.swg)
Expand Down Expand Up @@ -951,30 +952,14 @@ ADD_REPR(stir::Succeeded, %arg($self->succeeded() ? "yes" : "no"));
%include "stir/ByteOrder.h"

%include "stir_coordinates.i"

#if 0
// TODO enable this in STIR version 6 (breaks backwards compatibility
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, z1);
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, z2);
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, beta);
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, phi);
#else
%ignore *::z1() const;
%ignore *::z2() const;
%ignore *::beta() const;
%ignore *::phi() const;
#endif
%ignore *::check_state;
%include "stir/LORCoordinates.h"

%template(FloatLOR) stir::LOR<float>;
%template(FloatLORInAxialAndNoArcCorrSinogramCoordinates) stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>;
%include "stir_LOR.i"

%include "stir_array.i"
%include "stir_exam.i"

%shared_ptr(stir::DataSymmetriesForViewSegmentNumbers);
%include "stir_projdata.i"
%include "stir_listmode.i"
%include "stir/DataSymmetriesForViewSegmentNumbers.h"

%include "stir_voxels.i"
Expand Down Expand Up @@ -1009,10 +994,6 @@ ADD_REPR(stir::Succeeded, %arg($self->succeeded() ? "yes" : "no"));
%include "stir/multiply_crystal_factors.h"
%include "stir/decay_correction_factor.h"

%rename (set_template_proj_data_info) *::set_template_proj_data_info_sptr;
%shared_ptr(stir::LmToProjData);
%include "stir/listmode/LmToProjData.h"

%shared_ptr(stir::ScatterSimulation);
%shared_ptr(stir::RegisteredParsingObject<stir::SingleScatterSimulation,
stir::ScatterSimulation, stir::ScatterSimulation>);
Expand All @@ -1031,18 +1012,6 @@ ADD_REPR(stir::Succeeded, %arg($self->succeeded() ? "yes" : "no"));
%shared_ptr(stir::CreateTailMaskFromACFs);
%include "stir/scatter/CreateTailMaskFromACFs.h"

%shared_ptr(stir::ListModeData);
%include "stir/listmode/ListModeData.h"

%extend stir::ListModeData {
static shared_ptr<stir::ListModeData> read_from_file(const std::string& filename)
{
using namespace stir;
shared_ptr<ListModeData> ret(read_from_file<ListModeData>(filename));
return ret;
}
}

%shared_ptr(stir::FanProjData);
%shared_ptr(stir::GeoData3D);
%ignore operator<<;
Expand Down
49 changes: 49 additions & 0 deletions src/swig/stir_LOR.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright (C) 2023 University College London
Copyright (C) 2022 National Physical Laboratory
This file is part of STIR.

SPDX-License-Identifier: Apache-2.0

See STIR/LICENSE.txt for details
*/
/*!
\file
\brief Interface file for SWIG: stir::LOR and derived classes

\author Kris Thielemans
\author Daniel Deidda
*/

%shared_ptr(stir::LOR<float>);
%shared_ptr(stir::LORAs2Points<float>);
%shared_ptr(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>);
%shared_ptr(stir::LORInAxialAndSinogramCoordinates<float>);
%shared_ptr(stir::PointOnCylinder<float>);
%shared_ptr(stir::LORInCylinderCoordinates<float>);

#if 0
// TODO enable this in STIR version 6 (breaks backwards compatibility
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, z1);
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, z2);
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, beta);
%attributeref(stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>, float, phi);
#else
%ignore *::z1() const;
%ignore *::z2() const;
%ignore *::beta() const;
%ignore *::phi() const;
#endif
%ignore *::check_state;

%attributeref(stir::PointOnCylinder<float>, float, z);
%attributeref(stir::PointOnCylinder<float>, float, psi);

%include "stir/LORCoordinates.h"

%template(LOR) stir::LOR<float>;
%template(LORInAxialAndNoArcCorrSinogramCoordinates) stir::LORInAxialAndNoArcCorrSinogramCoordinates<float>;
%template(LORAs2Points) stir::LORAs2Points<float>;
%template(LORInAxialAndSinogramCoordinates) stir::LORInAxialAndSinogramCoordinates<float>;
%template(PointOnCylinder) stir::PointOnCylinder<float>;
%template(LORInCylinderCoordinates) stir::LORInCylinderCoordinates<float>;
Loading