Пример #1
0
	def testSetSourceIDTypeSafety(self):
		print 'Testing PySource: setting source ID type safety'
		with self.assertRaises(OverflowError):
			s = cp.PySource()
			s.setSourceID(-1)
		with self.assertRaises(TypeError):
			s = cp.PySource()
			s.setSourceID('1')
Пример #2
0
	def testSetSources(self):
		print 'Testing PySourceCatalog: inserting source dictionary'
		s1 = cp.PySource()
		s2 = cp.PySource()
		s1.ID = 10  # this is important, otherwise sources get overwritten
		s2.ID = 11
		s1.setParameter('speed1', 12.345, 0.987, 'm/s')
		s2.setParameter('speed2', 12.345, 0.987, 'm/s')
		sDict = {s1.ID: s1, s2.ID: s2}
		sc = cp.PySourceCatalog()
		sc.setSources(sDict)
Пример #3
0
	def testGetSourceIDs(self):
		print 'Testing PySourceCatalog: getting source IDs'
		s1 = cp.PySource()
		s2 = cp.PySource()
		s1.ID = 10  # this is important, otherwise sources get overwritten
		s2.ID = 11
		s1.setParameter('speed1', 12.345, 0.987, 'm/s')
		s2.setParameter('speed2', 12.345, 0.987, 'm/s')
		sDict = {s1.ID: s1, s2.ID: s2}
		sc = cp.PySourceCatalog()
		sc.setSources(sDict)
		allIDs = sc.getSourceIDs()
		self.assertIsInstance(allIDs, list)
		self.assertEqual(allIDs, [10, 11])
Пример #4
0
	def testSetParameters(self):
		print 'Testing PySource: setting parameters dictionary'
		s1 = cp.PySource()
		s2 = cp.PySource()
		s1.setParameter('speed1', 12.345, 0.987, 'm/s')
		s1.setParameter('speed2', 12.345, 0.987, 'm/s')
		s2.setParameters(s1.getParameters())
		self.assertIsInstance(s2.getParameters(), dict)
		self.assertEqual(len(s2.getParameters()), 2)
		self.assertListEqual(s2.getParameters().keys(), ['speed1', 'speed2'])
		self.assertIsInstance(s2.getParameters().values()[0], cp.PyMeasurement)
		s2.setParameters({})
		self.assertIsInstance(s2.getParameters(), dict)
		self.assertEqual(len(s2.getParameters()), 0)
Пример #5
0
	def testGetSources(self):
		print 'Testing PySourceCatalog: getting source dictionary'
		s1 = cp.PySource()
		s2 = cp.PySource()
		s1.ID = 10  # this is important, otherwise sources get overwritten
		s2.ID = 11
		s1.setParameter('speed1', 12.345, 0.987, 'm/s')
		s2.setParameter('speed2', 12.345, 0.987, 'm/s')
		sDict = {s1.ID: s1, s2.ID: s2}
		sc = cp.PySourceCatalog()
		sc.setSources(sDict)
		sDictReturned = sc.getSources()
		self.assertIsInstance(sDictReturned, dict)
		self.assertEqual(len(sDict), len(sDictReturned))
		self.assertIsInstance(sDictReturned.values()[0], cp.PySource)
Пример #6
0
	def testCopy(self):
		print 'Testing PySource: make sure, copies are deep'
		s1 = cp.PySource()
		m1 = cp.PyMeasurement('dist', 1.0, 0.5, 'm')
		s1.setParameter(m1)
		s1.ID = 10
		s1.name = 'source1'
		s2 = s1.copy()
		s2.clear()
		s2.setParameter('temp', 2.0, 1.0, 'K')
		s2.ID = 20
		s2.name = 'source2'
		self.assertEqual(m1.name, 'dist')
		self.assertEqual(m1.value, 1.0)
		self.assertEqual(m1.uncertainty, 0.5)
		self.assertEqual(m1.unit.asString(), 'm')
		m1 = s1.getParameter('dist')
		self.assertEqual(m1.name, 'dist')
		self.assertEqual(m1.value, 1.0)
		self.assertEqual(m1.uncertainty, 0.5)
		self.assertEqual(m1.unit.asString(), 'm')
		self.assertEqual(s1.ID, 10)
		self.assertEqual(s2.ID, 20)
		self.assertEqual(s1.name, 'source1')
		self.assertEqual(s2.name, 'source2')
Пример #7
0
	def testSetParameter(self):
		print 'Testing PySource: setting single parameter'
		s = cp.PySource()
		m = cp.PyMeasurement('speed', 12.345, 0.987, 'm/s')
		s.setParameter(m)
		s.setParameter('speed2', 12.345, 0.987, 'm/s')
		s.setParameter('speed3', 12.345, 0.987, cp.PyUnit('m/s'))
Пример #8
0
	def testIsDefined(self):
		print 'Testing PySource: isDefined method'
		s = cp.PySource()
		m = cp.PyMeasurement('speed', 12.345, 0.987, 'm/s')
		s.setParameter(m)
		self.assertEqual(s.parameterDefined('speed'), True)
		self.assertEqual(s.parameterDefined('bla'), False)
Пример #9
0
	def testUpdate(self):
		print 'Testing PySourceCatalog: updating a source'
		s1 = cp.PySource()
		s1.setParameter('speed', 12.345, 0.987, 'm/s')
		s1.ID = 0
		s2 = s1.copy()
		sc = cp.PySourceCatalog()
		sc.insert(s1)
		sc.update(s1.ID, s2)
Пример #10
0
	def testUpdateSourcesDuplicates(self):
		print 'Testing PySourceCatalog: updating source dictionary, test for assert on duplicate'
		s1 = cp.PySource()
		s1.setParameter('speed1', 12.345, 0.987, 'm/s')
		s1.ID = 0
		sc = cp.PySourceCatalog()
		sc.setSources({s1.ID: s1})
		sc.updateSources({s1.ID: s1}, warn_on_duplicate=False)  # this should silently overwrite
		with self.assertRaises(AssertionError):
			sc.updateSources({s1.ID: s1})
Пример #11
0
	def testUpdateParametersTypeSafety(self):
		print 'Testing PySource: updating parameters dictionary type safety'
		s = cp.PySource()
		with self.assertRaises(TypeError):
			s.updateParameters(1)
		with self.assertRaises(AssertionError):
			s.updateParameters({
				0: cp.PyMeasurement('speed', 12.345, 0.987, 'm/s'),
				1: 1
				})
Пример #12
0
	def testUpdateSources(self):
		print 'Testing PySourceCatalog: updating source dictionary'
		s1 = cp.PySource()
		s1.setParameter('speed1', 12.345, 0.987, 'm/s')
		s1.ID = 0
		s2 = s1.copy()
		s2.ID = 1
		sc = cp.PySourceCatalog()
		sc.setSources({s1.ID: s1})
		sc.updateSources({s2.ID: s2})
Пример #13
0
	def testDoubleInsert(self):
		print 'Testing PySourceCatalog: exception on inserting a source with same ID'
		s1 = cp.PySource()
		s1.setParameter('speed', 12.345, 0.987, 'm/s')
		s1.ID = 0
		s2 = s1.copy()
		sc = cp.PySourceCatalog()
		sc.insert(s1)
		with self.assertRaises(ValueError):
			sc.insert(s2)
Пример #14
0
	def testUpdateNotExistent(self):
		print 'Testing PySourceCatalog: exception on updating a source with missing ID'
		s1 = cp.PySource()
		s1.setParameter('speed', 12.345, 0.987, 'm/s')
		s1.ID = 0
		s2 = s1.copy()
		s2.ID = 1000
		sc = cp.PySourceCatalog()
		sc.insert(s1)
		with self.assertRaises(ValueError):
			sc.update(1000, s2)
Пример #15
0
	def testUpdateIDmismatch(self):
		print 'Testing PySourceCatalog: updating a source, mismatch exception on source id and call'
		s1 = cp.PySource()
		s1.setParameter('speed', 12.345, 0.987, 'm/s')
		s1.ID = 0
		s2 = s1.copy()
		self.assertEqual(s1.ID, s2.ID)
		s2.ID = 10
		sc = cp.PySourceCatalog()
		sc.insert(s1)
		with self.assertRaises(AssertionError):
			sc.update(0, s2)
Пример #16
0
	def testUpdateParameters(self):
		print 'Testing PySource: updating parameters dictionary'
		s = cp.PySource()
		s.setParameter('speed1', 12.345, 0.987, 'm/s')
		s.updateParameters({
				'speed2': cp.PyMeasurement('speed2', 12.345, 0.987, 'm/s')
				})
		self.assertIsInstance(s.getParameters(), dict)
		self.assertEqual(len(s.getParameters()), 2)
		self.assertListEqual(s.getParameters().keys(), ['speed1', 'speed2'])
		self.assertIsInstance(s.getParameters().values()[0], cp.PyMeasurement)
		s.updateParameters({})
		self.assertIsInstance(s.getParameters(), dict)
		self.assertEqual(len(s.getParameters()), 2)
		self.assertListEqual(s.getParameters().keys(), ['speed1', 'speed2'])
		self.assertIsInstance(s.getParameters().values()[0], cp.PyMeasurement)
Пример #17
0
		def addGauss(sName, sID, cube, mask, amp, x0, y0, z0, xw, yw, zw):
			"""adds a fake source (3D gauss) and initial mask (few pixels large)
			
			since cube/mask are ndarrays, it's 'call-by-reference'
			
			returns a PySource with just the right parameters"""
			
			def gaussian1D(dist, sigma):
				return np.exp(- dist**2 / 2.0 / sigma**2)
			
			# first need xyz indices
			z, y, x = np.indices(cube.shape)
			
			cube += amp * gaussian1D(x-x0, xw) * gaussian1D(y-y0, yw) * gaussian1D(z-z0, zw)
			mask[
				z0 - 3*zw: z0 + 3*zw,
				y0 - 3*yw: y0 + 3*yw,
				x0 - 3*xw: x0 + 3*xw,
				] = sID
			
			s = cp.PySource()
			s.name = sName
			s.ID = sID
			pDict = {
				'X': cp.PyMeasurement('X', x0, 0.0, ''),
				'Y': cp.PyMeasurement('Y', y0, 0.0, ''),
				'Z': cp.PyMeasurement('Z', z0, 0.0, ''),
				'BBOX_X_MIN': cp.PyMeasurement('BBOX_X_MIN', x0 - 3*xw, 0.0, ''),
				'BBOX_X_MAX': cp.PyMeasurement('BBOX_X_MAX', x0 + 3*xw, 0.0, ''),
				'BBOX_Y_MIN': cp.PyMeasurement('BBOX_Y_MIN', y0 - 3*yw, 0.0, ''),
				'BBOX_Y_MAX': cp.PyMeasurement('BBOX_Y_MAX', y0 + 3*yw, 0.0, ''),
				'BBOX_Z_MIN': cp.PyMeasurement('BBOX_Z_MIN', z0 - 3*zw, 0.0, ''),
				'BBOX_Z_MAX': cp.PyMeasurement('BBOX_Z_MAX', z0 + 3*zw, 0.0, ''),
				}
			s.setParameters(pDict)
			
			return s
Пример #18
0
	def testGetParameterTypeSafety(self):
		print 'Testing PySource: getting single parameter type safety'
		with self.assertRaises(AssertionError):
			s = cp.PySource()
			s.getParameter(1)
Пример #19
0
	def testSourceIDProperties(self):
		print 'Testing PySource: "ID" property getter/setter'
		s = cp.PySource()
		s.ID = 111
		self.assertEqual(s.ID, 111)
Пример #20
0
	def testGetSourceID(self):
		print 'Testing PySource: getting source ID'
		s = cp.PySource()
		s.setSourceID(111)
		self.assertEqual(s.getSourceID(), 111)
Пример #21
0
	def testGetParameterNotFound(self):
		print 'Testing PySource: getting single parameter, exception on not found'
		with self.assertRaises(KeyError):
			s = cp.PySource()
			s.setParameter('speed1', 12.345, 0.987, 'm/s')
			s.getParameter('speed2')
Пример #22
0
	def testSetSourceName(self):
		print 'Testing PySource: setting source name'
		s = cp.PySource()
		s.setSourceName('mysource')
Пример #23
0
	def testIsDefinedTypeSafety(self):
		print 'Testing PySource: isDefined method type safety'
		with self.assertRaises(AssertionError):
			s = cp.PySource()
			s.parameterDefined(1)
Пример #24
0
	def testSetSourceNameTypeSafety(self):
		print 'Testing PySource: setting source name type safety'
		with self.assertRaises(AssertionError):
			s = cp.PySource()
			s.setSourceName(1)
Пример #25
0
	def testSourceNameProperties(self):
		print 'Testing PySource: "name" property getter/setter'
		s = cp.PySource()
		s.name = 'source1'
		self.assertEqual(s.name, 'source1')
Пример #26
0
	def testGetSourceName(self):
		print 'Testing PySource: getting source name'
		s = cp.PySource()
		s.setSourceName('mysource')
		self.assertEqual(s.getSourceName(), 'mysource')
Пример #27
0
	def testGetParameter(self):
		print 'Testing PySource: getting single parameter'
		s = cp.PySource()
		s.setParameter('speed1', 12.345, 0.987, 'm/s')
		self.assertEqual(s.getParameter('speed1').asString(), '12.345 ± 0.987 m s⁻¹')
Пример #28
0
	def testInsert(self):
		print 'Testing PySourceCatalog: inserting a source'
		s = cp.PySource()
		s.setParameter('speed', 12.345, 0.987, 'm/s')
		sc = cp.PySourceCatalog()
		sc.insert(s)
Пример #29
0
def parametrise(cube, mask, objects, cathead, catformt, catparunits,
                Parameters, dunits):
    cathead = np.array(cathead)
    objects = np.array(objects)
    initcatalog = cp.PySourceCatalog()

    for obj in objects:
        # Check flags
        source_flag = create_source_flags(
            cube, mask, cathead, obj[cathead == "id"], obj[cathead == "x_min"],
            obj[cathead == "x_max"], obj[cathead == "y_min"],
            obj[cathead == "y_max"], obj[cathead == "z_min"],
            obj[cathead == "z_max"])

        newSource = cp.PySource()
        newSource.ID = obj[cathead == "id"]
        newParamsDict = {
            "flag": cp.PyMeasurement("flag", source_flag, 0.0, ""),
            "x": cp.PyMeasurement("x", obj[cathead == "x_geo"], 0.0, ""),
            "y": cp.PyMeasurement("y", obj[cathead == "y_geo"], 0.0, ""),
            "z": cp.PyMeasurement("z", obj[cathead == "z_geo"], 0.0, ""),
            "x_min": cp.PyMeasurement("x_min", obj[cathead == "x_min"], 0.0,
                                      ""),
            "x_max": cp.PyMeasurement("x_max", obj[cathead == "x_max"], 0.0,
                                      ""),
            "y_min": cp.PyMeasurement("y_min", obj[cathead == "y_min"], 0.0,
                                      ""),
            "y_max": cp.PyMeasurement("y_max", obj[cathead == "y_max"], 0.0,
                                      ""),
            "z_min": cp.PyMeasurement("z_min", obj[cathead == "z_min"], 0.0,
                                      ""),
            "z_max": cp.PyMeasurement("z_max", obj[cathead == "z_max"], 0.0,
                                      "")
        }
        newSource.setParameters(newParamsDict)
        initcatalog.insert(newSource)

    moduleParametrizer = cp.PyModuleParametrisation()
    moduleParametrizer.setFlags(Parameters["parameters"]["optimiseMask"],
                                Parameters["parameters"]["fitBusyFunction"])

    cube = cube.astype("<f4", copy=False)
    mask = mask.astype("<i2", copy=False)

    moduleParametrizer.run(cube, mask, initcatalog)
    results = moduleParametrizer.getCatalog()

    # Append the results to the objects array or reset
    replParam = [
        "x_min", "x_max", "y_min", "y_max", "z_min", "z_max", "id", "x", "y",
        "z", "n_pix"
    ]
    origParam = [
        "x_min", "x_max", "y_min", "y_max", "z_min", "z_max", "id", "x", "y",
        "z", "n_pix"
    ]
    d = results.getSources()

    # Select data set with maximum number of parameters
    parsListLen = [
        len(d[list(d.keys())[i]].getParameters()) for i in range(0, len(d))
    ]
    index = parsListLen.index(max(parsListLen))

    # Add parameter names from parameterisation
    pars = d[list(d.keys())[index]].getParameters()
    cathead = list(cathead)
    newunits = {
        "id": "-",
        "flag": "-",
        "x": "pix",
        "y": "pix",
        "z": "pix",
        "err_x": "pix",
        "err_y": "pix",
        "err_z": "pix",
        "x_min": "pix",
        "x_max": "pix",
        "y_min": "pix",
        "y_max": "pix",
        "z_min": "chan",
        "z_max": "chan",
        "w50": "chan",
        "w20": "chan",
        "err_w50": "chan",
        "err_w20": "chan",
        "wm50": "chan",
        "f_wm50": dunits,
        "ell_maj": "pix",
        "ell_min": "pix",
        "ell_pa": "deg",
        "ell3s_maj": "pix",
        "ell3s_min": "pix",
        "ell3s_pa": "deg",
        "kin_pa": "deg",
        "f_int": dunits,
        "bf_flag": "-",
        "bf_chi2": "-",
        "bf_z": "chan",
        "bf_a": dunits,
        "bf_b1": "chan**(-1)",
        "bf_b2": "chan**(-1)",
        "bf_c": "chan**(-2)",
        "bf_xe": "chan",
        "bf_xp": "chan",
        "bf_w": "chan",
        "bf_w50": "chan",
        "bf_w20": "chan",
        "bf_f_peak": dunits,
        "bf_f_int": dunits,
        "rms": dunits,
        "f_peak": dunits,
        "snr_int": "-"
    }
    catformt = list(catformt)
    catparunits = list(catparunits)

    for i in sorted(pars):
        if i not in replParam:
            cathead.append(i)
            catformt.append("%18.6e")
            catparunits.append(newunits[i])

    # Extend the parameter array
    tmpObjects = np.empty((objects.shape[0], len(cathead)))
    tmpObjects[:, :] = np.nan
    tmpObjects[:, 0:objects.shape[1]] = objects
    objects = tmpObjects

    for i in d:
        source_dict = d[i].getParameters()

        # Check source index
        ID = int(source_dict["id"].getValue())
        IDind = cathead.index("id")

        ind = np.where(objects[:, IDind] == ID)[0][0]

        for j in sorted(source_dict):
            if j in replParam:
                objects[ind][cathead.index(origParam[replParam.index(
                    j)])] = source_dict[j].getValue()
            else:
                objects[ind][cathead.index(j)] = source_dict[j].getValue()

    objects = np.array(objects)
    cathead = np.array(cathead)
    catparunits = np.array(catparunits)
    catformt = np.array(catformt)

    # if mask optimization is enabled, some parameters from the linker have to be updated
    if Parameters["parameters"]["optimiseMask"]:
        for i in range(objects.shape[0]):
            # bounding box coordinates
            coord = []
            for c in ["x_min", "x_max", "y_min", "y_max", "z_min", "z_max"]:
                coord.append(int(objects[i, cathead == c]))
            # cut out object submask
            submask = mask[coord[4]:coord[5] + 1, coord[2]:coord[3] + 1,
                           coord[0]:coord[1] + 1]
            objID = objects[i, cathead == "id"]
            submask[submask != objID] = 0

            # Update n_pix, x_geo and n_chan
            n_pix = submask.sum() / objID
            ind = np.vstack(np.where(submask == objID))
            cgeo = (ind.sum(axis=1)).astype(float) / float(n_pix)
            x_geo, y_geo, z_geo = cgeo[2] + coord[0], cgeo[1] + coord[2], cgeo[
                0] + coord[4]
            zmin, zmax = min(ind[0]), max(ind[0]) + 1
            n_chan = zmax - zmin

            # Update n_los
            submaskSumA0 = submask.sum(axis=0)
            submaskSumA0[submaskSumA0 > 1] = 1
            n_los = submaskSumA0.sum()

            objects[i, cathead == "n_pix"] = n_pix
            objects[i, cathead == "n_chan"] = n_chan
            objects[i, cathead == "n_los"] = n_los
            objects[i, cathead == "x_geo"] = x_geo
            objects[i, cathead == "y_geo"] = y_geo
            objects[i, cathead == "z_geo"] = z_geo
        del submask

    err.message("Parameterisation complete.")

    return cube, mask, objects, cathead, catformt, catparunits
Пример #30
0
	def testSetSourceID(self):
		print 'Testing PySource: setting source ID'
		s = cp.PySource()
		s.setSourceID(111)