def setUp(self, mock_getkey): self.r = hci.HiColorCube("dummy/PSP_010502_2090_RED4_0") self.c = hci.HiColorCube("dummy/PSP_010502_2090_IR10_0") self.j = hjr.JitterCube(self.c) self.conf = { "AutoRegistration": { "ControlNet": { "Control_Cols": 4, "Control_Lines": 200 }, "Algorithm": { "Tolerance": 0.5, "Increment": 0.1 }, "PatternChip": { "Samples": 100, "Lines": 60 }, "SearchChip": { "Samples": 120, "Lines": 100 }, "SearchLongerChip": { "Samples": 130, "Lines": 140 }, }, "Smoothing": { "Exclude_Limit": 2, "Badness_Limit": 1, "Boxcar_Length": 10, }, }
def test_parseFlatTab(self, mock_getkey): c = hjr.JitterCube("dummy/PSP_010502_2090_IR10_0") with patch("hiproc.HiJitReg.pvl.load", return_value=reg_def): c.parseRegDefs() with patch("hiproc.HiJitReg.open", mock_open(read_data=flat_tab_text)): c.parseFlatTab() self.assertEqual(c["MatchedLineCount"], 5) self.assertEqual(c["AvgSampleOffset"], 0.4470)
def test_parseCNetPVL(self, mock_getkey): c = hjr.JitterCube("dummy/PSP_010502_2090_IR10_0") with patch("hiproc.HiJitReg.pvl.load", return_value=reg_def): c.parseRegDefs() with patch("hiproc.HiJitReg.open", mock_open(read_data=flat_tab_text)): c.parseFlatTab() with patch("hiproc.HiJitReg.pvl.load", return_value=cnet_pvl): c.parseCNetPVL() self.assertFalse(c["CanSlither"]) self.assertEqual(c["MatchedLineCount"], 1)
def test_filterCNetPVL(self, mock_getkey): c = hjr.JitterCube("dummy/PSP_010502_2090_IR10_0") with patch("hiproc.HiJitReg.pvl.load", return_value=reg_def): c.parseRegDefs() with patch("hiproc.HiJitReg.open", mock_open(read_data=flat_tab_text)): c.parseFlatTab() c.IgnoredPoints.add("Row 2 Column 0") with patch("hiproc.HiJitReg.pvl.load", return_value=cnet_pvl): with patch("hiproc.HiJitReg.open", mock_open()) as m: c.filterCNetPVL() self.assertEqual( str(m().write.call_args[0][0]).count("Ignore"), 2)
def make_flats(cubes, common_cube, conf, temp_token, keep=False): # If the flat files already exist, don't remake them. # "$OBS_ID"."_".$ccd."-".$common.".flat.tab" jitter_cubes = list() n_row = int( common_cube.lines / conf["AutoRegistration"]["ControlNet"]["Control_Lines"] ) for c in cubes: if c == common_cube: continue jitter_cubes.append( hjr.JitterCube( c.next_path, matchccd=common_cube.get_ccd(), config=conf ) ) successful_flats = list() if not all(x.flattab_path.exists() for x in jitter_cubes): confauto = conf["AutoRegistration"] for c in jitter_cubes: params = { "ROWS": n_row, "TOLERANCE": confauto["Algorithm"]["Tolerance"], } min_fraction_good = confauto["AnaylyzeFlat"]["Minimum_Good"] if c.ccdname == "RED": redcolor = "Red" else: redcolor = "Color" params["GROUP"] = "ResolveJitter" params["COLS"] = confauto["ControlNet"]["Control_Cols_" + redcolor] params["PATTERN_SAMPLES"] = confauto["PatternChip" + redcolor][ "Samples" ] params["PATTERN_LINES"] = confauto["PatternChip" + redcolor][ "Lines" ] params["SEARCH_SAMPLES"] = confauto["SearchChip" + redcolor][ "Samples" ] params["SEARCH_LINES"] = confauto["SearchChip" + redcolor]["Lines"] if c.ccdname != "RED": channels = isis.getkey_k( c.path, "Instrument", "StitchedProductIds" ) if len(channels) < 2: logger.info( f"Increasing columns because {c.path} is " "missing a channel." ) params["COLS"] += 1 min_fraction_good *= 0.5 logger.info( "The minimum allowable Fraction Good " f"Matches = {min_fraction_good}" ) step = 0 while step <= confauto["Algorithm"]["Steps"]: logger.info(f"Step {step} begin") hjr.run_HiJitReg( common_cube.next_path, c, params, temp_token, keep=keep ) ret = hjr.Analyze_Flat( c, 0, (min_fraction_good * 2), hijitreg=False ) if ret == 1: successful_flats.append(c.flattab_path) break else: step += 1 c.regdef_path.unlink() c.flattab_path.unlink() c.cnet_path.unlink() params["TOLERANCE"] -= ( confauto["Algorithm"]["Increment"] * step ) else: raise RuntimeError( f"Flat file for {c} is not within tolerances." ) else: successful_flats = list(x.flattab_path for x in jitter_cubes) return successful_flats
def main(): args = arg_parser().parse_args() util.set_logger(args.verbose, args.logfile, args.log) conf = pvl.load(args.conf) with util.main_exceptions(args.verbose): cubes = list(map(hicolor.HiColorCube, args.cubes)) (red4, red5, ir10, ir11, bg12, bg13) = hicolor.separate_ccds(cubes) plt.ioff() fig, axes = plt.subplots(1, 4, sharex=True, sharey=True) axes[0].set_ylabel("Line") fig.text(0.5, 0.04, "Error Magnitude (pixels)", ha="center", va="center") for c, ax in zip([ir10, ir11, bg12, bg13], axes): if c is None: continue logger.info(f"Working on {c}") j = hjr.JitterCube(c, conf) j.reset() (accepted, rejected, smoothed) = filter_and_smooth(j) ax.set_title(j.get_ccd()) size = 5 if len(accepted) > 0: acceptA = np.array(accepted) ax.scatter( acceptA[:, 1], acceptA[:, 0], s=size, label="Accepted", facecolors="none", edgecolors="black", ) if len(rejected) > 0: rejectedA = np.array(rejected) ax.scatter( rejectedA[:, 1], rejectedA[:, 0], c="red", s=size, label="Rejected", ) if len(smoothed) > 0: smoothedA = np.array(smoothed) ax.scatter( smoothedA[:, 1], smoothedA[:, 0], c="blue", s=size, label="Smoothed", ) fig.suptitle(str(j.get_obsid())) bottom, top = plt.ylim() plt.ylim(top, bottom) plt.xlim(0, 10) plt.legend(loc="upper right", bbox_to_anchor=(1, -0.05), ncol=3) plt.show() return
def test_parseCNetPVL_file(self): c = hjr.JitterCube( "tmp/PSP_010502_2090_IR10.HiStitch.balance.precolor.cub") c.parseRegDefs() c.parseFlatTab() c.parseCNetPVL("tmp/test.pvl")
def test_parseRegDefs(self, mock_getkey): c = hjr.JitterCube("dummy/PSP_010502_2090_IR10_0") with patch("hiproc.HiJitReg.pvl.load", return_value=reg_def): c.parseRegDefs() self.assertEqual(c["PatternSamples"], 100)
def test_dict(self, mock_getkey): c = hjr.JitterCube("dummy/PSP_010502_2090_IR10_0") self.assertEqual(c["bin"], c.bin) self.assertEqual(c["ExcludeLimit"], 2)
def test_init(self, mock_getkey): c = hci.HiColorCube("dummy/PSP_010502_2090_IR10_0") j = hjr.JitterCube(c) self.assertEqual(j.cnet_path, Path("dummy/PSP_010502_2090_RED4-IR10.control.pvl"))