def test_scheduleRevisit(self):
        """Runs scheduleRevisit method
        """

        exclude_mods = []

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue

            if 'scheduleRevisit' in mod.__dict__:
                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)
                    sInd = [0]
                    smin = None
                    det = 0
                    pInds = [0]
                    sim.scheduleRevisit(sInd,smin,det,pInds)
    def test_revisitFilter(self):
        r"""Test revisitFilter method
        """

        exclude_mods = []
        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue

            if 'revisitFilter' in mod.__dict__:
                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')
                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)

                    sInds = np.asarray([0])
                    tovisit = np.zeros(sim.TargetList.nStars, dtype=bool)

                    sInds = sim.revisitFilter(sInds,sim.TimeKeeping.currentTimeNorm)
                try:
                    self.assertIsInstance(sInds, np.ndarray)
                except:
                    self.assertIsInstance(sInds, type(list()))
Esempio n. 3
0
    def test_calc_signal_noise(self):
        r"""Test calc_signal_noise method.

        Approach: Ensure that signal is greater than noise for dummy planet
        """

        exclude_mods = []

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue
            if 'calc_signal_noise' in mod.__dict__:

                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)

                    S,N = sim.calc_signal_noise(np.array([0]), np.array([0]), 1.0*u.d,
                                                sim.OpticalSystem.observingModes[0],
                                                fZ=np.array([0.0])/u.arcsec**2,
                                                fEZ=np.array([0.0])/u.arcsec**2,
                                                dMag=np.array([20]), WA=np.array([0.5])*u.arcsec)

                self.assertGreaterEqual(S,N)
Esempio n. 4
0
    def test_scheduleRevisit(self):
        """Runs scheduleRevisit method
        """

        exclude_mods = []

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue

            if 'scheduleRevisit' in mod.__dict__:
                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path(
                        'test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path(
                        'SurveySimulation/top100stars.txt')

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)
                    sInd = [0]
                    smin = None
                    det = 0
                    pInds = [0]
                    sim.scheduleRevisit(sInd, smin, det, pInds)
Esempio n. 5
0
    def test_revisitFilter(self):
        r"""Test revisitFilter method
        """

        exclude_mods = []
        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue

            if 'revisitFilter' in mod.__dict__:
                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path(
                        'test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path(
                        'SurveySimulation/top100stars.txt')
                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)

                    sInds = np.asarray([0])
                    tovisit = np.zeros(sim.TargetList.nStars, dtype=bool)

                    sInds = sim.revisitFilter(sInds,
                                              sim.TimeKeeping.currentTimeNorm)
                try:
                    self.assertIsInstance(sInds, np.ndarray)
                except:
                    self.assertIsInstance(sInds, type(list()))
Esempio n. 6
0
    def test_calc_targ_intTime(self):
        """Test calc_targ_intTime method.
        Checks that proper outputs are given (length and units).
        """

        exclude_mods = []

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue
            if 'calc_targ_intTime' in mod.__dict__:

                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')
                if 'KnownRV' in mod.__name__:
                    spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                    spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                    spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)
                    startTimes = sim.TimeKeeping.currentTimeAbs.copy() + np.zeros(sim.TargetList.nStars)*u.d
                    sInds = np.arange(sim.TargetList.nStars)
                    mode = list(filter(lambda mode: mode['detectionMode'] == True, sim.OpticalSystem.observingModes))[0]
                    intTimes = sim.calc_targ_intTime(sInds, startTimes, mode)
                self.assertTrue(len(intTimes) == len(sInds), 'calc_targ_intTime returns incorrect number of intTimes for %s'%mod.__name__)
                self.assertTrue(intTimes.unit == u.d, 'calc_targ_intTime returns incorrect unit for %s'%mod.__name__)
Esempio n. 7
0
 def setUp(self):
     self.dev_null = open(os.devnull,'w')
     # use template_minimal.json
     self.script1path = resource_path('test-scripts/template_minimal.json')
     self.script1dict = json.loads(open(self.script1path).read())
     self.script2path = resource_path('test-scripts/template_prototype_testing.json')
     self.script2dict = json.loads(open(self.script2path).read())
     self.script3path = resource_path('test-scripts/template_completeness_testing.json')
     self.script3dict = json.loads(open(self.script3path).read())
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        self.spec = json.loads(open(self.script).read())
        self.spec['modules']['PlanetPopulation'] = 'AlbedoByRadius'
        self.spec['prange'] = [0.1, 0.5]
        self.spec['Rprange'] = [1, 10]
        self.script2 = resource_path('test-scripts/simplest.json')
        self.spec2 = json.loads(open(self.script2).read())
 def setUp(self):
     self.dev_null = open(os.devnull,'w')
     # use template_minimal.json
     self.script1path = resource_path('test-scripts/template_minimal.json')
     self.script1dict = json.loads(open(self.script1path).read())
     self.script2path = resource_path('test-scripts/template_prototype_testing.json')
     self.script2dict = json.loads(open(self.script2path).read())
     self.script3path = resource_path('test-scripts/template_completeness_testing.json')
     self.script3dict = json.loads(open(self.script3path).read())
     self.script4path = resource_path('test-scripts/incorrectly_formatted.json')
Esempio n. 10
0
    def test_next_target(self):
        r"""Test next_target method.

        Approach: Ensure the next target is selected OK, and is a valid integer.
        Deficiencies: We are not checking that the occulter slew works.
        """

        exclude_mods = ['SS_det_only']
        exclude_mod_type = 'sotoSS'

        for mod in self.allmods:
            if mod.__name__ in exclude_mods or exclude_mod_type in mod.__name__:
                continue

            if 'next_target' in mod.__dict__:
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')
                    with RedirectStreams(stdout=self.dev_null):
                        sim = mod(**spec)
                        if 'tieredScheduler_DD' in mod.__name__:
                            DRM_out, sInd, occ_sInd, intTime, sd, occ_sInds, det_mode = sim.next_target(None, None, 
                                        sim.OpticalSystem.observingModes, sim.OpticalSystem.observingModes[0])
                        else:
                            DRM_out, sInd, occ_sInd, intTime, sd, occ_sInds = sim.next_target(None, None, 
                                        sim.OpticalSystem.observingModes[0], sim.OpticalSystem.observingModes[0])
                        self.assertIsInstance(occ_sInd, (int,np.int8,np.int16,np.int32,np.int64), 'occ_sInd is not an integer for %s'%mod.__name__)
                        self.assertEqual(occ_sInd - int(occ_sInd), 0, 'occ_sInd is not an integer for %s'%(mod.__name__))
                        self.assertGreaterEqual(occ_sInd, 0, 'occ_sInd is not a valid index for %s'%mod.__name__)
                        self.assertLess(occ_sInd, sim.TargetList.nStars, 'occ_sInd is not a valid index for %s'%mod.__name__)
                elif 'linearJScheduler_DDPC' in mod.__name__ or 'linearJScheduler_3DDPC' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_3DDPC.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    with RedirectStreams(stdout=self.dev_null):
                        sim = mod(**spec)
                        DRM_out, sInd, intTime, waitTime, det_mode = sim.next_target(None, sim.OpticalSystem.observingModes)
                else:
                    with RedirectStreams(stdout=self.dev_null):
                        sim = mod(scriptfile=self.script)

                        DRM_out, sInd, intTime, waitTime = sim.next_target(None, sim.OpticalSystem.observingModes[0])

                # result index is a scalar numpy ndarray, that is a valid integer
                # in a valid range
                self.assertIsInstance(sInd, (int,np.int8,np.int16,np.int32,np.int64), 'sInd is not an integer for %s'%mod.__name__)
                self.assertEqual(sInd - int(sInd), 0, 'sInd is not an integer for %s'%mod.__name__)
                self.assertGreaterEqual(sInd, 0, 'sInd is not a valid index for %s'%mod.__name__)
                self.assertLess(sInd, sim.TargetList.nStars, 'sInd is not a valid index for %s'%mod.__name__)

                # resulting DRM is a dictionary -- contents unimportant
                self.assertIsInstance(DRM_out, dict, 'DRM_out is not a dict for %s'%mod.__name__)
Esempio n. 11
0
    def setUp(self):
        # print '[setup] ',
        # do not instantiate it
        self.fixture = TimeKeeping

        self.dev_null = open(os.devnull, 'w')
        self.script1 = resource_path('test-scripts/simplest.json')
        self.script2 = resource_path('test-scripts/simplest_initOB.json')
    
        modtype = getattr(SurveySimulation,'_modtype')
        self.allmods = [get_module(modtype)]
Esempio n. 12
0
    def setUp(self):
        # print '[setup] ',
        # do not instantiate it
        self.fixture = TimeKeeping

        self.dev_null = open(os.devnull, 'w')
        self.script1 = resource_path('test-scripts/simplest.json')
        self.script2 = resource_path('test-scripts/simplest_initOB.json')

        modtype = getattr(SurveySimulation, '_modtype')
        self.allmods = [get_module(modtype)]
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())
        self.spec['modules']['PlanetPopulation'] = 'AlbedoByRadius'
        self.spec['prange'] = [0.1,0.5]
        self.spec['Rprange'] = [1,10]
        self.script2 = resource_path('test-scripts/simplest.json')
        with open(self.script2) as f:
            self.spec2 = json.loads(f.read())
Esempio n. 14
0
    def test_init_iwa_owa_throughput(self):
        r"""Test of initialization and __init__ -- IWA, OWA vs. throughput domain constraint.

        Method: We instantiate OpticalSystem objects and verify 
        that IWA and OWA vary as expected with the domain of WA of the throughput
        lookup table (from 0 to 1).  
        """
        filename = os.path.join(resource_path(), 'OpticalSystem', 'i_quad100.fits')
    
        our_specs = deepcopy(specs_default)
        for (IWA,OWA) in zip([0.0,0.2,0.5,1.1],[1.0,1.4,1.6,2.0]):
            for syst in our_specs['starlightSuppressionSystems']:
                syst['core_thruput'] = filename
                syst['IWA'] = IWA
                syst['OWA'] = OWA
            if IWA <= 1.0:
                optsys = self.fixture(**deepcopy(our_specs))
                # Check that the range constraint of the throughput lookup table
                # (it covers WA in 0 to 1) does constrain the system OWA and IWA.
                self.assertTrue(optsys.OWA.value == min(1.0, OWA))
                self.assertTrue(optsys.IWA.value == max(0.0, IWA))
            else:
                # IWA > 1 but lookup table covers [0,1] -- conflict
                with self.assertRaises(AssertionError):
                    optsys = self.fixture(**deepcopy(our_specs))
Esempio n. 15
0
    def test_init_iwa_owa_contrast(self):
        r"""Test of initialization and __init__ -- IWA, OWA vs. contrast domain constraint.

        Method: We instantiate OpticalSystem objects and verify 
        that IWA and OWA vary as expected with the domain of WA of the contrast
        lookup table (from 0 to 1).
        """
        filename = os.path.join(resource_path(), 'OpticalSystem',
                                'i_quad100.fits')
        Cmin = 0.25  # the minimum of the above lookup table is 0.25
        expected_dmaglim = -2.5 * np.log10(Cmin)
        # the input dict is modified in-place -- so copy it
        our_specs = deepcopy(specs_default)
        for (IWA, OWA) in zip([0.0, 0.2, 0.5, 1.1], [1.0, 1.4, 1.6, 2.0]):
            for syst in our_specs['starlightSuppressionSystems']:
                syst['core_contrast'] = filename
                syst['IWA'] = IWA
                syst['OWA'] = OWA
            if IWA <= 1.0:
                optsys = self.fixture(**deepcopy(our_specs))
                # Check that the range constraint of the contrast lookup table
                # (it covers WA in 0 to 1) does constrain the system OWA and IWA.
                self.assertTrue(optsys.OWA.value == min(1.0, OWA),
                                msg='contrast lookup table OWA')
                self.assertTrue(optsys.IWA.value == max(0.0, IWA),
                                msg='contrast lookup table IWA')
            else:
                # IWA > 1 but lookup table covers [0,1] -- conflict
                with self.assertRaises(AssertionError):
                    optsys = self.fixture(**deepcopy(our_specs))
Esempio n. 16
0
    def test_init_iwa_owa_throughput(self):
        r"""Test of initialization and __init__ -- IWA, OWA vs. throughput domain constraint.

        Method: We instantiate OpticalSystem objects and verify 
        that IWA and OWA vary as expected with the domain of WA of the throughput
        lookup table (from 0 to 1).  
        """
        filename = os.path.join(resource_path(), 'OpticalSystem',
                                'i_quad100.fits')

        our_specs = deepcopy(specs_default)
        for (IWA, OWA) in zip([0.0, 0.2, 0.5, 1.1], [1.0, 1.4, 1.6, 2.0]):
            for syst in our_specs['starlightSuppressionSystems']:
                syst['core_thruput'] = filename
                syst['IWA'] = IWA
                syst['OWA'] = OWA
            if IWA <= 1.0:
                optsys = self.fixture(**deepcopy(our_specs))
                # Check that the range constraint of the throughput lookup table
                # (it covers WA in 0 to 1) does constrain the system OWA and IWA.
                self.assertTrue(optsys.OWA.value == min(1.0, OWA))
                self.assertTrue(optsys.IWA.value == max(0.0, IWA))
            else:
                # IWA > 1 but lookup table covers [0,1] -- conflict
                with self.assertRaises(AssertionError):
                    optsys = self.fixture(**deepcopy(our_specs))
Esempio n. 17
0
    def setUp(self):
        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_prototype_testing.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())

        with RedirectStreams(stdout=self.dev_null):
            self.sim = MissionSim.MissionSim(self.script)
        self.TL = self.sim.TargetList
        self.nStars = self.TL.nStars
        self.star_index = np.array(range(0, self.nStars))
        self.Obs = self.sim.Observatory
        self.mode = self.sim.OpticalSystem.observingModes[0]
        self.TK = self.sim.TimeKeeping
        assert self.nStars > 10, "Need at least 10 stars in the target list for the unit test."
        self.unit = 1./u.arcsec**2

        modtype = getattr(EXOSIMS.Prototypes.ZodiacalLight.ZodiacalLight, '_modtype')
        pkg = EXOSIMS.ZodiacalLight
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__ + '.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1], modtype)
                self.assertTrue(mod._modtype is modtype, '_modtype mismatch for %s' % mod.__name__)
                self.allmods.append(mod)
Esempio n. 18
0
    def test_init_iwa_owa_contrast(self):
        r"""Test of initialization and __init__ -- IWA, OWA vs. contrast domain constraint.

        Method: We instantiate OpticalSystem objects and verify 
        that IWA and OWA vary as expected with the domain of WA of the contrast
        lookup table (from 0 to 1).
        """
        filename = os.path.join(resource_path(), 'OpticalSystem', 'i_quad100.fits')
        Cmin = 0.25 # the minimum of the above lookup table is 0.25
        expected_dmaglim = -2.5 * np.log10(Cmin)
        # the input dict is modified in-place -- so copy it
        our_specs = deepcopy(specs_default)
        for (IWA,OWA) in zip([0.0,0.2,0.5,1.1],[1.0,1.4,1.6,2.0]):
            for syst in our_specs['starlightSuppressionSystems']:
                syst['core_contrast'] = filename
                syst['IWA'] = IWA
                syst['OWA'] = OWA
            if IWA <= 1.0:
                optsys = self.fixture(**deepcopy(our_specs))
                # Check that the range constraint of the contrast lookup table
                # (it covers WA in 0 to 1) does constrain the system OWA and IWA.
                self.assertTrue(optsys.OWA.value == min(1.0, OWA),
                               msg='contrast lookup table OWA')
                self.assertTrue(optsys.IWA.value == max(0.0, IWA),
                               msg='contrast lookup table IWA')
            else:
                # IWA > 1 but lookup table covers [0,1] -- conflict
                with self.assertRaises(AssertionError):
                    optsys = self.fixture(**deepcopy(our_specs))
Esempio n. 19
0
    def test_observation_detection(self):
        r"""Test observation_detection method.

        Approach: Ensure that all outputs are set as expected
        """

        exclude_mods = []
        exclude_mod_type = 'sotoSS'

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue
            if 'observation_detection' in mod.__dict__ or exclude_mod_type in mod.__name__:

                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path(
                        'test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path(
                        'SurveySimulation/top100stars.txt')

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)

                    #default settings should create dummy planet around first star
                    sInd = 0
                    pInds = np.where(
                        sim.SimulatedUniverse.plan2star == sInd)[0]
                    detected, fZ, systemParams, SNR, FA = \
                            sim.observation_detection(sInd,1.0*u.d,\
                            sim.OpticalSystem.observingModes[0])

                self.assertEqual(len(detected),len(pInds),\
                        'len(detected) != len(pInds) for %s'%mod.__name__)
                self.assertIsInstance(detected[0],(int,np.int32,np.int64,np.int_),\
                        'detected elements not ints for %s'%mod.__name__)
                for s in SNR[detected == 1]:
                    self.assertGreaterEqual(s,sim.OpticalSystem.observingModes[0]['SNR'],\
                        'detection SNR < mode requirement for %s'%mod.__name__)
                self.assertIsInstance(FA, bool,\
                        'False Alarm not boolean for %s'%mod.__name__)
Esempio n. 20
0
 def setUp(self):
     self.dev_null = open(os.devnull, 'w')
     self.occDataPath = resource_path(
         'PlanetPopulation/NominalOcc_Mass.csv')
     self.spec = {
         "modules": {
             "PlanetPhysicalModel": "PlanetPhysicalModel"
         },
         "occDataPath": self.occDataPath
     }
Esempio n. 21
0
    def test_init(self):
        """
        Test of initialization and __init__.
        
        """

        exclude_mods = ['SS_char_only2']

        required_modules = [
            'BackgroundSources', 'Completeness', 'Observatory',
            'OpticalSystem', 'PlanetPhysicalModel', 'PlanetPopulation',
            'PostProcessing', 'SimulatedUniverse', 'TargetList', 'TimeKeeping',
            'ZodiacalLight'
        ]

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue

            spec = copy.deepcopy(self.spec)
            if 'tieredScheduler' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_occ.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())
                spec['occHIPs'] = resource_path(
                    'SurveySimulation/top100stars.txt')

            if 'KnownRV' in mod.__name__:
                spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'

            with RedirectStreams(stdout=self.dev_null):
                sim = mod(**spec)

            self.assertIsInstance(sim._outspec, dict)
            # check for presence of a couple of class attributes
            self.assertIn('DRM', sim.__dict__)

            for rmod in required_modules:
                self.assertIn(rmod, sim.__dict__)
                self.assertEqual(getattr(sim, rmod)._modtype, rmod)
Esempio n. 22
0
    def test_str(self):
        """
        Test __str__ method, for full coverage and check that all modules have required attributes.
        """
        atts_list = ['DRM', 'seed', 'starVisits']

        for mod in self.allmods:
            if '__str__' not in mod.__dict__:
                continue

            spec = copy.deepcopy(self.spec)
            if 'tieredScheduler' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_occ.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())
                spec['occHIPs'] = resource_path(
                    'SurveySimulation/top100stars.txt')

            if 'KnownRV' in mod.__name__:
                spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'

            with RedirectStreams(stdout=self.dev_null):
                obj = mod(**spec)
            original_stdout = sys.stdout
            sys.stdout = StringIO()
            # call __str__ method
            result = obj.__str__()
            # examine what was printed
            contents = sys.stdout.getvalue()
            self.assertEqual(type(contents), type(''))
            # attributes from ICD
            for att in atts_list:
                self.assertIn(att, contents,
                              '{} missing for {}'.format(att, mod.__name__))
            sys.stdout.close()
            # it also returns a string, which is not necessary
            self.assertEqual(type(result), type(''))
            # put stdout back
            sys.stdout = original_stdout
Esempio n. 23
0
    def test_observation_detection(self):
        r"""Test observation_detection method.

        Approach: Ensure that all outputs are set as expected
        """

        exclude_mods = []
        exclude_mod_type = 'sotoSS'

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue
            if 'observation_detection' in mod.__dict__ or exclude_mod_type in mod.__name__:

                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)

                    #default settings should create dummy planet around first star
                    sInd = 0
                    pInds = np.where(sim.SimulatedUniverse.plan2star == sInd)[0]
                    detected, fZ, systemParams, SNR, FA = \
                            sim.observation_detection(sInd,1.0*u.d,\
                            sim.OpticalSystem.observingModes[0])
                
                self.assertEqual(len(detected),len(pInds),\
                        'len(detected) != len(pInds) for %s'%mod.__name__)
                self.assertIsInstance(detected[0],(int,np.int32,np.int64,np.int_),\
                        'detected elements not ints for %s'%mod.__name__)
                for s in SNR[detected == 1]:
                    self.assertGreaterEqual(s,sim.OpticalSystem.observingModes[0]['SNR'],\
                        'detection SNR < mode requirement for %s'%mod.__name__)
                self.assertIsInstance(FA, bool,\
                        'False Alarm not boolean for %s'%mod.__name__)
Esempio n. 24
0
    def test_str(self):
        """
        Test __str__ method, for full coverage and check that all modules have required attributes.
        """
        atts_list = ['DRM','seed','starVisits']

        for mod in self.allmods:
            if '__str__' not in mod.__dict__:
                continue

            spec = copy.deepcopy(self.spec)
            if 'tieredScheduler' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_occ.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())
                spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')

            if 'KnownRV' in mod.__name__:
                spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'

            with RedirectStreams(stdout=self.dev_null):
                obj = mod(**spec)
            original_stdout = sys.stdout
            sys.stdout = StringIO()
            # call __str__ method
            result = obj.__str__()
            # examine what was printed
            contents = sys.stdout.getvalue()
            self.assertEqual(type(contents), type(''))
            # attributes from ICD
            for att in atts_list:
                self.assertIn(att,contents,'{} missing for {}'.format(att,mod.__name__))
            sys.stdout.close()
            # it also returns a string, which is not necessary
            self.assertEqual(type(result), type(''))
            # put stdout back
            sys.stdout = original_stdout
Esempio n. 25
0
    def test_init(self):
        """
        Test of initialization and __init__.
        
        """

        exclude_mods=['SS_char_only2']

        required_modules = [
            'BackgroundSources', 'Completeness', 'Observatory', 'OpticalSystem',
            'PlanetPhysicalModel', 'PlanetPopulation', 'PostProcessing', 
            'SimulatedUniverse', 'TargetList', 'TimeKeeping', 'ZodiacalLight' ]

        for mod in self.allmods:
            if mod.__name__ in exclude_mods:
                continue

            spec = copy.deepcopy(self.spec)
            if 'tieredScheduler' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_occ.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())
                spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')

            if 'KnownRV' in mod.__name__:
                spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'

            with RedirectStreams(stdout=self.dev_null):
                sim = mod(**spec)

            self.assertIsInstance(sim._outspec, dict)
            # check for presence of a couple of class attributes
            self.assertIn('DRM', sim.__dict__)

            for rmod in required_modules:
                self.assertIn(rmod, sim.__dict__)
                self.assertEqual(getattr(sim,rmod)._modtype,rmod)
Esempio n. 26
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())

        modtype = getattr(EXOSIMS.Prototypes.TimeKeeping.TimeKeeping,
                          '_modtype')
        pkg = EXOSIMS.TimeKeeping
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(
                pkg.__path__, pkg.__name__ + '.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1], modtype)
                self.assertTrue(mod._modtype is modtype,
                                '_modtype mismatch for %s' % mod.__name__)
                self.allmods.append(mod)

        self.script1 = resource_path('test-scripts/simplest.json')

        everymodtype = getattr(SurveySimulation, '_modtype')
        self.everymods = [get_module(everymodtype)]
Esempio n. 27
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/simplest.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())
    
        modtype = getattr(SurveySimulation,'_modtype')
        pkg = EXOSIMS.SurveySimulation
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__+'.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1],modtype)
                self.assertTrue(mod._modtype is modtype,'_modtype mismatch for %s'%mod.__name__)
                self.allmods.append(mod)
Esempio n. 28
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/simplest.json')

        modtype = getattr(SurveySimulation, '_modtype')
        pkg = EXOSIMS.SurveySimulation
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(
                pkg.__path__, pkg.__name__ + '.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1], modtype)
                self.assertTrue(mod._modtype is modtype,
                                '_modtype mismatch for %s' % mod.__name__)
                self.allmods.append(mod)
Esempio n. 29
0
 def setUp(self):
     self.dev_null = open(os.devnull, 'w')
     modtype = getattr(EXOSIMS.Prototypes.BackgroundSources.BackgroundSources,'_modtype')
     pkg = EXOSIMS.BackgroundSources
     self.allmods = [get_module(modtype)]
     for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__+'.'):
         if not is_pkg:
             mod = get_module(module_name.split('.')[-1],modtype)
             self.assertTrue(mod._modtype is modtype,'_modtype mismatch for %s'%mod.__name__)
             self.allmods.append(mod)
     # need a TargetList object for testing
     script = resource_path('test-scripts/template_prototype_testing.json')
     with open(script) as f:
         spec = json.loads(f.read())
     with RedirectStreams(stdout=self.dev_null):
         self.TL = TargetList(**spec)
Esempio n. 30
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.specs = {'modules':{'BackgroundSources':' '}}
        script = resource_path('test-scripts/template_minimal.json')
        spec = json.loads(open(script).read())     
        with RedirectStreams(stdout=self.dev_null):
            self.TL = TargetList(**spec)

        modtype = getattr(EXOSIMS.Prototypes.PostProcessing.PostProcessing,'_modtype')
        pkg = EXOSIMS.PostProcessing
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__+'.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1],modtype)
                self.assertTrue(mod._modtype is modtype,'_modtype mismatch for %s'%mod.__name__)
                self.allmods.append(mod)
Esempio n. 31
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')

        # self.spec = {"modules": {"PlanetPhysicalModel": "PlanetPhysicalModel"}}
        self.script = resource_path('test-scripts/template_minimal.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())

        modtype = getattr(EXOSIMS.Prototypes.Observatory.Observatory, '_modtype')
        pkg = EXOSIMS.Observatory
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__ + '.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1], modtype)
                self.assertTrue(mod._modtype is modtype, '_modtype mismatch for %s' % mod.__name__)
                self.allmods.append(mod)
Esempio n. 32
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())
        
        with RedirectStreams(stdout=self.dev_null):
            self.TL = TargetList(ntargs=10,**copy.deepcopy(self.spec))
        
        modtype = getattr(OpticalSystem,'_modtype')
        pkg = EXOSIMS.OpticalSystem
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__+'.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1],modtype)
                self.assertTrue(mod._modtype is modtype,'_modtype mismatch for %s'%mod.__name__)
                self.allmods.append(mod)
Esempio n. 33
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        self.spec = json.loads(open(self.script).read())

        with RedirectStreams(stdout=self.dev_null):
            self.TL = TargetList(ntargs=10, **copy.deepcopy(self.spec))

        modtype = getattr(OpticalSystem, '_modtype')
        pkg = EXOSIMS.OpticalSystem
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(
                pkg.__path__, pkg.__name__ + '.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1], modtype)
                self.assertTrue(mod._modtype is modtype,
                                '_modtype mismatch for %s' % mod.__name__)
                self.allmods.append(mod)
 def setUp(self):
     self.dev_null = open(os.devnull, 'w')
     modtype = getattr(
         EXOSIMS.Prototypes.BackgroundSources.BackgroundSources, '_modtype')
     pkg = EXOSIMS.BackgroundSources
     self.allmods = [get_module(modtype)]
     for loader, module_name, is_pkg in pkgutil.walk_packages(
             pkg.__path__, pkg.__name__ + '.'):
         if not is_pkg:
             mod = get_module(module_name.split('.')[-1], modtype)
             self.assertTrue(mod._modtype is modtype,
                             '_modtype mismatch for %s' % mod.__name__)
             self.allmods.append(mod)
     # need a TargetList object for testing
     script = resource_path('test-scripts/template_prototype_testing.json')
     with open(script) as f:
         spec = json.loads(f.read())
     with RedirectStreams(stdout=self.dev_null):
         self.TL = TargetList(**spec)
Esempio n. 35
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())
        
        with RedirectStreams(stdout=self.dev_null):
            self.TL = TargetList(ntargs=10,**copy.deepcopy(self.spec))
        self.TL.dist = np.random.uniform(low=0,high=100,size=self.TL.nStars)*u.pc
        
        modtype = getattr(SimulatedUniverse,'_modtype')
        pkg = EXOSIMS.SimulatedUniverse
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__+'.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1],modtype)
                self.assertTrue(mod._modtype is modtype,'_modtype mismatch for %s'%mod.__name__)
                self.allmods.append(mod)
Esempio n. 36
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())
        
        with RedirectStreams(stdout=self.dev_null):
            self.TL = TargetList(ntargs=10,**copy.deepcopy(self.spec))
        self.TL.dist = np.random.uniform(low=0,high=100,size=self.TL.nStars)*u.pc
        
        modtype = getattr(SimulatedUniverse,'_modtype')
        pkg = EXOSIMS.SimulatedUniverse
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(pkg.__path__, pkg.__name__+'.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1],modtype)
                self.assertTrue(mod._modtype is modtype,'_modtype mismatch for %s'%mod.__name__)
                self.allmods.append(mod)
Esempio n. 37
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')

        # self.spec = {"modules": {"PlanetPhysicalModel": "PlanetPhysicalModel"}}
        self.script = resource_path('test-scripts/template_minimal.json')
        with open(self.script) as f:
            self.spec = json.loads(f.read())

        modtype = getattr(EXOSIMS.Prototypes.Observatory.Observatory,
                          '_modtype')
        pkg = EXOSIMS.Observatory
        self.allmods = [get_module(modtype)]
        for loader, module_name, is_pkg in pkgutil.walk_packages(
                pkg.__path__, pkg.__name__ + '.'):
            if not is_pkg:
                mod = get_module(module_name.split('.')[-1], modtype)
                self.assertTrue(mod._modtype is modtype,
                                '_modtype mismatch for %s' % mod.__name__)
                self.allmods.append(mod)
Esempio n. 38
0
    def test_init_psf(self):
        r"""Test of initialization and __init__ -- PSF

        Method: We instantiate OpticalSystem objects and verify 
        that IWA and OWA vary as expected with the domain of WA of the throughput
        lookup table (from 0 to 1).
        """
        filename = os.path.join(resource_path(), 'OpticalSystem', 'psf_5x5.fits')
        sampling = 1.234e-5 * u.arcsec # sampling rate keyword in above file
        for specs in [specs_default]:
            # the input dict is modified in-place -- so copy it
            our_specs = deepcopy(specs_default)
            for syst in our_specs['starlightSuppressionSystems']:
                syst['PSF'] = filename
            optsys = self.fixture(**deepcopy(our_specs))
            # Check that the sampling rate is correct
            self.assertEqual(optsys.starlightSuppressionSystems[0]['samp'], sampling)
            # Check that the PSF is present and has right size
            # Values are checked elsewhere
            psf = optsys.starlightSuppressionSystems[0]['PSF'](1.0,1.0)
            self.assertIsInstance(psf, np.ndarray)
            self.assertEqual(psf.shape, (5,5))
Esempio n. 39
0
    def test_init_psf(self):
        r"""Test of initialization and __init__ -- PSF

        Method: We instantiate OpticalSystem objects and verify 
        that IWA and OWA vary as expected with the domain of WA of the throughput
        lookup table (from 0 to 1).
        """
        filename = os.path.join(resource_path(), 'OpticalSystem',
                                'psf_5x5.fits')
        sampling = 1.234e-5 * u.arcsec  # sampling rate keyword in above file
        for specs in [specs_default]:
            # the input dict is modified in-place -- so copy it
            our_specs = deepcopy(specs_default)
            for syst in our_specs['starlightSuppressionSystems']:
                syst['PSF'] = filename
            optsys = self.fixture(**deepcopy(our_specs))
            # Check that the sampling rate is correct
            self.assertEqual(optsys.starlightSuppressionSystems[0]['samp'],
                             sampling)
            # Check that the PSF is present and has right size
            # Values are checked elsewhere
            psf = optsys.starlightSuppressionSystems[0]['PSF'](1.0, 1.0)
            self.assertIsInstance(psf, np.ndarray)
            self.assertEqual(psf.shape, (5, 5))
Esempio n. 40
0
    def setUp(self):

        self.dev_null = open(os.devnull, 'w')
        self.script = resource_path('test-scripts/template_minimal.json')
        self.spec = json.loads(open(self.script).read())
Esempio n. 41
0
    def test_observation_characterization(self):
        r"""Test observation_characterization method.

        Approach: Ensure all outputs are set as expected
        """

        exclude_mods = ['SS_char_only', 'SS_char_only2']
        exclude_mod_type = 'sotoSS'

        for mod in self.allmods:
            if mod.__name__ in exclude_mods or exclude_mod_type in mod.__name__:
                continue

            if 'observation_characterization' in mod.__dict__:
                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__ or 'ExoC' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')
                if "DDPC" in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_3DDPC.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)

                    #default settings should create dummy planet around first star
                    sInd = 0
                    pInds = np.where(sim.SimulatedUniverse.plan2star == sInd)[0]

                    #in order to test for characterization, we need to have previously
                    #detected the planet, so let's do that first
                    spectroModes = list(filter(lambda mode: 'spec' in mode['inst']['name'], sim.OpticalSystem.observingModes))
                    detected, fZ, systemParams, SNR, FA = \
                            sim.observation_detection(sInd,1.0*u.d,\
                            sim.OpticalSystem.observingModes[0])
                    #now the characterization
                    if 'ExoC' in mod.__name__:
                        characterized, fZ, systemParams, SNR, intTime = \
                                sim.observation_characterization(sInd,\
                                sim.OpticalSystem.observingModes[0], 0)

                        self.assertEqual(len(characterized),len(pInds),'len(characerized) != len(pInds) for %s'%mod.__name__)
                        self.assertIsInstance(characterized[0],(int,np.int32,np.int64,np.int_),\
                                'characterized elements not ints for %s'%mod.__name__)
                        for s in SNR[characterized == 1]:
                            self.assertGreaterEqual(s,sim.OpticalSystem.observingModes[0]['SNR'],\
                                    'char SNR less than required for %s'%mod.__name__)

                    elif 'PC' in mod.__name__:
                        characterized, fZ, systemParams, SNR, intTime = \
                                sim.observation_characterization(sInd,\
                                spectroModes)
                        self.assertEqual(len(characterized[0,:]),len(spectroModes),'len(characerized_modes) != len(spectroModes) for %s'%mod.__name__)
                        self.assertEqual(len(characterized[:,0]),len(pInds),'len(characerized) != len(pInds) for %s'%mod.__name__)
                        self.assertIsInstance(characterized[0,0],(int,np.int32,np.int64,np.int_),\
                                'characterized elements not ints for %s'%mod.__name__)
                        for s in SNR[:,0][characterized[:,0] == 1]:
                            self.assertGreaterEqual(s, sim.OpticalSystem.observingModes[0]['SNR'],\
                                    'char SNR less than required for %s'%mod.__name__)

                    else:
                        characterized, fZ, systemParams, SNR, intTime = \
                                sim.observation_characterization(sInd,\
                                sim.OpticalSystem.observingModes[0])

                        self.assertEqual(len(characterized),len(pInds),'len(characerized) != len(pInds) for %s'%mod.__name__)
                        self.assertIsInstance(characterized[0],(int,np.int32,np.int64,np.int_),\
                                'characterized elements not ints for %s'%mod.__name__)
                        for s in SNR[characterized == 1]:
                            self.assertGreaterEqual(s,sim.OpticalSystem.observingModes[0]['SNR'],\
                                    'char SNR less than required for %s'%mod.__name__)

                self.assertLessEqual(intTime,sim.OpticalSystem.intCutoff,\
                        'char intTime greater than cutoff for %s'%mod.__name__)
Esempio n. 42
0
r"""SurveySimulation module unit tests

Michael Turmon, JPL, Apr. 2016
"""

import sys
import os
import unittest
import json
from EXOSIMS.Prototypes.SurveySimulation import SurveySimulation
import numpy as np
import astropy.units as u
from tests.TestSupport.Info import resource_path
from tests.TestSupport.Utilities import RedirectStreams

SimpleScript = resource_path('test-scripts/simplest.json')
ErrorScript = resource_path('test-scripts/simplest-error.json')

class TestSurveySimulationMethods(unittest.TestCase):
    r"""Test SurveySimulation class."""
    dev_null = open(os.devnull, 'w')

    required_modules = [
            'BackgroundSources', 'Completeness', 'Observatory', 'OpticalSystem',
            'PlanetPhysicalModel', 'PlanetPopulation', 'PostProcessing', 
            'SimulatedUniverse', 'TargetList', 'TimeKeeping', 'ZodiacalLight' ]

    def setUp(self):
        # print '[setup] ',
        self.fixture = SurveySimulation
Esempio n. 43
0
    def test_choose_next_target(self):
        r"""Test choose_next_target method.

        Approach: Ensure the next target is a valid index for different cases: old_sInd is none,
        old_sInd in sInds, old_sInd not in sInds
        """

        exclude_mods = ['SS_char_only', 'SS_char_only2', 'SS_det_only']
        exclude_mod_type = 'sotoSS'

        for mod in self.allmods:
            if mod.__name__ in exclude_mods or exclude_mod_type in mod.__name__:
                continue

            if 'choose_next_target' in mod.__dict__:

                spec = copy.deepcopy(self.spec)
                if 'tieredScheduler' in mod.__name__:
                    self.script = resource_path('test-scripts/simplest_occ.json')
                    with open(self.script) as f:
                        spec = json.loads(f.read())
                    spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')                
                if 'KnownRV' in mod.__name__:
                    spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                    spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                    spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'
                if ('occulterJScheduler' in mod.__name__) or ('linearJScheduler' in mod.__name__):
                    spec['starlightSuppressionSystems'] = [{'name': 'occulter', 'occulter': True,
                                                            'lam': 550, 'BW': 0.10, 'IWA': 0.1,
                                                            'OWA': 0, 'occ_trans': 1}]
                    spec['nSteps'] = 2
                    spec['modules']['Observatory'] = 'SotoStarshade'

                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)

                    #old sInd is None
                    sInds = np.array([0,1,2])
                    sInd, waitTime = sim.choose_next_target(None, sInds, \
                            np.array([1.0]*sim.TargetList.nStars)*u.d, \
                            np.ones((len(sInds),))*u.d)

                    self.assertTrue(sInd in sInds or sInd == None,'sInd not in passed sInds for %s'%mod.__name__)

                    #old sInd in sInds
                    sInds = np.random.choice(sim.TargetList.nStars,size=int(sim.TargetList.nStars/2.0),replace=False)
                    old_sInd = np.random.choice(sInds)
                    _ = sim.observation_detection(old_sInd,1.0*u.d,sim.OpticalSystem.observingModes[0])
                    sInd, waitTime = sim.choose_next_target(old_sInd,sInds,
                            np.array([1.0]*sim.TargetList.nStars)*u.d,
                            np.array([1.0]*len(sInds))*u.d)

                    self.assertTrue(sInd in sInds or sInd == None,'sInd not in passed sInds for %s'%mod.__name__)

                    #old sInd not in sInds
                    sInds = np.random.choice(sim.TargetList.nStars,size=int(sim.TargetList.nStars/2.0),replace=False)
                    tmp = list(set(np.arange(sim.TargetList.nStars)) - set(sInds))
                    old_sInd = np.random.choice(tmp)
                    _ = sim.observation_detection(old_sInd,1.0*u.d,sim.OpticalSystem.observingModes[0])
                    sInd, waitTime = sim.choose_next_target(old_sInd,sInds,
                            np.array([1.0]*sim.TargetList.nStars)*u.d,
                            np.array([1.0]*len(sInds))*u.d)

                    self.assertTrue(sInd in sInds or sInd == None,'sInd not in passed sInds for %s'%mod.__name__)
Esempio n. 44
0
import unittest
import os
from EXOSIMS import MissionSim
from EXOSIMS.Prototypes import TargetList
import numpy as np
from astropy import units as u
from tests.TestSupport.Info import resource_path
from tests.TestSupport.Utilities import RedirectStreams
import json

r"""TargetList module unit tests

Paul Nunez, JPL, Aug. 2016
"""

scriptfile = resource_path('test-scripts/template_prototype_testing.json')

class Test_TargetList_prototype(unittest.TestCase):
    dev_null = open(os.devnull, 'w')

    def setUp(self):
        with open(scriptfile) as f:
            self.spec = json.loads(f.read())
        
        # quiet the chatter at initialization
        with RedirectStreams(stdout=self.dev_null):
            sim = MissionSim.MissionSim(**self.spec)
        self.targetlist = sim.TargetList
        self.opticalsystem = sim.OpticalSystem
        self.planetpop = sim.PlanetPopulation
    
Esempio n. 45
0
    def test_run_sim(self):
        r"""Test run_sim method.

        Approach: Ensures the simulation runs to completion and the output is set.
        """

        #expected contents of DRM:
        All_DRM_keys =  ['FA_char_status',
                         'char_mode',
                         'det_status',
                         'char_params',
                         'star_name',
                         'plan_inds',
                         'FA_char_dMag',
                         'OB_nb',
                         'char_fZ',
                         'det_SNR',
                         'FA_char_fEZ',
                         'char_status',
                         'det_mode',
                         'det_time',
                         'arrival_time',
                         'char_SNR',
                         'det_params',
                         'char_time',
                         'FA_char_SNR',
                         'det_fZ',
                         'FA_det_status',
                         'star_ind',
                         'FA_char_WA']
        det_only_DRM_keys = ['det_status',
                             'star_name',
                             'plan_inds',
                             'OB_nb',
                             'det_SNR',
                             'det_mode',
                             'det_time',
                             'arrival_time',
                             'det_params',
                             'det_fZ',
                             'star_ind']
        TS_DRM_keys = ['plan_inds',
                       'OB_nb',
                       'arrival_time',
                       'star_ind']

        exclude_mods = ['SS_char_only','SS_char_only2','SS_det_only']
        exclude_mod_type = 'sotoSS'

        for mod in self.allmods:
            if mod.__name__ in exclude_mods or exclude_mod_type in mod.__name__:
                continue

            spec = copy.deepcopy(self.spec)
            if 'tieredScheduler' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_occ.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())
                spec['occHIPs'] = resource_path('SurveySimulation/top100stars.txt')

            if 'linearJScheduler' in mod.__name__ or 'ExoC' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_3DDPC.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())

            if 'KnownRV' in mod.__name__:
                spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'

            if 'run_sim' in mod.__dict__:
                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)
                    sim.run_sim()
                    # check that a mission constraint has been exceeded
                    allModes = sim.OpticalSystem.observingModes
                    mode = list(filter(lambda mode: mode['detectionMode'] == True, allModes))[0]
                    exoplanetObsTimeCondition = sim.TimeKeeping.exoplanetObsTime + sim.Observatory.settlingTime + mode['syst']['ohTime'] >= sim.TimeKeeping.missionLife*sim.TimeKeeping.missionPortion
                    missionLifeCondition = sim.TimeKeeping.currentTimeNorm + sim.Observatory.settlingTime + mode['syst']['ohTime'] >= sim.TimeKeeping.missionLife
                    OBcondition = sim.TimeKeeping.OBendTimes[sim.TimeKeeping.OBnumber] <= sim.TimeKeeping.currentTimeNorm + sim.Observatory.settlingTime + mode['syst']['ohTime']

                self.assertTrue(exoplanetObsTimeCondition or (missionLifeCondition or OBcondition), 'Mission did not run to completion for %s'%mod.__name__)

                # resulting DRM is a list...
                self.assertIsInstance(sim.DRM, list, 'DRM is not a list for %s'%mod.__name__)
                # ...and has nontrivial number of entries
                self.assertGreater(len(sim.DRM), 0, 'DRM is empty for %s'%mod.__name__)

                if 'det_only' in mod.__name__ or 'ExoC' in mod.__name__:
                    for key in det_only_DRM_keys:
                        self.assertIn(key,sim.DRM[0],'DRM is missing key %s for %s'%(key, mod.__name__))
                elif 'tieredScheduler' in mod.__name__:
                    for key in TS_DRM_keys:
                        self.assertIn(key,sim.DRM[0],'DRM is missing key %s for %s'%(key, mod.__name__))
                elif 'DDPC' in mod.__name__:
                    for keys in All_DRM_keys:
                        self.assertIn(key, list(sim.DRM[0]['det_info'][0].keys())
                                      + list(sim.DRM[0]['char_info'][0].keys())
                                      + list(sim.DRM[0].keys()),'DRM is missing key %s for %s'%(key, mod.__name__))
                else:
                    for key in All_DRM_keys:
                        self.assertIn(key,sim.DRM[0],'DRM is missing key %s for %s'%(key, mod.__name__))
Esempio n. 46
0
    def test_run_sim(self):
        r"""Test run_sim method.

        Approach: Ensures the simulation runs to completion and the output is set.
        """

        #expected contents of DRM:
        All_DRM_keys = [
            'FA_char_status', 'char_mode', 'det_status', 'char_params',
            'star_name', 'plan_inds', 'FA_char_dMag', 'OB_nb', 'char_fZ',
            'det_SNR', 'FA_char_fEZ', 'char_status', 'det_mode', 'det_time',
            'arrival_time', 'char_SNR', 'det_params', 'char_time',
            'FA_char_SNR', 'det_fZ', 'FA_det_status', 'star_ind', 'FA_char_WA'
        ]
        det_only_DRM_keys = [
            'det_status', 'star_name', 'plan_inds', 'OB_nb', 'det_SNR',
            'det_mode', 'det_time', 'arrival_time', 'det_params', 'det_fZ',
            'star_ind'
        ]
        TS_DRM_keys = ['plan_inds', 'OB_nb', 'arrival_time', 'star_ind']

        exclude_mods = [
            'SS_char_only', 'SS_char_only2', 'SS_det_only',
            'tieredScheduler_SLSQP', 'tieredScheduler_DD_SLSQP'
        ]
        exclude_mod_type = 'sotoSS'

        for mod in self.allmods:
            if mod.__name__ in exclude_mods or exclude_mod_type in mod.__name__:
                continue

            spec = copy.deepcopy(self.spec)
            if 'tieredScheduler' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_occ.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())
                spec['occHIPs'] = resource_path(
                    'SurveySimulation/top100stars.txt')

            if 'linearJScheduler' in mod.__name__ or 'ExoC' in mod.__name__ or 'coroOnly' in mod.__name__:
                self.script = resource_path('test-scripts/simplest_3DDPC.json')
                with open(self.script) as f:
                    spec = json.loads(f.read())

            if 'KnownRV' in mod.__name__:
                spec['modules']['PlanetPopulation'] = 'KnownRVPlanets'
                spec['modules']['TargetList'] = 'KnownRVPlanetsTargetList'
                spec['modules']['SimulatedUniverse'] = 'KnownRVPlanetsUniverse'

            if 'run_sim' in mod.__dict__:
                with RedirectStreams(stdout=self.dev_null):
                    sim = mod(**spec)
                    sim.run_sim()
                    # check that a mission constraint has been exceeded
                    allModes = sim.OpticalSystem.observingModes
                    mode = list(
                        filter(lambda mode: mode['detectionMode'] == True,
                               allModes))[0]
                    exoplanetObsTimeCondition = sim.TimeKeeping.exoplanetObsTime + sim.Observatory.settlingTime + mode[
                        'syst'][
                            'ohTime'] >= sim.TimeKeeping.missionLife * sim.TimeKeeping.missionPortion
                    missionLifeCondition = sim.TimeKeeping.currentTimeNorm + sim.Observatory.settlingTime + mode[
                        'syst']['ohTime'] >= sim.TimeKeeping.missionLife
                    OBcondition = sim.TimeKeeping.OBendTimes[
                        sim.TimeKeeping.
                        OBnumber] <= sim.TimeKeeping.currentTimeNorm + sim.Observatory.settlingTime + mode[
                            'syst']['ohTime']

                self.assertTrue(
                    exoplanetObsTimeCondition
                    or (missionLifeCondition or OBcondition),
                    'Mission did not run to completion for %s' % mod.__name__)

                # resulting DRM is a list...
                self.assertIsInstance(
                    sim.DRM, list, 'DRM is not a list for %s' % mod.__name__)
                # ...and has nontrivial number of entries
                self.assertGreater(len(sim.DRM), 0,
                                   'DRM is empty for %s' % mod.__name__)

                if 'det_only' in mod.__name__ or 'ExoC' in mod.__name__ or 'linearJScheduler_orbitChar' in mod.__name__:
                    for key in det_only_DRM_keys:
                        self.assertIn(
                            key, sim.DRM[0], 'DRM is missing key %s for %s' %
                            (key, mod.__name__))
                elif 'tieredScheduler' in mod.__name__ or 'coroOnly' in mod.__name__:
                    for key in TS_DRM_keys:
                        self.assertIn(
                            key, sim.DRM[0], 'DRM is missing key %s for %s' %
                            (key, mod.__name__))
                elif 'DDPC' in mod.__name__:
                    for keys in All_DRM_keys:
                        self.assertIn(
                            key,
                            list(sim.DRM[0]['det_info'][0].keys()) +
                            list(sim.DRM[0]['char_info'][0].keys()) +
                            list(sim.DRM[0].keys()),
                            'DRM is missing key %s for %s' %
                            (key, mod.__name__))
                else:
                    for key in All_DRM_keys:
                        self.assertIn(
                            key, sim.DRM[0], 'DRM is missing key %s for %s' %
                            (key, mod.__name__))
Esempio n. 47
0
    def test_init_ipac_compare(self):
        r"""Test of initialization and __init__ -- compare values to independently-extracted values.

        Method: Loop over the extracted values from EXOSIMS, and find the corresponding
        planet in the online catalog, as loaded in the first lines in this code.
        Compare values for all important parameters.  The values used by subsequent code
        are (from SimulatedUniverse/KnownRVPlanetsUniverse):
        *  (sma, smaerr) = semi-major axis & error
        *  (eccen, eccenerr) = eccentricity & error
        also present here:
           plan_pop.hostname (string)
        and, as well,
           plan_pop.allplanetdata[pl_orbitincl] = orbital inclination
        *  plan_pop.mass = mass of planet
           plan_pop.allplanetdata['pl_orblper'] = longitude of periapsis
        We check the starred values.
        """

        plan_pop = self.fixture
        # ensure basic module attributes are set up
        self.validate_planet_population(plan_pop)

        # Load canned RV planet information
        # d_ref is a dictionary, indexed by (stellar) host name,
        # containing lists of dictionaries of planet attributes.  For instance:
        #   d_ref['HD 12661']
        # contains a list of two entries (two planets), and
        #   d_ref['HD 12661'][0]['pl_orbeccen']
        # is a floating-point eccentricity for the first planet, or None if the
        # eccentricity is not known.
        comparison_file = os.path.join(resource_path(), 'ipac-rv-data-planets.csv')
        d_ref = load_vo_csvfile(comparison_file, exoplanet_unit_map)
        # d_ref = load_rv_planet_comparison()

        # loop over all RV planets, looking for matches
        hosts_not_matched = 0
        for n_planet in range(len(plan_pop.hostname)):
            # specific "plan_pop" information we will check for this planet
            host_1    = plan_pop.hostname[n_planet]
            sma_1     = plan_pop.sma[n_planet]
            sma_err_1 = plan_pop.smaerr[n_planet]
            ecc_1     = plan_pop.eccen[n_planet]
            ecc_err_1 = plan_pop.eccenerr[n_planet]
            mass_1    = plan_pop.mass[n_planet]
            # reference information:
            # it's a list, because there could be several planets around that host
            self.assertIn(host_1, d_ref)
            info_refs = d_ref[host_1]
            # pick out the right planet using agreement of SMA
            info_ref = [d for d in info_refs if (d['pl_orbsmax'] is not None
                                                 and
                                                 np.abs(d['pl_orbsmax'] - sma_1).to(u.au).value < 1e-3)]
            # zero matches is possible, because plan_pop.__init__ fills in a few
            # SMA values on its own if a planet is detected but SMA is not recorded by IPAC.
            if len(info_ref) != 1:
                hosts_not_matched += 1 # count them: we don't allow too many unmatched planets
                continue
            # assert that there was not a double-match -- should not happen
            self.assertEqual(len(info_ref), 1, msg=('Multiple matches for ' + host_1))
            # take the (unique) matching object
            info_ref = info_ref[0]
            # check info_ref attributes (i.e., our reference values) against plan_pop
            #   note, all these checks are very tight in tolerance, because any difference
            #   should only arise due to float -> ascii -> float conversions
            # 1: check SMA, use relative error
            if info_ref['pl_orbsmax'] is not None:
                delta = ((info_ref['pl_orbsmax'] - sma_1)/sma_1).decompose().value
                self.assertAlmostEqual(delta, 0.0, msg=('SMA difference in %s' % host_1), delta=1e-6)
            # 2: check SMA error, use absolute error in AU
            if info_ref['pl_orbsmaxerr1'] is not None:
                # the error can be small, so no relative error
                delta = (info_ref['pl_orbsmaxerr1'] - sma_err_1).to(u.au).value
                self.assertAlmostEqual(delta, 0.0, msg=('SMA_error difference in %s' % host_1), delta=1e-6)
            # 3: check eccentricity, just a float
            if info_ref['pl_orbeccen'] is not None:
                self.assertAlmostEqual(info_ref['pl_orbeccen'], ecc_1,
                                       msg=('eccentricity difference in %s' % host_1), delta=1e-6)
            # 4: check eccentricity error, also a float
            if info_ref['pl_orbeccenerr1'] is not None:
                self.assertAlmostEqual(info_ref['pl_orbeccenerr1'], ecc_err_1,
                                       msg=('eccen_error difference in %s' % host_1), delta=1e-6)
            # 5: check mass, use relative error
            if info_ref['pl_bmasse'] is not None:
                delta = ((info_ref['pl_bmasse'] - mass_1)/mass_1).decompose().value
                self.assertAlmostEqual(delta, 0.0, msg=('Mass difference in %s' % host_1), delta=1e-6)
        # ensure there are not too many un-matched host stars
        self.assertLess(hosts_not_matched, len(plan_pop.hostname)//10,
                        'Too many stars in PlanetPopulation are unmatched in the catalog.')
Esempio n. 48
0
 def setUp(self):
     self.dev_null = open(os.devnull, 'w')
     self.occDataPath = resource_path('PlanetPopulation/NominalOcc_Mass.csv')
     self.spec = {"modules": {"PlanetPhysicalModel": "PlanetPhysicalModel"},
                  "occDataPath": self.occDataPath}
Esempio n. 49
0
    def test_init_sweep_inputs(self):
        r"""Test __init__ method, sweeping over all parameters.

        Method: Driven by the table at the top of this file, we sweep
        through all input parameters to OpticalSystem, and in each case,
        we instantiate an OpticalSystem with the given parameter, and
        check that the parameter is properly set.  Additionally, for
        interpolants, we check that the proper function is obtained
        by EXOSIMS.  Additionally, also for file-based interpolants, we
        ensure that appropriate exceptions are raised for inaccessible
        files and for out-of-range values."""

        # iterate over all tests
        for (param, recipe) in opsys_params.iteritems():
            print(param)
            # one inner loop tests that we can set 'param'
            #   example simple param dictionary:
            #   pupilDiam = dict(default=4.0, trial=(1.0, 10.0), unit=u.m)
            # set up "raises" key, if not given
            if 'raises' not in recipe:
                recipe['raises'] = (None, ) * len(recipe['trial'])
            assert len(recipe['raises']) == len(recipe['trial']), \
                  'recipe length mismatch for ' + param
            # target dictionary: 0 for top-level, 1 for inst, 2 for starlight
            target = recipe['target']
            # the unit is multiplied by the numerical value when comparing
            unit = 1.0 if recipe['unit'] is float else recipe['unit']
            # loop over all trials requested for "param"
            for (param_val, err_val) in zip(recipe['trial'], recipe['raises']):
                # need a copy because we are about to alter the specs
                specs = deepcopy(specs_simple)
                # print param, '<--', param_val, '[', err_val, ']'
                # make strings into full filenames
                if isinstance(param_val, basestring):
                    param_val = os.path.join(resource_path(), 'OpticalSystem',
                                             param_val)
                # insert param_val into approriate slot within specs - a bit messy
                if target == 0:
                    specs[param] = param_val
                elif target == 1:
                    specs['scienceInstruments'][0][param] = param_val
                elif target == 2:
                    specs['starlightSuppressionSystems'][0][param] = param_val
                else:
                    # this is a failure of the "recipe" dictionary entry
                    assert False, "Target must be 0, 1, 2 -- " + param
                # prepare to get an OpticalSystem
                if err_val is None:
                    # no error expected in this branch
                    # note: deepcopy specs because EXOSIMS alters them,
                    # which would in turn alter our reference param_val
                    # if it's mutable!
                    optsys = self.fixture(**deepcopy(specs))
                    # find the right place to seek the param we tried
                    if target == 0:
                        d = optsys.__dict__
                    elif target == 1:
                        d = optsys.__dict__['scienceInstruments'][0]
                    elif target == 2:
                        d = optsys.__dict__['starlightSuppressionSystems'][0]
                    # check the value: scalar or interpolant
                    if hasattr(d[param], '__call__'):
                        # it's an interpolant -- make an interpolant ourselves
                        param_int = self.make_interpolant(
                            param, param_val, unit)
                        # compare our interpolant with the EXOSIMS interpolant
                        self.compare_interpolants(d[param], param_int, param)
                    elif isinstance(d[param], list):
                        # a small list-of-dict's in the case of the
                        # 'scienceInstruments' and 'starlightSuppressionSystems' checks.
                        # this checks that param_val is a subset of d[param]
                        self.compare_lists(d[param], param_val, param)
                    else:
                        # a number or Quantity - simple assertion
                        if isinstance(param_val, list):
                            print('***', param)
                            print(d[param])
                        self.assertEqual(d[param],
                                         param_val * unit,
                                         msg='failed to set "%s" parameter' %
                                         param)
                else:
                    # else, ensure the right error is raised
                    with self.assertRaises(err_val):
                        self.fixture(**deepcopy(specs))
Esempio n. 50
0
    def test_init_ipac_compare(self):
        r"""Test of initialization and __init__ -- compare values to independently-extracted values.

        Method: Loop over the extracted values from EXOSIMS, and find the corresponding
        planet in the online catalog, as loaded in the first lines in this code.
        Compare values for all important parameters.  The values used by subsequent code
        are (from SimulatedUniverse/KnownRVPlanetsUniverse):
        *  (sma, smaerr) = semi-major axis & error
        *  (eccen, eccenerr) = eccentricity & error
        also present here:
           plan_pop.hostname (string)
        and, as well,
           plan_pop.allplanetdata[pl_orbitincl] = orbital inclination
        *  plan_pop.mass = mass of planet
           plan_pop.allplanetdata['pl_orblper'] = longitude of periapsis
        We check the starred values.
        """

        plan_pop = self.fixture
        # ensure basic module attributes are set up
        self.validate_planet_population(plan_pop)

        # Load canned RV planet information
        # d_ref is a dictionary, indexed by (stellar) host name,
        # containing lists of dictionaries of planet attributes.  For instance:
        #   d_ref['HD 12661']
        # contains a list of two entries (two planets), and
        #   d_ref['HD 12661'][0]['pl_orbeccen']
        # is a floating-point eccentricity for the first planet, or None if the
        # eccentricity is not known.
        comparison_file = os.path.join(resource_path(),
                                       'ipac-rv-data-planets.csv')
        d_ref = load_vo_csvfile(comparison_file, exoplanet_unit_map)
        # d_ref = load_rv_planet_comparison()

        # loop over all RV planets, looking for matches
        hosts_not_matched = 0
        for n_planet in range(len(plan_pop.hostname)):
            # specific "plan_pop" information we will check for this planet
            host_1 = plan_pop.hostname[n_planet]
            sma_1 = plan_pop.sma[n_planet]
            sma_err_1 = plan_pop.smaerr[n_planet]
            ecc_1 = plan_pop.eccen[n_planet]
            ecc_err_1 = plan_pop.eccenerr[n_planet]
            mass_1 = plan_pop.mass[n_planet]
            # reference information:
            # it's a list, because there could be several planets around that host
            self.assertIn(host_1, d_ref)
            info_refs = d_ref[host_1]
            # pick out the right planet using agreement of SMA
            info_ref = [
                d for d in info_refs
                if (d['pl_orbsmax'] is not None
                    and np.abs(d['pl_orbsmax'] - sma_1).to(u.au).value < 1e-3)
            ]
            # zero matches is possible, because plan_pop.__init__ fills in a few
            # SMA values on its own if a planet is detected but SMA is not recorded by IPAC.
            if len(info_ref) != 1:
                hosts_not_matched += 1  # count them: we don't allow too many unmatched planets
                continue
            # assert that there was not a double-match -- should not happen
            self.assertEqual(len(info_ref),
                             1,
                             msg=('Multiple matches for ' + host_1))
            # take the (unique) matching object
            info_ref = info_ref[0]
            # check info_ref attributes (i.e., our reference values) against plan_pop
            #   note, all these checks are very tight in tolerance, because any difference
            #   should only arise due to float -> ascii -> float conversions
            # 1: check SMA, use relative error
            if info_ref['pl_orbsmax'] is not None:
                delta = ((info_ref['pl_orbsmax'] - sma_1) /
                         sma_1).decompose().value
                self.assertAlmostEqual(delta,
                                       0.0,
                                       msg=('SMA difference in %s' % host_1),
                                       delta=1e-6)
            # 2: check SMA error, use absolute error in AU
            if info_ref['pl_orbsmaxerr1'] is not None:
                # the error can be small, so no relative error
                delta = (info_ref['pl_orbsmaxerr1'] - sma_err_1).to(u.au).value
                self.assertAlmostEqual(delta,
                                       0.0,
                                       msg=('SMA_error difference in %s' %
                                            host_1),
                                       delta=1e-6)
            # 3: check eccentricity, just a float
            if info_ref['pl_orbeccen'] is not None:
                self.assertAlmostEqual(info_ref['pl_orbeccen'],
                                       ecc_1,
                                       msg=('eccentricity difference in %s' %
                                            host_1),
                                       delta=1e-6)
            # 4: check eccentricity error, also a float
            if info_ref['pl_orbeccenerr1'] is not None:
                self.assertAlmostEqual(info_ref['pl_orbeccenerr1'],
                                       ecc_err_1,
                                       msg=('eccen_error difference in %s' %
                                            host_1),
                                       delta=1e-6)
            # 5: check mass, use relative error
            if info_ref['pl_bmasse'] is not None:
                delta = ((info_ref['pl_bmasse'] - mass_1) /
                         mass_1).decompose().value
                self.assertAlmostEqual(delta,
                                       0.0,
                                       msg=('Mass difference in %s' % host_1),
                                       delta=1e-6)
        # ensure there are not too many un-matched host stars
        self.assertLess(
            hosts_not_matched,
            len(plan_pop.hostname) // 10,
            'Too many stars in PlanetPopulation are unmatched in the catalog.')
Esempio n. 51
0
    def test_init_stellar_attributes(self):
        r"""Test of initialization and __init__ -- stellar attributes.

        Method: Comprehensive check of all target-star stored attributes
        against tabulated values, for all stars.
        TODO: Numerical checks on comp0 attribute
        """
        tlist = self.fixture
        self.basic_validation(tlist)
        # Load canned RV exo-star information
        # d_ref is a dictionary, indexed by (stellar) host name,
        # containing lists of dictionaries of star attributes.  For instance:
        #   d_ref['HD 12661']
        # contains a list of two entries (two planets for that star), and
        #   d_ref['HD 12661'][0]['st_plx']
        # is a floating-point parallax for the first planet, or None if the
        # parallax is not known.
        # Because we're checking stellar properties here, the list entries
        # refer to the same host star, so index [0] is the same as index [1],
        # etc.
        comparison_file = os.path.join(resource_path(), 'ipac-rv-data-stars.csv')
        d_ref = load_vo_csvfile(comparison_file, exostar_unit_map)
        # loop over all RV host stars, looking for matching stars in the file
        hosts_not_matched = 0
        for n_host in range(len(tlist.Name)):
            # check information for the following host
            host = tlist.Name[n_host]
            if host not in d_ref:
                hosts_not_matched += 1
                continue
            # check all the attributes created in the tlist's __init__
            # the "atts_mapping" dictionary maps:
            #    tlist attribute names => votable attribute names
            for (name_att, name_vo) in tlist.atts_mapping.items():
                # the EXOSIMS value: tlist.$name_e[n_host]
                val_e = getattr(tlist, name_att)[n_host]
                # the validation value
                val_v = d_ref[host][0][name_vo]
                # if missing data in the validation table, skip -- it's a NaN
                if val_v is None:
                    continue
                assert isinstance(val_e, u.quantity.Quantity) == isinstance(val_v, u.quantity.Quantity), \
                    'Units mismatch in key %s, check atts_mapping dictionary in test code.' % name_att
                if isinstance(val_e, u.quantity.Quantity):
                    val_e = val_e.value
                    val_v = val_v.value
                # otherwise, check it
                self.assertEqual(val_e, val_v,
                                 msg=('Difference in star "%s", attribute "%s": %s vs. %s' %
                                      (host, name_att, val_e, val_v)))
            # check ra, dec, which are handled separately
            for name_att in ['ra', 'dec']:
                # the EXOSIMS value
                val_e  = getattr(tlist.coords[n_host], name_att)
                # the validation value
                val_v = d_ref[host][0][name_att]
                # allow a small difference in case of truncation issues
                self.assertAlmostEqual(val_e.to(u.deg).value, val_v.to(u.deg).value,
                                msg=('Difference in star "%s", attribute "%s": %s vs. %s' %
                                (host, name_att, val_e, val_v)), delta=1e-4)
            # optionally:
            # check tlist.maxintTime, tlist.comp0
            # noted: tlist.Binary_Cut seems not based in measurements
        # ensure there are not too many un-matched host stars
        self.assertLess(hosts_not_matched, len(tlist.Name)//10,
                        'Too many stars in TargetList are unmatched in the catalog.')
Esempio n. 52
0
    def test_init_sweep_inputs(self):
        r"""Test __init__ method, sweeping over all parameters.

        Method: Driven by the table at the top of this file, we sweep
        through all input parameters to OpticalSystem, and in each case,
        we instantiate an OpticalSystem with the given parameter, and
        check that the parameter is properly set.  Additionally, for
        interpolants, we check that the proper function is obtained
        by EXOSIMS.  Additionally, also for file-based interpolants, we
        ensure that appropriate exceptions are raised for inaccessible
        files and for out-of-range values."""

        # iterate over all tests
        for (param,recipe) in opsys_params.iteritems():
            print(param)
            # one inner loop tests that we can set 'param'
            #   example simple param dictionary:
            #   pupilDiam = dict(default=4.0, trial=(1.0, 10.0), unit=u.m)
            # set up "raises" key, if not given
            if 'raises' not in recipe:
                recipe['raises'] = (None,) * len(recipe['trial'])
            assert len(recipe['raises']) == len(recipe['trial']), \
                  'recipe length mismatch for ' + param
            # target dictionary: 0 for top-level, 1 for inst, 2 for starlight
            target = recipe['target']
            # the unit is multiplied by the numerical value when comparing
            unit = 1.0 if recipe['unit'] is float else recipe['unit']
            # loop over all trials requested for "param"
            for (param_val,err_val) in zip(recipe['trial'], recipe['raises']):
                # need a copy because we are about to alter the specs
                specs = deepcopy(specs_simple)
                # print param, '<--', param_val, '[', err_val, ']'
                # make strings into full filenames
                if isinstance(param_val, basestring):
                    param_val = os.path.join(resource_path(), 'OpticalSystem', param_val)
                # insert param_val into approriate slot within specs - a bit messy
                if target == 0:
                    specs[param] = param_val
                elif target == 1:
                    specs['scienceInstruments'][0][param] = param_val
                elif target == 2:
                    specs['starlightSuppressionSystems'][0][param] = param_val
                else:
                    # this is a failure of the "recipe" dictionary entry
                    assert False, "Target must be 0, 1, 2 -- " + param
                # prepare to get an OpticalSystem
                if err_val is None:
                    # no error expected in this branch
                    # note: deepcopy specs because EXOSIMS alters them,
                    # which would in turn alter our reference param_val
                    # if it's mutable!
                    optsys = self.fixture(**deepcopy(specs))
                    # find the right place to seek the param we tried
                    if target == 0:
                        d = optsys.__dict__
                    elif target == 1:
                        d = optsys.__dict__['scienceInstruments'][0]
                    elif target == 2:
                        d = optsys.__dict__['starlightSuppressionSystems'][0]
                    # check the value: scalar or interpolant
                    if hasattr(d[param], '__call__'):
                        # it's an interpolant -- make an interpolant ourselves
                        param_int = self.make_interpolant(param, param_val, unit)
                        # compare our interpolant with the EXOSIMS interpolant
                        self.compare_interpolants(d[param], param_int, param)
                    elif isinstance(d[param], list):
                        # a small list-of-dict's in the case of the
                        # 'scienceInstruments' and 'starlightSuppressionSystems' checks.
                        # this checks that param_val is a subset of d[param]
                        self.compare_lists(d[param], param_val, param)
                    else:
                        # a number or Quantity - simple assertion
                        if isinstance(param_val, list):
                            print('***', param)
                            print(d[param])
                        self.assertEqual(d[param], param_val*unit,
                                         msg='failed to set "%s" parameter' % param)
                else:
                    # else, ensure the right error is raised
                    with self.assertRaises(err_val):
                        self.fixture(**deepcopy(specs))