當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。