Skip to content

Mods to support spectra file IO out of fortran #34

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

Draft
wants to merge 109 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
ba53a82
Add install stuff and support for Finding Specutils in CMake
hbivens Jul 25, 2024
ab9e951
Merge branch 'master' into issue/31-spectra-io-out-of-fortran
wcjohns Jul 25, 2024
cc1b35f
remove install interface thing that doesn't seem to matter.
hbivens Jul 26, 2024
0334e4d
fortran integration mods
hbivens Aug 5, 2024
aed541f
add extension that accesses a count at a given channel
hbivens Aug 6, 2024
aca0c47
make the *_at extensions 1-based, since calling from fortran
hbivens Aug 6, 2024
4b8bdc9
enable testing at the topmost cmakefile
hbivens Aug 8, 2024
aefc329
cherry pick boost test removal
hbivens Aug 8, 2024
fd24efd
syntax highlighting getting confused
hbivens Aug 8, 2024
17cc031
Make API a little easier
hbivens Aug 10, 2024
48421d9
add round trip test
hbivens Aug 12, 2024
0c59d47
use seconds precision
hbivens Aug 12, 2024
8af3a3b
add deviation pairs to test
hbivens Aug 12, 2024
358b706
check neutrons
hbivens Aug 12, 2024
c89674c
use vax string
hbivens Aug 12, 2024
4aef394
Yeah!!! Figured out fortran array to c++ typemap.
hbivens Aug 14, 2024
fb679da
add a few more extensions and include filesystem stuff
hbivens Aug 15, 2024
45bed0e
call correct method
hbivens Aug 28, 2024
c498e6d
trim the filename and add some error diagnostics
hbivens Aug 28, 2024
e6032bb
add realtime check
hbivens Sep 9, 2024
738cd14
merge master
hbivens Sep 9, 2024
6d8371f
Fix issue compiling with fast_float
wcjohns Sep 9, 2024
80d3ce5
fix windows build error
hbivens Sep 12, 2024
7f4a595
update wrapper
hbivens Sep 13, 2024
2b03814
start some tests to map dev pairs
hbivens Sep 13, 2024
ae3c9a3
make strtod default
hbivens Sep 16, 2024
31cb465
Merge remote-tracking branch 'origin/master' into issue/31-spectra-io…
hbivens Sep 16, 2024
af3631d
checkpoint
hbivens Sep 17, 2024
f39635e
pcf spectra file mods
hbivens Sep 18, 2024
0e27b16
updates - add fortran round trip test
hbivens Sep 18, 2024
62e653c
checkpoint
hbivens Sep 20, 2024
46dbbc5
lose the pcf namespace
hbivens Sep 23, 2024
936b7af
figured out deviation pairs
hbivens Sep 26, 2024
48a3268
update fortran wrapper
hbivens Sep 26, 2024
8d547a3
more dev pair mapping
hbivens Sep 27, 2024
9b195e4
cherrypick Will's tag support
hbivens Sep 27, 2024
a96f242
merge master and fix conflicts
hbivens Sep 30, 2024
e0391ad
revert back to default
hbivens Sep 30, 2024
854b836
update test to show Will
hbivens Oct 2, 2024
431c65c
Fix issues with writing and reading deviation pairs to PCF files.
wcjohns Oct 9, 2024
2f35d06
Make compiling the bindings optional (defaulting to off).
wcjohns Oct 9, 2024
e29f078
Promote measurement source and descriptions to be member variables of…
wcjohns Oct 10, 2024
20c6e55
Remove PcfExtensions.
wcjohns Oct 10, 2024
1b77e69
Fix compile issue.
wcjohns Oct 10, 2024
882b503
Fix some function not always returning values in c-interface.
wcjohns Oct 10, 2024
a1e4396
Fix `SpecUtils_Measurement_set_start_time_str` not returning a value.
wcjohns Oct 10, 2024
68babc7
update fortran bindings
hbivens Oct 10, 2024
6a07a20
add ctest args
hbivens Oct 10, 2024
d6e44d7
rename this, I was getting compiler warning:
hbivens Oct 10, 2024
74d4418
add debug print for CICD
hbivens Oct 10, 2024
13bb819
mas debug
hbivens Oct 10, 2024
0caf7ca
add index, size to exception message
hbivens Oct 10, 2024
3981033
add size
hbivens Oct 10, 2024
cdc7ecd
add build script to aid in cicd debug
hbivens Oct 10, 2024
0f58916
up minimum to avoid cmake warning
hbivens Oct 10, 2024
a80f9af
uri spectra causes fortran test to fail
hbivens Oct 10, 2024
074926e
wrong line
hbivens Oct 10, 2024
1047d13
First try at adding an automated release of shared libraries for windows
wcjohns Oct 11, 2024
9c6163e
Merge branch 'issue/31-spectra-io-out-of-fortran' of github.com:sandi…
wcjohns Oct 11, 2024
9451d07
Add package command to CMake, and bring out option for Windows runtim…
wcjohns Oct 11, 2024
fee1904
Merge branch 'issue/31-spectra-io-out-of-fortran' of github.com:sandi…
wcjohns Oct 11, 2024
02c71ab
Merge branch 'master' into issue/31-spectra-io-out-of-fortran
wcjohns Oct 11, 2024
a22e940
Fix a few things up that I think got messed up during various merges.
wcjohns Oct 11, 2024
61f3224
Maybe fix compile error on Windows
wcjohns Oct 11, 2024
65621cb
Add another missing include
wcjohns Oct 11, 2024
3018b0d
Add another missing include
wcjohns Oct 11, 2024
2ce11ec
Ad UTF-8 BOM to test_utf8_limit_str_size.cpp
wcjohns Oct 11, 2024
dccb2e4
Update ci.yml to upload Windows artifact, maybe
wcjohns Oct 11, 2024
ee04054
Update ci.yml to upload Windows artifact, now a little closer maybe
wcjohns Oct 11, 2024
5e97ccc
Update ci.yml to upload Windows artifact, try again
wcjohns Oct 11, 2024
5408630
Update ci.yml to upload Windows artifact, try again
wcjohns Oct 12, 2024
79d81cb
Update ci.yml to upload Windows artifact, try again
wcjohns Oct 12, 2024
ba26b47
Update ci.yml to upload Windows artifact, try again
wcjohns Oct 12, 2024
03d8889
Minor change
wcjohns Oct 12, 2024
960c637
Fix issue when converting to a VAX string.
wcjohns Oct 14, 2024
6f55a3b
Debug windows automated build of shared libraries a bit
wcjohns Oct 14, 2024
4e6c62b
Maybe fix syntax error in ci.yml
wcjohns Oct 14, 2024
6310f65
Maybe fix syntax error in ci.yml
wcjohns Oct 14, 2024
caab293
Maybe fix syntax error in ci.yml
wcjohns Oct 14, 2024
06fcc6e
Try to improve CI install targets
wcjohns Oct 14, 2024
56d8b78
Debugging chany ci.yml
wcjohns Oct 14, 2024
679bf4e
Remove trying to install export file
wcjohns Oct 14, 2024
53b5039
Turn building C bindings on for CI build
wcjohns Oct 14, 2024
160b5bd
Add exporting classes/structs/functions for Windows DLLs.
wcjohns Oct 14, 2024
16643d3
Trye having CMake generate export header.
wcjohns Oct 14, 2024
1359228
Fix missed export
wcjohns Oct 14, 2024
e3ffee4
Temporarily re-include `pcf_det_name_to_dev_pair_index` so fortran bi…
wcjohns Oct 14, 2024
9adcc44
Fix infinite recursion
wcjohns Oct 14, 2024
902074e
add script
hbivens Oct 15, 2024
bb9fb76
Merge remote-tracking branch 'origin/issue/31-spectra-io-out-of-fortr…
hbivens Oct 15, 2024
f1c5e48
add install
hbivens Oct 17, 2024
bf77e65
Turn off c-bindings
hbivens Oct 17, 2024
a93c8c8
add -fPic
hbivens Oct 17, 2024
5b47ea2
Revert "Turn building C bindings on for CI build"
hbivens Oct 18, 2024
301ee76
Revert "Add exporting classes/structs/functions for Windows DLLs."
hbivens Oct 18, 2024
ae11b15
update swig-gen
hbivens Oct 18, 2024
7de1a45
Revert "Fix missed export"
hbivens Oct 18, 2024
2cceea1
make it work
hbivens Oct 18, 2024
874d8a5
keep the swig generated files in repo
hbivens Oct 18, 2024
cbfc685
update interface
hbivens Oct 21, 2024
1cd879c
update test
hbivens Oct 21, 2024
0e9ebde
Add method to update detector name
hbivens Oct 22, 2024
9ca9f3c
cmake mods to enable valgrind memcheck with ctest
hbivens Oct 23, 2024
ef7c3dc
update version and add to cpack file
hbivens Jan 28, 2025
01c8443
Merge remote-tracking branch 'origin/master' into issue/31-spectra-io…
hbivens Jan 29, 2025
d87b2db
Update SpecUtils_config.h.in
hbivens Jan 29, 2025
ecf3d0e
maybe fix macos wheel build
hbivens Jan 29, 2025
4e70828
Make so install stuff is only called if building as a top-level project.
wcjohns Jan 30, 2025
4f0f402
Fix last commit not generating SpecUtils_config.h
wcjohns Jan 30, 2025
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
59 changes: 54 additions & 5 deletions CMakeLists.txt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If SpecUtils_FLT_PARSE_METHOD is left as 'default_value' here, there is some logic below to hopefully pick the most reasonable float parsing method. Like for MSVC ≥2019, it should pick 'FromChars'.
Is there is reason to default to the slowest common denominator, or was it giving you an issue?

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ if(POLICY CMP0091)
cmake_policy(SET CMP0091 NEW) # For CMake 3.15 and newer, enable use of CMAKE_MSVC_RUNTIME_LIBRARY, without requiring CMake 3.15
endif()

cmake_minimum_required(VERSION 3.1...3.22)
cmake_minimum_required(VERSION 3.1...3.29)

if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

project( SpecUtils )
project( SpecUtils VERSION 1.0.0 )

enable_testing()

option( SpecUtils_ENABLE_D3_CHART "Enables exporting of the D3 chart format" ON )
option( SpecUtils_D3_SUPPORT_FILE_STATIC "Compiles the JS and CSS files into memory, rather than reading files on disk; disable to allow changing JS/CSS without having to recompile" ON )
Expand All @@ -27,7 +29,6 @@ option( SpecUtils_USE_FROM_CHARS "Use c++17 std::from_chars for parsing floats/i
option( SpecUtils_ENABLE_EQUALITY_CHECKS "Enables the equal_enough(...) functions for comparing two spectrum files." OFF ) #code size is only reason to default to off, I think
option( PERFORM_DEVELOPER_CHECKS "Performs additional computationally expensive tests during execution" off )


# If we are building this on Windows, not as a sub-project, lets enable selecting C++ runtime
if( MSVC AND (SpecUtils_PYTHON_BINDINGS OR SpecUtils_BUILD_UNIT_TESTS) )
get_directory_property(hasParent PARENT_DIRECTORY)
Expand Down Expand Up @@ -230,6 +231,11 @@ add_library( SpecUtils ${SpecUtils_LIB_TYPE} ${sources} ${headers} ${OTHER_SUPPO
set_target_properties( SpecUtils PROPERTIES PREFIX "lib" OUTPUT_NAME "SpecUtils" )


#For CMake 3.8+, could specify C++11 using the following so it will be propagated to down-line targets...
#target_compile_features(SpecUtils PUBLIC cxx_std_11)
#set_target_properties(SpecUtils PROPERTIES CXX_EXTENSIONS OFF)


if( SpecUtils_USE_FROM_CHARS )
if( NOT (MSVC AND (MSVC_VERSION GREATER_EQUAL 1920)) )
# MSVC >=2019 supports floating point from_chars, while Xcode 14 still doesnt (havent checked Android Studio)
Expand Down Expand Up @@ -305,7 +311,7 @@ elseif( WIN32 )
target_link_libraries( SpecUtils PUBLIC "Shlwapi.lib" )
endif( MINGW )

target_include_directories( SpecUtils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${Boost_INCLUDE_DIRS}
target_include_directories( SpecUtils PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> ${Boost_INCLUDE_DIRS}
PRIVATE ${THIRD_PARTY_DIR} ${PYTHON_INCLUDE_DIRS} )

if( SpecUtils_BUILD_REGRESSION_TEST )
Expand All @@ -326,9 +332,52 @@ if( SpecUtils_BUILD_FUZZING_TESTS )
add_subdirectory( fuzz_test )
endif( SpecUtils_BUILD_FUZZING_TESTS )

set_property( TARGET SpecUtils PROPERTY POSITION_INDEPENDENT_CODE ON )

# Specify installation rules
include(GNUInstallDirs)

# Install the library
install(TARGETS SpecUtils
EXPORT SpecUtilsTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

set( SpecUtils_config_OUT ${CMAKE_CURRENT_BINARY_DIR}/SpecUtils_config.h )
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/SpecUtils/SpecUtils_config.h.in
${CMAKE_CURRENT_BINARY_DIR}/SpecUtils_config.h
${SpecUtils_config_OUT}
)
LIST(APPEND headers ${SpecUtils_config_OUT})
# Install the headers
install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/SpecUtils)

# Create and install the CMake package configuration files
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/SpecUtilsConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)

configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/SpecUtilsConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/SpecUtilsConfig.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SpecUtils
)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/SpecUtilsConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SpecUtilsConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SpecUtils
)

install(EXPORT SpecUtilsTargets
FILE SpecUtilsTargets.cmake
NAMESPACE SpecUtils::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SpecUtils
)
37 changes: 37 additions & 0 deletions SpecUtils/ParseUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <istream>
#include <ostream>

#include "SpecUtils/StringAlgo.h"

/** Some functions and definitions that help to parse and write spectrum files,
but maybe dont fit in other sections of code.
*/
Expand Down Expand Up @@ -290,6 +292,41 @@ Integral float_to_integral( float d )
static constexpr Integral max_int_val = std::numeric_limits<Integral>::max();
return std::signbit(d) ? min_int_val : max_int_val;
}//float_to_integral

template<class StringVec>
inline std::string get_source(const StringVec & vec)
{
auto retVal = std::string();
auto sourceStringOffset = 8U;
for(auto str : vec)
{
if(SpecUtils::istarts_with(str, "Source:"))
{
retVal = str.substr(sourceStringOffset);
trim(retVal);
break;
}
}
return retVal;
}

template<class StringVec>
inline std::string get_description(const StringVec & vec)
{
auto retVal = std::string();
auto descrOffset = 12U;
for(auto str : vec)
{
if(SpecUtils::istarts_with(str, "Description:"))
{
retVal = str.substr(descrOffset);
trim(retVal);
break;
}
}
return retVal;
}

}//namespace SpecUtils

#endif //SpecUtils_ParseUtils_h
Expand Down
35 changes: 24 additions & 11 deletions SpecUtils/SpecFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
Shortcommings that wcjohns should address:
- Many of the N24 fields possible are not checked for
- comments for multiple different tags, ...
- Neutron measurements should have their own live and real time
- Neutron counts are typically merged into a gamma detectors Measurement if a
reasonable pairing can be made. When and if this is done needs to be clearly
specified, and either stopped of facilities added to keep neutron det. info.
Expand Down Expand Up @@ -513,12 +512,8 @@ double gamma_integral( const std::shared_ptr<const Measurement> &hist,
// InterSpec is using to represent detector response functions on disk.
const std::string &detectorTypeToString( const DetectorType type );







using FloatVec = std::vector<float>;
using FloatVecPtr = std::shared_ptr<FloatVec>;

class Measurement
{
Expand All @@ -538,9 +533,19 @@ class Measurement

//live_time(): returned in units of seconds. Will be 0 if not known.
float live_time() const;

void set_live_time(float time)
{
live_time_ = time;
}

//real_time(): returned in units of seconds. Will be 0 if not known.
float real_time() const;

void set_real_time(float time)
{
real_time_ = time;
}

//contained_neutron(): returns whether or not the measurement is thought to
// contain the possibility to detect neutrons (e.g. if a neutron detector was
Expand Down Expand Up @@ -793,7 +798,15 @@ class Measurement
*/
void set_gamma_counts( std::shared_ptr<const std::vector<float>> counts,
const float livetime, const float realtime );


void set_gamma_counts( std::shared_ptr<const std::vector<float>> counts );

void set_gamma_counts(const FloatVec& spectrum)
{
auto counts = std::make_shared<FloatVec>(spectrum);

set_gamma_counts(counts, 0.0F, 0.0F);
}
/** Sets the neutron counts, and also updates
#Measurement::neutron_counts_sum_ and #Measurement::contained_neutron_ .

Expand All @@ -809,7 +822,7 @@ class Measurement

If `neutron_live_time` is less than or equal to zero, the gamma real time will be used.
*/
void set_neutron_counts( const std::vector<float> &counts, const float neutron_live_time );
void set_neutron_counts( const std::vector<float> &counts, const float neutron_live_time=-1.0F );

//To set real and live times, see SpecFile::set_live_time(...)

Expand Down Expand Up @@ -1157,7 +1170,7 @@ class Measurement
std::shared_ptr<const SpecUtils::LocationState> location_;

friend class ::SpecMeas;
friend class SpecFile;
friend class SpecFile; // bruh...
friend struct N42DecodeHelper2006;
friend struct N42DecodeHelper2012;
friend struct GrossCountNodeDecodeWorker;
Expand Down Expand Up @@ -1470,7 +1483,7 @@ class SpecFile
// number available if that detector does not already have that one or else
// its assigned to be one larger sample number - this by no means captures
// all use cases, but good enough for now.
void add_measurement( std::shared_ptr<Measurement> meas, const bool doCleanup );
void add_measurement( std::shared_ptr<Measurement> meas, const bool doCleanup=true );

//remove_measurement(...): removes the measurement from this SpecFile
// object and if 'doCleanup' is specified, then all sums will be
Expand Down
2 changes: 2 additions & 0 deletions SpecUtils/StringAlgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ namespace SpecUtils
unsigned int levenshtein_distance( const std::string &source,
const std::string &target,
const size_t max_str_len = 128 );



}//namespace SpecUtils

Expand Down
11 changes: 11 additions & 0 deletions SpecUtilsConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

# Add any dependencies here, e.g.,
# find_dependency(SomeOtherLibrary)

include("${CMAKE_CURRENT_LIST_DIR}/SpecUtilsTargets.cmake")

set(SpecUtils_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIR@")
set(SpecUtils_LIBRARIES SpecUtils)
69 changes: 69 additions & 0 deletions bindings/swig/fortran/SpecUtilsFortran.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
%module SpecUtilsWrap

%{
#include <SpecUtils/SpecFile.h>
#include <SpecUtils/ParseUtils.h>
%}


%include "stl.i"
//%include "std_vector.i"
/* instantiate the required template specializations */
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
%template(FloatVector) vector<float>;
%template(MeasurementVector) vector<SpecUtils::Measurement>;

}


%include "std_shared_ptr.i"
%shared_ptr(vector<SpecUtils::Measurement>)
%shared_ptr(SpecUtils::Measurement)
%shared_ptr(std::vector<float>)

%include "std_string.i"
%apply std::string { std::string& }
//%include "std_wstring.i"
%include "wchar.i"

%ignore combine_gamma_channels;
%ignore truncate_gamma_channels;
%ignore set_energy_calibration;
%ignore descriptionText;
%ignore operator=;

%include "SpecUtils/SpecFile.h"

%extend SpecUtils::Measurement
{
/// Return the count at a given index.
/// @param index is 1-based
float gamma_count_at(int index)
{
return $self->gamma_counts()->at(index-1);
}

std::string get_description()
{
auto &remarks = $self->remarks();
return SpecUtils::get_description(remarks);
}

std::string get_source()
{
auto &remarks = $self->remarks();
return SpecUtils::get_source(remarks);
}
}

%extend SpecUtils::SpecFile
{
/// Return the measurement at a given index.
/// @param index is 1-based
std::shared_ptr<const SpecUtils::Measurement> measurement_at(int index)
{
return $self->measurement(static_cast<size_t>(index-1));
}
}
Loading