Skip to content

Commit

Permalink
Handle empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkannawadi committed Mar 16, 2022
1 parent e8198aa commit b2d20e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
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

# 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
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)

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

Expand Down
8 changes: 6 additions & 2 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 @@ -503,8 +504,11 @@ def scatterPlotWithTwoHists(self, catPlot, plotInfo, sumStats, yLims=False, xLim
else:
if len(ysStars) > 0:
[ys1, ys99] = np.nanpercentile(ysStars, [1, 99])
else:
elif len(ysGalaxies) > 0:
[ys1, ys99] = np.nanpercentile(ysGalaxies, [1, 99])
else:
ys1, ys99 = np.nan, np.nan

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

0 comments on commit b2d20e7

Please sign in to comment.