Skip to content

Conversation

@dfinke
Copy link

@dfinke dfinke commented Oct 1, 2017

Fix #2358

@SteveL-MSFT SteveL-MSFT added the Hacktoberfest Potential candidate to participate in Hacktoberfest label Oct 1, 2017
@SteveL-MSFT
Copy link
Member

Added the issue

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 2, 2017

@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 _measureSum, _measureAverage, _measureStdDeviation - can we refactor and optimize?


/// <summary>
///
/// The StdDeviation of property values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the comments, we should spell out Standard Deviation


/// <summary>
///
/// The StdDeviation of property values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the comments, we should spell out Standard Deviation

return _measureStdDeviation;
}
set
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stddeviation requires _measureAverage, so I think we should set _measureAverage = true. If _measureAverage = false, perhaps we should error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Where should it throw, in CreateGenericMeasureInfo?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that's the right place.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we request Average parameter if we can set _measureAverage = true here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that A*A is faster than Math.Pow(A,2)


if (_measureStdDeviation && _measureAverage && stat.count > 0)
{
var popdev = 0.0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sumOfDerivation is more descriptive than popdev

@dfinke
Copy link
Author

dfinke commented Oct 2, 2017

I noticed that the property StdDeviation doesn't show up in help measure. Do the maml files need to get updated and rebuilt? I tweaked one of the maml files, but no luck.

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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

messages should be in the corresponding .resx file. See line 537 as an example.

@SteveL-MSFT
Copy link
Member

@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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 3, 2017

@dfinke #4972 was merged. Please resolve merge conflict.

return _measureStdDeviation;
}
set
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it must be Diagnostic.Assert().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With different parameter sets, this code is not needed.


foreach (double n in stat.stdDeviationNumbers)
{
var m = n - (double)average;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move the cast out of foreach ?

sumOfDerivation += m * m;
}

stdDeviation = Math.Round(Math.Sqrt(sumOfDerivation / (stat.stdDeviationNumbers.Count - 1)), 4);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clarify why we round? I believe it must be a double.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rounded for no particular reason. I can remove it.

@lzybkr
Copy link
Contributor

lzybkr commented Oct 3, 2017

Maybe not for this PR - but the output of Measure-Object is not great - most of the properties are $null, and adding a new property will make it slightly worse.

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:

<ListItem>
    <PropertyName WhenPresent="true">Average</PropertyName>
</ListItem>

This could test if the property is present and non-null before selecting the item.

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 3, 2017

Can we create a custom PSObject with non-null properties in the cmdlet?

return _measureStdDeviation;
}
set
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With different parameter sets, this code is not needed.

@dfinke
Copy link
Author

dfinke commented Oct 3, 2017

Another option might be to calculate average when std deviation is specified. Or use the already calculated average if the -Average switch was passed.

@dfinke
Copy link
Author

dfinke commented Oct 5, 2017

I'm confused. I checked the type etc. In the .cs file I took out the Math.Round.

image

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 7, 2017

@dfinke Please remove Math.Round because we should return honest double - the round is only formatting. We have separate formatting engine as @lzybkr mentioned above. If we want better formatting we should make it there. I believe we can skip this in the PR.

Please resolve merge conflict and address feedbacks.

@iSazonov
Copy link
Collaborator

@dfinke Could you please continue?

@dfinke
Copy link
Author

dfinke commented Oct 30, 2017

@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.

@iSazonov
Copy link
Collaborator

@dfinke Very sorry. Welcome back with new ideas and contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hacktoberfest Potential candidate to participate in Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants