Example #1
0
 def test_GetImageDims(self, mock_getkey):
     c1 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
     c2 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED3_1")
     cubes = [c1, c2]
     new_c = hcs.GetImageDims(cubes, self.conf, None, None)
     self.assertEqual(1, new_c[0].smag)
     self.assertEqual(1, new_c[0].lmag)
Example #2
0
    def test_make_balance(self, mock_getkey, mock_algebra):
        c = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
        c.correction = 1
        hcs.make_balance(c, self.conf["HiccdStitch"],
                         Path("dummy_balance.cub"))
        mock_algebra.assert_has_calls([
            call(
                Path("dummy/PSP_010502_2090_RED5_0"),
                a=1,
                c=0,
                operator="unary",
                to="dummy_balance.cub+SignedWord+0.0:1.5",
            )
        ])

        mock_algebra.reset_mock()
        new_conf = self.conf["HiccdStitch"]
        new_conf["HiccdStitch_Balance_Correction"] = "ADD"
        hcs.make_balance(c, new_conf, Path("dummy_balance.cub"))
        mock_algebra.assert_has_calls([
            call(
                Path("dummy/PSP_010502_2090_RED5_0"),
                a=1,
                c=1,
                operator="unary",
                to="dummy_balance.cub+SignedWord+0.0:1.5",
            )
        ])
Example #3
0
 def test_SNR_Check(self, m_getkey):
     c1 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED1")
     c2 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED2")
     self.assertIsNone(hcs.SNR_Check([c1, c2], 50))
     c1.bin = c2.bin = 1
     c1.snr_list = [5]
     c2.snr_list = [3, 4]
     with self.assertLogs(level="WARN"):
         self.assertIsNone(hcs.SNR_Check([c1, c2], 50))
Example #4
0
 def test_get_correction(self, mock_getkey):
     c5 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5")
     c6 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED6")
     c5.correction = 2
     c5.rstats = 2
     c6.lstats = 2
     self.assertEqual(2, hcs.get_correction(c6, c5, "MULTIPLY", 6))
     self.assertEqual(1, hcs.get_correction(c6, c5, "MULTIPLY", 0))
     self.assertEqual(0, hcs.get_correction(c6, c5, "ADD", 0))
Example #5
0
 def test_crop_and_scale(self, mock_getkey, mock_crop, moc_enlarge):
     c1 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
     c2 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED3_1")
     c1.smag = 1
     c1.lmag = 1
     c2.smag = 1
     c2.lmag = 1
     new_c, to_del = hcs.crop_and_scale([c1, c2])
     p = Path("dummy/PSP_010502_2090_RED5_0.left.crop.cub")
     self.assertEqual(p, new_c[0].ls_path)
     self.assertIn(p, to_del)
Example #6
0
 def test_CubeNormStep(self, mock_crop, mock_cubenorm, mock_getkey,
                       mock_PathSetunlink):
     m = mock_open(read_data=self.cubenorm_data)
     m.return_value.__iter__ = lambda self: self
     m.return_value.__next__ = lambda self: next(iter(self.readline, ""))
     with patch("hiproc.HiccdStitch.open", m):
         c = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
         new_c = hcs.CubeNormStep(c, self.conf["HiccdStitch"])
         self.assertEqual(
             Path("dummy/PSP_010502_2090_RED5_0.cubenorm.cub"),
             new_c.nextpath,
         )
Example #7
0
    def test_set_outpath(self, mock_getkey):
        c1 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
        c2 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED3_1")
        diff = hcs.HiccdStitchCube("tmp/PSP_005632_1225_RED5_1")

        self.assertRaises(ValueError, hcs.set_outpath, "dummy", [c1, c2, diff])

        out = Path("foo")
        self.assertEqual(out, hcs.set_outpath(out, [c1, c2]))

        truth = Path("dummy/PSP_010502_2090_RED.foo")
        self.assertEqual(truth, hcs.set_outpath(".foo", [c1, c2]))
Example #8
0
 def test_SpecialProcessingFlags(self, m_getkey, m_editlab):
     p = Path("dummy/PSP_010502_2090_RED1")
     c = hcs.HiccdStitchCube(p)
     hcs.SpecialProcessingFlags(c)
     m_editlab.assert_has_calls([
         call(
             p,
             grpname="Instrument",
             keyword="Special_Processing_Flag",
             option="MODKEY",
             value="NOMINAL",
         )
     ])
     m_editlab.reset_mock()
     c.hical_status = "BadCal"
     hcs.SpecialProcessingFlags(c)
     m_editlab.assert_has_calls([
         call(
             p,
             grpname="Instrument",
             keyword="Special_Processing_Flag",
             option="MODKEY",
             value="BADCAL",
         )
     ])
     m_editlab.reset_mock()
     c.cubenormstep = True
     hcs.SpecialProcessingFlags(c)
     m_editlab.assert_has_calls([
         call(
             p,
             grpname="Instrument",
             keyword="Special_Processing_Flag",
             option="MODKEY",
             value="CUBENORM",
         )
     ])
     with patch(
             "hiproc.HiccdStitch.isis.getkey_k",
             side_effect=subprocess.CalledProcessError(cmd="foo",
                                                       returncode=1),
     ):
         m_editlab.reset_mock()
         hcs.SpecialProcessingFlags(c)
         m_editlab.assert_has_calls([
             call(
                 p,
                 grpname="Instrument",
                 keyword="Special_Processing_Flag",
                 option="ADDKEY",
                 value="CUBENORM",
             )
         ])
Example #9
0
 def test_get_group_i(self, mock_getkey):
     c3 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED3")
     c5 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5")
     c6 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED6")
     c3.rstats = 1
     c3.lstats = 1
     c5.rstats = 1
     c5.lstats = 1
     c6.lstats = 1
     c6.rstats = 1
     pairs = hcs.get_group_i([c5, c3, c6])
     self.assertListEqual([3], pairs[0][1])
     self.assertListEqual([5, 6], pairs[1][1])
Example #10
0
 def test_set_balance(self, mock_getkey):
     c = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
     c.set_balance(1, 1, {2: [1, 1]}, 1)
     self.assertEqual(2, c.ss_balance_left)
     self.assertEqual(1, c.ns_balance_left)
     self.assertEqual(1023, c.ss_balance_right)
     self.assertEqual(1, c.ns_balance_right)
     self.assertEqual(1, c.sl_balance)
     self.assertEqual(0, c.nl_balance)
Example #11
0
 def test_AnalyzeStats(self):
     m = mock_open(read_data=self.cubenorm_data)
     m.return_value.__iter__ = lambda self: self
     m.return_value.__next__ = lambda self: next(iter(self.readline, ""))
     with patch("hiproc.HiccdStitch.open", m):
         self.assertAlmostEqual(
             2.371842000000029e-06,
             hcs.AnalyzeStats("dummy_cubenormout", "dummy_filtered"),
             places=10,
         )
Example #12
0
 def test_gather_from_db(self, mock_getkey):
     c = hcs.HiccdStitchCube("tmp/PSP_010502_2090_RED5_0")
     db = {"hical_status": "BadCal", "IMAGE_SIGNAL_TO_NOISE_RATIO": 5}
     with patch("hiproc.HiccdStitch.json.load", return_value=db):
         with patch("hiproc.HiccdStitch.Path.glob", return_value=["d",
                                                                  "d"]):
             with patch("hiproc.HiccdStitch.open", mock_open()):
                 c.gather_from_db()
                 self.assertEqual("BadCal", c.hical_status)
                 self.assertListEqual([5.0, 5.0], c.snr_list)
Example #13
0
    def test_set_cubenorm_lines(self, mock_getkey):

        c = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
        self.assertTrue((1.5, 1023.0),
                        c.set_cubenorm_lines(1, 1, None, None, 1))
        self.assertRaises(ValueError, c.set_cubenorm_lines, 1, 1, 2000, None,
                          2)
        self.assertRaises(ValueError, c.set_cubenorm_lines, 1, 1, None, 2000,
                          2)
        self.assertRaises(RuntimeError, c.set_cubenorm_lines, 1, 1, 1000, 800,
                          2)
Example #14
0
 def test_HiccdStitch(
     self,
     m_getkey,
     m_unlink,
     m_CubeNormStep,
     m_Balance,
     m_Special,
     m_hiccdstitch,
     m_SNR,
 ):
     c1 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED1")
     c2 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED2")
     p = "out-dummy/foo.cub"
     hcs.HiccdStitch([c1, c2], p, self.conf)
     m_CubeNormStep.assert_not_called()
     m_Balance.assert_has_calls(
         [call([c1, c2], self.conf["HiccdStitch"], keep=False)])
     m_Special.assert_has_calls([call(c1), call(c2)])
     m_hiccdstitch.assert_called_once()
     m_SNR.assert_has_calls([call([c1, c2], 50)])
Example #15
0
 def test_make_area_dict(self):
     # print(hcs.make_area_dict(c['HiccdStitch']))
     truth = {
         1: [12, 36],
         2: [6, 18],
         3: [5, 15],
         4: [5, 9],
         8: [5, 5],
         16: [1, 2],
     }
     self.assertDictEqual(truth, hcs.make_area_dict(conf["HiccdStitch"]))
Example #16
0
 def test_get_normalization(self, mock_getkey):
     c1 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED1")
     c2 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED2")
     c3 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED3")
     c4 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED4")
     c5 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5")
     c6 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED6")
     c1.correction = 1
     c2.correction = 2
     c3.correction = 3
     c4.correction = 4
     c5.correction = 5
     c6.correction = 6
     self.assertEqual(
         5,
         hcs.get_normalization([c1, c2, c3, c4, c5, c6], [1, 2, 3, 4, 5, 6],
                               -1, [5, 11, 13]),
     )
     self.assertEqual(
         3, hcs.get_normalization([c2, c3, c4], [2, 3, 4], -2, [5, 11, 13]))
Example #17
0
    def test_BalanceStep(
        self,
        m_getkey,
        m_mask,
        m_algebra,
        m_crop,
        m_enlarge,
        m_stats,
        m_unlink,
        m_loads,
    ):
        c1 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED1")
        c2 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED2")
        c3 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED3")
        c4 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED4")
        c5 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5")
        c6 = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED6")

        cubes = hcs.BalanceStep([c1, c2, c3, c4, c5, c6],
                                self.conf["HiccdStitch"])
        self.assertEqual(1, cubes[0].lstats)
        self.assertEqual(1, cubes[0].correction)
        self.assertEqual(Path("dummy/PSP_010502_2090_RED1.balance.cub"),
                         cubes[0].nextpath)
Example #18
0
 def test_set_rs_path(self, mock_getkey):
     c = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
     p = "dummy-path"
     c.set_rs_path(p)
     self.assertEqual(p, c.rs_path)
     self.assertEqual(p, c.rm_path)
Example #19
0
 def test_init(self, mock_getkey):
     c = hcs.HiccdStitchCube("dummy/PSP_010502_2090_RED5_0")
     self.assertTrue(c.ns, 1024)
Example #20
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
Example #21
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
Example #22
0
 def test_conf_check(self):
     self.assertIsNone(hcs.conf_check(conf))