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_make_dummy_IR(self, m_getkey, m_mask, m_editlab):
     r = hci.HiColorCube("dummy/PSP_010502_2090_RED4.HiStitch.balance.cub")
     b = hci.HiColorCube(
         "dummy/PSP_010502_2090_BG12.HiStitch.balance.precolor.cub")
     self.assertEqual(
         sli.make_dummy_IR(r, b),
         Path("dummy/PSP_010502_2090_IR10.slither.cub"),
     )
 def test_cube_check(self, m_getkey):
     r = hci.HiColorCube("dummy/PSP_010502_2090_RED5.HiStitch.balance.cub")
     i = hci.HiColorCube(
         "dummy/PSP_010502_2090_IR11.HiStitch.balance.precolor.cub")
     b = hci.HiColorCube(
         "dummy/PSP_010502_2090_BG13.HiStitch.balance.precolor.cub")
     self.assertTrue(sli.cube_check(r, i, b))
     self.assertTrue(sli.cube_check(r, None, b))
     self.assertFalse(sli.cube_check(None, None, None))
     self.assertFalse(sli.cube_check(None, i, b))
     self.assertFalse(sli.cube_check(r, i, None))
 def test_process_set(self, m_getkey, m_sli, m_hicubeit, m_trim):
     r = hci.HiColorCube("dummy/PSP_010502_2090_RED5.HiStitch.balance.cub")
     i = hci.HiColorCube(
         "dummy/PSP_010502_2090_IR11.HiStitch.balance.precolor.cub")
     b = hci.HiColorCube(
         "dummy/PSP_010502_2090_BG13.HiStitch.balance.precolor.cub")
     self.assertEqual(
         sli.process_set(r, i, b, keep=True),
         Path("dummy/PSP_010502_2090_COLOR5.cub"),
     )
     _, trim_args = m_trim.call_args
     self.assertEqual(trim_args["left"], 3)
Exemple #5
0
def HiJitReg(cube_paths: list, conf: dict, keep=False) -> list:
    cubes = list(map(hicolor.HiColorCube, cube_paths))
    red4, red5, ir10, ir11, bg12, bg13 = hicolor.separate_ccds(cubes)

    ccds = list()
    for c in red4, red5, ir10, ir11, bg12, bg13:
        if c is not None:
            ccds.append(str(c))

    logger.info(f"HiJitReg start: {', '.join(map(str, ccds))}")

    successful_ccds = list()
    if red4 is not None:
        for c in [ir10, bg12]:
            if c is not None:
                if jitter_iter(red4, c, conf, keep=keep):
                    logger.info(f"Iterations completed for {c}")
                    successful_ccds.append(c)
    if red5 is not None:
        for c in [ir11, bg13]:
            if c is not None:
                if jitter_iter(red5, c, conf, keep=keep):
                    logger.info(f"Iterations completed for {c}")
                    successful_ccds.append(c)

    # Not going to check to make sure that at most one pair fails.

    if bg12 not in successful_ccds and bg13 not in successful_ccds:
        raise RuntimeError("Registration failed for both BG halves.")

    logger.info(f"HiJitReg done: {', '.join(map(str, successful_ccds))}")
    return successful_ccds
 def test_get_slither_path(self, m_getkey):
     c = hci.HiColorCube(
         "dummy/PSP_010502_2090_IR10.HiStitch.balance.precolor.cub")
     self.assertEqual(
         sli.get_slither_path(c),
         Path("dummy/PSP_010502_2090_IR10.slither.cub"),
     )
 def test_HiColorInit_reduce2(self, m_getkey, m_unlink, m_editlab,
                              m_handmos, m_enlarge, m_reduce):
     red = Path("dummy/PSP_010502_2090_RED4")
     ir = Path("dummy/PSP_010502_2090_IR10")
     s = ".dummy.cub"
     hci.HiColorInit([red, ir], s)
     m_reduce.assert_called_once()
     self.assertAlmostEqual(1.0006, m_reduce.call_args[1]["sscale"])
     m_enlarge.assert_not_called()
     m_unlink.assert_called_once()
 def test_HiJitReg(self, m_jit_it, m_getkey):
     c04 = Path("dummy/PSP_010502_2090_RED4")
     c05 = Path("dummy/PSP_010502_2090_RED5")
     c10 = Path("dummy/PSP_010502_2090_IR10")
     c11 = Path("dummy/PSP_010502_2090_IR11")
     c12 = Path("dummy/PSP_010502_2090_BG12")
     c13 = Path("dummy/PSP_010502_2090_BG13")
     conf = dict(dummy="yes")
     # self.assertRaises(
     #     RuntimeError, hjr.HiJitReg, [c04, c05, c10, c11, c12, c13], conf
     # )
     self.assertListEqual([hci.HiColorCube(c12)],
                          hjr.HiJitReg([c04, c12, c13], conf))
     self.assertListEqual(
         [
             hci.HiColorCube(c10),
             hci.HiColorCube(c12),
             hci.HiColorCube(c11),
             hci.HiColorCube(c13)
         ],
         hjr.HiJitReg([c04, c05, c10, c11, c12, c13], conf),
     )
Exemple #9
0
def process_set(
    red: hicolor.HiColorCube,
    ir: hicolor.HiColorCube,
    bg: hicolor.HiColorCube,
    keep=False,
):
    if red is None:
        raise TypeError("Expected a RED HiColorCube, but got None.")
    if bg is None:
        raise TypeError("Expected a BG HiColorCube, but got None.")

    logger.info("Beginning HiSlither for {}".format(str(red)))

    temp_token = datetime.now().strftime("HiSlither-%y%m%d%H%M%S")

    if ir is None:
        # If the IR band is missing, it is filled with a "dummy" band
        # consisting entirely of null pixels. This band is created by
        # masking the red band with itself. The dummy band allows for
        # RGB (synthetic B) to be created by HiBeautify even if the
        # IR band is missing.
        logger.info("Missing IR, creating a null band.")
        ir_slither_p = make_dummy_IR(red, bg)
        ir = hicolor.HiColorCube(ir_slither_p)
    else:
        ir_slither_p = run_slither(ir)

    bg_slither_p = run_slither(bg)

    logger.info("Slither finished, merging stacks.")
    logger.info("Creating {}-{}-{} mosaic".format(ir.get_ccd(), red.get_ccd(),
                                                  bg.get_ccd()))

    stacked_path = red.path.parent / "{}_COLOR{}.cub".format(
        red.get_obsid(), red.ccdnumber)
    tmp_stack = stacked_path.with_suffix(f".{temp_token}.cub")
    isis.hicubeit(ir=ir_slither_p, red=red.path, bg=bg_slither_p, to=tmp_stack)

    trim_args = {"from": tmp_stack, "to": stacked_path}
    # trim 5 px in bin 1, 3 in bin 2, 2 in bin 4
    trim = math.ceil(5 / red.bin)
    if red.ccdnumber == "4":
        trim_args["right"] = trim
    else:
        trim_args["left"] = trim
    isis.trim(**trim_args)

    if not keep:
        tmp_stack.unlink()

    return stacked_path
 def test_HiColorInit_reduce1(self, m_getkey, m_unlink, m_editlab,
                              m_handmos, m_enlarge, m_reduce):
     red = Path("dummy/PSP_010502_2090_RED4")
     ir = Path("dummy/PSP_010502_2090_IR10")
     bg = Path("dummy/PSP_010502_2090_BG12")
     suffix = ".dummy.cub"
     hci.HiColorInit([red, ir, bg], suffix, keep=True)
     self.assertEqual(2, m_reduce.call_count)
     self.assertAlmostEqual(m_reduce.call_args[1]["sscale"], 1.0006)
     m_enlarge.assert_not_called()
     self.assertEqual(2, m_editlab.call_count)
     self.assertTrue(str(m_editlab.call_args[0][0]).endswith(suffix))
     self.assertAlmostEqual(m_handmos.call_args[1]["nlines"], 1024)
     m_unlink.assert_not_called()
    def test_separate_ccds(self, mock_getkey):
        c04 = hci.HiColorCube("dummy/PSP_010502_2090_RED4")
        c05 = hci.HiColorCube("dummy/PSP_010502_2090_RED5")
        c10 = hci.HiColorCube("dummy/PSP_010502_2090_IR10")
        c11 = hci.HiColorCube("dummy/PSP_010502_2090_IR11")
        c12 = hci.HiColorCube("dummy/PSP_010502_2090_BG12")
        c13 = hci.HiColorCube("dummy/PSP_010502_2090_BG13")

        (red4, red5, ir10, ir11, bg12,
         bg13) = hci.separate_ccds([c04, c05, c10, c11, c12, c13])
        self.assertEqual(c04, red4)
        self.assertEqual(c05, red5)
        self.assertEqual(c10, ir10)
        self.assertEqual(c11, ir11)
        self.assertEqual(c12, bg12)
        self.assertEqual(c13, bg13)
Exemple #12
0
def HiSlither(cube_paths: list, keep=False):
    cubes = list(map(hicolor.HiColorCube, cube_paths))
    (red4, red5, ir10, ir11, bg12, bg13) = hicolor.separate_ccds(cubes)

    ccds = list()
    for c in red4, red5, ir10, ir11, bg12, bg13:
        if c is not None:
            ccds.append(str(c))

    logger.info(f"HiSlither start: {', '.join(map(str, ccds))}")

    completed = list()
    if cube_check(red4, ir10, bg12):
        completed.append(str(process_set(red4, ir10, bg12, keep=keep)))

    if cube_check(red5, ir11, bg13):
        completed.append(str(process_set(red5, ir11, bg13, keep=keep)))

    logger.info(f"HiSlither done: {', '.join(completed)}")

    return
Exemple #13
0
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_run_slither(self, m_getkey, m_slither):
     c = hci.HiColorCube(
         "dummy/PSP_010502_2090_IR10.HiStitch.balance.precolor.cub")
     sli.run_slither(c)
     m_slither.called_once()
Exemple #15
0
 def test_badinit(self, mock_getkey):
     c = hci.HiColorCube("dummy/PSP_010502_2090_RED4_0")
     self.assertRaises(KeyError, hjr.JitterCube, c)
Exemple #16
0
def edr2stitch(images, conf_dir, bitflipwidth=0, lis_tolerance=1, keep=False):
    chids = list()
    for i in images:
        out_edr = util.path_w_suffix(".EDR_Stats.cub", i)

        # EDR_Stats
        db = EDR_Stats.EDR_Stats(i,
                                 out_edr,
                                 pvl.load(conf_dir /
                                          "EDR_Stats_gains_config.pvl"),
                                 keep=keep)

        # HiCal
        out_hical = util.path_w_suffix(".HiCal.cub", out_edr)

        db = HiCal.HiCal(
            out_edr,
            out_hical,
            db,
            HiCal.conf_setup(pvl.load(conf_dir / "HiCal.conf"),
                             pvl.load(conf_dir / "NoiseFilter.conf")),
            conf_dir / "HiCal.conf",
            None,
            None,
            bitflipwidth,
            lis_tolerance,
            keep=keep,
        )

        chids.append(ChannelCube(out_hical, db))

    # HiStitch
    # get Channel pairs
    cids = list()
    for chid1, chid2 in get_CCDpairs(chids):
        (db, o_path) = HiStitch.HiStitch(
            chid1.nextpath,
            chid2.nextpath,
            chid1.db,
            chid2.db,
            ".HiStitch.cub",
            pvl.load(conf_dir / "HiStitch.conf"),
            keep=keep,
        )
        cid = HiccdStitch.HiccdStitchCube(o_path)
        cid.gather_from_db(db)
        cids.append(cid)

    # HiccdStitch, makes balance cubes
    # need to separate by color:
    color_groups = get_color_groups(cids)
    for color_group in color_groups.values():
        db, out_stitch = HiccdStitch.HiccdStitch(
            color_group,
            ".HiccdStitch.cub",
            pvl.load(conf_dir / "HiccdStitch.conf"),
            sline=None,
            eline=None,
            keep=keep,
        )
    # HiColorInit
    #   takes *balance.cub
    #   creates *[IR|BG]*.balance.precolor.cub
    #   Can then run JitPlot on these *.balance.precolor.cub
    HiColorInit.HiColorInit([c.nextpath for c in cids],
                            ".precolor.cub",
                            keep=keep)

    # HiJitReg
    #   takes tmp/*balance.cub tmp/*balance.precolor.cub
    #   creates *regdef.pvl and *flat.tab files
    for_jitreg = list()
    for color, balcubes in color_groups.items():
        if color == "RED":
            for c in balcubes:
                for_jitreg.append(c.nextpath)
        else:
            for c in balcubes:
                for_jitreg.append(c.nextpath.with_suffix(".precolor.cub"))

    HiJitReg.HiJitReg(for_jitreg,
                      pvl.load(conf_dir / "HiJitReg.conf"),
                      keep=keep)

    # HiSlither
    #   takes same as HiJitReg (and assumes its products are available.
    #   creates *slither.txt, *slither.cub, and *COLOR[4|5].cub
    #   Can then run SliterStats on the *slither.txt
    HiSlither.HiSlither(for_jitreg)

    return chids
Exemple #17
0
def edr2stitch(
    images,
    conf_dir,
    bitflipwidth=0,
    lis_tolerance=1.0,
    max_workers=None,
    keep=False,
):
    chids = list()
    edr_conf = pvl.load(conf_dir / "EDR_Stats_gains_config.pvl")
    hical_conf = HiCal.conf_setup(
        pvl.load(conf_dir / "HiCal.conf"),
        pvl.load(conf_dir / "NoiseFilter.conf"),
    )
    with concurrent.futures.ProcessPoolExecutor(
                max_workers=max_workers
    ) as executor:
        future_dbs = dict()
        for i in images:
            out_edr = util.path_w_suffix(".EDR_Stats.cub", i)
            out_hical = util.path_w_suffix(".HiCal.cub", out_edr)

            f = executor.submit(
                edr_cal,
                i,
                out_edr,
                out_hical,
                edr_conf,
                hical_conf,
                conf_dir / "HiCal.conf",
                bitflipwidth=bitflipwidth,
                lis_tolerance=lis_tolerance,
                keep=False,
            )
            future_dbs[f] = out_hical

        for future in concurrent.futures.as_completed(future_dbs):
            out_hical = future_dbs[future]
            chids.append(ChannelCube(out_hical, future.result()))

    # HiStitch
    # get Channel pairs
    cids = list()
    stitch_conf = pvl.load(conf_dir / "HiStitch.conf")
    with concurrent.futures.ProcessPoolExecutor(
        max_workers=max_workers
    ) as executor:
        future_tuples = list()
        for chid1, chid2 in get_CCDpairs(chids):
            f = executor.submit(
                # (db, o_path) = HiStitch.HiStitch(
                HiStitch.HiStitch,
                chid1.nextpath,
                chid2.nextpath,
                chid1.db,
                chid2.db,
                ".HiStitch.cub",
                stitch_conf,
                keep=keep,
            )
            future_tuples.append(f)

        for future in concurrent.futures.as_completed(future_tuples):
            (db, o_path) = future.result()
            cid = HiccdStitch.HiccdStitchCube(o_path)
            cid.gather_from_db(db)
            cids.append(cid)

    # HiccdStitch, makes balance cubes
    # need to separate by color:
    color_groups = get_color_groups(cids)
    for color_group in color_groups.values():
        db, out_stitch = HiccdStitch.HiccdStitch(
            color_group,
            ".HiccdStitch.cub",
            pvl.load(conf_dir / "HiccdStitch.conf"),
            sline=None,
            eline=None,
            keep=keep,
        )
    # HiColorInit
    #   takes *balance.cub
    #   creates *[IR|BG]*.balance.precolor.cub
    #   Can then run JitPlot on these *.balance.precolor.cub
    HiColorInit.HiColorInit(
        [c.nextpath for c in cids], ".precolor.cub", keep=keep
    )

    # HiJitReg
    #   takes tmp/*balance.cub tmp/*balance.precolor.cub
    #   creates *regdef.pvl and *flat.tab files
    for_jitreg = list()
    for color, balcubes in color_groups.items():
        if color == "RED":
            for c in balcubes:
                for_jitreg.append(c.nextpath)
        else:
            for c in balcubes:
                for_jitreg.append(c.nextpath.with_suffix(".precolor.cub"))

    HiJitReg.HiJitReg(
        for_jitreg, pvl.load(conf_dir / "HiJitReg.conf"), keep=keep
    )

    # HiSlither
    #   takes same as HiJitReg (and assumes its products are available.
    #   creates *slither.txt, *slither.cub, and *COLOR[4|5].cub
    #   Can then run SlitherStats on the *slither.txt
    HiSlither.HiSlither(for_jitreg)

    return chids
Exemple #18
0
 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"))
 def test_init(self, mock_getkey):
     c = hci.HiColorCube("dummy/PSP_010502_2090_RED5_0")
     self.assertTrue(c.tdi, 64)