Skip to content

Commit

Permalink
add subpopulationsWithNames() to Community
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaller committed Jul 17, 2024
1 parent 8385fb2 commit 2001981
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion QtSLiM/help/SLiMHelpClasses.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="2299.5">
<meta name="CocoaVersion" content="2299.77">
<style type="text/css">
p.p1 {margin: 18.0px 0.0px 3.0px 0.0px; font: 11.0px Optima}
p.p2 {margin: 6.0px 0.0px 3.0px 0.0px; font: 11.0px Optima}
Expand Down Expand Up @@ -221,6 +221,8 @@
<p class="p6">Find and return the <span class="s1">Species</span> objects with <span class="s1">id</span> values matching the values in <span class="s1">ids</span>.<span class="Apple-converted-space">  </span>If no matching <span class="s1">Species</span> object can be found with a given <span class="s1">id</span>, an error results.</p>
<p class="p5">– (object&lt;Subpopulation&gt;)subpopulationsWithIDs(integer ids)</p>
<p class="p6">Find and return the <span class="s1">Subpopulation</span> objects with <span class="s1">id</span> values matching the values in <span class="s1">ids</span>.<span class="Apple-converted-space">  </span>If no matching <span class="s1">Subpopulation</span> object can be found with a given <span class="s1">id</span>, an error results.</p>
<p class="p5">– (object&lt;Subpopulation&gt;)subpopulationsWithNames(string names)</p>
<p class="p6">Find and return the <span class="s1">Subpopulation</span> objects with <span class="s1">name</span> values matching the values in <span class="s1">names</span>.<span class="Apple-converted-space">  </span>If no matching <span class="s1">Subpopulation</span> object can be found with a given name, an error results.</p>
<p class="p5">– (float$)usage(void)</p>
<p class="p6">Return the current memory usage of the simulation.<span class="Apple-converted-space">  </span>The specifics of what is totalled up should not be relied upon as it may change from version to version of SLiM.<span class="Apple-converted-space">  </span>This method is primarily useful for understanding where the memory usage of a simulation predominantly resides, for debugging or optimization.<span class="Apple-converted-space">  </span>Note that it does not capture <i>all</i> memory usage by the process; rather, it summarizes the memory usage by SLiM and Eidos in directly allocated objects and buffers.<span class="Apple-converted-space">  </span>To see details of this internal memory usage, use the <span class="s1">Community</span> method <span class="s1">outputUsage()</span>.<span class="Apple-converted-space">  </span>To get the <i>total</i> memory usage of the running process (either current or peak), use the Eidos function <span class="s1">usage()</span>.</p>
<p class="p1"><b>5.4<span class="Apple-converted-space">  </span>Class Genome</b></p>
Expand Down
14 changes: 14 additions & 0 deletions SLiMgui/SLiMHelpClasses.rtf
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,20 @@ Note that new script blocks can also be created and scheduled using the
\f4\fs20 , an error results.\
\pard\pardeftab397\li720\fi-446\ri720\sb180\sa60\partightenfactor0
\f3\fs18 \cf2 \'96\'a0(object<Subpopulation>)subpopulationsWithNames(string\'a0names)\
\pard\pardeftab720\li547\ri720\sb60\sa60\partightenfactor0
\f4\fs20 \cf2 Find and return the
\f3\fs18 Subpopulation
\f4\fs20 objects with
\f3\fs18 name
\f4\fs20 values matching the values in
\f3\fs18 names
\f4\fs20 . If no matching
\f3\fs18 Subpopulation
\f4\fs20 object can be found with a given name, an error results.\
\pard\pardeftab397\li720\fi-446\ri720\sb180\sa60\partightenfactor0
\f3\fs18 \cf2 \'96\'a0(float$)usage(void)\
\pard\pardeftab720\li547\ri720\sb60\sa60\partightenfactor0
Expand Down
2 changes: 2 additions & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Note that not every commit will be logged here; that is what the Github commit h

development head (in the master branch):
fixed a bug in calcWattersonsTheta(): the value calculated would be incorrect if a window (start/end) was specified
added TOC I and TOC II to the PDF index of the SLiM manual; removed the bottom-of-page links section, which made search difficult
add subpopulationsWithNames() method to Community, to facilitate being able to use user-defined names for subpops


version 4.2.2 (Eidos version 3.2.2):
Expand Down
13 changes: 13 additions & 0 deletions core/community.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,19 @@ Subpopulation *Community::SubpopulationWithID(slim_objectid_t p_subpop_id)
return nullptr;
}

Subpopulation *Community::SubpopulationWithName(const std::string &p_subpop_name)
{
for (Species *species : all_species_)
{
Subpopulation *found_subpop = species->SubpopulationWithName(p_subpop_name);

if (found_subpop)
return found_subpop;
}

return nullptr;
}

MutationType *Community::MutationTypeWithID(slim_objectid_t p_muttype_id)
{
for (Species *species : all_species_)
Expand Down
2 changes: 2 additions & 0 deletions core/community.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class Community : public EidosDictionaryUnretained
bool SubpopulationNameInUse(const std::string &p_subpop_name); // not whether a SLiM subpop with this name currently exists, but whether the name is "in use"

Subpopulation *SubpopulationWithID(slim_objectid_t p_subpop_id);
Subpopulation *SubpopulationWithName(const std::string &p_subpop_name);
MutationType *MutationTypeWithID(slim_objectid_t p_muttype_id);
GenomicElementType *GenomicElementTypeWithID(slim_objectid_t p_getype_id);
SLiMEidosBlock *ScriptBlockWithID(slim_objectid_t p_script_block_id);
Expand Down Expand Up @@ -366,6 +367,7 @@ class Community : public EidosDictionaryUnretained
EidosValue_SP ExecuteMethod_scriptBlocksWithIDs(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
EidosValue_SP ExecuteMethod_speciesWithIDs(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
EidosValue_SP ExecuteMethod_subpopulationsWithIDs(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
EidosValue_SP ExecuteMethod_subpopulationsWithNames(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
EidosValue_SP ExecuteMethod_outputUsage(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
EidosValue_SP ExecuteMethod_registerFirstEarlyLateEvent(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
EidosValue_SP ExecuteMethod_registerInteractionCallback(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter);
Expand Down
26 changes: 26 additions & 0 deletions core/community_eidos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ EidosValue_SP Community::ExecuteInstanceMethod(EidosGlobalStringID p_method_id,
case gID_scriptBlocksWithIDs: return ExecuteMethod_scriptBlocksWithIDs(p_method_id, p_arguments, p_interpreter);
case gID_speciesWithIDs: return ExecuteMethod_speciesWithIDs(p_method_id, p_arguments, p_interpreter);
case gID_subpopulationsWithIDs: return ExecuteMethod_subpopulationsWithIDs(p_method_id, p_arguments, p_interpreter);
case gID_subpopulationsWithNames: return ExecuteMethod_subpopulationsWithNames(p_method_id, p_arguments, p_interpreter);
case gID_outputUsage: return ExecuteMethod_outputUsage(p_method_id, p_arguments, p_interpreter);
case gID_registerFirstEvent:
case gID_registerEarlyEvent:
Expand Down Expand Up @@ -872,6 +873,30 @@ EidosValue_SP Community::ExecuteMethod_subpopulationsWithIDs(EidosGlobalStringID
return EidosValue_SP(vec);
}

// ********************* – (object<Subpopulation>)subpopulationsWithNames(string names)
//
EidosValue_SP Community::ExecuteMethod_subpopulationsWithNames(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter)
{
#pragma unused (p_method_id, p_interpreter)
EidosValue *names_value = p_arguments[0].get();
int names_count = names_value->Count();
const std::string *names_data = names_value->StringData();
EidosValue_Object *vec = (new (gEidosValuePool->AllocateChunk()) EidosValue_Object(gSLiM_Subpopulation_Class))->resize_no_initialize_RR(names_count);

for (int name_index = 0; name_index < names_count; name_index++)
{
const std::string &name = names_data[name_index];
Subpopulation *object = SubpopulationWithName(name);

if (!object)
EIDOS_TERMINATION << "ERROR (Community::ExecuteMethod_subpopulationsWithNames): subpopulationsWithNames() did not find a subpopulation with name " << name << "." << EidosTerminate();

vec->set_object_element_no_check_NORR(object, name_index);
}

return EidosValue_SP(vec);
}

// ********************* – (void)outputUsage(void)
//
EidosValue_SP Community::ExecuteMethod_outputUsage(EidosGlobalStringID p_method_id, const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter)
Expand Down Expand Up @@ -1396,6 +1421,7 @@ const std::vector<EidosMethodSignature_CSP> *Community_Class::Methods(void) cons
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_simulationFinished, kEidosValueMaskVOID)));
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_speciesWithIDs, kEidosValueMaskObject, gSLiM_Species_Class))->AddInt("ids"));
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_subpopulationsWithIDs, kEidosValueMaskObject, gSLiM_Subpopulation_Class))->AddInt("ids"));
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gStr_subpopulationsWithNames, kEidosValueMaskObject, gSLiM_Subpopulation_Class))->AddString("names"));
methods->emplace_back((EidosInstanceMethodSignature *)(new EidosInstanceMethodSignature(gEidosStr_usage, kEidosValueMaskFloat | kEidosValueMaskSingleton)));

std::sort(methods->begin(), methods->end(), CompareEidosCallSignatures);
Expand Down
1 change: 1 addition & 0 deletions core/slim_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ const std::string &gStr_mutationTypesWithIDs = EidosRegisteredString("mutationTy
const std::string &gStr_scriptBlocksWithIDs = EidosRegisteredString("scriptBlocksWithIDs", gID_scriptBlocksWithIDs);
const std::string &gStr_speciesWithIDs = EidosRegisteredString("speciesWithIDs", gID_speciesWithIDs);
const std::string &gStr_subpopulationsWithIDs = EidosRegisteredString("subpopulationsWithIDs", gID_subpopulationsWithIDs);
const std::string &gStr_subpopulationsWithNames = EidosRegisteredString("subpopulationsWithNames", gID_subpopulationsWithNames);
const std::string &gStr_individualsWithPedigreeIDs = EidosRegisteredString("individualsWithPedigreeIDs", gID_individualsWithPedigreeIDs);
const std::string &gStr_killIndividuals = EidosRegisteredString("killIndividuals", gID_killIndividuals);
const std::string &gStr_mutationCounts = EidosRegisteredString("mutationCounts", gID_mutationCounts);
Expand Down
2 changes: 2 additions & 0 deletions core/slim_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ extern const std::string &gStr_mutationTypesWithIDs;
extern const std::string &gStr_scriptBlocksWithIDs;
extern const std::string &gStr_speciesWithIDs;
extern const std::string &gStr_subpopulationsWithIDs;
extern const std::string &gStr_subpopulationsWithNames;
extern const std::string &gStr_individualsWithPedigreeIDs;
extern const std::string &gStr_killIndividuals;
extern const std::string &gStr_mutationCounts;
Expand Down Expand Up @@ -1320,6 +1321,7 @@ enum _SLiMGlobalStringID : int {
gID_scriptBlocksWithIDs,
gID_speciesWithIDs,
gID_subpopulationsWithIDs,
gID_subpopulationsWithNames,
gID_individualsWithPedigreeIDs,
gID_killIndividuals,
gID_mutationCounts,
Expand Down
10 changes: 10 additions & 0 deletions core/species.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,16 @@ void Species::DeleteAllMutationRuns(void)
}
}

Subpopulation *Species::SubpopulationWithName(const std::string &p_subpop_name) {
for (auto subpop_iter : population_.subpops_)
{
Subpopulation *subpop = subpop_iter.second;
if (subpop->name_ == p_subpop_name)
return subpop;
}
return nullptr;
}


//
// Running cycles
Expand Down
1 change: 1 addition & 0 deletions core/species.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ class Species : public EidosDictionaryUnretained
auto id_iter = population_.subpops_.find(p_subpop_id);
return (id_iter == population_.subpops_.end()) ? nullptr : id_iter->second;
}
Subpopulation *SubpopulationWithName(const std::string &p_subpop_name);
inline MutationType *MutationTypeWithID(slim_objectid_t p_muttype_id) {
auto id_iter = mutation_types_.find(p_muttype_id);
return (id_iter == mutation_types_.end()) ? nullptr : id_iter->second;
Expand Down

0 comments on commit 2001981

Please sign in to comment.