def test_pull_params(self): """ Basic functionality tested here, but this function is deprecated due to some serious flaws. See ``test_get_toplevel_params``. """ swift_params = vp.pull_params(self.swift_grb_v2_packet) # General example, check basic functionality self.assertEqual(swift_params[None]['Packet_Type']['value'], '61') self.assertEqual( swift_params['Misc_Flags']['Values_Out_of_Range']['value'], 'false') # Check ordering is preserved self.assertEqual( list(swift_params[None].keys())[:3], ['Packet_Type', 'Pkt_Ser_Num', 'TrigID']) # Test empty What section params = vp.pull_params(self.blank) self.assertEqual(params, {}) # Test known (generated) example single_par = copy(self.blank) single_par.What.append(vp.Param(name="test_param", value=123)) params = vp.pull_params(single_par) self.assertEqual(len(params), 1) self.assertEqual(list(params[None].keys()), ['test_param']) # Test case where a What Group is present, but empty: params = vp.pull_params(self.moa_packet) self.assertEqual(params['Misc_Flags'], {}) # Test case where a Param is present with no name: params = vp.pull_params(self.gaia_noname_param_packet)
def test_pull_params(self): """ Basic functionality tested here, but this function is deprecated due to some serious flaws. See ``test_get_toplevel_params``. """ swift_params = vp.pull_params(self.swift_grb_v2_packet) # General example, check basic functionality self.assertEqual(swift_params[None]['Packet_Type']['value'], '61') self.assertEqual( swift_params['Misc_Flags']['Values_Out_of_Range']['value'], 'false') # Check ordering is preserved self.assertEqual(list(swift_params[None].keys())[:3], ['Packet_Type', 'Pkt_Ser_Num', 'TrigID']) # Test empty What section params = vp.pull_params(self.blank) self.assertEqual(params, {}) # Test known (generated) example single_par = copy(self.blank) single_par.What.append(vp.Param(name="test_param", value=123)) params = vp.pull_params(single_par) self.assertEqual(len(params), 1) self.assertEqual(list(params[None].keys()), ['test_param']) # Test case where a What Group is present, but empty: params = vp.pull_params(self.moa_packet) self.assertEqual(params['Misc_Flags'], {}) # Test case where a Param is present with no name: params = vp.pull_params(self.gaia_noname_param_packet)
def test_get_toplevel_params(self): v = self.blank p_foo1 = vp.Param(name='foo', value=42, unit='bars', ac=True ) p_foo2 = vp.Param(name='foo', value=123, unit='bars', ac=True ) p_noname = vp.Param(name='delete_me', value=111) param_list = [p_foo1, p_foo2, p_noname] del p_noname.attrib['name'] v.What.extend(param_list) # Show flaws in old routine: with pytest.warns(FutureWarning): old_style_toplevel_param_dict = vp.pull_params(v)[None] # print(old_style_toplevel_param_dict) vp.assert_valid_as_v2_0(v) # The old 'pull_params' routine will simply drop Params with duplicate # names: assert len(old_style_toplevel_param_dict)==(len(param_list) - 1) none_group = vp.Group([],name=None) complex_group1 =vp.Group([vp.Param(name='real', value=1.), vp.Param(name='imag', value=0.5)], name='complex') complex_group2 =vp.Group([vp.Param(name='real', value=1.5), vp.Param(name='imag', value=2.5)], name='complex') group_list = [none_group, complex_group1, complex_group2] v.What.extend(group_list) vp.assert_valid_as_v2_0(v) old_style_toplevel_param_dict_w_group = vp.pull_params(v)[None] # An un-named group will also overshadow top-level Params. # This is a total fail, even though it's actually in-spec. assert len(old_style_toplevel_param_dict_w_group)==0 toplevel_params = vp.get_toplevel_params(v) # .values method behaves like regular dict, one value for each key: assert len(toplevel_params.values())==(len(param_list) - 1) # Use .allvalues if you want to see duplicates: assert len(toplevel_params.allvalues())==len(param_list) grouped_params = vp.get_grouped_params(v) assert len(grouped_params.values())==len(group_list) - 1 assert len(grouped_params.allvalues())==len(group_list)
def test_get_toplevel_params(self): v = self.blank p_foo1 = vp.Param(name='foo', value=42, unit='bars', ac=True) p_foo2 = vp.Param(name='foo', value=123, unit='bars', ac=True) p_noname = vp.Param(name='delete_me', value=111) param_list = [p_foo1, p_foo2, p_noname] del p_noname.attrib['name'] v.What.extend(param_list) # Show flaws in old routine: with pytest.warns(FutureWarning): old_style_toplevel_param_dict = vp.pull_params(v)[None] # print(old_style_toplevel_param_dict) vp.assert_valid_as_v2_0(v) # The old 'pull_params' routine will simply drop Params with duplicate # names: assert len(old_style_toplevel_param_dict) == (len(param_list) - 1) none_group = vp.Group([], name=None) complex_group1 = vp.Group([ vp.Param(name='real', value=1.), vp.Param(name='imag', value=0.5) ], name='complex') complex_group2 = vp.Group([ vp.Param(name='real', value=1.5), vp.Param(name='imag', value=2.5) ], name='complex') group_list = [none_group, complex_group1, complex_group2] v.What.extend(group_list) vp.assert_valid_as_v2_0(v) old_style_toplevel_param_dict_w_group = vp.pull_params(v)[None] # An un-named group will also overshadow top-level Params. # This is a total fail, even though it's actually in-spec. assert len(old_style_toplevel_param_dict_w_group) == 0 toplevel_params = vp.get_toplevel_params(v) # .values method behaves like regular dict, one value for each key: assert len(toplevel_params.values()) == (len(param_list) - 1) # Use .allvalues if you want to see duplicates: assert len(toplevel_params.allvalues()) == len(param_list) grouped_params = vp.get_grouped_params(v) assert len(grouped_params.values()) == len(group_list) - 1 assert len(grouped_params.allvalues()) == len(group_list)
def __init__(self, voevent): self.voevent = voevent if not filters.is_bat_grb_pkt(voevent): raise ValueError("Cannot instantiate BatGrb; packet header mismatch.") self.description = "Swift BAT GRB - initial position" id_long_short = BatGrb._pull_swift_bat_id(self.voevent) self.id_long = 'SWIFT' + id_long_short[0] self.id = 'SWIFT_' + id_long_short[1] #Assigned name according to the 'why' section of voevent packet: self.inferred_name = self.voevent.Why.Inference.Name self.isotime = voeventparse.pull_isotime(self.voevent) self.params = voeventparse.pull_params(self.voevent) self.position = convert_voe_coords_to_eqposn( voeventparse.pull_astro_coords(self.voevent))
print(v.Why.Inference.Name.text) # The safe option print(v.Why.Inference.Name.text[:3]) # Indexing on the string as you'd expect print(v.Why.Inference.Name[:3]) # This is indexing on the *list of elements*, not the string! # If that all sounds awfully messy, help is at hand: you're most likely to encounter sibling elements under the ``What`` entry of a VOEvent, and voevent-parse has a function to convert that to a nested Python dictionary for you: # In[ ]: # Consult the docstring #?vp.pull_params # In[ ]: what_dict = vp.pull_params(v) what_dict # In[ ]: what_dict['source_flux']['peak_flux']['value'] # ##Advanced## # Since voevent-parse uses lxml.objectify, the full power of the LXML library is available when handling VOEvents loaded with voevent-parse. # # ###Iterating over child-elements### # We already saw how you can access a group of child-elements by name, in list-like fashion. But you can also iterate over all the children of an element, even if you don't know the names ('tags', in XML-speak) ahead of time: # In[ ]:
) # More syntax sugar - if it has a string-value but no children, print the string print(v.Why.Inference.Name.text) # The safe option print(v.Why.Inference.Name.text[:3]) # Indexing on the string as you'd expect print(v.Why.Inference.Name[:3] ) # This is indexing on the *list of elements*, not the string! # If that all sounds awfully messy, help is at hand: you're most likely to encounter sibling elements under the ``What`` entry of a VOEvent, and voevent-parse has a function to convert that to a nested Python dictionary for you: # In[ ]: # Consult the docstring #?vp.pull_params # In[ ]: what_dict = vp.pull_params(v) what_dict # In[ ]: what_dict['source_flux']['peak_flux']['value'] # ##Advanced## # Since voevent-parse uses lxml.objectify, the full power of the LXML library is available when handling VOEvents loaded with voevent-parse. # # ###Iterating over child-elements### # We already saw how you can access a group of child-elements by name, in list-like fashion. But you can also iterate over all the children of an element, even if you don't know the names ('tags', in XML-speak) ahead of time: # In[ ]: for child in v.Who.iterchildren():