First pass at adding std deviation to measure-object#4969
First pass at adding std deviation to measure-object#4969dfinke wants to merge 6 commits intoPowerShell:masterfrom dfinke:master
Conversation
|
Added the issue |
|
@dfinke Thanks for your contribution! The tests file contains multiple formatting issue. We should split code and formatting changes. So I open #4972 - please rebase after it will be merged. We should think about optimizations. Currently we do multiple checks |
|
|
||
| /// <summary> | ||
| /// | ||
| /// The StdDeviation of property values |
There was a problem hiding this comment.
For the comments, we should spell out Standard Deviation
|
|
||
| /// <summary> | ||
| /// | ||
| /// The StdDeviation of property values |
There was a problem hiding this comment.
For the comments, we should spell out Standard Deviation
| return _measureStdDeviation; | ||
| } | ||
| set | ||
| { |
There was a problem hiding this comment.
stddeviation requires _measureAverage, so I think we should set _measureAverage = true. If _measureAverage = false, perhaps we should error.
There was a problem hiding this comment.
Makes sense. Where should it throw, in CreateGenericMeasureInfo?
There was a problem hiding this comment.
Yes, I think that's the right place.
There was a problem hiding this comment.
I wonder why we request Average parameter if we can set _measureAverage = true here?
There was a problem hiding this comment.
Thinking about this, it seems that -Average:$false -StdDeviation should not be a valid combination and perhaps a better way to solve this is to make them different parameter sets. The problem here is that depending on which one is set first, you may get unintended behavior -StdDeviation -Average:$false since the code here overwrites _measureAverage.
There was a problem hiding this comment.
We need new flag to process a sum. We can set it in BeginProcessing.
|
|
||
| foreach (double n in stat.stdDeviationNumbers) | ||
| { | ||
| popdev += Math.Pow((n - (double)average), 2); |
There was a problem hiding this comment.
My understanding is that A*A is faster than Math.Pow(A,2)
|
|
||
| if (_measureStdDeviation && _measureAverage && stat.count > 0) | ||
| { | ||
| var popdev = 0.0; |
There was a problem hiding this comment.
I think sumOfDerivation is more descriptive than popdev
|
I noticed that the property |
| average = stat.sum / stat.count; | ||
|
|
||
| if(_measureStdDeviation && !_measureAverage) { | ||
| var message = "StdDeviation was requested and requires the average to be calculated, please add the -Average switch"; |
There was a problem hiding this comment.
messages should be in the corresponding .resx file. See line 537 as an example.
|
@dfinke the MAML files are generated. You need to update the help found in https://github.com/powershell/powershell-docs |
| <value>Input object "{0}" is not numeric.</value> | ||
| </data> | ||
| <data name="AverageSwitchNotSet" xml:space="preserve"> | ||
| <value>StdDeviation was requested and requires the average to be calculated, please add the -Average switch.</value> |
There was a problem hiding this comment.
I think we should default -Average to be set if -StdDeviation is set, I think the error only applies if someone explicitly used -Average:$false. So I think the error message should be a bit more specific:
StdDeviation was requested and requires the average to be calculated, however '-Average' was set to $false.
| return _measureStdDeviation; | ||
| } | ||
| set | ||
| { |
There was a problem hiding this comment.
I wonder why we request Average parameter if we can set _measureAverage = true here?
| if (_measureAverage && stat.count > 0) | ||
| average = stat.sum / stat.count; | ||
|
|
||
| if(_measureStdDeviation && !_measureAverage) { |
There was a problem hiding this comment.
I think it must be Diagnostic.Assert().
There was a problem hiding this comment.
With different parameter sets, this code is not needed.
|
|
||
| foreach (double n in stat.stdDeviationNumbers) | ||
| { | ||
| var m = n - (double)average; |
There was a problem hiding this comment.
Should we move the cast out of foreach ?
| sumOfDerivation += m * m; | ||
| } | ||
|
|
||
| stdDeviation = Math.Round(Math.Sqrt(sumOfDerivation / (stat.stdDeviationNumbers.Count - 1)), 4); |
There was a problem hiding this comment.
Please clarify why we round? I believe it must be a double.
There was a problem hiding this comment.
I rounded for no particular reason. I can remove it.
|
Maybe not for this PR - but the output of I was going to propose something like what I use in my profile (see here), but that is messy and not quite right (doesn't support strict mode properly), so I might actually want a new formatting feature to make it simpler to specify optional properties, something like: This could test if the property is present and non-null before selecting the item. |
|
Can we create a custom PSObject with non-null properties in the cmdlet? |
| return _measureStdDeviation; | ||
| } | ||
| set | ||
| { |
There was a problem hiding this comment.
Thinking about this, it seems that -Average:$false -StdDeviation should not be a valid combination and perhaps a better way to solve this is to make them different parameter sets. The problem here is that depending on which one is set first, you may get unintended behavior -StdDeviation -Average:$false since the code here overwrites _measureAverage.
| if (_measureAverage && stat.count > 0) | ||
| average = stat.sum / stat.count; | ||
|
|
||
| if(_measureStdDeviation && !_measureAverage) { |
There was a problem hiding this comment.
With different parameter sets, this code is not needed.
|
Another option might be to calculate average when std deviation is specified. Or use the already calculated average if the |
|
@dfinke Please remove Please resolve merge conflict and address feedbacks. |
|
@dfinke Could you please continue? |
|
@iSazonov sorry, my free time got pulled in a different direction. I also got blocked testing. Returning the double from the cmdlet and testing it in Pester. The number looked correct from the cmdlet and in Pester to several decimal places. I won't be able to get back to this in the near future. |
|
@dfinke Very sorry. Welcome back with new ideas and contributions! |

Fix #2358