Skip to content
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

DM-34082: Handle empty arrays #27

Merged
merged 4 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/lsst/analysis/drp/colorColorFitPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ def colorColorFitPlot(self, catPlot, plotInfo, fitParams):
ys = catPlot[self.config.axisLabels["y"]].values
mags = catPlot[self.config.axisLabels["mag"]].values

if len(xs) == 0 or len(ys) == 0:
return fig
Copy link
Contributor

Choose a reason for hiding this comment

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

I fail to see how the length of xs and ys could differ, but I guess it doesn't hurt to check both.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't either, but this is the first time I am seeing this code, so I am being extra cautious.


# Points to use for the fit
fitPoints = np.where((xs > fitParams["xMin"]) & (xs < fitParams["xMax"])
& (ys > fitParams["yMin"]) & (ys < fitParams["yMax"]))[0]
Expand Down
53 changes: 31 additions & 22 deletions python/lsst/analysis/drp/colorColorPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,18 @@ def colorColorPlot(self, catPlot, plotInfo):
ysStars = catPlot.loc[stars, yCol]
zsStars = catPlot.loc[stars, zCol]

[vminGals, vmaxGals] = np.nanpercentile(zsGalaxies, [1, 99])
[vminStars, vmaxStars] = np.nanpercentile(zsStars, [1, 99])
galPoints = ax.scatter(xsGalaxies, ysGalaxies, c=zsGalaxies, cmap=newReds, label="Galaxies",
s=0.5, vmin=vminGals, vmax=vmaxGals)
starPoints = ax.scatter(xsStars, ysStars, c=zsStars, cmap=newBlues, label="Stars", s=0.5,
vmin=vminStars, vmax=vmaxStars)
if len(zsGalaxies) > 0:
[vminGals, vmaxGals] = np.nanpercentile(zsGalaxies, [1, 99])
galPoints = ax.scatter(xsGalaxies, ysGalaxies, c=zsGalaxies, cmap=newReds, label="Galaxies",
s=0.5, vmin=vminGals, vmax=vmaxGals)
else:
galPoints = None
Copy link
Contributor

Choose a reason for hiding this comment

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

As a style choice, I think a blank line would be nice here. But not necessary of course.

if len(zsStars) > 0:
[vminStars, vmaxStars] = np.nanpercentile(zsStars, [1, 99])
starPoints = ax.scatter(xsStars, ysStars, c=zsStars, cmap=newBlues, label="Stars", s=0.5,
vmin=vminStars, vmax=vmaxStars)
else:
starPoints = None

# Add text details
galBBox = dict(facecolor="lemonchiffon", alpha=0.5, edgecolor="none")
Expand All @@ -205,28 +211,31 @@ def colorColorPlot(self, catPlot, plotInfo):
fig.text(0.70, 0.96, "Num. Stars: {}".format(stars.sum()), bbox=starBBox, fontsize=8)

# Add colorbars
galCbAx = fig.add_axes([0.85, 0.11, 0.04, 0.75])
plt.colorbar(galPoints, cax=galCbAx, extend="both")
galCbAx.yaxis.set_ticks_position("left")
starCbAx = fig.add_axes([0.89, 0.11, 0.04, 0.75])
plt.colorbar(starPoints, cax=starCbAx, extend="both")
magLabel = self.config.axisLabels["z"]
galText = galCbAx.text(0.5, 0.5, magLabel + ": Galaxies", color="k", rotation="vertical",
transform=galCbAx.transAxes, ha="center", va="center", fontsize=10)
galText.set_path_effects([pathEffects.Stroke(linewidth=3, foreground="w"), pathEffects.Normal()])
starText = starCbAx.text(0.5, 0.5, magLabel + ": Stars", color="k", rotation="vertical",
transform=starCbAx.transAxes, ha="center", va="center", fontsize=10)
starText.set_path_effects([pathEffects.Stroke(linewidth=3, foreground="w"), pathEffects.Normal()])
if galPoints:
galCbAx = fig.add_axes([0.85, 0.11, 0.04, 0.75])
plt.colorbar(galPoints, cax=galCbAx, extend="both")
galCbAx.yaxis.set_ticks_position("left")
galText = galCbAx.text(0.5, 0.5, magLabel + ": Galaxies", color="k", rotation="vertical",
transform=galCbAx.transAxes, ha="center", va="center", fontsize=10)
galText.set_path_effects([pathEffects.Stroke(linewidth=3, foreground="w"), pathEffects.Normal()])
if starPoints:
starCbAx = fig.add_axes([0.89, 0.11, 0.04, 0.75])
plt.colorbar(starPoints, cax=starCbAx, extend="both")
starText = starCbAx.text(0.5, 0.5, magLabel + ": Stars", color="k", rotation="vertical",
transform=starCbAx.transAxes, ha="center", va="center", fontsize=10)
starText.set_path_effects([pathEffects.Stroke(linewidth=3, foreground="w"), pathEffects.Normal()])

ax.set_xlabel(self.config.axisLabels["x"])
ax.set_ylabel(self.config.axisLabels["y"])

# Set useful axis limits
starPercsX = np.nanpercentile(xsStars, [1, 99.5])
starPercsY = np.nanpercentile(ysStars, [1, 99.5])
pad = (starPercsX[1] - starPercsX[0])/10
ax.set_xlim(starPercsX[0] - pad, starPercsX[1] + pad)
ax.set_ylim(starPercsY[0] - pad, starPercsY[1] + pad)
if len(xsStars) > 0:
starPercsX = np.nanpercentile(xsStars, [1, 99.5])
starPercsY = np.nanpercentile(ysStars, [1, 99.5])
pad = (starPercsX[1] - starPercsX[0])/10
ax.set_xlim(starPercsX[0] - pad, starPercsX[1] + pad)
ax.set_ylim(starPercsY[0] - pad, starPercsY[1] + pad)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't have any stars, what happens to the limits? (Though better than crashing!)

Copy link
Member Author

Choose a reason for hiding this comment

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

The example case I had had both stars and galaxies list empty. So the axis limits were [0,1] in both directions. I suppose matplotlib still autoscales the limits so they are not going to be totally useless.


fig = addPlotInfo(plt.gcf(), plotInfo)

Expand Down
10 changes: 7 additions & 3 deletions python/lsst/analysis/drp/scatterPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ def scatterPlotWithTwoHists(self, catPlot, plotInfo, sumStats, yLims=False, xLim
label=r"$\sigma_{MAD}$: " + f"{sigMads[0]:0.2f}")
ax.plot(xs, meds - 1.0*sigMads, color, alpha=0.8)
linesForLegend.append(sigMadLine)
histIm = None

# Set the scatter plot limits
if len(ysStars) > 0:
Expand All @@ -502,9 +503,12 @@ def scatterPlotWithTwoHists(self, catPlot, plotInfo, sumStats, yLims=False, xLim
ax.set_ylim(yLims[0], yLims[1])
else:
if len(ysStars) > 0:
[ys1, ys99] = np.nanpercentile(ysStars, [1, 99])
[ys1, ys99] = np.nanpercentile(ysStars, [1, 99]) # noqa: F841
Copy link
Member Author

Choose a reason for hiding this comment

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

These variables are not used anywhere and makes flake8 unhappy. I could remove this, but I don't want to since I don't know what is was intended for, and I just want to get it in by tonight.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry @erykoff , the force push was introducing these noqa comments since the linting failed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh wow, just delete the whole section then.

Copy link
Contributor

Choose a reason for hiding this comment

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

And how did this ever pass linting? Anyway, these should be deleted.

Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out if you defined something like [ys1, ys99] = [blah, blah], then it's okay according to F841. But if I made it ys1, ys99 = blah, blah (as I do now) and don't use ys1 and ys99, then flake8 is unhappy. That's how it passed the linting previously.

elif len(ysGalaxies) > 0:
[ys1, ys99] = np.nanpercentile(ysGalaxies, [1, 99]) # noqa: F841
else:
[ys1, ys99] = np.nanpercentile(ysGalaxies, [1, 99])
ys1, ys99 = np.nan, np.nan # noqa: F841

numSig = 4
yLimMin = plotMed - numSig*sigMadYs
yLimMax = plotMed + numSig*sigMadYs
Expand Down Expand Up @@ -576,7 +580,7 @@ def scatterPlotWithTwoHists(self, catPlot, plotInfo, sumStats, yLims=False, xLim

sideHist.axes.get_yaxis().set_visible(False)
sideHist.set_xlabel("Number", fontsize=8)
if self.config.plot2DHist:
if self.config.plot2DHist and histIm is not None:
divider = make_axes_locatable(sideHist)
cax = divider.append_axes("right", size="8%", pad=0)
plt.colorbar(histIm, cax=cax, orientation="vertical", label="Number of Points Per Bin")
Expand Down