当前位置: 首页>>代码示例>>Python>>正文


Python FieldArray.add_fields方法代码示例

本文整理汇总了Python中pycbc.io.FieldArray.add_fields方法的典型用法代码示例。如果您正苦于以下问题:Python FieldArray.add_fields方法的具体用法?Python FieldArray.add_fields怎么用?Python FieldArray.add_fields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pycbc.io.FieldArray的用法示例。


在下文中一共展示了FieldArray.add_fields方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TemplateBank

# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import add_fields [as 别名]
class TemplateBank(object):
    """ Class to provide some basic helper functions and information
    about elements of an xml template bank.
    """
    def __init__(self, filename, approximant=None, **kwds):
        ext = os.path.basename(filename)
        if 'xml' in ext:
            self.indoc = ligolw_utils.load_filename(
                filename, False, contenthandler=LIGOLWContentHandler)
            self.table = table.get_table(
                self.indoc, lsctables.SnglInspiralTable.tableName)
            self.table = FieldArray.from_ligolw_table(self.table)

            # inclination stored in xml alpha3 column
            names = list(self.table.dtype.names)
            names = tuple([n if n != 'alpha3' else 'inclination' for n in names]) 
            self.table.dtype.names = names    

        elif 'hdf' in ext:
            f = h5py.File(filename, 'r')
            dtype = []
            data = {}
            for key in f.keys():
                try:
                    data[str(key)] = f[key][:]
                    dtype.append((str(key), data[key].dtype))
                except:
                    pass

            num = len(data[data.keys()[0]])
            self.table = FieldArray(num, dtype=dtype)
            for key in data:
                self.table[key] = data[key]
        else:
            raise ValueError("Unsupported template bank file extension %s" % ext)

        if not hasattr(self.table, 'template_duration'):
            self.table = self.table.add_fields(numpy.zeros(len(self.table),
                                     dtype=numpy.float32), 'template_duration') 
        self.extra_args = kwds  
        self.approximant_str = approximant

    @staticmethod
    def parse_option(row, arg):
        safe_dict = {}
        safe_dict.update(row.__dict__)
        safe_dict.update(math.__dict__)
        return eval(arg, {"__builtins__":None}, safe_dict)

    def end_frequency(self, index):
        """ Return the end frequency of the waveform at the given index value
        """
        from pycbc.waveform.waveform import props

        return pycbc.waveform.get_waveform_end_frequency(self.table[index],
                              approximant=self.approximant(index),
                              **self.extra_args)      

    def approximant(self, index):
        """ Return the name of the approximant ot use at the given index
        """
        if self.approximant_str is not None:
            if 'params' in self.approximant_str:
                t = type('t', (object,), {'params' : self.table[index]})
                approximant = str(self.parse_option(t, self.approximant_str)) 
            else:
                approximant = self.approximant_str
        else:
            raise ValueError("Reading approximant from template bank not yet supported")

        return approximant

    def __len__(self):
        return len(self.table)
开发者ID:millsjc,项目名称:pycbc,代码行数:76,代码来源:bank.py


注:本文中的pycbc.io.FieldArray.add_fields方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。