Skip to content

Commit

Permalink
Fix bug where when exporting only a sub-set of samples from a file, t…
Browse files Browse the repository at this point in the history
…he peaks in the N42 file would be assigned the wrong sample numbers.

The sample numbers of the exported measurement would be changed from original, but the sample numbers of the peaks would not be changed.
  • Loading branch information
wcjohns committed Jun 18, 2024
1 parent a24c9a4 commit 2a0e526
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 2 additions & 0 deletions InterSpec/SpecMeas.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class SpecMeas : public SpecUtils::SpecFile
*/
void cleanup_orphaned_info();

virtual void change_sample_numbers( const std::vector<std::pair<int,int>> &from_to_sample_nums );

std::shared_ptr< std::deque< std::shared_ptr<const PeakDef> > >
peaks( const std::set<int> &samplenums );
/** CAUTION: the returned deque may be modified elsewhere in the app, if for
Expand Down
15 changes: 13 additions & 2 deletions src/ExportSpecFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2911,7 +2911,7 @@ std::shared_ptr<const SpecMeas> ExportSpecFileTool::generateFileToSave()
if( !meas_to_remove.empty() )
{
vector<shared_ptr<const SpecUtils::Measurement>> to_remove( begin(meas_to_remove), end(meas_to_remove) );
answer->remove_measurements( to_remove );
answer->remove_measurements( to_remove ); //This calls `cleanup_after_load(...)`
}

if( !meas_to_add.empty() )
Expand All @@ -2923,7 +2923,7 @@ std::shared_ptr<const SpecMeas> ExportSpecFileTool::generateFileToSave()
if( !meas_to_remove.empty() || !meas_to_add.empty() )
{
answer->set_uuid( "" );
answer->cleanup_after_load();
answer->cleanup_after_load( SpecUtils::SpecFile::DontChangeOrReorderSamples );

// `detectors` and `samples` are maybe no longer valid, since we may have summed them together
// - we will fix this up, although it probably makes more sense to just filter out unwanted
Expand Down Expand Up @@ -2951,6 +2951,17 @@ std::shared_ptr<const SpecMeas> ExportSpecFileTool::generateFileToSave()
}
for( const int sample : samples_to_remove )
samples.erase( sample );

// Now lets map sample numbers to 1 through N
int new_sample_number = 0;
vector<pair<int,int>> old_to_new_samplenum;
for( const int old_sample_number : answer->sample_numbers() )
{
++new_sample_number;
old_to_new_samplenum.emplace_back( old_sample_number, new_sample_number );
}//for( loop over sample numbers )

answer->change_sample_numbers( old_to_new_samplenum );
}//if( !meas_to_remove.empty() || !meas_to_add.empty() )


Expand Down
53 changes: 53 additions & 0 deletions src/SpecMeas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,59 @@ void SpecMeas::cleanup_orphaned_info()
}//void cleanup_orphaned_info()


void SpecMeas::change_sample_numbers( const vector<pair<int,int>> &from_to_sample_nums )
{
SpecFile::change_sample_numbers( from_to_sample_nums );

// I *think*, m_peaks, m_autoSearchPeaks, and m_dbUserStateIndexes are the only
// SpecMeas specific things that use the sample numbers

auto update_peak_map = [&from_to_sample_nums]( SampleNumsToPeakMap &peaks ){
SampleNumsToPeakMap new_peak_map;

for( const SampleNumsToPeakMap::value_type &t : peaks )
{
set<int> new_samples;
for( const int orig_sample : t.first )
{
const auto pos = std::find_if( begin(from_to_sample_nums), end(from_to_sample_nums),
[orig_sample]( const pair<int,int> &val ){ return val.first == orig_sample;} );
new_samples.insert( (pos == end(from_to_sample_nums)) ? orig_sample : pos->second );
}//for( loop over original sample numbers )

new_peak_map[new_samples] = t.second;
}//for( const SampleNumsToPeakMap::value_type &t : peaks )

peaks = new_peak_map;
};//update_peak_map

if( m_peaks )
update_peak_map( *m_peaks );

update_peak_map( m_autoSearchPeaks );


// Need to update m_dbUserStateIndexes
map<set<int>,long long int> newDbUserStateIndexes;
for( const auto &samplenums_dbindex : m_dbUserStateIndexes )
{
set<int> new_sample_nums;
for( const int sample : samplenums_dbindex.first )
{
// Put original sample number in `new_sample_nums`, unless it is one of the sample numbers
// to change, in which case put in the new sample number
const auto pos = std::find_if( begin(from_to_sample_nums), end(from_to_sample_nums),
[sample](const pair<int,int> &lhs) -> bool { return lhs.first == sample; } );

new_sample_nums.insert( (pos == end(from_to_sample_nums)) ? sample : pos->second );
}//for( const int sample : samplenums_dbindex.first )

newDbUserStateIndexes[new_sample_nums] = samplenums_dbindex.second;
}//for( const auto &samplenums_dbindex : m_dbUserStateIndexes )

m_dbUserStateIndexes = newDbUserStateIndexes;
}//void change_sample_numbers( const std::vector<std::pair<int,int>> &from_to_sample_nums );


Wt::Signal<> &SpecMeas::aboutToBeDeleted()
{
Expand Down

0 comments on commit 2a0e526

Please sign in to comment.