-
Notifications
You must be signed in to change notification settings - Fork 6
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
Renaming demes within Graph object #499
Renaming demes within Graph object #499
Conversation
Hi @aabiddanda! This is a good idea, so I agree we need to export an API function for this. And I think your choice to put this at Lines 824 to 839 in 392c6a0
|
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #499 +/- ##
=======================================
Coverage 99.81% 99.81%
=======================================
Files 5 5
Lines 1585 1595 +10
=======================================
+ Hits 1582 1592 +10
Misses 3 3
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
@grahamgower great spot on this lurking already in the codebase! I'll move this code to the appropriate place and implement some tests explicitly targeting this functionality to pass |
…axed assumption of all demes needing to be named outside of ms case; updated tests for 100 pct cov on demes.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @aabiddanda, this is looking good. Would you be able to expand the tests to include the nominal cases too? I.e. check the deme names after renaming are what the should be, for each of the deme names, ancestors, migration dest/source, pulse dest/source.
demes/demes.py
Outdated
:rtype: Graph | ||
""" | ||
graph = copy.deepcopy(self) | ||
if not isinstance(names, MutableMapping): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think its necessary to do explicit type checking here. If a non-dict is passed, this function will fail anyhow with an indexing error of some sort.
demes/demes.py
Outdated
@@ -1997,6 +1997,36 @@ def in_generations(self) -> Graph: | |||
graph.generation_time = 1 | |||
return graph | |||
|
|||
def rename_demes(self, names: MutableMapping[str, str]) -> Graph: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reasoning for this being a MutableMapping
rather than just a Mapping
? This function shouldn't be modifying the mapping, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reasoning for the MutableMapping
was for the scenario where you only want to change the label of a single deme but not mess with all of the other demes (specifically the ancestral demes). The basis is that if an existing deme name does not exist you may want to default to keeping that name by adding it to the renaming mapping (see code below).
This relaxes the requirement that every deme needs to be defined in the names dictionary as well. If you think that adding in conditional statements to the cases to only rename demes that are named (rather than all demes in the graph) I can certainly implement that if its more in keeping with the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, renaming only a subset of the demes seems reasonable, but I don't think we should be mutating the argument here. Using conditionals would be fine, but if you want to keep the code as it is, then you could copy the names
dict at the start of the function.
… processing; test positive cases of renaming;
@grahamgower should the changelog be changed as well? I don't think that this necessarily constitutes a new version per-se but to maintain that it is a new feature in a to-be-released version? Let me know if this would be easier to handle closer to when you are thinking about releasing another version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great!
Don't worry about the changelog - I'll update that when I do the next release. |
I encountered a need for this feature when looking to develop a
snakemake
workflow that expected specific deme labels for post-processing of simulations. The method is fairly straightforward and implements the type-checks. I will update the tests for this as well if it is a useful feature for others to have or if alternative spec-checks should be performed.