-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[Candidate] Add getAge function #5945
Conversation
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.
Looks good!
It might be helpful to have an example of where it would be used. If not that, maybe a unit test demonstrating that it returns what we expect it to return.
Not worth blocking though
@zaliqarosli I dont have a problem with it but I'm not sure why you are truncating the months and days. You are making it unusable for any autism study, they would only get 0 all the time. |
Hmm yeah it may be good to change the name of the function to Maybe an even cleaner approach would be to move calculateAge from Utility to Candidate. Then you could add wrapper functions in the Candidate class that call calculateAge, e.g. public function getAgeInYears(): int {
return $this->calculateAge(
$this->candidateInfo['DoB'],
(new \DateTime()->format('Y-m-d'))
)['year'];
} And do another one for getAgeInMonths and getAgeInDays. Then it would work for many types of studies. |
@johnsaigle @ridz1208 ya i am down to make separate functions for in years, in months, and in days. I don't agree with moving calculateAge to the candidate class however. It is a function that's used a lot in NDB_BVL_Instrument and I think makes sense to be a generic 'Utility' function rather than candidate specific |
Well... this is not technically giving you an age IN years. it's giving you the number of full years in the age. ie a 3 month old baby should not be 0 but rather 0.25 years |
@ridz1208 Yeah it's int years not float years. I am speculating but I imagine it's more typical for researchers to want the age of a child in months rather than decimal years anyway. 5 months is more useful than 0.4166666666666666 years |
@ridz1208 i know what you mean, but in that case, the project can use getAgeInMonths() instead of getAgeInYears(). I don't think a Parkinson's project for example would find use in seeing a candidate's age as 85.83 years as opposed to just 85 years |
but now I'm thinking do we also want a function called getAgeInYears&Months???? |
Why not take an input variable that specifies years, months, or days instead of having 3 functions? |
@thomasbeaudry because the calculations are all different and grouping them into one function that takes as input the different cases wouldn't necessarily make the code or functions any cleaner/succinct |
30c741e
to
73106f0
Compare
@johnsaigle @ridz1208 okay checkout the calculations using php's DateTime diff() functionality in my last commit. I made commit 2 incase commit 3 causes controversy xD |
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'm a big big fan of using DateTime objects for this sort of thing so I'm happy you're doing that. 😄
I have a couple of concerns:
Utility::calculateAge()
will give different results from thegetAge*()
functions.
That's not a bad thing necessarily but we'll have to keep this in mind and should document it somewhere clearly.
It's possible that this could cause an issue in analysis. Someone with project experience could better speak to this.
Maybe a point in a LORIS meeting would be good?
I'm gonna add the Needs Documentation and Add to Release Notes labels. We can remove them if this turns out not to be a big deal.
- I think the names of the function should be consistent.
Either use the word "In" for all of them or remove it from all of them.
-
Could you add unit tests?
-
Since getAge() doesn't use calculateAge anymore, it should probably be changed to just return a DateTime. It doesn't need to conform to the format used by calculateAge(). It might be confusing if it does actually.
This would also let you call the main function getAge()
from your other getAge*()
functions so they could simplify to e.g.
public function getAgeInMonths(): int
{
$this->getAge();
return (int)$age->format('m') + 12 * $age->format('y');
}
ps. the diff() function returns a DateInterval type |
Don't these sentences contradict each other? In the first you're saying that you want the users to format on their own and then you're saying you want to format it for them. DateTime is unformatted which I think is ideal. If you return a DateTime then the calling code function could just do It would also allow us to move forward with using DateTimes everywhere for internal code instead of flip-flopping between arrays, strings, and DateTimes as we do now. To me it seems DateTimes give us more flexibility and consistency and allow us to take advantage of types. You're right about the "->days"/DateInterval part. I don't have a problem there. |
OK hold on I realize how I was being confusing/was confused. I think getAge could return a |
@johnsaigle i don't see the contradiction. i think you're misunderstanding me. what the functions are formatting is not a DateTime object, its a DateInterval object, as that is what is returned by diff(). Returning a DateInterval object without formatting it doesn't seem useful to me. Users can format the age in whatever way they want. And I think providing the DateInterval data in a way that makes this possible can be useful. For example, having getAge() return a string "%y Years, %m Months, %d days" is not as useful as returning an array where its values %y, %m, %d can then be used. You're talking about DateTime but that is not the result type that we're dealing with here. How would you yourself deal with the DateInterval returned by diff before you return it in the getAge() function? |
@johnsaigle getAge could just return a DateInterval but then we'd need more functions to then return meaningful data from the candidate class |
I definitely was. Sorry. I think what I said still holds if getAge returns a DateInterval though. I was wrong everywhere I said DateTime. This should all be DateIntervals.
DateInterval does that on its own though. You're creating an array around DateInterval that has fewer options. $ageInterval = $candidate->getAge();
// print time since birth.
echo $ageInterval->format("%y years, %m months, %d days, and %h hours");
I think you'd just need to modify the ones you have here function getAgeInMonths(): int {
$ageInterval = $this->getAge();
return (int) $ageInternval->format("%y") * 12 + (int) $ageInterval->format("%m");
} |
@zaliqarosli I like how this is looking |
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.
Refactor looks good! 😄 Sorry again for the initial confusion.
Let me know if you want assistance with the unit tests.
As far as I can tell, the integration tests in If you add DoBs for the candidates by modifying the arrays in this file, I think that should fix it. Loris/modules/timepoint_list/test/timepoint_listTest.php Lines 111 to 149 in 73ba888
|
days instead of %a
Co-Authored-By: John Saigle <4022790+johnsaigle@users.noreply.github.com>
Co-authored-by: Dave MacFarlane <driusan@gmail.com>
1fb076d
to
ab015ce
Compare
@zaliqarosli is this ready? It seems to be passing Travis |
@driusan i believe so! |
Calculates the candidate's age today using php's DateTime obj diff() function, rather than the custom function used in the Utility class.
Calculates the candidate's age today using php's DateTime obj diff() function, rather than the custom function used in the Utility class.
Calculates the candidate's age today using php's DateTime obj diff() function, rather than the custom function used in the Utility class.
Brief summary of changes
Calculates the candidate's age today using php's DateTime obj diff() function in:
If you're not a huge fan of doing it this way, look at commit 2 which uses \Utility::calculateAge function (not as accurate for calculating number of days)
I have added changes to timepoint_list to show an example of how to use these functions and for testing. These can be removed before merging. Travis will fail until these changes to timepoint_list are removed.
Testing instructions (if applicable)