repository_name
stringlengths
5
67
func_path_in_repository
stringlengths
4
234
func_name
stringlengths
0
314
whole_func_string
stringlengths
52
3.87M
language
stringclasses
6 values
func_code_string
stringlengths
39
1.84M
func_code_tokens
sequencelengths
15
672k
func_documentation_string
stringlengths
1
47.2k
func_documentation_tokens
sequencelengths
1
3.92k
split_name
stringclasses
1 value
func_code_url
stringlengths
85
339
gem/oq-engine
openquake/calculators/export/hazard.py
export_ruptures_csv
def export_ruptures_csv(ekey, dstore): """ :param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object """ oq = dstore['oqparam'] if 'scenario' in oq.calculation_mode: return [] dest = dstore.export_path('ruptures.csv') header = ('rupid multiplicity mag centroid_lon centroid_lat ' 'centroid_depth trt strike dip rake boundary').split() rows = [] for rgetter in gen_rupture_getters(dstore): rups = rgetter.get_ruptures() rup_data = calc.RuptureData(rgetter.trt, rgetter.rlzs_by_gsim) for r in rup_data.to_array(rups): rows.append( (r['rup_id'], r['multiplicity'], r['mag'], r['lon'], r['lat'], r['depth'], rgetter.trt, r['strike'], r['dip'], r['rake'], r['boundary'])) rows.sort() # by rupture serial comment = 'investigation_time=%s, ses_per_logic_tree_path=%s' % ( oq.investigation_time, oq.ses_per_logic_tree_path) writers.write_csv(dest, rows, header=header, sep='\t', comment=comment) return [dest]
python
def export_ruptures_csv(ekey, dstore): oq = dstore['oqparam'] if 'scenario' in oq.calculation_mode: return [] dest = dstore.export_path('ruptures.csv') header = ('rupid multiplicity mag centroid_lon centroid_lat ' 'centroid_depth trt strike dip rake boundary').split() rows = [] for rgetter in gen_rupture_getters(dstore): rups = rgetter.get_ruptures() rup_data = calc.RuptureData(rgetter.trt, rgetter.rlzs_by_gsim) for r in rup_data.to_array(rups): rows.append( (r['rup_id'], r['multiplicity'], r['mag'], r['lon'], r['lat'], r['depth'], rgetter.trt, r['strike'], r['dip'], r['rake'], r['boundary'])) rows.sort() comment = 'investigation_time=%s, ses_per_logic_tree_path=%s' % ( oq.investigation_time, oq.ses_per_logic_tree_path) writers.write_csv(dest, rows, header=header, sep='\t', comment=comment) return [dest]
[ "def", "export_ruptures_csv", "(", "ekey", ",", "dstore", ")", ":", "oq", "=", "dstore", "[", "'oqparam'", "]", "if", "'scenario'", "in", "oq", ".", "calculation_mode", ":", "return", "[", "]", "dest", "=", "dstore", ".", "export_path", "(", "'ruptures.csv'", ")", "header", "=", "(", "'rupid multiplicity mag centroid_lon centroid_lat '", "'centroid_depth trt strike dip rake boundary'", ")", ".", "split", "(", ")", "rows", "=", "[", "]", "for", "rgetter", "in", "gen_rupture_getters", "(", "dstore", ")", ":", "rups", "=", "rgetter", ".", "get_ruptures", "(", ")", "rup_data", "=", "calc", ".", "RuptureData", "(", "rgetter", ".", "trt", ",", "rgetter", ".", "rlzs_by_gsim", ")", "for", "r", "in", "rup_data", ".", "to_array", "(", "rups", ")", ":", "rows", ".", "append", "(", "(", "r", "[", "'rup_id'", "]", ",", "r", "[", "'multiplicity'", "]", ",", "r", "[", "'mag'", "]", ",", "r", "[", "'lon'", "]", ",", "r", "[", "'lat'", "]", ",", "r", "[", "'depth'", "]", ",", "rgetter", ".", "trt", ",", "r", "[", "'strike'", "]", ",", "r", "[", "'dip'", "]", ",", "r", "[", "'rake'", "]", ",", "r", "[", "'boundary'", "]", ")", ")", "rows", ".", "sort", "(", ")", "# by rupture serial", "comment", "=", "'investigation_time=%s, ses_per_logic_tree_path=%s'", "%", "(", "oq", ".", "investigation_time", ",", "oq", ".", "ses_per_logic_tree_path", ")", "writers", ".", "write_csv", "(", "dest", ",", "rows", ",", "header", "=", "header", ",", "sep", "=", "'\\t'", ",", "comment", "=", "comment", ")", "return", "[", "dest", "]" ]
:param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object
[ ":", "param", "ekey", ":", "export", "key", "i", ".", "e", ".", "a", "pair", "(", "datastore", "key", "fmt", ")", ":", "param", "dstore", ":", "datastore", "object" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L81-L106
gem/oq-engine
openquake/calculators/export/hazard.py
export_hmaps_csv
def export_hmaps_csv(key, dest, sitemesh, array, comment): """ Export the hazard maps of the given realization into CSV. :param key: output_type and export_type :param dest: name of the exported file :param sitemesh: site collection :param array: a composite array of dtype hmap_dt :param comment: comment to use as header of the exported CSV file """ curves = util.compose_arrays(sitemesh, array) writers.write_csv(dest, curves, comment=comment) return [dest]
python
def export_hmaps_csv(key, dest, sitemesh, array, comment): curves = util.compose_arrays(sitemesh, array) writers.write_csv(dest, curves, comment=comment) return [dest]
[ "def", "export_hmaps_csv", "(", "key", ",", "dest", ",", "sitemesh", ",", "array", ",", "comment", ")", ":", "curves", "=", "util", ".", "compose_arrays", "(", "sitemesh", ",", "array", ")", "writers", ".", "write_csv", "(", "dest", ",", "curves", ",", "comment", "=", "comment", ")", "return", "[", "dest", "]" ]
Export the hazard maps of the given realization into CSV. :param key: output_type and export_type :param dest: name of the exported file :param sitemesh: site collection :param array: a composite array of dtype hmap_dt :param comment: comment to use as header of the exported CSV file
[ "Export", "the", "hazard", "maps", "of", "the", "given", "realization", "into", "CSV", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L223-L235
gem/oq-engine
openquake/calculators/export/hazard.py
add_imt
def add_imt(fname, imt): """ >>> add_imt('/path/to/hcurve_23.csv', 'SA(0.1)') '/path/to/hcurve-SA(0.1)_23.csv' """ name = os.path.basename(fname) newname = re.sub(r'(_\d+\.)', '-%s\\1' % imt, name) return os.path.join(os.path.dirname(fname), newname)
python
def add_imt(fname, imt): name = os.path.basename(fname) newname = re.sub(r'(_\d+\.)', '-%s\\1' % imt, name) return os.path.join(os.path.dirname(fname), newname)
[ "def", "add_imt", "(", "fname", ",", "imt", ")", ":", "name", "=", "os", ".", "path", ".", "basename", "(", "fname", ")", "newname", "=", "re", ".", "sub", "(", "r'(_\\d+\\.)'", ",", "'-%s\\\\1'", "%", "imt", ",", "name", ")", "return", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "dirname", "(", "fname", ")", ",", "newname", ")" ]
>>> add_imt('/path/to/hcurve_23.csv', 'SA(0.1)') '/path/to/hcurve-SA(0.1)_23.csv'
[ ">>>", "add_imt", "(", "/", "path", "/", "to", "/", "hcurve_23", ".", "csv", "SA", "(", "0", ".", "1", ")", ")", "/", "path", "/", "to", "/", "hcurve", "-", "SA", "(", "0", ".", "1", ")", "_23", ".", "csv" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L238-L245
gem/oq-engine
openquake/calculators/export/hazard.py
export_hcurves_by_imt_csv
def export_hcurves_by_imt_csv( key, kind, rlzs_assoc, fname, sitecol, array, oq, checksum): """ Export the curves of the given realization into CSV. :param key: output_type and export_type :param kind: a string with the kind of output (realization or statistics) :param rlzs_assoc: a :class:`openquake.commonlib.source.RlzsAssoc` instance :param fname: name of the exported file :param sitecol: site collection :param array: an array of shape (N, L) and dtype numpy.float32 :param oq: job.ini parameters """ nsites = len(sitecol) fnames = [] for imt, imls in oq.imtls.items(): slc = oq.imtls(imt) dest = add_imt(fname, imt) lst = [('lon', F32), ('lat', F32), ('depth', F32)] for iml in imls: lst.append(('poe-%s' % iml, F32)) hcurves = numpy.zeros(nsites, lst) for sid, lon, lat, dep in zip( range(nsites), sitecol.lons, sitecol.lats, sitecol.depths): hcurves[sid] = (lon, lat, dep) + tuple(array[sid, slc]) fnames.append(writers.write_csv(dest, hcurves, comment=_comment( rlzs_assoc, kind, oq.investigation_time) + ( ', imt="%s", checksum=%d' % (imt, checksum) ), header=[name for (name, dt) in lst])) return fnames
python
def export_hcurves_by_imt_csv( key, kind, rlzs_assoc, fname, sitecol, array, oq, checksum): nsites = len(sitecol) fnames = [] for imt, imls in oq.imtls.items(): slc = oq.imtls(imt) dest = add_imt(fname, imt) lst = [('lon', F32), ('lat', F32), ('depth', F32)] for iml in imls: lst.append(('poe-%s' % iml, F32)) hcurves = numpy.zeros(nsites, lst) for sid, lon, lat, dep in zip( range(nsites), sitecol.lons, sitecol.lats, sitecol.depths): hcurves[sid] = (lon, lat, dep) + tuple(array[sid, slc]) fnames.append(writers.write_csv(dest, hcurves, comment=_comment( rlzs_assoc, kind, oq.investigation_time) + ( ', imt="%s", checksum=%d' % (imt, checksum) ), header=[name for (name, dt) in lst])) return fnames
[ "def", "export_hcurves_by_imt_csv", "(", "key", ",", "kind", ",", "rlzs_assoc", ",", "fname", ",", "sitecol", ",", "array", ",", "oq", ",", "checksum", ")", ":", "nsites", "=", "len", "(", "sitecol", ")", "fnames", "=", "[", "]", "for", "imt", ",", "imls", "in", "oq", ".", "imtls", ".", "items", "(", ")", ":", "slc", "=", "oq", ".", "imtls", "(", "imt", ")", "dest", "=", "add_imt", "(", "fname", ",", "imt", ")", "lst", "=", "[", "(", "'lon'", ",", "F32", ")", ",", "(", "'lat'", ",", "F32", ")", ",", "(", "'depth'", ",", "F32", ")", "]", "for", "iml", "in", "imls", ":", "lst", ".", "append", "(", "(", "'poe-%s'", "%", "iml", ",", "F32", ")", ")", "hcurves", "=", "numpy", ".", "zeros", "(", "nsites", ",", "lst", ")", "for", "sid", ",", "lon", ",", "lat", ",", "dep", "in", "zip", "(", "range", "(", "nsites", ")", ",", "sitecol", ".", "lons", ",", "sitecol", ".", "lats", ",", "sitecol", ".", "depths", ")", ":", "hcurves", "[", "sid", "]", "=", "(", "lon", ",", "lat", ",", "dep", ")", "+", "tuple", "(", "array", "[", "sid", ",", "slc", "]", ")", "fnames", ".", "append", "(", "writers", ".", "write_csv", "(", "dest", ",", "hcurves", ",", "comment", "=", "_comment", "(", "rlzs_assoc", ",", "kind", ",", "oq", ".", "investigation_time", ")", "+", "(", "', imt=\"%s\", checksum=%d'", "%", "(", "imt", ",", "checksum", ")", ")", ",", "header", "=", "[", "name", "for", "(", "name", ",", "dt", ")", "in", "lst", "]", ")", ")", "return", "fnames" ]
Export the curves of the given realization into CSV. :param key: output_type and export_type :param kind: a string with the kind of output (realization or statistics) :param rlzs_assoc: a :class:`openquake.commonlib.source.RlzsAssoc` instance :param fname: name of the exported file :param sitecol: site collection :param array: an array of shape (N, L) and dtype numpy.float32 :param oq: job.ini parameters
[ "Export", "the", "curves", "of", "the", "given", "realization", "into", "CSV", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L248-L277
gem/oq-engine
openquake/calculators/export/hazard.py
hazard_curve_name
def hazard_curve_name(dstore, ekey, kind, rlzs_assoc): """ :param calc_id: the calculation ID :param ekey: the export key :param kind: the kind of key :param rlzs_assoc: a RlzsAssoc instance """ key, fmt = ekey prefix = {'hcurves': 'hazard_curve', 'hmaps': 'hazard_map', 'uhs': 'hazard_uhs'}[key] if kind.startswith('quantile-'): # strip the 7 characters 'hazard_' fname = dstore.build_fname('quantile_' + prefix[7:], kind[9:], fmt) else: fname = dstore.build_fname(prefix, kind, fmt) return fname
python
def hazard_curve_name(dstore, ekey, kind, rlzs_assoc): key, fmt = ekey prefix = {'hcurves': 'hazard_curve', 'hmaps': 'hazard_map', 'uhs': 'hazard_uhs'}[key] if kind.startswith('quantile-'): fname = dstore.build_fname('quantile_' + prefix[7:], kind[9:], fmt) else: fname = dstore.build_fname(prefix, kind, fmt) return fname
[ "def", "hazard_curve_name", "(", "dstore", ",", "ekey", ",", "kind", ",", "rlzs_assoc", ")", ":", "key", ",", "fmt", "=", "ekey", "prefix", "=", "{", "'hcurves'", ":", "'hazard_curve'", ",", "'hmaps'", ":", "'hazard_map'", ",", "'uhs'", ":", "'hazard_uhs'", "}", "[", "key", "]", "if", "kind", ".", "startswith", "(", "'quantile-'", ")", ":", "# strip the 7 characters 'hazard_'", "fname", "=", "dstore", ".", "build_fname", "(", "'quantile_'", "+", "prefix", "[", "7", ":", "]", ",", "kind", "[", "9", ":", "]", ",", "fmt", ")", "else", ":", "fname", "=", "dstore", ".", "build_fname", "(", "prefix", ",", "kind", ",", "fmt", ")", "return", "fname" ]
:param calc_id: the calculation ID :param ekey: the export key :param kind: the kind of key :param rlzs_assoc: a RlzsAssoc instance
[ ":", "param", "calc_id", ":", "the", "calculation", "ID", ":", "param", "ekey", ":", "the", "export", "key", ":", "param", "kind", ":", "the", "kind", "of", "key", ":", "param", "rlzs_assoc", ":", "a", "RlzsAssoc", "instance" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L280-L294
gem/oq-engine
openquake/calculators/export/hazard.py
get_kkf
def get_kkf(ekey): """ :param ekey: export key, for instance ('uhs/rlz-1', 'xml') :returns: key, kind and fmt from the export key, i.e. 'uhs', 'rlz-1', 'xml' """ key, fmt = ekey if '/' in key: key, kind = key.split('/', 1) else: kind = '' return key, kind, fmt
python
def get_kkf(ekey): key, fmt = ekey if '/' in key: key, kind = key.split('/', 1) else: kind = '' return key, kind, fmt
[ "def", "get_kkf", "(", "ekey", ")", ":", "key", ",", "fmt", "=", "ekey", "if", "'/'", "in", "key", ":", "key", ",", "kind", "=", "key", ".", "split", "(", "'/'", ",", "1", ")", "else", ":", "kind", "=", "''", "return", "key", ",", "kind", ",", "fmt" ]
:param ekey: export key, for instance ('uhs/rlz-1', 'xml') :returns: key, kind and fmt from the export key, i.e. 'uhs', 'rlz-1', 'xml'
[ ":", "param", "ekey", ":", "export", "key", "for", "instance", "(", "uhs", "/", "rlz", "-", "1", "xml", ")", ":", "returns", ":", "key", "kind", "and", "fmt", "from", "the", "export", "key", "i", ".", "e", ".", "uhs", "rlz", "-", "1", "xml" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L319-L329
gem/oq-engine
openquake/calculators/export/hazard.py
export_hcurves_csv
def export_hcurves_csv(ekey, dstore): """ Exports the hazard curves into several .csv files :param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object """ oq = dstore['oqparam'] info = get_info(dstore) rlzs_assoc = dstore['csm_info'].get_rlzs_assoc() R = len(rlzs_assoc.realizations) sitecol = dstore['sitecol'] sitemesh = get_mesh(sitecol) key, kind, fmt = get_kkf(ekey) fnames = [] checksum = dstore.get_attr('/', 'checksum32') hmap_dt = oq.hmap_dt() for kind in oq.get_kinds(kind, R): fname = hazard_curve_name(dstore, (key, fmt), kind, rlzs_assoc) comment = _comment(rlzs_assoc, kind, oq.investigation_time) if (key in ('hmaps', 'uhs') and oq.uniform_hazard_spectra or oq.hazard_maps): hmap = extract(dstore, 'hmaps?kind=' + kind)[kind] if key == 'uhs' and oq.poes and oq.uniform_hazard_spectra: uhs_curves = calc.make_uhs(hmap, info) writers.write_csv( fname, util.compose_arrays(sitemesh, uhs_curves), comment=comment + ', checksum=%d' % checksum) fnames.append(fname) elif key == 'hmaps' and oq.poes and oq.hazard_maps: fnames.extend( export_hmaps_csv(ekey, fname, sitemesh, hmap.flatten().view(hmap_dt), comment + ', checksum=%d' % checksum)) elif key == 'hcurves': hcurves = extract(dstore, 'hcurves?kind=' + kind)[kind] fnames.extend( export_hcurves_by_imt_csv( ekey, kind, rlzs_assoc, fname, sitecol, hcurves, oq, checksum)) return sorted(fnames)
python
def export_hcurves_csv(ekey, dstore): oq = dstore['oqparam'] info = get_info(dstore) rlzs_assoc = dstore['csm_info'].get_rlzs_assoc() R = len(rlzs_assoc.realizations) sitecol = dstore['sitecol'] sitemesh = get_mesh(sitecol) key, kind, fmt = get_kkf(ekey) fnames = [] checksum = dstore.get_attr('/', 'checksum32') hmap_dt = oq.hmap_dt() for kind in oq.get_kinds(kind, R): fname = hazard_curve_name(dstore, (key, fmt), kind, rlzs_assoc) comment = _comment(rlzs_assoc, kind, oq.investigation_time) if (key in ('hmaps', 'uhs') and oq.uniform_hazard_spectra or oq.hazard_maps): hmap = extract(dstore, 'hmaps?kind=' + kind)[kind] if key == 'uhs' and oq.poes and oq.uniform_hazard_spectra: uhs_curves = calc.make_uhs(hmap, info) writers.write_csv( fname, util.compose_arrays(sitemesh, uhs_curves), comment=comment + ', checksum=%d' % checksum) fnames.append(fname) elif key == 'hmaps' and oq.poes and oq.hazard_maps: fnames.extend( export_hmaps_csv(ekey, fname, sitemesh, hmap.flatten().view(hmap_dt), comment + ', checksum=%d' % checksum)) elif key == 'hcurves': hcurves = extract(dstore, 'hcurves?kind=' + kind)[kind] fnames.extend( export_hcurves_by_imt_csv( ekey, kind, rlzs_assoc, fname, sitecol, hcurves, oq, checksum)) return sorted(fnames)
[ "def", "export_hcurves_csv", "(", "ekey", ",", "dstore", ")", ":", "oq", "=", "dstore", "[", "'oqparam'", "]", "info", "=", "get_info", "(", "dstore", ")", "rlzs_assoc", "=", "dstore", "[", "'csm_info'", "]", ".", "get_rlzs_assoc", "(", ")", "R", "=", "len", "(", "rlzs_assoc", ".", "realizations", ")", "sitecol", "=", "dstore", "[", "'sitecol'", "]", "sitemesh", "=", "get_mesh", "(", "sitecol", ")", "key", ",", "kind", ",", "fmt", "=", "get_kkf", "(", "ekey", ")", "fnames", "=", "[", "]", "checksum", "=", "dstore", ".", "get_attr", "(", "'/'", ",", "'checksum32'", ")", "hmap_dt", "=", "oq", ".", "hmap_dt", "(", ")", "for", "kind", "in", "oq", ".", "get_kinds", "(", "kind", ",", "R", ")", ":", "fname", "=", "hazard_curve_name", "(", "dstore", ",", "(", "key", ",", "fmt", ")", ",", "kind", ",", "rlzs_assoc", ")", "comment", "=", "_comment", "(", "rlzs_assoc", ",", "kind", ",", "oq", ".", "investigation_time", ")", "if", "(", "key", "in", "(", "'hmaps'", ",", "'uhs'", ")", "and", "oq", ".", "uniform_hazard_spectra", "or", "oq", ".", "hazard_maps", ")", ":", "hmap", "=", "extract", "(", "dstore", ",", "'hmaps?kind='", "+", "kind", ")", "[", "kind", "]", "if", "key", "==", "'uhs'", "and", "oq", ".", "poes", "and", "oq", ".", "uniform_hazard_spectra", ":", "uhs_curves", "=", "calc", ".", "make_uhs", "(", "hmap", ",", "info", ")", "writers", ".", "write_csv", "(", "fname", ",", "util", ".", "compose_arrays", "(", "sitemesh", ",", "uhs_curves", ")", ",", "comment", "=", "comment", "+", "', checksum=%d'", "%", "checksum", ")", "fnames", ".", "append", "(", "fname", ")", "elif", "key", "==", "'hmaps'", "and", "oq", ".", "poes", "and", "oq", ".", "hazard_maps", ":", "fnames", ".", "extend", "(", "export_hmaps_csv", "(", "ekey", ",", "fname", ",", "sitemesh", ",", "hmap", ".", "flatten", "(", ")", ".", "view", "(", "hmap_dt", ")", ",", "comment", "+", "', checksum=%d'", "%", "checksum", ")", ")", "elif", "key", "==", "'hcurves'", ":", "hcurves", "=", "extract", "(", "dstore", ",", "'hcurves?kind='", "+", "kind", ")", "[", "kind", "]", "fnames", ".", "extend", "(", "export_hcurves_by_imt_csv", "(", "ekey", ",", "kind", ",", "rlzs_assoc", ",", "fname", ",", "sitecol", ",", "hcurves", ",", "oq", ",", "checksum", ")", ")", "return", "sorted", "(", "fnames", ")" ]
Exports the hazard curves into several .csv files :param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object
[ "Exports", "the", "hazard", "curves", "into", "several", ".", "csv", "files" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L333-L373
gem/oq-engine
openquake/calculators/export/hazard.py
get_metadata
def get_metadata(realizations, kind): """ :param list realizations: realization objects :param str kind: kind of data, i.e. a key in the datastore :returns: a dictionary with smlt_path, gsimlt_path, statistics, quantile_value """ metadata = {} if kind.startswith('rlz-'): rlz = realizations[int(kind[4:])] metadata['smlt_path'] = '_'.join(rlz.sm_lt_path) metadata['gsimlt_path'] = rlz.gsim_rlz.uid elif kind.startswith('quantile-'): metadata['statistics'] = 'quantile' metadata['quantile_value'] = float(kind[9:]) elif kind == 'mean': metadata['statistics'] = 'mean' elif kind == 'max': metadata['statistics'] = 'max' elif kind == 'std': metadata['statistics'] = 'std' return metadata
python
def get_metadata(realizations, kind): metadata = {} if kind.startswith('rlz-'): rlz = realizations[int(kind[4:])] metadata['smlt_path'] = '_'.join(rlz.sm_lt_path) metadata['gsimlt_path'] = rlz.gsim_rlz.uid elif kind.startswith('quantile-'): metadata['statistics'] = 'quantile' metadata['quantile_value'] = float(kind[9:]) elif kind == 'mean': metadata['statistics'] = 'mean' elif kind == 'max': metadata['statistics'] = 'max' elif kind == 'std': metadata['statistics'] = 'std' return metadata
[ "def", "get_metadata", "(", "realizations", ",", "kind", ")", ":", "metadata", "=", "{", "}", "if", "kind", ".", "startswith", "(", "'rlz-'", ")", ":", "rlz", "=", "realizations", "[", "int", "(", "kind", "[", "4", ":", "]", ")", "]", "metadata", "[", "'smlt_path'", "]", "=", "'_'", ".", "join", "(", "rlz", ".", "sm_lt_path", ")", "metadata", "[", "'gsimlt_path'", "]", "=", "rlz", ".", "gsim_rlz", ".", "uid", "elif", "kind", ".", "startswith", "(", "'quantile-'", ")", ":", "metadata", "[", "'statistics'", "]", "=", "'quantile'", "metadata", "[", "'quantile_value'", "]", "=", "float", "(", "kind", "[", "9", ":", "]", ")", "elif", "kind", "==", "'mean'", ":", "metadata", "[", "'statistics'", "]", "=", "'mean'", "elif", "kind", "==", "'max'", ":", "metadata", "[", "'statistics'", "]", "=", "'max'", "elif", "kind", "==", "'std'", ":", "metadata", "[", "'statistics'", "]", "=", "'std'", "return", "metadata" ]
:param list realizations: realization objects :param str kind: kind of data, i.e. a key in the datastore :returns: a dictionary with smlt_path, gsimlt_path, statistics, quantile_value
[ ":", "param", "list", "realizations", ":", "realization", "objects", ":", "param", "str", "kind", ":", "kind", "of", "data", "i", ".", "e", ".", "a", "key", "in", "the", "datastore", ":", "returns", ":", "a", "dictionary", "with", "smlt_path", "gsimlt_path", "statistics", "quantile_value" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L379-L402
gem/oq-engine
openquake/calculators/export/hazard.py
export_gmf
def export_gmf(ekey, dstore): """ :param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object """ oq = dstore['oqparam'] if not oq.calculation_mode.startswith('scenario'): return [] sitecol = dstore['sitecol'] investigation_time = (None if oq.calculation_mode == 'scenario' else oq.investigation_time) fmt = ekey[-1] gmf_data = dstore['gmf_data'] nbytes = gmf_data.attrs['nbytes'] logging.info('Internal size of the GMFs: %s', humansize(nbytes)) if nbytes > GMF_MAX_SIZE: logging.warning(GMF_WARNING, dstore.filename) data = gmf_data['data'].value ses_idx = 1 # for scenario only events = [] for eid, gmfa in group_array(data, 'eid').items(): rup = Event(eid, ses_idx, sorted(set(gmfa['sid'])), gmfa) events.append(rup) fname = dstore.build_fname('gmf', 'scenario', fmt) writer = hazard_writers.EventBasedGMFXMLWriter( fname, sm_lt_path='', gsim_lt_path='') writer.serialize( GmfCollection(sitecol, oq.imtls, events, investigation_time)) return [fname]
python
def export_gmf(ekey, dstore): oq = dstore['oqparam'] if not oq.calculation_mode.startswith('scenario'): return [] sitecol = dstore['sitecol'] investigation_time = (None if oq.calculation_mode == 'scenario' else oq.investigation_time) fmt = ekey[-1] gmf_data = dstore['gmf_data'] nbytes = gmf_data.attrs['nbytes'] logging.info('Internal size of the GMFs: %s', humansize(nbytes)) if nbytes > GMF_MAX_SIZE: logging.warning(GMF_WARNING, dstore.filename) data = gmf_data['data'].value ses_idx = 1 events = [] for eid, gmfa in group_array(data, 'eid').items(): rup = Event(eid, ses_idx, sorted(set(gmfa['sid'])), gmfa) events.append(rup) fname = dstore.build_fname('gmf', 'scenario', fmt) writer = hazard_writers.EventBasedGMFXMLWriter( fname, sm_lt_path='', gsim_lt_path='') writer.serialize( GmfCollection(sitecol, oq.imtls, events, investigation_time)) return [fname]
[ "def", "export_gmf", "(", "ekey", ",", "dstore", ")", ":", "oq", "=", "dstore", "[", "'oqparam'", "]", "if", "not", "oq", ".", "calculation_mode", ".", "startswith", "(", "'scenario'", ")", ":", "return", "[", "]", "sitecol", "=", "dstore", "[", "'sitecol'", "]", "investigation_time", "=", "(", "None", "if", "oq", ".", "calculation_mode", "==", "'scenario'", "else", "oq", ".", "investigation_time", ")", "fmt", "=", "ekey", "[", "-", "1", "]", "gmf_data", "=", "dstore", "[", "'gmf_data'", "]", "nbytes", "=", "gmf_data", ".", "attrs", "[", "'nbytes'", "]", "logging", ".", "info", "(", "'Internal size of the GMFs: %s'", ",", "humansize", "(", "nbytes", ")", ")", "if", "nbytes", ">", "GMF_MAX_SIZE", ":", "logging", ".", "warning", "(", "GMF_WARNING", ",", "dstore", ".", "filename", ")", "data", "=", "gmf_data", "[", "'data'", "]", ".", "value", "ses_idx", "=", "1", "# for scenario only", "events", "=", "[", "]", "for", "eid", ",", "gmfa", "in", "group_array", "(", "data", ",", "'eid'", ")", ".", "items", "(", ")", ":", "rup", "=", "Event", "(", "eid", ",", "ses_idx", ",", "sorted", "(", "set", "(", "gmfa", "[", "'sid'", "]", ")", ")", ",", "gmfa", ")", "events", ".", "append", "(", "rup", ")", "fname", "=", "dstore", ".", "build_fname", "(", "'gmf'", ",", "'scenario'", ",", "fmt", ")", "writer", "=", "hazard_writers", ".", "EventBasedGMFXMLWriter", "(", "fname", ",", "sm_lt_path", "=", "''", ",", "gsim_lt_path", "=", "''", ")", "writer", ".", "serialize", "(", "GmfCollection", "(", "sitecol", ",", "oq", ".", "imtls", ",", "events", ",", "investigation_time", ")", ")", "return", "[", "fname", "]" ]
:param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object
[ ":", "param", "ekey", ":", "export", "key", "i", ".", "e", ".", "a", "pair", "(", "datastore", "key", "fmt", ")", ":", "param", "dstore", ":", "datastore", "object" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L535-L563
gem/oq-engine
openquake/calculators/export/hazard.py
save_disagg_to_csv
def save_disagg_to_csv(metadata, matrices): """ Save disaggregation matrices to multiple .csv files. """ skip_keys = ('Mag', 'Dist', 'Lon', 'Lat', 'Eps', 'TRT') base_header = ','.join( '%s=%s' % (key, value) for key, value in metadata.items() if value is not None and key not in skip_keys) for disag_tup, (poe, iml, matrix, fname) in matrices.items(): header = '%s,poe=%.7f,iml=%.7e\n' % (base_header, poe, iml) if disag_tup == ('Mag', 'Lon', 'Lat'): matrix = numpy.swapaxes(matrix, 0, 1) matrix = numpy.swapaxes(matrix, 1, 2) disag_tup = ('Lon', 'Lat', 'Mag') axis = [metadata[v] for v in disag_tup] header += ','.join(v for v in disag_tup) header += ',poe' # compute axis mid points axis = [(ax[: -1] + ax[1:]) / 2. if ax.dtype == float else ax for ax in axis] values = None if len(axis) == 1: values = numpy.array([axis[0], matrix.flatten()]).T else: grids = numpy.meshgrid(*axis, indexing='ij') values = [g.flatten() for g in grids] values.append(matrix.flatten()) values = numpy.array(values).T writers.write_csv(fname, values, comment=header, fmt='%.5E')
python
def save_disagg_to_csv(metadata, matrices): skip_keys = ('Mag', 'Dist', 'Lon', 'Lat', 'Eps', 'TRT') base_header = ','.join( '%s=%s' % (key, value) for key, value in metadata.items() if value is not None and key not in skip_keys) for disag_tup, (poe, iml, matrix, fname) in matrices.items(): header = '%s,poe=%.7f,iml=%.7e\n' % (base_header, poe, iml) if disag_tup == ('Mag', 'Lon', 'Lat'): matrix = numpy.swapaxes(matrix, 0, 1) matrix = numpy.swapaxes(matrix, 1, 2) disag_tup = ('Lon', 'Lat', 'Mag') axis = [metadata[v] for v in disag_tup] header += ','.join(v for v in disag_tup) header += ',poe' axis = [(ax[: -1] + ax[1:]) / 2. if ax.dtype == float else ax for ax in axis] values = None if len(axis) == 1: values = numpy.array([axis[0], matrix.flatten()]).T else: grids = numpy.meshgrid(*axis, indexing='ij') values = [g.flatten() for g in grids] values.append(matrix.flatten()) values = numpy.array(values).T writers.write_csv(fname, values, comment=header, fmt='%.5E')
[ "def", "save_disagg_to_csv", "(", "metadata", ",", "matrices", ")", ":", "skip_keys", "=", "(", "'Mag'", ",", "'Dist'", ",", "'Lon'", ",", "'Lat'", ",", "'Eps'", ",", "'TRT'", ")", "base_header", "=", "','", ".", "join", "(", "'%s=%s'", "%", "(", "key", ",", "value", ")", "for", "key", ",", "value", "in", "metadata", ".", "items", "(", ")", "if", "value", "is", "not", "None", "and", "key", "not", "in", "skip_keys", ")", "for", "disag_tup", ",", "(", "poe", ",", "iml", ",", "matrix", ",", "fname", ")", "in", "matrices", ".", "items", "(", ")", ":", "header", "=", "'%s,poe=%.7f,iml=%.7e\\n'", "%", "(", "base_header", ",", "poe", ",", "iml", ")", "if", "disag_tup", "==", "(", "'Mag'", ",", "'Lon'", ",", "'Lat'", ")", ":", "matrix", "=", "numpy", ".", "swapaxes", "(", "matrix", ",", "0", ",", "1", ")", "matrix", "=", "numpy", ".", "swapaxes", "(", "matrix", ",", "1", ",", "2", ")", "disag_tup", "=", "(", "'Lon'", ",", "'Lat'", ",", "'Mag'", ")", "axis", "=", "[", "metadata", "[", "v", "]", "for", "v", "in", "disag_tup", "]", "header", "+=", "','", ".", "join", "(", "v", "for", "v", "in", "disag_tup", ")", "header", "+=", "',poe'", "# compute axis mid points", "axis", "=", "[", "(", "ax", "[", ":", "-", "1", "]", "+", "ax", "[", "1", ":", "]", ")", "/", "2.", "if", "ax", ".", "dtype", "==", "float", "else", "ax", "for", "ax", "in", "axis", "]", "values", "=", "None", "if", "len", "(", "axis", ")", "==", "1", ":", "values", "=", "numpy", ".", "array", "(", "[", "axis", "[", "0", "]", ",", "matrix", ".", "flatten", "(", ")", "]", ")", ".", "T", "else", ":", "grids", "=", "numpy", ".", "meshgrid", "(", "*", "axis", ",", "indexing", "=", "'ij'", ")", "values", "=", "[", "g", ".", "flatten", "(", ")", "for", "g", "in", "grids", "]", "values", ".", "append", "(", "matrix", ".", "flatten", "(", ")", ")", "values", "=", "numpy", ".", "array", "(", "values", ")", ".", "T", "writers", ".", "write_csv", "(", "fname", ",", "values", ",", "comment", "=", "header", ",", "fmt", "=", "'%.5E'", ")" ]
Save disaggregation matrices to multiple .csv files.
[ "Save", "disaggregation", "matrices", "to", "multiple", ".", "csv", "files", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/calculators/export/hazard.py#L743-L776
gem/oq-engine
openquake/hazardlib/gsim/bradley_2013b.py
Bradley2013bChchCBD.get_mean_and_stddevs
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values. """ # extracting dictionary of coefficients specific to required # intensity measure type. C = self.COEFFS[imt] if isinstance(imt, PGA): imt_per = 0.0 else: imt_per = imt.period # Fix site parameters for consistent dS2S application. sites.vs30 = np.array([250]) sites.z1pt0 = np.array([330]) # intensity on a reference soil is used for both mean # and stddev calculations. ln_y_ref = self._get_ln_y_ref(rup, dists, C) # exp1 and exp2 are parts of eq. 7 exp1 = np.exp(C['phi3'] * (sites.vs30.clip(-np.inf, 1130) - 360)) exp2 = np.exp(C['phi3'] * (1130 - 360)) # v1 is the period dependent site term. The Vs30 above which, the # amplification is constant v1 = self._get_v1(imt) # Get log-mean from regular unadjusted model b13a_mean = self._get_mean(sites, C, ln_y_ref, exp1, exp2, v1) # Adjust mean and standard deviation mean = b13a_mean + self._get_dL2L(imt_per) + self._get_dS2S(imt_per) mean += convert_to_LHC(imt) stddevs = self._get_adjusted_stddevs(sites, rup, C, stddev_types, ln_y_ref, exp1, exp2, imt_per) return mean, stddevs
python
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): C = self.COEFFS[imt] if isinstance(imt, PGA): imt_per = 0.0 else: imt_per = imt.period sites.vs30 = np.array([250]) sites.z1pt0 = np.array([330]) ln_y_ref = self._get_ln_y_ref(rup, dists, C) exp1 = np.exp(C['phi3'] * (sites.vs30.clip(-np.inf, 1130) - 360)) exp2 = np.exp(C['phi3'] * (1130 - 360)) v1 = self._get_v1(imt) b13a_mean = self._get_mean(sites, C, ln_y_ref, exp1, exp2, v1) mean = b13a_mean + self._get_dL2L(imt_per) + self._get_dS2S(imt_per) mean += convert_to_LHC(imt) stddevs = self._get_adjusted_stddevs(sites, rup, C, stddev_types, ln_y_ref, exp1, exp2, imt_per) return mean, stddevs
[ "def", "get_mean_and_stddevs", "(", "self", ",", "sites", ",", "rup", ",", "dists", ",", "imt", ",", "stddev_types", ")", ":", "# extracting dictionary of coefficients specific to required", "# intensity measure type.", "C", "=", "self", ".", "COEFFS", "[", "imt", "]", "if", "isinstance", "(", "imt", ",", "PGA", ")", ":", "imt_per", "=", "0.0", "else", ":", "imt_per", "=", "imt", ".", "period", "# Fix site parameters for consistent dS2S application.", "sites", ".", "vs30", "=", "np", ".", "array", "(", "[", "250", "]", ")", "sites", ".", "z1pt0", "=", "np", ".", "array", "(", "[", "330", "]", ")", "# intensity on a reference soil is used for both mean", "# and stddev calculations.", "ln_y_ref", "=", "self", ".", "_get_ln_y_ref", "(", "rup", ",", "dists", ",", "C", ")", "# exp1 and exp2 are parts of eq. 7", "exp1", "=", "np", ".", "exp", "(", "C", "[", "'phi3'", "]", "*", "(", "sites", ".", "vs30", ".", "clip", "(", "-", "np", ".", "inf", ",", "1130", ")", "-", "360", ")", ")", "exp2", "=", "np", ".", "exp", "(", "C", "[", "'phi3'", "]", "*", "(", "1130", "-", "360", ")", ")", "# v1 is the period dependent site term. The Vs30 above which, the", "# amplification is constant", "v1", "=", "self", ".", "_get_v1", "(", "imt", ")", "# Get log-mean from regular unadjusted model", "b13a_mean", "=", "self", ".", "_get_mean", "(", "sites", ",", "C", ",", "ln_y_ref", ",", "exp1", ",", "exp2", ",", "v1", ")", "# Adjust mean and standard deviation", "mean", "=", "b13a_mean", "+", "self", ".", "_get_dL2L", "(", "imt_per", ")", "+", "self", ".", "_get_dS2S", "(", "imt_per", ")", "mean", "+=", "convert_to_LHC", "(", "imt", ")", "stddevs", "=", "self", ".", "_get_adjusted_stddevs", "(", "sites", ",", "rup", ",", "C", ",", "stddev_types", ",", "ln_y_ref", ",", "exp1", ",", "exp2", ",", "imt_per", ")", "return", "mean", ",", "stddevs" ]
See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values.
[ "See", ":", "meth", ":", "superclass", "method", "<", ".", "base", ".", "GroundShakingIntensityModel", ".", "get_mean_and_stddevs", ">", "for", "spec", "of", "input", "and", "result", "values", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/bradley_2013b.py#L63-L96
gem/oq-engine
openquake/hazardlib/gsim/bradley_2013b.py
Bradley2013bChchCBD._interp_function
def _interp_function(self, y_ip1, y_i, t_ip1, t_i, imt_per): """ Generic interpolation function used in equation 19 of 2013 report. """ return y_i + (y_ip1 - y_i) / (t_ip1 - t_i) * (imt_per - t_i)
python
def _interp_function(self, y_ip1, y_i, t_ip1, t_i, imt_per): return y_i + (y_ip1 - y_i) / (t_ip1 - t_i) * (imt_per - t_i)
[ "def", "_interp_function", "(", "self", ",", "y_ip1", ",", "y_i", ",", "t_ip1", ",", "t_i", ",", "imt_per", ")", ":", "return", "y_i", "+", "(", "y_ip1", "-", "y_i", ")", "/", "(", "t_ip1", "-", "t_i", ")", "*", "(", "imt_per", "-", "t_i", ")" ]
Generic interpolation function used in equation 19 of 2013 report.
[ "Generic", "interpolation", "function", "used", "in", "equation", "19", "of", "2013", "report", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/bradley_2013b.py#L154-L158
gem/oq-engine
openquake/hazardlib/gsim/bradley_2013b.py
Bradley2013bChchCBD._get_SRF_tau
def _get_SRF_tau(self, imt_per): """ Table 6 and equation 19 of 2013 report. """ if imt_per < 1: srf = 0.87 elif 1 <= imt_per < 5: srf = self._interp_function(0.58, 0.87, 5, 1, imt_per) elif 5 <= imt_per <= 10: srf = 0.58 else: srf = 1 return srf
python
def _get_SRF_tau(self, imt_per): if imt_per < 1: srf = 0.87 elif 1 <= imt_per < 5: srf = self._interp_function(0.58, 0.87, 5, 1, imt_per) elif 5 <= imt_per <= 10: srf = 0.58 else: srf = 1 return srf
[ "def", "_get_SRF_tau", "(", "self", ",", "imt_per", ")", ":", "if", "imt_per", "<", "1", ":", "srf", "=", "0.87", "elif", "1", "<=", "imt_per", "<", "5", ":", "srf", "=", "self", ".", "_interp_function", "(", "0.58", ",", "0.87", ",", "5", ",", "1", ",", "imt_per", ")", "elif", "5", "<=", "imt_per", "<=", "10", ":", "srf", "=", "0.58", "else", ":", "srf", "=", "1", "return", "srf" ]
Table 6 and equation 19 of 2013 report.
[ "Table", "6", "and", "equation", "19", "of", "2013", "report", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/bradley_2013b.py#L160-L173
gem/oq-engine
openquake/hazardlib/gsim/bradley_2013b.py
Bradley2013bChchCBD._get_SRF_phi
def _get_SRF_phi(self, imt_per): """ Table 7 and equation 19 of 2013 report. NB change in notation, 2013 report calls this term 'sigma' but it is referred to here as phi. """ if imt_per < 0.6: srf = 0.8 elif 0.6 <= imt_per < 1: srf = self._interp_function(0.7, 0.8, 1, 0.6, imt_per) elif 1 <= imt_per <= 10: srf = self._interp_function(0.6, 0.7, 10, 1, imt_per) else: srf = 1 return srf
python
def _get_SRF_phi(self, imt_per): if imt_per < 0.6: srf = 0.8 elif 0.6 <= imt_per < 1: srf = self._interp_function(0.7, 0.8, 1, 0.6, imt_per) elif 1 <= imt_per <= 10: srf = self._interp_function(0.6, 0.7, 10, 1, imt_per) else: srf = 1 return srf
[ "def", "_get_SRF_phi", "(", "self", ",", "imt_per", ")", ":", "if", "imt_per", "<", "0.6", ":", "srf", "=", "0.8", "elif", "0.6", "<=", "imt_per", "<", "1", ":", "srf", "=", "self", ".", "_interp_function", "(", "0.7", ",", "0.8", ",", "1", ",", "0.6", ",", "imt_per", ")", "elif", "1", "<=", "imt_per", "<=", "10", ":", "srf", "=", "self", ".", "_interp_function", "(", "0.6", ",", "0.7", ",", "10", ",", "1", ",", "imt_per", ")", "else", ":", "srf", "=", "1", "return", "srf" ]
Table 7 and equation 19 of 2013 report. NB change in notation, 2013 report calls this term 'sigma' but it is referred to here as phi.
[ "Table", "7", "and", "equation", "19", "of", "2013", "report", ".", "NB", "change", "in", "notation", "2013", "report", "calls", "this", "term", "sigma", "but", "it", "is", "referred", "to", "here", "as", "phi", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/bradley_2013b.py#L175-L190
gem/oq-engine
openquake/hazardlib/gsim/bradley_2013b.py
Bradley2013bChchCBD._get_SRF_sigma
def _get_SRF_sigma(self, imt_per): """ Table 8 and equation 19 of 2013 report. NB change in notation, 2013 report calls this term 'sigma_t' but it is referred to here as sigma. Note that Table 8 is identical to Table 7 in the 2013 report. """ if imt_per < 0.6: srf = 0.8 elif 0.6 <= imt_per < 1: srf = self._interp_function(0.7, 0.8, 1, 0.6, imt_per) elif 1 <= imt_per <= 10: srf = self._interp_function(0.6, 0.7, 10, 1, imt_per) else: srf = 1 return srf
python
def _get_SRF_sigma(self, imt_per): if imt_per < 0.6: srf = 0.8 elif 0.6 <= imt_per < 1: srf = self._interp_function(0.7, 0.8, 1, 0.6, imt_per) elif 1 <= imt_per <= 10: srf = self._interp_function(0.6, 0.7, 10, 1, imt_per) else: srf = 1 return srf
[ "def", "_get_SRF_sigma", "(", "self", ",", "imt_per", ")", ":", "if", "imt_per", "<", "0.6", ":", "srf", "=", "0.8", "elif", "0.6", "<=", "imt_per", "<", "1", ":", "srf", "=", "self", ".", "_interp_function", "(", "0.7", ",", "0.8", ",", "1", ",", "0.6", ",", "imt_per", ")", "elif", "1", "<=", "imt_per", "<=", "10", ":", "srf", "=", "self", ".", "_interp_function", "(", "0.6", ",", "0.7", ",", "10", ",", "1", ",", "imt_per", ")", "else", ":", "srf", "=", "1", "return", "srf" ]
Table 8 and equation 19 of 2013 report. NB change in notation, 2013 report calls this term 'sigma_t' but it is referred to here as sigma. Note that Table 8 is identical to Table 7 in the 2013 report.
[ "Table", "8", "and", "equation", "19", "of", "2013", "report", ".", "NB", "change", "in", "notation", "2013", "report", "calls", "this", "term", "sigma_t", "but", "it", "is", "referred", "to", "here", "as", "sigma", ".", "Note", "that", "Table", "8", "is", "identical", "to", "Table", "7", "in", "the", "2013", "report", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/bradley_2013b.py#L192-L208
gem/oq-engine
openquake/hazardlib/gsim/bradley_2013b.py
Bradley2013bChchCBD._get_dL2L
def _get_dL2L(self, imt_per): """ Table 3 and equation 19 of 2013 report. """ if imt_per < 0.18: dL2L = -0.06 elif 0.18 <= imt_per < 0.35: dL2L = self._interp_function(0.12, -0.06, 0.35, 0.18, imt_per) elif 0.35 <= imt_per <= 10: dL2L = self._interp_function(0.65, 0.12, 10, 0.35, imt_per) else: dL2L = 0 return dL2L
python
def _get_dL2L(self, imt_per): if imt_per < 0.18: dL2L = -0.06 elif 0.18 <= imt_per < 0.35: dL2L = self._interp_function(0.12, -0.06, 0.35, 0.18, imt_per) elif 0.35 <= imt_per <= 10: dL2L = self._interp_function(0.65, 0.12, 10, 0.35, imt_per) else: dL2L = 0 return dL2L
[ "def", "_get_dL2L", "(", "self", ",", "imt_per", ")", ":", "if", "imt_per", "<", "0.18", ":", "dL2L", "=", "-", "0.06", "elif", "0.18", "<=", "imt_per", "<", "0.35", ":", "dL2L", "=", "self", ".", "_interp_function", "(", "0.12", ",", "-", "0.06", ",", "0.35", ",", "0.18", ",", "imt_per", ")", "elif", "0.35", "<=", "imt_per", "<=", "10", ":", "dL2L", "=", "self", ".", "_interp_function", "(", "0.65", ",", "0.12", ",", "10", ",", "0.35", ",", "imt_per", ")", "else", ":", "dL2L", "=", "0", "return", "dL2L" ]
Table 3 and equation 19 of 2013 report.
[ "Table", "3", "and", "equation", "19", "of", "2013", "report", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/bradley_2013b.py#L210-L223
gem/oq-engine
openquake/hazardlib/gsim/bradley_2013b.py
Bradley2013bChchCBD._get_dS2S
def _get_dS2S(self, imt_per): """ Table 4 of 2013 report """ if imt_per == 0: dS2S = 0.05 elif 0 < imt_per < 0.15: dS2S = self._interp_function(-0.15, 0.05, 0.15, 0, imt_per) elif 0.15 <= imt_per < 0.45: dS2S = self._interp_function(0.4, -0.15, 0.45, 0.15, imt_per) elif 0.45 <= imt_per < 3.2: dS2S = 0.4 elif 3.2 <= imt_per < 5: dS2S = self._interp_function(0.08, 0.4, 5, 3.2, imt_per) elif 5 <= imt_per <= 10: dS2S = 0.08 else: dS2S = 0 return dS2S
python
def _get_dS2S(self, imt_per): if imt_per == 0: dS2S = 0.05 elif 0 < imt_per < 0.15: dS2S = self._interp_function(-0.15, 0.05, 0.15, 0, imt_per) elif 0.15 <= imt_per < 0.45: dS2S = self._interp_function(0.4, -0.15, 0.45, 0.15, imt_per) elif 0.45 <= imt_per < 3.2: dS2S = 0.4 elif 3.2 <= imt_per < 5: dS2S = self._interp_function(0.08, 0.4, 5, 3.2, imt_per) elif 5 <= imt_per <= 10: dS2S = 0.08 else: dS2S = 0 return dS2S
[ "def", "_get_dS2S", "(", "self", ",", "imt_per", ")", ":", "if", "imt_per", "==", "0", ":", "dS2S", "=", "0.05", "elif", "0", "<", "imt_per", "<", "0.15", ":", "dS2S", "=", "self", ".", "_interp_function", "(", "-", "0.15", ",", "0.05", ",", "0.15", ",", "0", ",", "imt_per", ")", "elif", "0.15", "<=", "imt_per", "<", "0.45", ":", "dS2S", "=", "self", ".", "_interp_function", "(", "0.4", ",", "-", "0.15", ",", "0.45", ",", "0.15", ",", "imt_per", ")", "elif", "0.45", "<=", "imt_per", "<", "3.2", ":", "dS2S", "=", "0.4", "elif", "3.2", "<=", "imt_per", "<", "5", ":", "dS2S", "=", "self", ".", "_interp_function", "(", "0.08", ",", "0.4", ",", "5", ",", "3.2", ",", "imt_per", ")", "elif", "5", "<=", "imt_per", "<=", "10", ":", "dS2S", "=", "0.08", "else", ":", "dS2S", "=", "0", "return", "dS2S" ]
Table 4 of 2013 report
[ "Table", "4", "of", "2013", "report" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/bradley_2013b.py#L225-L244
gem/oq-engine
openquake/hazardlib/calc/filters.py
context
def context(src): """ Used to add the source_id to the error message. To be used as with context(src): operation_with(src) Typically the operation is filtering a source, that can fail for tricky geometries. """ try: yield except Exception: etype, err, tb = sys.exc_info() msg = 'An error occurred with source id=%s. Error: %s' msg %= (src.source_id, err) raise_(etype, msg, tb)
python
def context(src): try: yield except Exception: etype, err, tb = sys.exc_info() msg = 'An error occurred with source id=%s. Error: %s' msg %= (src.source_id, err) raise_(etype, msg, tb)
[ "def", "context", "(", "src", ")", ":", "try", ":", "yield", "except", "Exception", ":", "etype", ",", "err", ",", "tb", "=", "sys", ".", "exc_info", "(", ")", "msg", "=", "'An error occurred with source id=%s. Error: %s'", "msg", "%=", "(", "src", ".", "source_id", ",", "err", ")", "raise_", "(", "etype", ",", "msg", ",", "tb", ")" ]
Used to add the source_id to the error message. To be used as with context(src): operation_with(src) Typically the operation is filtering a source, that can fail for tricky geometries.
[ "Used", "to", "add", "the", "source_id", "to", "the", "error", "message", ".", "To", "be", "used", "as" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L39-L55
gem/oq-engine
openquake/hazardlib/calc/filters.py
split_sources
def split_sources(srcs): """ :param srcs: sources :returns: a pair (split sources, split time) or just the split_sources """ from openquake.hazardlib.source import splittable sources = [] split_time = {} # src.id -> time for src in srcs: t0 = time.time() mag_a, mag_b = src.get_min_max_mag() min_mag = src.min_mag if mag_b < min_mag: # discard the source completely continue has_serial = hasattr(src, 'serial') if has_serial: src.serial = numpy.arange( src.serial, src.serial + src.num_ruptures) if not splittable(src): sources.append(src) split_time[src.id] = time.time() - t0 continue if min_mag: splits = [] for s in src: s.min_mag = min_mag mag_a, mag_b = s.get_min_max_mag() if mag_b < min_mag: continue s.num_ruptures = s.count_ruptures() if s.num_ruptures: splits.append(s) else: splits = list(src) split_time[src.id] = time.time() - t0 sources.extend(splits) has_samples = hasattr(src, 'samples') if len(splits) > 1: start = 0 for i, split in enumerate(splits): split.source_id = '%s:%s' % (src.source_id, i) split.src_group_id = src.src_group_id split.id = src.id if has_serial: nr = split.num_ruptures split.serial = src.serial[start:start + nr] start += nr if has_samples: split.samples = src.samples elif splits: # single source splits[0].id = src.id if has_serial: splits[0].serial = src.serial if has_samples: splits[0].samples = src.samples return sources, split_time
python
def split_sources(srcs): from openquake.hazardlib.source import splittable sources = [] split_time = {} for src in srcs: t0 = time.time() mag_a, mag_b = src.get_min_max_mag() min_mag = src.min_mag if mag_b < min_mag: continue has_serial = hasattr(src, 'serial') if has_serial: src.serial = numpy.arange( src.serial, src.serial + src.num_ruptures) if not splittable(src): sources.append(src) split_time[src.id] = time.time() - t0 continue if min_mag: splits = [] for s in src: s.min_mag = min_mag mag_a, mag_b = s.get_min_max_mag() if mag_b < min_mag: continue s.num_ruptures = s.count_ruptures() if s.num_ruptures: splits.append(s) else: splits = list(src) split_time[src.id] = time.time() - t0 sources.extend(splits) has_samples = hasattr(src, 'samples') if len(splits) > 1: start = 0 for i, split in enumerate(splits): split.source_id = '%s:%s' % (src.source_id, i) split.src_group_id = src.src_group_id split.id = src.id if has_serial: nr = split.num_ruptures split.serial = src.serial[start:start + nr] start += nr if has_samples: split.samples = src.samples elif splits: splits[0].id = src.id if has_serial: splits[0].serial = src.serial if has_samples: splits[0].samples = src.samples return sources, split_time
[ "def", "split_sources", "(", "srcs", ")", ":", "from", "openquake", ".", "hazardlib", ".", "source", "import", "splittable", "sources", "=", "[", "]", "split_time", "=", "{", "}", "# src.id -> time", "for", "src", "in", "srcs", ":", "t0", "=", "time", ".", "time", "(", ")", "mag_a", ",", "mag_b", "=", "src", ".", "get_min_max_mag", "(", ")", "min_mag", "=", "src", ".", "min_mag", "if", "mag_b", "<", "min_mag", ":", "# discard the source completely", "continue", "has_serial", "=", "hasattr", "(", "src", ",", "'serial'", ")", "if", "has_serial", ":", "src", ".", "serial", "=", "numpy", ".", "arange", "(", "src", ".", "serial", ",", "src", ".", "serial", "+", "src", ".", "num_ruptures", ")", "if", "not", "splittable", "(", "src", ")", ":", "sources", ".", "append", "(", "src", ")", "split_time", "[", "src", ".", "id", "]", "=", "time", ".", "time", "(", ")", "-", "t0", "continue", "if", "min_mag", ":", "splits", "=", "[", "]", "for", "s", "in", "src", ":", "s", ".", "min_mag", "=", "min_mag", "mag_a", ",", "mag_b", "=", "s", ".", "get_min_max_mag", "(", ")", "if", "mag_b", "<", "min_mag", ":", "continue", "s", ".", "num_ruptures", "=", "s", ".", "count_ruptures", "(", ")", "if", "s", ".", "num_ruptures", ":", "splits", ".", "append", "(", "s", ")", "else", ":", "splits", "=", "list", "(", "src", ")", "split_time", "[", "src", ".", "id", "]", "=", "time", ".", "time", "(", ")", "-", "t0", "sources", ".", "extend", "(", "splits", ")", "has_samples", "=", "hasattr", "(", "src", ",", "'samples'", ")", "if", "len", "(", "splits", ")", ">", "1", ":", "start", "=", "0", "for", "i", ",", "split", "in", "enumerate", "(", "splits", ")", ":", "split", ".", "source_id", "=", "'%s:%s'", "%", "(", "src", ".", "source_id", ",", "i", ")", "split", ".", "src_group_id", "=", "src", ".", "src_group_id", "split", ".", "id", "=", "src", ".", "id", "if", "has_serial", ":", "nr", "=", "split", ".", "num_ruptures", "split", ".", "serial", "=", "src", ".", "serial", "[", "start", ":", "start", "+", "nr", "]", "start", "+=", "nr", "if", "has_samples", ":", "split", ".", "samples", "=", "src", ".", "samples", "elif", "splits", ":", "# single source", "splits", "[", "0", "]", ".", "id", "=", "src", ".", "id", "if", "has_serial", ":", "splits", "[", "0", "]", ".", "serial", "=", "src", ".", "serial", "if", "has_samples", ":", "splits", "[", "0", "]", ".", "samples", "=", "src", ".", "samples", "return", "sources", ",", "split_time" ]
:param srcs: sources :returns: a pair (split sources, split time) or just the split_sources
[ ":", "param", "srcs", ":", "sources", ":", "returns", ":", "a", "pair", "(", "split", "sources", "split", "time", ")", "or", "just", "the", "split_sources" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L186-L241
gem/oq-engine
openquake/hazardlib/calc/filters.py
IntegrationDistance.get_bounding_box
def get_bounding_box(self, lon, lat, trt=None, mag=None): """ Build a bounding box around the given lon, lat by computing the maximum_distance at the given tectonic region type and magnitude. :param lon: longitude :param lat: latitude :param trt: tectonic region type, possibly None :param mag: magnitude, possibly None :returns: min_lon, min_lat, max_lon, max_lat """ if trt is None: # take the greatest integration distance maxdist = max(self(trt, mag) for trt in self.dic) else: # get the integration distance for the given TRT maxdist = self(trt, mag) a1 = min(maxdist * KM_TO_DEGREES, 90) a2 = min(angular_distance(maxdist, lat), 180) return lon - a2, lat - a1, lon + a2, lat + a1
python
def get_bounding_box(self, lon, lat, trt=None, mag=None): if trt is None: maxdist = max(self(trt, mag) for trt in self.dic) else: maxdist = self(trt, mag) a1 = min(maxdist * KM_TO_DEGREES, 90) a2 = min(angular_distance(maxdist, lat), 180) return lon - a2, lat - a1, lon + a2, lat + a1
[ "def", "get_bounding_box", "(", "self", ",", "lon", ",", "lat", ",", "trt", "=", "None", ",", "mag", "=", "None", ")", ":", "if", "trt", "is", "None", ":", "# take the greatest integration distance", "maxdist", "=", "max", "(", "self", "(", "trt", ",", "mag", ")", "for", "trt", "in", "self", ".", "dic", ")", "else", ":", "# get the integration distance for the given TRT", "maxdist", "=", "self", "(", "trt", ",", "mag", ")", "a1", "=", "min", "(", "maxdist", "*", "KM_TO_DEGREES", ",", "90", ")", "a2", "=", "min", "(", "angular_distance", "(", "maxdist", ",", "lat", ")", ",", "180", ")", "return", "lon", "-", "a2", ",", "lat", "-", "a1", ",", "lon", "+", "a2", ",", "lat", "+", "a1" ]
Build a bounding box around the given lon, lat by computing the maximum_distance at the given tectonic region type and magnitude. :param lon: longitude :param lat: latitude :param trt: tectonic region type, possibly None :param mag: magnitude, possibly None :returns: min_lon, min_lat, max_lon, max_lat
[ "Build", "a", "bounding", "box", "around", "the", "given", "lon", "lat", "by", "computing", "the", "maximum_distance", "at", "the", "given", "tectonic", "region", "type", "and", "magnitude", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L138-L155
gem/oq-engine
openquake/hazardlib/calc/filters.py
IntegrationDistance.get_affected_box
def get_affected_box(self, src): """ Get the enlarged bounding box of a source. :param src: a source object :returns: a bounding box (min_lon, min_lat, max_lon, max_lat) """ mag = src.get_min_max_mag()[1] maxdist = self(src.tectonic_region_type, mag) bbox = get_bounding_box(src, maxdist) return (fix_lon(bbox[0]), bbox[1], fix_lon(bbox[2]), bbox[3])
python
def get_affected_box(self, src): mag = src.get_min_max_mag()[1] maxdist = self(src.tectonic_region_type, mag) bbox = get_bounding_box(src, maxdist) return (fix_lon(bbox[0]), bbox[1], fix_lon(bbox[2]), bbox[3])
[ "def", "get_affected_box", "(", "self", ",", "src", ")", ":", "mag", "=", "src", ".", "get_min_max_mag", "(", ")", "[", "1", "]", "maxdist", "=", "self", "(", "src", ".", "tectonic_region_type", ",", "mag", ")", "bbox", "=", "get_bounding_box", "(", "src", ",", "maxdist", ")", "return", "(", "fix_lon", "(", "bbox", "[", "0", "]", ")", ",", "bbox", "[", "1", "]", ",", "fix_lon", "(", "bbox", "[", "2", "]", ")", ",", "bbox", "[", "3", "]", ")" ]
Get the enlarged bounding box of a source. :param src: a source object :returns: a bounding box (min_lon, min_lat, max_lon, max_lat)
[ "Get", "the", "enlarged", "bounding", "box", "of", "a", "source", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L157-L167
gem/oq-engine
openquake/hazardlib/calc/filters.py
SourceFilter.sitecol
def sitecol(self): """ Read the site collection from .filename and cache it """ if 'sitecol' in vars(self): return self.__dict__['sitecol'] if self.filename is None or not os.path.exists(self.filename): # case of nofilter/None sitecol return with hdf5.File(self.filename, 'r') as h5: self.__dict__['sitecol'] = sc = h5.get('sitecol') return sc
python
def sitecol(self): if 'sitecol' in vars(self): return self.__dict__['sitecol'] if self.filename is None or not os.path.exists(self.filename): return with hdf5.File(self.filename, 'r') as h5: self.__dict__['sitecol'] = sc = h5.get('sitecol') return sc
[ "def", "sitecol", "(", "self", ")", ":", "if", "'sitecol'", "in", "vars", "(", "self", ")", ":", "return", "self", ".", "__dict__", "[", "'sitecol'", "]", "if", "self", ".", "filename", "is", "None", "or", "not", "os", ".", "path", ".", "exists", "(", "self", ".", "filename", ")", ":", "# case of nofilter/None sitecol", "return", "with", "hdf5", ".", "File", "(", "self", ".", "filename", ",", "'r'", ")", "as", "h5", ":", "self", ".", "__dict__", "[", "'sitecol'", "]", "=", "sc", "=", "h5", ".", "get", "(", "'sitecol'", ")", "return", "sc" ]
Read the site collection from .filename and cache it
[ "Read", "the", "site", "collection", "from", ".", "filename", "and", "cache", "it" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L276-L287
gem/oq-engine
openquake/hazardlib/calc/filters.py
SourceFilter.get_rectangle
def get_rectangle(self, src): """ :param src: a source object :returns: ((min_lon, min_lat), width, height), useful for plotting """ min_lon, min_lat, max_lon, max_lat = ( self.integration_distance.get_affected_box(src)) return (min_lon, min_lat), (max_lon - min_lon) % 360, max_lat - min_lat
python
def get_rectangle(self, src): min_lon, min_lat, max_lon, max_lat = ( self.integration_distance.get_affected_box(src)) return (min_lon, min_lat), (max_lon - min_lon) % 360, max_lat - min_lat
[ "def", "get_rectangle", "(", "self", ",", "src", ")", ":", "min_lon", ",", "min_lat", ",", "max_lon", ",", "max_lat", "=", "(", "self", ".", "integration_distance", ".", "get_affected_box", "(", "src", ")", ")", "return", "(", "min_lon", ",", "min_lat", ")", ",", "(", "max_lon", "-", "min_lon", ")", "%", "360", ",", "max_lat", "-", "min_lat" ]
:param src: a source object :returns: ((min_lon, min_lat), width, height), useful for plotting
[ ":", "param", "src", ":", "a", "source", "object", ":", "returns", ":", "((", "min_lon", "min_lat", ")", "width", "height", ")", "useful", "for", "plotting" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L289-L296
gem/oq-engine
openquake/hazardlib/calc/filters.py
SourceFilter.get_bounding_boxes
def get_bounding_boxes(self, trt=None, mag=None): """ :param trt: a tectonic region type (used for the integration distance) :param mag: a magnitude (used for the integration distance) :returns: a list of bounding boxes, one per site """ bbs = [] for site in self.sitecol: bb = self.integration_distance.get_bounding_box( site.location.longitude, site.location.latitude, trt, mag) bbs.append(bb) return bbs
python
def get_bounding_boxes(self, trt=None, mag=None): bbs = [] for site in self.sitecol: bb = self.integration_distance.get_bounding_box( site.location.longitude, site.location.latitude, trt, mag) bbs.append(bb) return bbs
[ "def", "get_bounding_boxes", "(", "self", ",", "trt", "=", "None", ",", "mag", "=", "None", ")", ":", "bbs", "=", "[", "]", "for", "site", "in", "self", ".", "sitecol", ":", "bb", "=", "self", ".", "integration_distance", ".", "get_bounding_box", "(", "site", ".", "location", ".", "longitude", ",", "site", ".", "location", ".", "latitude", ",", "trt", ",", "mag", ")", "bbs", ".", "append", "(", "bb", ")", "return", "bbs" ]
:param trt: a tectonic region type (used for the integration distance) :param mag: a magnitude (used for the integration distance) :returns: a list of bounding boxes, one per site
[ ":", "param", "trt", ":", "a", "tectonic", "region", "type", "(", "used", "for", "the", "integration", "distance", ")", ":", "param", "mag", ":", "a", "magnitude", "(", "used", "for", "the", "integration", "distance", ")", ":", "returns", ":", "a", "list", "of", "bounding", "boxes", "one", "per", "site" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L319-L330
gem/oq-engine
openquake/hazardlib/calc/filters.py
SourceFilter.close_sids
def close_sids(self, rec, trt, mag): """ :param rec: a record with fields minlon, minlat, maxlon, maxlat :param trt: tectonic region type string :param mag: magnitude :returns: the site indices within the bounding box enlarged by the integration distance for the given TRT and magnitude """ if self.sitecol is None: return [] elif not self.integration_distance: # do not filter return self.sitecol.sids if hasattr(rec, 'dtype'): bbox = rec['minlon'], rec['minlat'], rec['maxlon'], rec['maxlat'] else: bbox = rec # assume it is a 4-tuple maxdist = self.integration_distance(trt, mag) a1 = min(maxdist * KM_TO_DEGREES, 90) a2 = min(angular_distance(maxdist, bbox[1], bbox[3]), 180) bb = bbox[0] - a2, bbox[1] - a1, bbox[2] + a2, bbox[3] + a1 if hasattr(self, 'index'): # RtreeFilter return within(bb, self.index) return self.sitecol.within_bbox(bb)
python
def close_sids(self, rec, trt, mag): if self.sitecol is None: return [] elif not self.integration_distance: return self.sitecol.sids if hasattr(rec, 'dtype'): bbox = rec['minlon'], rec['minlat'], rec['maxlon'], rec['maxlat'] else: bbox = rec maxdist = self.integration_distance(trt, mag) a1 = min(maxdist * KM_TO_DEGREES, 90) a2 = min(angular_distance(maxdist, bbox[1], bbox[3]), 180) bb = bbox[0] - a2, bbox[1] - a1, bbox[2] + a2, bbox[3] + a1 if hasattr(self, 'index'): return within(bb, self.index) return self.sitecol.within_bbox(bb)
[ "def", "close_sids", "(", "self", ",", "rec", ",", "trt", ",", "mag", ")", ":", "if", "self", ".", "sitecol", "is", "None", ":", "return", "[", "]", "elif", "not", "self", ".", "integration_distance", ":", "# do not filter", "return", "self", ".", "sitecol", ".", "sids", "if", "hasattr", "(", "rec", ",", "'dtype'", ")", ":", "bbox", "=", "rec", "[", "'minlon'", "]", ",", "rec", "[", "'minlat'", "]", ",", "rec", "[", "'maxlon'", "]", ",", "rec", "[", "'maxlat'", "]", "else", ":", "bbox", "=", "rec", "# assume it is a 4-tuple", "maxdist", "=", "self", ".", "integration_distance", "(", "trt", ",", "mag", ")", "a1", "=", "min", "(", "maxdist", "*", "KM_TO_DEGREES", ",", "90", ")", "a2", "=", "min", "(", "angular_distance", "(", "maxdist", ",", "bbox", "[", "1", "]", ",", "bbox", "[", "3", "]", ")", ",", "180", ")", "bb", "=", "bbox", "[", "0", "]", "-", "a2", ",", "bbox", "[", "1", "]", "-", "a1", ",", "bbox", "[", "2", "]", "+", "a2", ",", "bbox", "[", "3", "]", "+", "a1", "if", "hasattr", "(", "self", ",", "'index'", ")", ":", "# RtreeFilter", "return", "within", "(", "bb", ",", "self", ".", "index", ")", "return", "self", ".", "sitecol", ".", "within_bbox", "(", "bb", ")" ]
:param rec: a record with fields minlon, minlat, maxlon, maxlat :param trt: tectonic region type string :param mag: magnitude :returns: the site indices within the bounding box enlarged by the integration distance for the given TRT and magnitude
[ ":", "param", "rec", ":", "a", "record", "with", "fields", "minlon", "minlat", "maxlon", "maxlat", ":", "param", "trt", ":", "tectonic", "region", "type", "string", ":", "param", "mag", ":", "magnitude", ":", "returns", ":", "the", "site", "indices", "within", "the", "bounding", "box", "enlarged", "by", "the", "integration", "distance", "for", "the", "given", "TRT", "and", "magnitude" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L332-L358
gem/oq-engine
openquake/hazardlib/calc/filters.py
SourceFilter.filter
def filter(self, sources): """ :param sources: a sequence of sources :yields: sources with .indices """ for src in sources: if hasattr(src, 'indices'): # already filtered yield src continue box = self.integration_distance.get_affected_box(src) indices = self.sitecol.within_bbox(box) if len(indices): src.indices = indices yield src
python
def filter(self, sources): for src in sources: if hasattr(src, 'indices'): yield src continue box = self.integration_distance.get_affected_box(src) indices = self.sitecol.within_bbox(box) if len(indices): src.indices = indices yield src
[ "def", "filter", "(", "self", ",", "sources", ")", ":", "for", "src", "in", "sources", ":", "if", "hasattr", "(", "src", ",", "'indices'", ")", ":", "# already filtered", "yield", "src", "continue", "box", "=", "self", ".", "integration_distance", ".", "get_affected_box", "(", "src", ")", "indices", "=", "self", ".", "sitecol", ".", "within_bbox", "(", "box", ")", "if", "len", "(", "indices", ")", ":", "src", ".", "indices", "=", "indices", "yield", "src" ]
:param sources: a sequence of sources :yields: sources with .indices
[ ":", "param", "sources", ":", "a", "sequence", "of", "sources", ":", "yields", ":", "sources", "with", ".", "indices" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L360-L373
gem/oq-engine
openquake/hazardlib/calc/filters.py
RtreeFilter.filter
def filter(self, sources): """ :param sources: a sequence of sources :yields: rtree-filtered sources """ if self.sitecol is None: # do not filter yield from sources return for src in sources: box = self.integration_distance.get_affected_box(src) indices = within(box, self.index) if len(indices): src.indices = indices yield src
python
def filter(self, sources): if self.sitecol is None: yield from sources return for src in sources: box = self.integration_distance.get_affected_box(src) indices = within(box, self.index) if len(indices): src.indices = indices yield src
[ "def", "filter", "(", "self", ",", "sources", ")", ":", "if", "self", ".", "sitecol", "is", "None", ":", "# do not filter", "yield", "from", "sources", "return", "for", "src", "in", "sources", ":", "box", "=", "self", ".", "integration_distance", ".", "get_affected_box", "(", "src", ")", "indices", "=", "within", "(", "box", ",", "self", ".", "index", ")", "if", "len", "(", "indices", ")", ":", "src", ".", "indices", "=", "indices", "yield", "src" ]
:param sources: a sequence of sources :yields: rtree-filtered sources
[ ":", "param", "sources", ":", "a", "sequence", "of", "sources", ":", "yields", ":", "rtree", "-", "filtered", "sources" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/calc/filters.py#L419-L432
gem/oq-engine
openquake/commands/to_shapefile.py
to_shapefile
def to_shapefile(output, input_nrml_file, validate): """ Convert a NRML source model file to ESRI Shapefile(s). For each type of source geometry defined in the NRML file (point, area, simple fault, complex fault, planar) a separate shapefile is created. Each shapefile is differentiated by a specific ending('_point', '_area', '_simple', '_complex', '_planar'). NB: nonparametric sources are not supported. """ input_parser = shapefileparser.SourceModelParser() source_model = input_parser.read(input_nrml_file, validate) if not output: output = os.path.splitext(input_nrml_file)[0] print('Extracting %s_ files' % output) shapefileparser.ShapefileParser().write(output, source_model)
python
def to_shapefile(output, input_nrml_file, validate): input_parser = shapefileparser.SourceModelParser() source_model = input_parser.read(input_nrml_file, validate) if not output: output = os.path.splitext(input_nrml_file)[0] print('Extracting %s_ files' % output) shapefileparser.ShapefileParser().write(output, source_model)
[ "def", "to_shapefile", "(", "output", ",", "input_nrml_file", ",", "validate", ")", ":", "input_parser", "=", "shapefileparser", ".", "SourceModelParser", "(", ")", "source_model", "=", "input_parser", ".", "read", "(", "input_nrml_file", ",", "validate", ")", "if", "not", "output", ":", "output", "=", "os", ".", "path", ".", "splitext", "(", "input_nrml_file", ")", "[", "0", "]", "print", "(", "'Extracting %s_ files'", "%", "output", ")", "shapefileparser", ".", "ShapefileParser", "(", ")", ".", "write", "(", "output", ",", "source_model", ")" ]
Convert a NRML source model file to ESRI Shapefile(s). For each type of source geometry defined in the NRML file (point, area, simple fault, complex fault, planar) a separate shapefile is created. Each shapefile is differentiated by a specific ending('_point', '_area', '_simple', '_complex', '_planar'). NB: nonparametric sources are not supported.
[ "Convert", "a", "NRML", "source", "model", "file", "to", "ESRI", "Shapefile", "(", "s", ")", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/commands/to_shapefile.py#L24-L40
gem/oq-engine
openquake/hazardlib/geo/surface/simple_fault.py
simple_fault_node
def simple_fault_node(fault_trace, dip, upper_depth, lower_depth): """ :param fault_trace: an object with an attribute .points :param dip: dip parameter :param upper_depth: upper seismogenic depth :param lower_depth: lower seismogenic depth :returns: a Node of kind simpleFaultGeometry """ node = Node('simpleFaultGeometry') line = [] for p in fault_trace.points: line.append(p.longitude) line.append(p.latitude) node.append(Node('gml:LineString', nodes=[Node('gml:posList', {}, line)])) node.append(Node('dip', {}, dip)) node.append(Node('upperSeismoDepth', {}, upper_depth)) node.append(Node('lowerSeismoDepth', {}, lower_depth)) return node
python
def simple_fault_node(fault_trace, dip, upper_depth, lower_depth): node = Node('simpleFaultGeometry') line = [] for p in fault_trace.points: line.append(p.longitude) line.append(p.latitude) node.append(Node('gml:LineString', nodes=[Node('gml:posList', {}, line)])) node.append(Node('dip', {}, dip)) node.append(Node('upperSeismoDepth', {}, upper_depth)) node.append(Node('lowerSeismoDepth', {}, lower_depth)) return node
[ "def", "simple_fault_node", "(", "fault_trace", ",", "dip", ",", "upper_depth", ",", "lower_depth", ")", ":", "node", "=", "Node", "(", "'simpleFaultGeometry'", ")", "line", "=", "[", "]", "for", "p", "in", "fault_trace", ".", "points", ":", "line", ".", "append", "(", "p", ".", "longitude", ")", "line", ".", "append", "(", "p", ".", "latitude", ")", "node", ".", "append", "(", "Node", "(", "'gml:LineString'", ",", "nodes", "=", "[", "Node", "(", "'gml:posList'", ",", "{", "}", ",", "line", ")", "]", ")", ")", "node", ".", "append", "(", "Node", "(", "'dip'", ",", "{", "}", ",", "dip", ")", ")", "node", ".", "append", "(", "Node", "(", "'upperSeismoDepth'", ",", "{", "}", ",", "upper_depth", ")", ")", "node", ".", "append", "(", "Node", "(", "'lowerSeismoDepth'", ",", "{", "}", ",", "lower_depth", ")", ")", "return", "node" ]
:param fault_trace: an object with an attribute .points :param dip: dip parameter :param upper_depth: upper seismogenic depth :param lower_depth: lower seismogenic depth :returns: a Node of kind simpleFaultGeometry
[ ":", "param", "fault_trace", ":", "an", "object", "with", "an", "attribute", ".", "points", ":", "param", "dip", ":", "dip", "parameter", ":", "param", "upper_depth", ":", "upper", "seismogenic", "depth", ":", "param", "lower_depth", ":", "lower", "seismogenic", "depth", ":", "returns", ":", "a", "Node", "of", "kind", "simpleFaultGeometry" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/geo/surface/simple_fault.py#L35-L52
gem/oq-engine
openquake/hazardlib/geo/surface/simple_fault.py
SimpleFaultSurface.check_fault_data
def check_fault_data(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip, mesh_spacing): """ Verify the fault data and raise ``ValueError`` if anything is wrong. This method doesn't have to be called by hands before creating the surface object, because it is called from :meth:`from_fault_data`. """ if not len(fault_trace) >= 2: raise ValueError("the fault trace must have at least two points") if not fault_trace.horizontal(): raise ValueError("the fault trace must be horizontal") tlats = [point.latitude for point in fault_trace.points] tlons = [point.longitude for point in fault_trace.points] if geo_utils.line_intersects_itself(tlons, tlats): raise ValueError("fault trace intersects itself") if not 0.0 < dip <= 90.0: raise ValueError("dip must be between 0.0 and 90.0") if not lower_seismogenic_depth > upper_seismogenic_depth: raise ValueError("lower seismogenic depth must be greater than " "upper seismogenic depth") if not upper_seismogenic_depth >= fault_trace[0].depth: raise ValueError("upper seismogenic depth must be greater than " "or equal to depth of fault trace") if not mesh_spacing > 0.0: raise ValueError("mesh spacing must be positive")
python
def check_fault_data(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip, mesh_spacing): if not len(fault_trace) >= 2: raise ValueError("the fault trace must have at least two points") if not fault_trace.horizontal(): raise ValueError("the fault trace must be horizontal") tlats = [point.latitude for point in fault_trace.points] tlons = [point.longitude for point in fault_trace.points] if geo_utils.line_intersects_itself(tlons, tlats): raise ValueError("fault trace intersects itself") if not 0.0 < dip <= 90.0: raise ValueError("dip must be between 0.0 and 90.0") if not lower_seismogenic_depth > upper_seismogenic_depth: raise ValueError("lower seismogenic depth must be greater than " "upper seismogenic depth") if not upper_seismogenic_depth >= fault_trace[0].depth: raise ValueError("upper seismogenic depth must be greater than " "or equal to depth of fault trace") if not mesh_spacing > 0.0: raise ValueError("mesh spacing must be positive")
[ "def", "check_fault_data", "(", "cls", ",", "fault_trace", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ",", "mesh_spacing", ")", ":", "if", "not", "len", "(", "fault_trace", ")", ">=", "2", ":", "raise", "ValueError", "(", "\"the fault trace must have at least two points\"", ")", "if", "not", "fault_trace", ".", "horizontal", "(", ")", ":", "raise", "ValueError", "(", "\"the fault trace must be horizontal\"", ")", "tlats", "=", "[", "point", ".", "latitude", "for", "point", "in", "fault_trace", ".", "points", "]", "tlons", "=", "[", "point", ".", "longitude", "for", "point", "in", "fault_trace", ".", "points", "]", "if", "geo_utils", ".", "line_intersects_itself", "(", "tlons", ",", "tlats", ")", ":", "raise", "ValueError", "(", "\"fault trace intersects itself\"", ")", "if", "not", "0.0", "<", "dip", "<=", "90.0", ":", "raise", "ValueError", "(", "\"dip must be between 0.0 and 90.0\"", ")", "if", "not", "lower_seismogenic_depth", ">", "upper_seismogenic_depth", ":", "raise", "ValueError", "(", "\"lower seismogenic depth must be greater than \"", "\"upper seismogenic depth\"", ")", "if", "not", "upper_seismogenic_depth", ">=", "fault_trace", "[", "0", "]", ".", "depth", ":", "raise", "ValueError", "(", "\"upper seismogenic depth must be greater than \"", "\"or equal to depth of fault trace\"", ")", "if", "not", "mesh_spacing", ">", "0.0", ":", "raise", "ValueError", "(", "\"mesh spacing must be positive\"", ")" ]
Verify the fault data and raise ``ValueError`` if anything is wrong. This method doesn't have to be called by hands before creating the surface object, because it is called from :meth:`from_fault_data`.
[ "Verify", "the", "fault", "data", "and", "raise", "ValueError", "if", "anything", "is", "wrong", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/geo/surface/simple_fault.py#L107-L132
gem/oq-engine
openquake/hazardlib/geo/surface/simple_fault.py
SimpleFaultSurface.from_fault_data
def from_fault_data(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip, mesh_spacing): """ Create and return a fault surface using fault source data. :param openquake.hazardlib.geo.line.Line fault_trace: Geographical line representing the intersection between the fault surface and the earth surface. The line must be horizontal (i.e. all depth values must be equal). If the depths are not given, they are assumed to be zero, meaning the trace intersects the surface at sea level, e.g. fault_trace = Line([Point(1, 1), Point(1, 2)]). :param upper_seismo_depth: Minimum depth ruptures can reach, in km (i.e. depth to fault's top edge). :param lower_seismo_depth: Maximum depth ruptures can reach, in km (i.e. depth to fault's bottom edge). :param dip: Dip angle (i.e. angle between fault surface and earth surface), in degrees. :param mesh_spacing: Distance between two subsequent points in a mesh, in km. :returns: An instance of :class:`SimpleFaultSurface` created using that data. Uses :meth:`check_fault_data` for checking parameters. """ cls.check_fault_data(fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip, mesh_spacing) # Loops over points in the top edge, for each point # on the top edge compute corresponding point on the bottom edge, then # computes equally spaced points between top and bottom points. vdist_top = upper_seismogenic_depth - fault_trace[0].depth vdist_bottom = lower_seismogenic_depth - fault_trace[0].depth hdist_top = vdist_top / math.tan(math.radians(dip)) hdist_bottom = vdist_bottom / math.tan(math.radians(dip)) strike = fault_trace[0].azimuth(fault_trace[-1]) azimuth = (strike + 90.0) % 360 mesh = [] for point in fault_trace.resample(mesh_spacing): top = point.point_at(hdist_top, vdist_top, azimuth) bottom = point.point_at(hdist_bottom, vdist_bottom, azimuth) mesh.append(top.equally_spaced_points(bottom, mesh_spacing)) # number of rows corresponds to number of points along dip # number of columns corresponds to number of points along strike surface_points = numpy.array(mesh).transpose().tolist() mesh = RectangularMesh.from_points_list(surface_points) assert 1 not in mesh.shape, ( "Mesh must have at least 2 nodes along both length and width." " Possible cause: Mesh spacing could be too large with respect to" " the fault length and width." ) self = cls(mesh) self.surface_nodes = [simple_fault_node( fault_trace, dip, upper_seismogenic_depth, lower_seismogenic_depth)] return self
python
def from_fault_data(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip, mesh_spacing): cls.check_fault_data(fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip, mesh_spacing) vdist_top = upper_seismogenic_depth - fault_trace[0].depth vdist_bottom = lower_seismogenic_depth - fault_trace[0].depth hdist_top = vdist_top / math.tan(math.radians(dip)) hdist_bottom = vdist_bottom / math.tan(math.radians(dip)) strike = fault_trace[0].azimuth(fault_trace[-1]) azimuth = (strike + 90.0) % 360 mesh = [] for point in fault_trace.resample(mesh_spacing): top = point.point_at(hdist_top, vdist_top, azimuth) bottom = point.point_at(hdist_bottom, vdist_bottom, azimuth) mesh.append(top.equally_spaced_points(bottom, mesh_spacing)) surface_points = numpy.array(mesh).transpose().tolist() mesh = RectangularMesh.from_points_list(surface_points) assert 1 not in mesh.shape, ( "Mesh must have at least 2 nodes along both length and width." " Possible cause: Mesh spacing could be too large with respect to" " the fault length and width." ) self = cls(mesh) self.surface_nodes = [simple_fault_node( fault_trace, dip, upper_seismogenic_depth, lower_seismogenic_depth)] return self
[ "def", "from_fault_data", "(", "cls", ",", "fault_trace", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ",", "mesh_spacing", ")", ":", "cls", ".", "check_fault_data", "(", "fault_trace", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ",", "mesh_spacing", ")", "# Loops over points in the top edge, for each point", "# on the top edge compute corresponding point on the bottom edge, then", "# computes equally spaced points between top and bottom points.", "vdist_top", "=", "upper_seismogenic_depth", "-", "fault_trace", "[", "0", "]", ".", "depth", "vdist_bottom", "=", "lower_seismogenic_depth", "-", "fault_trace", "[", "0", "]", ".", "depth", "hdist_top", "=", "vdist_top", "/", "math", ".", "tan", "(", "math", ".", "radians", "(", "dip", ")", ")", "hdist_bottom", "=", "vdist_bottom", "/", "math", ".", "tan", "(", "math", ".", "radians", "(", "dip", ")", ")", "strike", "=", "fault_trace", "[", "0", "]", ".", "azimuth", "(", "fault_trace", "[", "-", "1", "]", ")", "azimuth", "=", "(", "strike", "+", "90.0", ")", "%", "360", "mesh", "=", "[", "]", "for", "point", "in", "fault_trace", ".", "resample", "(", "mesh_spacing", ")", ":", "top", "=", "point", ".", "point_at", "(", "hdist_top", ",", "vdist_top", ",", "azimuth", ")", "bottom", "=", "point", ".", "point_at", "(", "hdist_bottom", ",", "vdist_bottom", ",", "azimuth", ")", "mesh", ".", "append", "(", "top", ".", "equally_spaced_points", "(", "bottom", ",", "mesh_spacing", ")", ")", "# number of rows corresponds to number of points along dip", "# number of columns corresponds to number of points along strike", "surface_points", "=", "numpy", ".", "array", "(", "mesh", ")", ".", "transpose", "(", ")", ".", "tolist", "(", ")", "mesh", "=", "RectangularMesh", ".", "from_points_list", "(", "surface_points", ")", "assert", "1", "not", "in", "mesh", ".", "shape", ",", "(", "\"Mesh must have at least 2 nodes along both length and width.\"", "\" Possible cause: Mesh spacing could be too large with respect to\"", "\" the fault length and width.\"", ")", "self", "=", "cls", "(", "mesh", ")", "self", ".", "surface_nodes", "=", "[", "simple_fault_node", "(", "fault_trace", ",", "dip", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ")", "]", "return", "self" ]
Create and return a fault surface using fault source data. :param openquake.hazardlib.geo.line.Line fault_trace: Geographical line representing the intersection between the fault surface and the earth surface. The line must be horizontal (i.e. all depth values must be equal). If the depths are not given, they are assumed to be zero, meaning the trace intersects the surface at sea level, e.g. fault_trace = Line([Point(1, 1), Point(1, 2)]). :param upper_seismo_depth: Minimum depth ruptures can reach, in km (i.e. depth to fault's top edge). :param lower_seismo_depth: Maximum depth ruptures can reach, in km (i.e. depth to fault's bottom edge). :param dip: Dip angle (i.e. angle between fault surface and earth surface), in degrees. :param mesh_spacing: Distance between two subsequent points in a mesh, in km. :returns: An instance of :class:`SimpleFaultSurface` created using that data. Uses :meth:`check_fault_data` for checking parameters.
[ "Create", "and", "return", "a", "fault", "surface", "using", "fault", "source", "data", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/geo/surface/simple_fault.py#L135-L196
gem/oq-engine
openquake/hazardlib/geo/surface/simple_fault.py
SimpleFaultSurface.get_fault_patch_vertices
def get_fault_patch_vertices(cls, rupture_top_edge, upper_seismogenic_depth, lower_seismogenic_depth, dip, index_patch=1): """ Get surface main vertices. Parameters are the same as for :meth:`from_fault_data`, excluding fault_trace, and mesh spacing. :param rupture_top_edge: A instances of :class:`openquake.hazardlib.geo.line.Line` representing the rupture surface's top edge. :param index_patch: Indicate the patch of the fault in order to output the vertices. The fault patch numbering follows the same logic of the right-hand rule i.e. patch with index 1 is the first patch along the trace. :returns: Four :class:~openquake.hazardlib.geo.point.Point objects representing the four vertices of the target patch. """ # Similar to :meth:`from_fault_data`, we just don't resample edges dip_tan = math.tan(math.radians(dip)) hdist_bottom = ( lower_seismogenic_depth - upper_seismogenic_depth) / dip_tan strike = rupture_top_edge[0].azimuth(rupture_top_edge[-1]) azimuth = (strike + 90.0) % 360 # Collect coordinates of vertices on the top and bottom edge lons = [] lats = [] deps = [] t_lon = [] t_lat = [] t_dep = [] for point in rupture_top_edge.points: top_edge_point = point bottom_edge_point = point.point_at(hdist_bottom, 0, azimuth) lons.append(top_edge_point.longitude) lats.append(top_edge_point.latitude) deps.append(upper_seismogenic_depth) t_lon.append(bottom_edge_point.longitude) t_lat.append(bottom_edge_point.latitude) t_dep.append(lower_seismogenic_depth) all_lons = numpy.array(lons + list(reversed(t_lon)), float) all_lats = numpy.array(lats + list(reversed(t_lat)), float) all_deps = numpy.array(deps + list(reversed(t_dep)), float) index1 = int(index_patch - 1) index2 = int(index_patch) index3 = int(2 * len(rupture_top_edge) - (index_patch + 1)) index4 = int(2 * len(rupture_top_edge) - index_patch) p0 = Point(all_lons[index1], all_lats[index1], all_deps[index1]) p1 = Point(all_lons[index2], all_lats[index2], all_deps[index2]) p2 = Point(all_lons[index3], all_lats[index3], all_deps[index3]) p3 = Point(all_lons[index4], all_lats[index4], all_deps[index4]) return p0, p1, p2, p3
python
def get_fault_patch_vertices(cls, rupture_top_edge, upper_seismogenic_depth, lower_seismogenic_depth, dip, index_patch=1): dip_tan = math.tan(math.radians(dip)) hdist_bottom = ( lower_seismogenic_depth - upper_seismogenic_depth) / dip_tan strike = rupture_top_edge[0].azimuth(rupture_top_edge[-1]) azimuth = (strike + 90.0) % 360 lons = [] lats = [] deps = [] t_lon = [] t_lat = [] t_dep = [] for point in rupture_top_edge.points: top_edge_point = point bottom_edge_point = point.point_at(hdist_bottom, 0, azimuth) lons.append(top_edge_point.longitude) lats.append(top_edge_point.latitude) deps.append(upper_seismogenic_depth) t_lon.append(bottom_edge_point.longitude) t_lat.append(bottom_edge_point.latitude) t_dep.append(lower_seismogenic_depth) all_lons = numpy.array(lons + list(reversed(t_lon)), float) all_lats = numpy.array(lats + list(reversed(t_lat)), float) all_deps = numpy.array(deps + list(reversed(t_dep)), float) index1 = int(index_patch - 1) index2 = int(index_patch) index3 = int(2 * len(rupture_top_edge) - (index_patch + 1)) index4 = int(2 * len(rupture_top_edge) - index_patch) p0 = Point(all_lons[index1], all_lats[index1], all_deps[index1]) p1 = Point(all_lons[index2], all_lats[index2], all_deps[index2]) p2 = Point(all_lons[index3], all_lats[index3], all_deps[index3]) p3 = Point(all_lons[index4], all_lats[index4], all_deps[index4]) return p0, p1, p2, p3
[ "def", "get_fault_patch_vertices", "(", "cls", ",", "rupture_top_edge", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ",", "index_patch", "=", "1", ")", ":", "# Similar to :meth:`from_fault_data`, we just don't resample edges", "dip_tan", "=", "math", ".", "tan", "(", "math", ".", "radians", "(", "dip", ")", ")", "hdist_bottom", "=", "(", "lower_seismogenic_depth", "-", "upper_seismogenic_depth", ")", "/", "dip_tan", "strike", "=", "rupture_top_edge", "[", "0", "]", ".", "azimuth", "(", "rupture_top_edge", "[", "-", "1", "]", ")", "azimuth", "=", "(", "strike", "+", "90.0", ")", "%", "360", "# Collect coordinates of vertices on the top and bottom edge", "lons", "=", "[", "]", "lats", "=", "[", "]", "deps", "=", "[", "]", "t_lon", "=", "[", "]", "t_lat", "=", "[", "]", "t_dep", "=", "[", "]", "for", "point", "in", "rupture_top_edge", ".", "points", ":", "top_edge_point", "=", "point", "bottom_edge_point", "=", "point", ".", "point_at", "(", "hdist_bottom", ",", "0", ",", "azimuth", ")", "lons", ".", "append", "(", "top_edge_point", ".", "longitude", ")", "lats", ".", "append", "(", "top_edge_point", ".", "latitude", ")", "deps", ".", "append", "(", "upper_seismogenic_depth", ")", "t_lon", ".", "append", "(", "bottom_edge_point", ".", "longitude", ")", "t_lat", ".", "append", "(", "bottom_edge_point", ".", "latitude", ")", "t_dep", ".", "append", "(", "lower_seismogenic_depth", ")", "all_lons", "=", "numpy", ".", "array", "(", "lons", "+", "list", "(", "reversed", "(", "t_lon", ")", ")", ",", "float", ")", "all_lats", "=", "numpy", ".", "array", "(", "lats", "+", "list", "(", "reversed", "(", "t_lat", ")", ")", ",", "float", ")", "all_deps", "=", "numpy", ".", "array", "(", "deps", "+", "list", "(", "reversed", "(", "t_dep", ")", ")", ",", "float", ")", "index1", "=", "int", "(", "index_patch", "-", "1", ")", "index2", "=", "int", "(", "index_patch", ")", "index3", "=", "int", "(", "2", "*", "len", "(", "rupture_top_edge", ")", "-", "(", "index_patch", "+", "1", ")", ")", "index4", "=", "int", "(", "2", "*", "len", "(", "rupture_top_edge", ")", "-", "index_patch", ")", "p0", "=", "Point", "(", "all_lons", "[", "index1", "]", ",", "all_lats", "[", "index1", "]", ",", "all_deps", "[", "index1", "]", ")", "p1", "=", "Point", "(", "all_lons", "[", "index2", "]", ",", "all_lats", "[", "index2", "]", ",", "all_deps", "[", "index2", "]", ")", "p2", "=", "Point", "(", "all_lons", "[", "index3", "]", ",", "all_lats", "[", "index3", "]", ",", "all_deps", "[", "index3", "]", ")", "p3", "=", "Point", "(", "all_lons", "[", "index4", "]", ",", "all_lats", "[", "index4", "]", ",", "all_deps", "[", "index4", "]", ")", "return", "p0", ",", "p1", ",", "p2", ",", "p3" ]
Get surface main vertices. Parameters are the same as for :meth:`from_fault_data`, excluding fault_trace, and mesh spacing. :param rupture_top_edge: A instances of :class:`openquake.hazardlib.geo.line.Line` representing the rupture surface's top edge. :param index_patch: Indicate the patch of the fault in order to output the vertices. The fault patch numbering follows the same logic of the right-hand rule i.e. patch with index 1 is the first patch along the trace. :returns: Four :class:~openquake.hazardlib.geo.point.Point objects representing the four vertices of the target patch.
[ "Get", "surface", "main", "vertices", ".", "Parameters", "are", "the", "same", "as", "for", ":", "meth", ":", "from_fault_data", "excluding", "fault_trace", "and", "mesh", "spacing", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/geo/surface/simple_fault.py#L199-L253
gem/oq-engine
openquake/hazardlib/geo/surface/simple_fault.py
SimpleFaultSurface.hypocentre_patch_index
def hypocentre_patch_index(cls, hypocentre, rupture_top_edge, upper_seismogenic_depth, lower_seismogenic_depth, dip): """ This methods finds the index of the fault patch including the hypocentre. :param hypocentre: :class:`~openquake.hazardlib.geo.point.Point` object representing the location of hypocentre. :param rupture_top_edge: A instances of :class:`openquake.hazardlib.geo.line.Line` representing the rupture surface's top edge. :param upper_seismo_depth: Minimum depth ruptures can reach, in km (i.e. depth to fault's top edge). :param lower_seismo_depth: Maximum depth ruptures can reach, in km (i.e. depth to fault's bottom edge). :param dip: Dip angle (i.e. angle between fault surface and earth surface), in degrees. :return: An integer corresponding to the index of the fault patch which contains the hypocentre. """ totaln_patch = len(rupture_top_edge) indexlist = [] dist_list = [] for i, index in enumerate(range(1, totaln_patch)): p0, p1, p2, p3 = cls.get_fault_patch_vertices( rupture_top_edge, upper_seismogenic_depth, lower_seismogenic_depth, dip, index_patch=index) [normal, dist_to_plane] = get_plane_equation(p0, p1, p2, hypocentre) indexlist.append(index) dist_list.append(dist_to_plane) if numpy.allclose(dist_to_plane, 0., atol=25., rtol=0.): return index break index = indexlist[numpy.argmin(dist_list)] return index
python
def hypocentre_patch_index(cls, hypocentre, rupture_top_edge, upper_seismogenic_depth, lower_seismogenic_depth, dip): totaln_patch = len(rupture_top_edge) indexlist = [] dist_list = [] for i, index in enumerate(range(1, totaln_patch)): p0, p1, p2, p3 = cls.get_fault_patch_vertices( rupture_top_edge, upper_seismogenic_depth, lower_seismogenic_depth, dip, index_patch=index) [normal, dist_to_plane] = get_plane_equation(p0, p1, p2, hypocentre) indexlist.append(index) dist_list.append(dist_to_plane) if numpy.allclose(dist_to_plane, 0., atol=25., rtol=0.): return index break index = indexlist[numpy.argmin(dist_list)] return index
[ "def", "hypocentre_patch_index", "(", "cls", ",", "hypocentre", ",", "rupture_top_edge", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ")", ":", "totaln_patch", "=", "len", "(", "rupture_top_edge", ")", "indexlist", "=", "[", "]", "dist_list", "=", "[", "]", "for", "i", ",", "index", "in", "enumerate", "(", "range", "(", "1", ",", "totaln_patch", ")", ")", ":", "p0", ",", "p1", ",", "p2", ",", "p3", "=", "cls", ".", "get_fault_patch_vertices", "(", "rupture_top_edge", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ",", "index_patch", "=", "index", ")", "[", "normal", ",", "dist_to_plane", "]", "=", "get_plane_equation", "(", "p0", ",", "p1", ",", "p2", ",", "hypocentre", ")", "indexlist", ".", "append", "(", "index", ")", "dist_list", ".", "append", "(", "dist_to_plane", ")", "if", "numpy", ".", "allclose", "(", "dist_to_plane", ",", "0.", ",", "atol", "=", "25.", ",", "rtol", "=", "0.", ")", ":", "return", "index", "break", "index", "=", "indexlist", "[", "numpy", ".", "argmin", "(", "dist_list", ")", "]", "return", "index" ]
This methods finds the index of the fault patch including the hypocentre. :param hypocentre: :class:`~openquake.hazardlib.geo.point.Point` object representing the location of hypocentre. :param rupture_top_edge: A instances of :class:`openquake.hazardlib.geo.line.Line` representing the rupture surface's top edge. :param upper_seismo_depth: Minimum depth ruptures can reach, in km (i.e. depth to fault's top edge). :param lower_seismo_depth: Maximum depth ruptures can reach, in km (i.e. depth to fault's bottom edge). :param dip: Dip angle (i.e. angle between fault surface and earth surface), in degrees. :return: An integer corresponding to the index of the fault patch which contains the hypocentre.
[ "This", "methods", "finds", "the", "index", "of", "the", "fault", "patch", "including", "the", "hypocentre", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/geo/surface/simple_fault.py#L256-L298
gem/oq-engine
openquake/hazardlib/geo/surface/simple_fault.py
SimpleFaultSurface.get_surface_vertexes
def get_surface_vertexes(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip): """ Get surface main vertexes. Parameters are the same as for :meth:`from_fault_data`, excluding mesh spacing. :returns: Instance of :class:`~openquake.hazardlib.geo.polygon.Polygon` describing the surface projection of the simple fault with specified parameters. """ # Similar to :meth:`from_fault_data`, we just don't resample edges dip_tan = math.tan(math.radians(dip)) hdist_top = upper_seismogenic_depth / dip_tan hdist_bottom = lower_seismogenic_depth / dip_tan strike = fault_trace[0].azimuth(fault_trace[-1]) azimuth = (strike + 90.0) % 360 # Collect coordinates of vertices on the top and bottom edge lons = [] lats = [] for point in fault_trace.points: top_edge_point = point.point_at(hdist_top, 0, azimuth) bottom_edge_point = point.point_at(hdist_bottom, 0, azimuth) lons.append(top_edge_point.longitude) lats.append(top_edge_point.latitude) lons.append(bottom_edge_point.longitude) lats.append(bottom_edge_point.latitude) lons = numpy.array(lons, float) lats = numpy.array(lats, float) return lons, lats
python
def get_surface_vertexes(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip): dip_tan = math.tan(math.radians(dip)) hdist_top = upper_seismogenic_depth / dip_tan hdist_bottom = lower_seismogenic_depth / dip_tan strike = fault_trace[0].azimuth(fault_trace[-1]) azimuth = (strike + 90.0) % 360 lons = [] lats = [] for point in fault_trace.points: top_edge_point = point.point_at(hdist_top, 0, azimuth) bottom_edge_point = point.point_at(hdist_bottom, 0, azimuth) lons.append(top_edge_point.longitude) lats.append(top_edge_point.latitude) lons.append(bottom_edge_point.longitude) lats.append(bottom_edge_point.latitude) lons = numpy.array(lons, float) lats = numpy.array(lats, float) return lons, lats
[ "def", "get_surface_vertexes", "(", "cls", ",", "fault_trace", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ")", ":", "# Similar to :meth:`from_fault_data`, we just don't resample edges", "dip_tan", "=", "math", ".", "tan", "(", "math", ".", "radians", "(", "dip", ")", ")", "hdist_top", "=", "upper_seismogenic_depth", "/", "dip_tan", "hdist_bottom", "=", "lower_seismogenic_depth", "/", "dip_tan", "strike", "=", "fault_trace", "[", "0", "]", ".", "azimuth", "(", "fault_trace", "[", "-", "1", "]", ")", "azimuth", "=", "(", "strike", "+", "90.0", ")", "%", "360", "# Collect coordinates of vertices on the top and bottom edge", "lons", "=", "[", "]", "lats", "=", "[", "]", "for", "point", "in", "fault_trace", ".", "points", ":", "top_edge_point", "=", "point", ".", "point_at", "(", "hdist_top", ",", "0", ",", "azimuth", ")", "bottom_edge_point", "=", "point", ".", "point_at", "(", "hdist_bottom", ",", "0", ",", "azimuth", ")", "lons", ".", "append", "(", "top_edge_point", ".", "longitude", ")", "lats", ".", "append", "(", "top_edge_point", ".", "latitude", ")", "lons", ".", "append", "(", "bottom_edge_point", ".", "longitude", ")", "lats", ".", "append", "(", "bottom_edge_point", ".", "latitude", ")", "lons", "=", "numpy", ".", "array", "(", "lons", ",", "float", ")", "lats", "=", "numpy", ".", "array", "(", "lats", ",", "float", ")", "return", "lons", ",", "lats" ]
Get surface main vertexes. Parameters are the same as for :meth:`from_fault_data`, excluding mesh spacing. :returns: Instance of :class:`~openquake.hazardlib.geo.polygon.Polygon` describing the surface projection of the simple fault with specified parameters.
[ "Get", "surface", "main", "vertexes", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/geo/surface/simple_fault.py#L301-L336
gem/oq-engine
openquake/hazardlib/geo/surface/simple_fault.py
SimpleFaultSurface.surface_projection_from_fault_data
def surface_projection_from_fault_data(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip): """ Get a surface projection of the simple fault surface. Parameters are the same as for :meth:`from_fault_data`, excluding mesh spacing. :returns: Instance of :class:`~openquake.hazardlib.geo.polygon.Polygon` describing the surface projection of the simple fault with specified parameters. """ lons, lats = cls.get_surface_vertexes(fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip) return Mesh(lons, lats, depths=None).get_convex_hull()
python
def surface_projection_from_fault_data(cls, fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip): lons, lats = cls.get_surface_vertexes(fault_trace, upper_seismogenic_depth, lower_seismogenic_depth, dip) return Mesh(lons, lats, depths=None).get_convex_hull()
[ "def", "surface_projection_from_fault_data", "(", "cls", ",", "fault_trace", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ")", ":", "lons", ",", "lats", "=", "cls", ".", "get_surface_vertexes", "(", "fault_trace", ",", "upper_seismogenic_depth", ",", "lower_seismogenic_depth", ",", "dip", ")", "return", "Mesh", "(", "lons", ",", "lats", ",", "depths", "=", "None", ")", ".", "get_convex_hull", "(", ")" ]
Get a surface projection of the simple fault surface. Parameters are the same as for :meth:`from_fault_data`, excluding mesh spacing. :returns: Instance of :class:`~openquake.hazardlib.geo.polygon.Polygon` describing the surface projection of the simple fault with specified parameters.
[ "Get", "a", "surface", "projection", "of", "the", "simple", "fault", "surface", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/geo/surface/simple_fault.py#L339-L356
gem/oq-engine
openquake/hazardlib/gsim/zhao_2006.py
ZhaoEtAl2006Asc.get_mean_and_stddevs
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values. """ # extracting dictionary of coefficients specific to required # intensity measure type. C = self.COEFFS_ASC[imt] # mean value as given by equation 1, p. 901, without considering the # interface and intraslab terms (that is SI, SS, SSL = 0) and the # inter and intra event terms, plus the magnitude-squared term # correction factor (equation 5 p. 909). mean = self._compute_magnitude_term(C, rup.mag) +\ self._compute_distance_term(C, rup.mag, dists.rrup) +\ self._compute_focal_depth_term(C, rup.hypo_depth) +\ self._compute_faulting_style_term(C, rup.rake) +\ self._compute_site_class_term(C, sites.vs30) +\ self._compute_magnitude_squared_term(P=0.0, M=6.3, Q=C['QC'], W=C['WC'], mag=rup.mag) # convert from cm/s**2 to g mean = np.log(np.exp(mean) * 1e-2 / g) stddevs = self._get_stddevs(C['sigma'], C['tauC'], stddev_types, num_sites=len(sites.vs30)) return mean, stddevs
python
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): C = self.COEFFS_ASC[imt] mean = self._compute_magnitude_term(C, rup.mag) +\ self._compute_distance_term(C, rup.mag, dists.rrup) +\ self._compute_focal_depth_term(C, rup.hypo_depth) +\ self._compute_faulting_style_term(C, rup.rake) +\ self._compute_site_class_term(C, sites.vs30) +\ self._compute_magnitude_squared_term(P=0.0, M=6.3, Q=C['QC'], W=C['WC'], mag=rup.mag) mean = np.log(np.exp(mean) * 1e-2 / g) stddevs = self._get_stddevs(C['sigma'], C['tauC'], stddev_types, num_sites=len(sites.vs30)) return mean, stddevs
[ "def", "get_mean_and_stddevs", "(", "self", ",", "sites", ",", "rup", ",", "dists", ",", "imt", ",", "stddev_types", ")", ":", "# extracting dictionary of coefficients specific to required", "# intensity measure type.", "C", "=", "self", ".", "COEFFS_ASC", "[", "imt", "]", "# mean value as given by equation 1, p. 901, without considering the", "# interface and intraslab terms (that is SI, SS, SSL = 0) and the", "# inter and intra event terms, plus the magnitude-squared term", "# correction factor (equation 5 p. 909).", "mean", "=", "self", ".", "_compute_magnitude_term", "(", "C", ",", "rup", ".", "mag", ")", "+", "self", ".", "_compute_distance_term", "(", "C", ",", "rup", ".", "mag", ",", "dists", ".", "rrup", ")", "+", "self", ".", "_compute_focal_depth_term", "(", "C", ",", "rup", ".", "hypo_depth", ")", "+", "self", ".", "_compute_faulting_style_term", "(", "C", ",", "rup", ".", "rake", ")", "+", "self", ".", "_compute_site_class_term", "(", "C", ",", "sites", ".", "vs30", ")", "+", "self", ".", "_compute_magnitude_squared_term", "(", "P", "=", "0.0", ",", "M", "=", "6.3", ",", "Q", "=", "C", "[", "'QC'", "]", ",", "W", "=", "C", "[", "'WC'", "]", ",", "mag", "=", "rup", ".", "mag", ")", "# convert from cm/s**2 to g", "mean", "=", "np", ".", "log", "(", "np", ".", "exp", "(", "mean", ")", "*", "1e-2", "/", "g", ")", "stddevs", "=", "self", ".", "_get_stddevs", "(", "C", "[", "'sigma'", "]", ",", "C", "[", "'tauC'", "]", ",", "stddev_types", ",", "num_sites", "=", "len", "(", "sites", ".", "vs30", ")", ")", "return", "mean", ",", "stddevs" ]
See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values.
[ "See", ":", "meth", ":", "superclass", "method", "<", ".", "base", ".", "GroundShakingIntensityModel", ".", "get_mean_and_stddevs", ">", "for", "spec", "of", "input", "and", "result", "values", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/zhao_2006.py#L81-L109
gem/oq-engine
openquake/hazardlib/gsim/zhao_2006.py
ZhaoEtAl2006Asc._compute_distance_term
def _compute_distance_term(self, C, mag, rrup): """ Compute second and third terms in equation 1, p. 901. """ term1 = C['b'] * rrup term2 = - np.log(rrup + C['c'] * np.exp(C['d'] * mag)) return term1 + term2
python
def _compute_distance_term(self, C, mag, rrup): term1 = C['b'] * rrup term2 = - np.log(rrup + C['c'] * np.exp(C['d'] * mag)) return term1 + term2
[ "def", "_compute_distance_term", "(", "self", ",", "C", ",", "mag", ",", "rrup", ")", ":", "term1", "=", "C", "[", "'b'", "]", "*", "rrup", "term2", "=", "-", "np", ".", "log", "(", "rrup", "+", "C", "[", "'c'", "]", "*", "np", ".", "exp", "(", "C", "[", "'d'", "]", "*", "mag", ")", ")", "return", "term1", "+", "term2" ]
Compute second and third terms in equation 1, p. 901.
[ "Compute", "second", "and", "third", "terms", "in", "equation", "1", "p", ".", "901", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/zhao_2006.py#L133-L140
gem/oq-engine
openquake/hazardlib/gsim/zhao_2006.py
ZhaoEtAl2006Asc._compute_focal_depth_term
def _compute_focal_depth_term(self, C, hypo_depth): """ Compute fourth term in equation 1, p. 901. """ # p. 901. "(i.e, depth is capped at 125 km)". focal_depth = hypo_depth if focal_depth > 125.0: focal_depth = 125.0 # p. 902. "We used the value of 15 km for the # depth coefficient hc ...". hc = 15.0 # p. 901. "When h is larger than hc, the depth terms takes # effect ...". The next sentence specifies h>=hc. return float(focal_depth >= hc) * C['e'] * (focal_depth - hc)
python
def _compute_focal_depth_term(self, C, hypo_depth): focal_depth = hypo_depth if focal_depth > 125.0: focal_depth = 125.0 hc = 15.0 return float(focal_depth >= hc) * C['e'] * (focal_depth - hc)
[ "def", "_compute_focal_depth_term", "(", "self", ",", "C", ",", "hypo_depth", ")", ":", "# p. 901. \"(i.e, depth is capped at 125 km)\".", "focal_depth", "=", "hypo_depth", "if", "focal_depth", ">", "125.0", ":", "focal_depth", "=", "125.0", "# p. 902. \"We used the value of 15 km for the", "# depth coefficient hc ...\".", "hc", "=", "15.0", "# p. 901. \"When h is larger than hc, the depth terms takes", "# effect ...\". The next sentence specifies h>=hc.", "return", "float", "(", "focal_depth", ">=", "hc", ")", "*", "C", "[", "'e'", "]", "*", "(", "focal_depth", "-", "hc", ")" ]
Compute fourth term in equation 1, p. 901.
[ "Compute", "fourth", "term", "in", "equation", "1", "p", ".", "901", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/zhao_2006.py#L142-L157
gem/oq-engine
openquake/hazardlib/gsim/zhao_2006.py
ZhaoEtAl2006Asc._compute_site_class_term
def _compute_site_class_term(self, C, vs30): """ Compute nine-th term in equation 1, p. 901. """ # map vs30 value to site class, see table 2, p. 901. site_term = np.zeros(len(vs30)) # hard rock site_term[vs30 > 1100.0] = C['CH'] # rock site_term[(vs30 > 600) & (vs30 <= 1100)] = C['C1'] # hard soil site_term[(vs30 > 300) & (vs30 <= 600)] = C['C2'] # medium soil site_term[(vs30 > 200) & (vs30 <= 300)] = C['C3'] # soft soil site_term[vs30 <= 200] = C['C4'] return site_term
python
def _compute_site_class_term(self, C, vs30): site_term = np.zeros(len(vs30)) site_term[vs30 > 1100.0] = C['CH'] site_term[(vs30 > 600) & (vs30 <= 1100)] = C['C1'] site_term[(vs30 > 300) & (vs30 <= 600)] = C['C2'] site_term[(vs30 > 200) & (vs30 <= 300)] = C['C3'] site_term[vs30 <= 200] = C['C4'] return site_term
[ "def", "_compute_site_class_term", "(", "self", ",", "C", ",", "vs30", ")", ":", "# map vs30 value to site class, see table 2, p. 901.", "site_term", "=", "np", ".", "zeros", "(", "len", "(", "vs30", ")", ")", "# hard rock", "site_term", "[", "vs30", ">", "1100.0", "]", "=", "C", "[", "'CH'", "]", "# rock", "site_term", "[", "(", "vs30", ">", "600", ")", "&", "(", "vs30", "<=", "1100", ")", "]", "=", "C", "[", "'C1'", "]", "# hard soil", "site_term", "[", "(", "vs30", ">", "300", ")", "&", "(", "vs30", "<=", "600", ")", "]", "=", "C", "[", "'C2'", "]", "# medium soil", "site_term", "[", "(", "vs30", ">", "200", ")", "&", "(", "vs30", "<=", "300", ")", "]", "=", "C", "[", "'C3'", "]", "# soft soil", "site_term", "[", "vs30", "<=", "200", "]", "=", "C", "[", "'C4'", "]", "return", "site_term" ]
Compute nine-th term in equation 1, p. 901.
[ "Compute", "nine", "-", "th", "term", "in", "equation", "1", "p", ".", "901", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/zhao_2006.py#L168-L190
gem/oq-engine
openquake/hazardlib/gsim/zhao_2006.py
ZhaoEtAl2006Asc._compute_magnitude_squared_term
def _compute_magnitude_squared_term(self, P, M, Q, W, mag): """ Compute magnitude squared term, equation 5, p. 909. """ return P * (mag - M) + Q * (mag - M) ** 2 + W
python
def _compute_magnitude_squared_term(self, P, M, Q, W, mag): return P * (mag - M) + Q * (mag - M) ** 2 + W
[ "def", "_compute_magnitude_squared_term", "(", "self", ",", "P", ",", "M", ",", "Q", ",", "W", ",", "mag", ")", ":", "return", "P", "*", "(", "mag", "-", "M", ")", "+", "Q", "*", "(", "mag", "-", "M", ")", "**", "2", "+", "W" ]
Compute magnitude squared term, equation 5, p. 909.
[ "Compute", "magnitude", "squared", "term", "equation", "5", "p", ".", "909", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/zhao_2006.py#L192-L196
gem/oq-engine
openquake/hazardlib/gsim/zhao_2006.py
ZhaoEtAl2006SSlab._compute_slab_correction_term
def _compute_slab_correction_term(self, C, rrup): """ Compute path modification term for slab events, that is the 8-th term in equation 1, p. 901. """ slab_term = C['SSL'] * np.log(rrup) return slab_term
python
def _compute_slab_correction_term(self, C, rrup): slab_term = C['SSL'] * np.log(rrup) return slab_term
[ "def", "_compute_slab_correction_term", "(", "self", ",", "C", ",", "rrup", ")", ":", "slab_term", "=", "C", "[", "'SSL'", "]", "*", "np", ".", "log", "(", "rrup", ")", "return", "slab_term" ]
Compute path modification term for slab events, that is the 8-th term in equation 1, p. 901.
[ "Compute", "path", "modification", "term", "for", "slab", "events", "that", "is", "the", "8", "-", "th", "term", "in", "equation", "1", "p", ".", "901", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/zhao_2006.py#L369-L376
gem/oq-engine
openquake/hazardlib/gsim/zhao_2006.py
ZhaoEtAl2006AscSGS.get_mean_and_stddevs
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ Using a minimum distance of 5km for the calculation. """ dists_mod = copy.deepcopy(dists) dists_mod.rrup[dists.rrup <= 5.] = 5. return super().get_mean_and_stddevs( sites, rup, dists_mod, imt, stddev_types)
python
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): dists_mod = copy.deepcopy(dists) dists_mod.rrup[dists.rrup <= 5.] = 5. return super().get_mean_and_stddevs( sites, rup, dists_mod, imt, stddev_types)
[ "def", "get_mean_and_stddevs", "(", "self", ",", "sites", ",", "rup", ",", "dists", ",", "imt", ",", "stddev_types", ")", ":", "dists_mod", "=", "copy", ".", "deepcopy", "(", "dists", ")", "dists_mod", ".", "rrup", "[", "dists", ".", "rrup", "<=", "5.", "]", "=", "5.", "return", "super", "(", ")", ".", "get_mean_and_stddevs", "(", "sites", ",", "rup", ",", "dists_mod", ",", "imt", ",", "stddev_types", ")" ]
Using a minimum distance of 5km for the calculation.
[ "Using", "a", "minimum", "distance", "of", "5km", "for", "the", "calculation", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/zhao_2006.py#L654-L663
gem/oq-engine
openquake/engine/utils/__init__.py
confirm
def confirm(prompt): """ Ask for confirmation, given a ``prompt`` and return a boolean value. """ while True: try: answer = input(prompt) except KeyboardInterrupt: # the user presses ctrl+c, just say 'no' return False answer = answer.strip().lower() if answer not in ('y', 'n'): print('Please enter y or n') continue return answer == 'y'
python
def confirm(prompt): while True: try: answer = input(prompt) except KeyboardInterrupt: return False answer = answer.strip().lower() if answer not in ('y', 'n'): print('Please enter y or n') continue return answer == 'y'
[ "def", "confirm", "(", "prompt", ")", ":", "while", "True", ":", "try", ":", "answer", "=", "input", "(", "prompt", ")", "except", "KeyboardInterrupt", ":", "# the user presses ctrl+c, just say 'no'", "return", "False", "answer", "=", "answer", ".", "strip", "(", ")", ".", "lower", "(", ")", "if", "answer", "not", "in", "(", "'y'", ",", "'n'", ")", ":", "print", "(", "'Please enter y or n'", ")", "continue", "return", "answer", "==", "'y'" ]
Ask for confirmation, given a ``prompt`` and return a boolean value.
[ "Ask", "for", "confirmation", "given", "a", "prompt", "and", "return", "a", "boolean", "value", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/engine/utils/__init__.py#L20-L34
gem/oq-engine
openquake/risklib/asset.py
build_asset_array
def build_asset_array(assets_by_site, tagnames=(), time_event=None): """ :param assets_by_site: a list of lists of assets :param tagnames: a list of tag names :returns: an array `assetcol` """ for assets in assets_by_site: if len(assets): first_asset = assets[0] break else: # no break raise ValueError('There are no assets!') loss_types = [] occupancy_periods = [] for name in sorted(first_asset.values): if name.startswith('occupants_'): period = name.split('_', 1)[1] if period != 'None': # see scenario_risk test_case_2d occupancy_periods.append(period) loss_types.append(name) # discard occupants for different time periods else: loss_types.append('value-' + name) # loss_types can be ['value-business_interruption', 'value-contents', # 'value-nonstructural', 'occupants_None', 'occupants_day', # 'occupants_night', 'occupants_transit'] deductible_d = first_asset.deductibles or {} limit_d = first_asset.insurance_limits or {} if deductible_d or limit_d: logging.warning('Exposures with insuranceLimit/deductible fields are ' 'deprecated and may be removed in the future') retro = ['retrofitted'] if first_asset._retrofitted else [] float_fields = loss_types + retro int_fields = [(str(name), U16) for name in tagnames] tagi = {str(name): i for i, name in enumerate(tagnames)} asset_dt = numpy.dtype( [('ordinal', U32), ('lon', F32), ('lat', F32), ('site_id', U32), ('number', F32), ('area', F32)] + [ (str(name), float) for name in float_fields] + int_fields) num_assets = sum(len(assets) for assets in assets_by_site) assetcol = numpy.zeros(num_assets, asset_dt) asset_ordinal = 0 fields = set(asset_dt.fields) for sid, assets_ in enumerate(assets_by_site): for asset in assets_: asset.ordinal = asset_ordinal record = assetcol[asset_ordinal] asset_ordinal += 1 for field in fields: if field == 'ordinal': value = asset.ordinal elif field == 'number': value = asset.number elif field == 'area': value = asset.area elif field == 'site_id': value = sid elif field == 'lon': value = asset.location[0] elif field == 'lat': value = asset.location[1] elif field.startswith('occupants_'): value = asset.values[field] elif field == 'retrofitted': value = asset.retrofitted() elif field in tagnames: value = asset.tagidxs[tagi[field]] else: name, lt = field.split('-') value = asset.value(lt, time_event) record[field] = value return assetcol, ' '.join(occupancy_periods)
python
def build_asset_array(assets_by_site, tagnames=(), time_event=None): for assets in assets_by_site: if len(assets): first_asset = assets[0] break else: raise ValueError('There are no assets!') loss_types = [] occupancy_periods = [] for name in sorted(first_asset.values): if name.startswith('occupants_'): period = name.split('_', 1)[1] if period != 'None': occupancy_periods.append(period) loss_types.append(name) else: loss_types.append('value-' + name) deductible_d = first_asset.deductibles or {} limit_d = first_asset.insurance_limits or {} if deductible_d or limit_d: logging.warning('Exposures with insuranceLimit/deductible fields are ' 'deprecated and may be removed in the future') retro = ['retrofitted'] if first_asset._retrofitted else [] float_fields = loss_types + retro int_fields = [(str(name), U16) for name in tagnames] tagi = {str(name): i for i, name in enumerate(tagnames)} asset_dt = numpy.dtype( [('ordinal', U32), ('lon', F32), ('lat', F32), ('site_id', U32), ('number', F32), ('area', F32)] + [ (str(name), float) for name in float_fields] + int_fields) num_assets = sum(len(assets) for assets in assets_by_site) assetcol = numpy.zeros(num_assets, asset_dt) asset_ordinal = 0 fields = set(asset_dt.fields) for sid, assets_ in enumerate(assets_by_site): for asset in assets_: asset.ordinal = asset_ordinal record = assetcol[asset_ordinal] asset_ordinal += 1 for field in fields: if field == 'ordinal': value = asset.ordinal elif field == 'number': value = asset.number elif field == 'area': value = asset.area elif field == 'site_id': value = sid elif field == 'lon': value = asset.location[0] elif field == 'lat': value = asset.location[1] elif field.startswith('occupants_'): value = asset.values[field] elif field == 'retrofitted': value = asset.retrofitted() elif field in tagnames: value = asset.tagidxs[tagi[field]] else: name, lt = field.split('-') value = asset.value(lt, time_event) record[field] = value return assetcol, ' '.join(occupancy_periods)
[ "def", "build_asset_array", "(", "assets_by_site", ",", "tagnames", "=", "(", ")", ",", "time_event", "=", "None", ")", ":", "for", "assets", "in", "assets_by_site", ":", "if", "len", "(", "assets", ")", ":", "first_asset", "=", "assets", "[", "0", "]", "break", "else", ":", "# no break", "raise", "ValueError", "(", "'There are no assets!'", ")", "loss_types", "=", "[", "]", "occupancy_periods", "=", "[", "]", "for", "name", "in", "sorted", "(", "first_asset", ".", "values", ")", ":", "if", "name", ".", "startswith", "(", "'occupants_'", ")", ":", "period", "=", "name", ".", "split", "(", "'_'", ",", "1", ")", "[", "1", "]", "if", "period", "!=", "'None'", ":", "# see scenario_risk test_case_2d", "occupancy_periods", ".", "append", "(", "period", ")", "loss_types", ".", "append", "(", "name", ")", "# discard occupants for different time periods", "else", ":", "loss_types", ".", "append", "(", "'value-'", "+", "name", ")", "# loss_types can be ['value-business_interruption', 'value-contents',", "# 'value-nonstructural', 'occupants_None', 'occupants_day',", "# 'occupants_night', 'occupants_transit']", "deductible_d", "=", "first_asset", ".", "deductibles", "or", "{", "}", "limit_d", "=", "first_asset", ".", "insurance_limits", "or", "{", "}", "if", "deductible_d", "or", "limit_d", ":", "logging", ".", "warning", "(", "'Exposures with insuranceLimit/deductible fields are '", "'deprecated and may be removed in the future'", ")", "retro", "=", "[", "'retrofitted'", "]", "if", "first_asset", ".", "_retrofitted", "else", "[", "]", "float_fields", "=", "loss_types", "+", "retro", "int_fields", "=", "[", "(", "str", "(", "name", ")", ",", "U16", ")", "for", "name", "in", "tagnames", "]", "tagi", "=", "{", "str", "(", "name", ")", ":", "i", "for", "i", ",", "name", "in", "enumerate", "(", "tagnames", ")", "}", "asset_dt", "=", "numpy", ".", "dtype", "(", "[", "(", "'ordinal'", ",", "U32", ")", ",", "(", "'lon'", ",", "F32", ")", ",", "(", "'lat'", ",", "F32", ")", ",", "(", "'site_id'", ",", "U32", ")", ",", "(", "'number'", ",", "F32", ")", ",", "(", "'area'", ",", "F32", ")", "]", "+", "[", "(", "str", "(", "name", ")", ",", "float", ")", "for", "name", "in", "float_fields", "]", "+", "int_fields", ")", "num_assets", "=", "sum", "(", "len", "(", "assets", ")", "for", "assets", "in", "assets_by_site", ")", "assetcol", "=", "numpy", ".", "zeros", "(", "num_assets", ",", "asset_dt", ")", "asset_ordinal", "=", "0", "fields", "=", "set", "(", "asset_dt", ".", "fields", ")", "for", "sid", ",", "assets_", "in", "enumerate", "(", "assets_by_site", ")", ":", "for", "asset", "in", "assets_", ":", "asset", ".", "ordinal", "=", "asset_ordinal", "record", "=", "assetcol", "[", "asset_ordinal", "]", "asset_ordinal", "+=", "1", "for", "field", "in", "fields", ":", "if", "field", "==", "'ordinal'", ":", "value", "=", "asset", ".", "ordinal", "elif", "field", "==", "'number'", ":", "value", "=", "asset", ".", "number", "elif", "field", "==", "'area'", ":", "value", "=", "asset", ".", "area", "elif", "field", "==", "'site_id'", ":", "value", "=", "sid", "elif", "field", "==", "'lon'", ":", "value", "=", "asset", ".", "location", "[", "0", "]", "elif", "field", "==", "'lat'", ":", "value", "=", "asset", ".", "location", "[", "1", "]", "elif", "field", ".", "startswith", "(", "'occupants_'", ")", ":", "value", "=", "asset", ".", "values", "[", "field", "]", "elif", "field", "==", "'retrofitted'", ":", "value", "=", "asset", ".", "retrofitted", "(", ")", "elif", "field", "in", "tagnames", ":", "value", "=", "asset", ".", "tagidxs", "[", "tagi", "[", "field", "]", "]", "else", ":", "name", ",", "lt", "=", "field", ".", "split", "(", "'-'", ")", "value", "=", "asset", ".", "value", "(", "lt", ",", "time_event", ")", "record", "[", "field", "]", "=", "value", "return", "assetcol", ",", "' '", ".", "join", "(", "occupancy_periods", ")" ]
:param assets_by_site: a list of lists of assets :param tagnames: a list of tag names :returns: an array `assetcol`
[ ":", "param", "assets_by_site", ":", "a", "list", "of", "lists", "of", "assets", ":", "param", "tagnames", ":", "a", "list", "of", "tag", "names", ":", "returns", ":", "an", "array", "assetcol" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L583-L655
gem/oq-engine
openquake/risklib/asset.py
_get_exposure
def _get_exposure(fname, stop=None): """ :param fname: path of the XML file containing the exposure :param stop: node at which to stop parsing (or None) :returns: a pair (Exposure instance, list of asset nodes) """ [exposure] = nrml.read(fname, stop=stop) if not exposure.tag.endswith('exposureModel'): raise InvalidFile('%s: expected exposureModel, got %s' % (fname, exposure.tag)) description = exposure.description try: conversions = exposure.conversions except AttributeError: conversions = Node('conversions', nodes=[Node('costTypes', [])]) try: inslimit = conversions.insuranceLimit except AttributeError: inslimit = Node('insuranceLimit', text=True) try: deductible = conversions.deductible except AttributeError: deductible = Node('deductible', text=True) try: area = conversions.area except AttributeError: # NB: the area type cannot be an empty string because when sending # around the CostCalculator object we would run into this numpy bug # about pickling dictionaries with empty strings: # https://github.com/numpy/numpy/pull/5475 area = Node('area', dict(type='?')) try: occupancy_periods = exposure.occupancyPeriods.text or '' except AttributeError: occupancy_periods = '' try: tagNames = exposure.tagNames except AttributeError: tagNames = Node('tagNames', text='') tagnames = ~tagNames or [] if set(tagnames) & {'taxonomy', 'exposure', 'country'}: raise InvalidFile('taxonomy, exposure and country are reserved names ' 'you cannot use it in <tagNames>: %s' % fname) tagnames.insert(0, 'taxonomy') # read the cost types and make some check cost_types = [] retrofitted = False for ct in conversions.costTypes: with context(fname, ct): ctname = ct['name'] if ctname == 'structural' and 'retrofittedType' in ct.attrib: if ct['retrofittedType'] != ct['type']: raise ValueError( 'The retrofittedType %s is different from the type' '%s' % (ct['retrofittedType'], ct['type'])) if ct['retrofittedUnit'] != ct['unit']: raise ValueError( 'The retrofittedUnit %s is different from the unit' '%s' % (ct['retrofittedUnit'], ct['unit'])) retrofitted = True cost_types.append( (ctname, valid.cost_type_type(ct['type']), ct['unit'])) if 'occupants' in cost_types: cost_types.append(('occupants', 'per_area', 'people')) cost_types.sort(key=operator.itemgetter(0)) cost_types = numpy.array(cost_types, cost_type_dt) insurance_limit_is_absolute = il = inslimit.get('isAbsolute') deductible_is_absolute = de = deductible.get('isAbsolute') cc = CostCalculator( {}, {}, {}, True if de is None else de, True if il is None else il, {name: i for i, name in enumerate(tagnames)}, ) for ct in cost_types: name = ct['name'] # structural, nonstructural, ... cc.cost_types[name] = ct['type'] # aggregated, per_asset, per_area cc.area_types[name] = area['type'] cc.units[name] = ct['unit'] assets = [] asset_refs = [] exp = Exposure( exposure['id'], exposure['category'], description.text, cost_types, occupancy_periods, insurance_limit_is_absolute, deductible_is_absolute, retrofitted, area.attrib, assets, asset_refs, cc, TagCollection(tagnames)) assets_text = exposure.assets.text.strip() if assets_text: # the <assets> tag contains a list of file names dirname = os.path.dirname(fname) exp.datafiles = [os.path.join(dirname, f) for f in assets_text.split()] else: exp.datafiles = [] return exp, exposure.assets
python
def _get_exposure(fname, stop=None): [exposure] = nrml.read(fname, stop=stop) if not exposure.tag.endswith('exposureModel'): raise InvalidFile('%s: expected exposureModel, got %s' % (fname, exposure.tag)) description = exposure.description try: conversions = exposure.conversions except AttributeError: conversions = Node('conversions', nodes=[Node('costTypes', [])]) try: inslimit = conversions.insuranceLimit except AttributeError: inslimit = Node('insuranceLimit', text=True) try: deductible = conversions.deductible except AttributeError: deductible = Node('deductible', text=True) try: area = conversions.area except AttributeError: area = Node('area', dict(type='?')) try: occupancy_periods = exposure.occupancyPeriods.text or '' except AttributeError: occupancy_periods = '' try: tagNames = exposure.tagNames except AttributeError: tagNames = Node('tagNames', text='') tagnames = ~tagNames or [] if set(tagnames) & {'taxonomy', 'exposure', 'country'}: raise InvalidFile('taxonomy, exposure and country are reserved names ' 'you cannot use it in <tagNames>: %s' % fname) tagnames.insert(0, 'taxonomy') cost_types = [] retrofitted = False for ct in conversions.costTypes: with context(fname, ct): ctname = ct['name'] if ctname == 'structural' and 'retrofittedType' in ct.attrib: if ct['retrofittedType'] != ct['type']: raise ValueError( 'The retrofittedType %s is different from the type' '%s' % (ct['retrofittedType'], ct['type'])) if ct['retrofittedUnit'] != ct['unit']: raise ValueError( 'The retrofittedUnit %s is different from the unit' '%s' % (ct['retrofittedUnit'], ct['unit'])) retrofitted = True cost_types.append( (ctname, valid.cost_type_type(ct['type']), ct['unit'])) if 'occupants' in cost_types: cost_types.append(('occupants', 'per_area', 'people')) cost_types.sort(key=operator.itemgetter(0)) cost_types = numpy.array(cost_types, cost_type_dt) insurance_limit_is_absolute = il = inslimit.get('isAbsolute') deductible_is_absolute = de = deductible.get('isAbsolute') cc = CostCalculator( {}, {}, {}, True if de is None else de, True if il is None else il, {name: i for i, name in enumerate(tagnames)}, ) for ct in cost_types: name = ct['name'] cc.cost_types[name] = ct['type'] cc.area_types[name] = area['type'] cc.units[name] = ct['unit'] assets = [] asset_refs = [] exp = Exposure( exposure['id'], exposure['category'], description.text, cost_types, occupancy_periods, insurance_limit_is_absolute, deductible_is_absolute, retrofitted, area.attrib, assets, asset_refs, cc, TagCollection(tagnames)) assets_text = exposure.assets.text.strip() if assets_text: dirname = os.path.dirname(fname) exp.datafiles = [os.path.join(dirname, f) for f in assets_text.split()] else: exp.datafiles = [] return exp, exposure.assets
[ "def", "_get_exposure", "(", "fname", ",", "stop", "=", "None", ")", ":", "[", "exposure", "]", "=", "nrml", ".", "read", "(", "fname", ",", "stop", "=", "stop", ")", "if", "not", "exposure", ".", "tag", ".", "endswith", "(", "'exposureModel'", ")", ":", "raise", "InvalidFile", "(", "'%s: expected exposureModel, got %s'", "%", "(", "fname", ",", "exposure", ".", "tag", ")", ")", "description", "=", "exposure", ".", "description", "try", ":", "conversions", "=", "exposure", ".", "conversions", "except", "AttributeError", ":", "conversions", "=", "Node", "(", "'conversions'", ",", "nodes", "=", "[", "Node", "(", "'costTypes'", ",", "[", "]", ")", "]", ")", "try", ":", "inslimit", "=", "conversions", ".", "insuranceLimit", "except", "AttributeError", ":", "inslimit", "=", "Node", "(", "'insuranceLimit'", ",", "text", "=", "True", ")", "try", ":", "deductible", "=", "conversions", ".", "deductible", "except", "AttributeError", ":", "deductible", "=", "Node", "(", "'deductible'", ",", "text", "=", "True", ")", "try", ":", "area", "=", "conversions", ".", "area", "except", "AttributeError", ":", "# NB: the area type cannot be an empty string because when sending", "# around the CostCalculator object we would run into this numpy bug", "# about pickling dictionaries with empty strings:", "# https://github.com/numpy/numpy/pull/5475", "area", "=", "Node", "(", "'area'", ",", "dict", "(", "type", "=", "'?'", ")", ")", "try", ":", "occupancy_periods", "=", "exposure", ".", "occupancyPeriods", ".", "text", "or", "''", "except", "AttributeError", ":", "occupancy_periods", "=", "''", "try", ":", "tagNames", "=", "exposure", ".", "tagNames", "except", "AttributeError", ":", "tagNames", "=", "Node", "(", "'tagNames'", ",", "text", "=", "''", ")", "tagnames", "=", "~", "tagNames", "or", "[", "]", "if", "set", "(", "tagnames", ")", "&", "{", "'taxonomy'", ",", "'exposure'", ",", "'country'", "}", ":", "raise", "InvalidFile", "(", "'taxonomy, exposure and country are reserved names '", "'you cannot use it in <tagNames>: %s'", "%", "fname", ")", "tagnames", ".", "insert", "(", "0", ",", "'taxonomy'", ")", "# read the cost types and make some check", "cost_types", "=", "[", "]", "retrofitted", "=", "False", "for", "ct", "in", "conversions", ".", "costTypes", ":", "with", "context", "(", "fname", ",", "ct", ")", ":", "ctname", "=", "ct", "[", "'name'", "]", "if", "ctname", "==", "'structural'", "and", "'retrofittedType'", "in", "ct", ".", "attrib", ":", "if", "ct", "[", "'retrofittedType'", "]", "!=", "ct", "[", "'type'", "]", ":", "raise", "ValueError", "(", "'The retrofittedType %s is different from the type'", "'%s'", "%", "(", "ct", "[", "'retrofittedType'", "]", ",", "ct", "[", "'type'", "]", ")", ")", "if", "ct", "[", "'retrofittedUnit'", "]", "!=", "ct", "[", "'unit'", "]", ":", "raise", "ValueError", "(", "'The retrofittedUnit %s is different from the unit'", "'%s'", "%", "(", "ct", "[", "'retrofittedUnit'", "]", ",", "ct", "[", "'unit'", "]", ")", ")", "retrofitted", "=", "True", "cost_types", ".", "append", "(", "(", "ctname", ",", "valid", ".", "cost_type_type", "(", "ct", "[", "'type'", "]", ")", ",", "ct", "[", "'unit'", "]", ")", ")", "if", "'occupants'", "in", "cost_types", ":", "cost_types", ".", "append", "(", "(", "'occupants'", ",", "'per_area'", ",", "'people'", ")", ")", "cost_types", ".", "sort", "(", "key", "=", "operator", ".", "itemgetter", "(", "0", ")", ")", "cost_types", "=", "numpy", ".", "array", "(", "cost_types", ",", "cost_type_dt", ")", "insurance_limit_is_absolute", "=", "il", "=", "inslimit", ".", "get", "(", "'isAbsolute'", ")", "deductible_is_absolute", "=", "de", "=", "deductible", ".", "get", "(", "'isAbsolute'", ")", "cc", "=", "CostCalculator", "(", "{", "}", ",", "{", "}", ",", "{", "}", ",", "True", "if", "de", "is", "None", "else", "de", ",", "True", "if", "il", "is", "None", "else", "il", ",", "{", "name", ":", "i", "for", "i", ",", "name", "in", "enumerate", "(", "tagnames", ")", "}", ",", ")", "for", "ct", "in", "cost_types", ":", "name", "=", "ct", "[", "'name'", "]", "# structural, nonstructural, ...", "cc", ".", "cost_types", "[", "name", "]", "=", "ct", "[", "'type'", "]", "# aggregated, per_asset, per_area", "cc", ".", "area_types", "[", "name", "]", "=", "area", "[", "'type'", "]", "cc", ".", "units", "[", "name", "]", "=", "ct", "[", "'unit'", "]", "assets", "=", "[", "]", "asset_refs", "=", "[", "]", "exp", "=", "Exposure", "(", "exposure", "[", "'id'", "]", ",", "exposure", "[", "'category'", "]", ",", "description", ".", "text", ",", "cost_types", ",", "occupancy_periods", ",", "insurance_limit_is_absolute", ",", "deductible_is_absolute", ",", "retrofitted", ",", "area", ".", "attrib", ",", "assets", ",", "asset_refs", ",", "cc", ",", "TagCollection", "(", "tagnames", ")", ")", "assets_text", "=", "exposure", ".", "assets", ".", "text", ".", "strip", "(", ")", "if", "assets_text", ":", "# the <assets> tag contains a list of file names", "dirname", "=", "os", ".", "path", ".", "dirname", "(", "fname", ")", "exp", ".", "datafiles", "=", "[", "os", ".", "path", ".", "join", "(", "dirname", ",", "f", ")", "for", "f", "in", "assets_text", ".", "split", "(", ")", "]", "else", ":", "exp", ".", "datafiles", "=", "[", "]", "return", "exp", ",", "exposure", ".", "assets" ]
:param fname: path of the XML file containing the exposure :param stop: node at which to stop parsing (or None) :returns: a pair (Exposure instance, list of asset nodes)
[ ":", "param", "fname", ":", "path", "of", "the", "XML", "file", "containing", "the", "exposure", ":", "param", "stop", ":", "node", "at", "which", "to", "stop", "parsing", "(", "or", "None", ")", ":", "returns", ":", "a", "pair", "(", "Exposure", "instance", "list", "of", "asset", "nodes", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L665-L762
gem/oq-engine
openquake/risklib/asset.py
CostCalculator.get_units
def get_units(self, loss_types): """ :param: a list of loss types :returns: an array of units as byte strings, suitable for HDF5 """ lst = [] for lt in loss_types: if lt.endswith('_ins'): lt = lt[:-4] if lt == 'occupants': unit = 'people' else: unit = self.units[lt] lst.append(encode(unit)) return numpy.array(lst)
python
def get_units(self, loss_types): lst = [] for lt in loss_types: if lt.endswith('_ins'): lt = lt[:-4] if lt == 'occupants': unit = 'people' else: unit = self.units[lt] lst.append(encode(unit)) return numpy.array(lst)
[ "def", "get_units", "(", "self", ",", "loss_types", ")", ":", "lst", "=", "[", "]", "for", "lt", "in", "loss_types", ":", "if", "lt", ".", "endswith", "(", "'_ins'", ")", ":", "lt", "=", "lt", "[", ":", "-", "4", "]", "if", "lt", "==", "'occupants'", ":", "unit", "=", "'people'", "else", ":", "unit", "=", "self", ".", "units", "[", "lt", "]", "lst", ".", "append", "(", "encode", "(", "unit", ")", ")", "return", "numpy", ".", "array", "(", "lst", ")" ]
:param: a list of loss types :returns: an array of units as byte strings, suitable for HDF5
[ ":", "param", ":", "a", "list", "of", "loss", "types", ":", "returns", ":", "an", "array", "of", "units", "as", "byte", "strings", "suitable", "for", "HDF5" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L82-L96
gem/oq-engine
openquake/risklib/asset.py
Asset.value
def value(self, loss_type, time_event=None): """ :returns: the total asset value for `loss_type` """ if loss_type == 'occupants': return self.values['occupants_' + str(time_event)] try: # extract from the cache val = self._cost[loss_type] except KeyError: # compute val = self.calc(loss_type, self.values, self.area, self.number) self._cost[loss_type] = val return val
python
def value(self, loss_type, time_event=None): if loss_type == 'occupants': return self.values['occupants_' + str(time_event)] try: val = self._cost[loss_type] except KeyError: val = self.calc(loss_type, self.values, self.area, self.number) self._cost[loss_type] = val return val
[ "def", "value", "(", "self", ",", "loss_type", ",", "time_event", "=", "None", ")", ":", "if", "loss_type", "==", "'occupants'", ":", "return", "self", ".", "values", "[", "'occupants_'", "+", "str", "(", "time_event", ")", "]", "try", ":", "# extract from the cache", "val", "=", "self", ".", "_cost", "[", "loss_type", "]", "except", "KeyError", ":", "# compute", "val", "=", "self", ".", "calc", "(", "loss_type", ",", "self", ".", "values", ",", "self", ".", "area", ",", "self", ".", "number", ")", "self", ".", "_cost", "[", "loss_type", "]", "=", "val", "return", "val" ]
:returns: the total asset value for `loss_type`
[ ":", "returns", ":", "the", "total", "asset", "value", "for", "loss_type" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L195-L206
gem/oq-engine
openquake/risklib/asset.py
Asset.deductible
def deductible(self, loss_type, dummy=None): """ :returns: the deductible fraction of the asset cost for `loss_type` """ val = self.calc(loss_type, self.deductibles, self.area, self.number) if self.calc.deduct_abs: # convert to relative value return val / self.calc(loss_type, self.values, self.area, self.number) else: return val
python
def deductible(self, loss_type, dummy=None): val = self.calc(loss_type, self.deductibles, self.area, self.number) if self.calc.deduct_abs: return val / self.calc(loss_type, self.values, self.area, self.number) else: return val
[ "def", "deductible", "(", "self", ",", "loss_type", ",", "dummy", "=", "None", ")", ":", "val", "=", "self", ".", "calc", "(", "loss_type", ",", "self", ".", "deductibles", ",", "self", ".", "area", ",", "self", ".", "number", ")", "if", "self", ".", "calc", ".", "deduct_abs", ":", "# convert to relative value", "return", "val", "/", "self", ".", "calc", "(", "loss_type", ",", "self", ".", "values", ",", "self", ".", "area", ",", "self", ".", "number", ")", "else", ":", "return", "val" ]
:returns: the deductible fraction of the asset cost for `loss_type`
[ ":", "returns", ":", "the", "deductible", "fraction", "of", "the", "asset", "cost", "for", "loss_type" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L208-L217
gem/oq-engine
openquake/risklib/asset.py
Asset.insurance_limit
def insurance_limit(self, loss_type, dummy=None): """ :returns: the limit fraction of the asset cost for `loss_type` """ val = self.calc(loss_type, self.insurance_limits, self.area, self.number) if self.calc.limit_abs: # convert to relative value return val / self.calc(loss_type, self.values, self.area, self.number) else: return val
python
def insurance_limit(self, loss_type, dummy=None): val = self.calc(loss_type, self.insurance_limits, self.area, self.number) if self.calc.limit_abs: return val / self.calc(loss_type, self.values, self.area, self.number) else: return val
[ "def", "insurance_limit", "(", "self", ",", "loss_type", ",", "dummy", "=", "None", ")", ":", "val", "=", "self", ".", "calc", "(", "loss_type", ",", "self", ".", "insurance_limits", ",", "self", ".", "area", ",", "self", ".", "number", ")", "if", "self", ".", "calc", ".", "limit_abs", ":", "# convert to relative value", "return", "val", "/", "self", ".", "calc", "(", "loss_type", ",", "self", ".", "values", ",", "self", ".", "area", ",", "self", ".", "number", ")", "else", ":", "return", "val" ]
:returns: the limit fraction of the asset cost for `loss_type`
[ ":", "returns", ":", "the", "limit", "fraction", "of", "the", "asset", "cost", "for", "loss_type" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L219-L229
gem/oq-engine
openquake/risklib/asset.py
Asset.retrofitted
def retrofitted(self): """ :returns: the asset retrofitted value """ return self.calc('structural', {'structural': self._retrofitted}, self.area, self.number)
python
def retrofitted(self): return self.calc('structural', {'structural': self._retrofitted}, self.area, self.number)
[ "def", "retrofitted", "(", "self", ")", ":", "return", "self", ".", "calc", "(", "'structural'", ",", "{", "'structural'", ":", "self", ".", "_retrofitted", "}", ",", "self", ".", "area", ",", "self", ".", "number", ")" ]
:returns: the asset retrofitted value
[ ":", "returns", ":", "the", "asset", "retrofitted", "value" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L231-L236
gem/oq-engine
openquake/risklib/asset.py
Asset.tagmask
def tagmask(self, tags): """ :returns: a boolean array with True where the assets has tags """ mask = numpy.zeros(len(tags), bool) for t, tag in enumerate(tags): tagname, tagvalue = tag.split('=') mask[t] = self.tagvalue(tagname) == tagvalue return mask
python
def tagmask(self, tags): mask = numpy.zeros(len(tags), bool) for t, tag in enumerate(tags): tagname, tagvalue = tag.split('=') mask[t] = self.tagvalue(tagname) == tagvalue return mask
[ "def", "tagmask", "(", "self", ",", "tags", ")", ":", "mask", "=", "numpy", ".", "zeros", "(", "len", "(", "tags", ")", ",", "bool", ")", "for", "t", ",", "tag", "in", "enumerate", "(", "tags", ")", ":", "tagname", ",", "tagvalue", "=", "tag", ".", "split", "(", "'='", ")", "mask", "[", "t", "]", "=", "self", ".", "tagvalue", "(", "tagname", ")", "==", "tagvalue", "return", "mask" ]
:returns: a boolean array with True where the assets has tags
[ ":", "returns", ":", "a", "boolean", "array", "with", "True", "where", "the", "assets", "has", "tags" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L238-L246
gem/oq-engine
openquake/risklib/asset.py
TagCollection.add
def add(self, tagname, tagvalue): """ :returns: numeric index associated to the tag """ dic = getattr(self, tagname + '_idx') try: return dic[tagvalue] except KeyError: dic[tagvalue] = idx = len(dic) getattr(self, tagname).append(tagvalue) if idx > TWO16: raise InvalidFile('contains more then %d tags' % TWO16) return idx
python
def add(self, tagname, tagvalue): dic = getattr(self, tagname + '_idx') try: return dic[tagvalue] except KeyError: dic[tagvalue] = idx = len(dic) getattr(self, tagname).append(tagvalue) if idx > TWO16: raise InvalidFile('contains more then %d tags' % TWO16) return idx
[ "def", "add", "(", "self", ",", "tagname", ",", "tagvalue", ")", ":", "dic", "=", "getattr", "(", "self", ",", "tagname", "+", "'_idx'", ")", "try", ":", "return", "dic", "[", "tagvalue", "]", "except", "KeyError", ":", "dic", "[", "tagvalue", "]", "=", "idx", "=", "len", "(", "dic", ")", "getattr", "(", "self", ",", "tagname", ")", ".", "append", "(", "tagvalue", ")", "if", "idx", ">", "TWO16", ":", "raise", "InvalidFile", "(", "'contains more then %d tags'", "%", "TWO16", ")", "return", "idx" ]
:returns: numeric index associated to the tag
[ ":", "returns", ":", "numeric", "index", "associated", "to", "the", "tag" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L287-L299
gem/oq-engine
openquake/risklib/asset.py
TagCollection.add_tags
def add_tags(self, dic, prefix): """ :param dic: a dictionary tagname -> tagvalue :returns: a list of tag indices, one per tagname """ # fill missing tagvalues with "?", raise an error for unknown tagnames idxs = [] for tagname in self.tagnames: if tagname in ('exposure', 'country'): idxs.append(self.add(tagname, prefix)) continue try: tagvalue = dic.pop(tagname) except KeyError: tagvalue = '?' else: if tagvalue in '?*': raise ValueError( 'Invalid tagvalue="%s"' % tagvalue) idxs.append(self.add(tagname, tagvalue)) if dic: raise ValueError( 'Unknown tagname %s or <tagNames> not ' 'specified in the exposure' % ', '.join(dic)) return idxs
python
def add_tags(self, dic, prefix): idxs = [] for tagname in self.tagnames: if tagname in ('exposure', 'country'): idxs.append(self.add(tagname, prefix)) continue try: tagvalue = dic.pop(tagname) except KeyError: tagvalue = '?' else: if tagvalue in '?*': raise ValueError( 'Invalid tagvalue="%s"' % tagvalue) idxs.append(self.add(tagname, tagvalue)) if dic: raise ValueError( 'Unknown tagname %s or <tagNames> not ' 'specified in the exposure' % ', '.join(dic)) return idxs
[ "def", "add_tags", "(", "self", ",", "dic", ",", "prefix", ")", ":", "# fill missing tagvalues with \"?\", raise an error for unknown tagnames", "idxs", "=", "[", "]", "for", "tagname", "in", "self", ".", "tagnames", ":", "if", "tagname", "in", "(", "'exposure'", ",", "'country'", ")", ":", "idxs", ".", "append", "(", "self", ".", "add", "(", "tagname", ",", "prefix", ")", ")", "continue", "try", ":", "tagvalue", "=", "dic", ".", "pop", "(", "tagname", ")", "except", "KeyError", ":", "tagvalue", "=", "'?'", "else", ":", "if", "tagvalue", "in", "'?*'", ":", "raise", "ValueError", "(", "'Invalid tagvalue=\"%s\"'", "%", "tagvalue", ")", "idxs", ".", "append", "(", "self", ".", "add", "(", "tagname", ",", "tagvalue", ")", ")", "if", "dic", ":", "raise", "ValueError", "(", "'Unknown tagname %s or <tagNames> not '", "'specified in the exposure'", "%", "', '", ".", "join", "(", "dic", ")", ")", "return", "idxs" ]
:param dic: a dictionary tagname -> tagvalue :returns: a list of tag indices, one per tagname
[ ":", "param", "dic", ":", "a", "dictionary", "tagname", "-", ">", "tagvalue", ":", "returns", ":", "a", "list", "of", "tag", "indices", "one", "per", "tagname" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L301-L325
gem/oq-engine
openquake/risklib/asset.py
TagCollection.get_tag
def get_tag(self, tagname, tagidx): """ :returns: the tag associated to the given tagname and tag index """ return '%s=%s' % (tagname, decode(getattr(self, tagname)[tagidx]))
python
def get_tag(self, tagname, tagidx): return '%s=%s' % (tagname, decode(getattr(self, tagname)[tagidx]))
[ "def", "get_tag", "(", "self", ",", "tagname", ",", "tagidx", ")", ":", "return", "'%s=%s'", "%", "(", "tagname", ",", "decode", "(", "getattr", "(", "self", ",", "tagname", ")", "[", "tagidx", "]", ")", ")" ]
:returns: the tag associated to the given tagname and tag index
[ ":", "returns", ":", "the", "tag", "associated", "to", "the", "given", "tagname", "and", "tag", "index" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L332-L336
gem/oq-engine
openquake/risklib/asset.py
TagCollection.get_tagvalues
def get_tagvalues(self, tagnames, tagidxs): """ :returns: the tag associated to the given tagname and tag index """ values = tuple(getattr(self, tagname)[tagidx + 1] for tagidx, tagname in zip(tagidxs, tagnames)) return values
python
def get_tagvalues(self, tagnames, tagidxs): values = tuple(getattr(self, tagname)[tagidx + 1] for tagidx, tagname in zip(tagidxs, tagnames)) return values
[ "def", "get_tagvalues", "(", "self", ",", "tagnames", ",", "tagidxs", ")", ":", "values", "=", "tuple", "(", "getattr", "(", "self", ",", "tagname", ")", "[", "tagidx", "+", "1", "]", "for", "tagidx", ",", "tagname", "in", "zip", "(", "tagidxs", ",", "tagnames", ")", ")", "return", "values" ]
:returns: the tag associated to the given tagname and tag index
[ ":", "returns", ":", "the", "tag", "associated", "to", "the", "given", "tagname", "and", "tag", "index" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L338-L344
gem/oq-engine
openquake/risklib/asset.py
TagCollection.gen_tags
def gen_tags(self, tagname): """ :yields: the tags associated to the given tagname """ for tagvalue in getattr(self, tagname): yield '%s=%s' % (tagname, decode(tagvalue))
python
def gen_tags(self, tagname): for tagvalue in getattr(self, tagname): yield '%s=%s' % (tagname, decode(tagvalue))
[ "def", "gen_tags", "(", "self", ",", "tagname", ")", ":", "for", "tagvalue", "in", "getattr", "(", "self", ",", "tagname", ")", ":", "yield", "'%s=%s'", "%", "(", "tagname", ",", "decode", "(", "tagvalue", ")", ")" ]
:yields: the tags associated to the given tagname
[ ":", "yields", ":", "the", "tags", "associated", "to", "the", "given", "tagname" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L346-L351
gem/oq-engine
openquake/risklib/asset.py
TagCollection.agg_shape
def agg_shape(self, shp, aggregate_by): """ :returns: a shape shp + (T, ...) depending on the tagnames """ return shp + tuple( len(getattr(self, tagname)) - 1 for tagname in aggregate_by)
python
def agg_shape(self, shp, aggregate_by): return shp + tuple( len(getattr(self, tagname)) - 1 for tagname in aggregate_by)
[ "def", "agg_shape", "(", "self", ",", "shp", ",", "aggregate_by", ")", ":", "return", "shp", "+", "tuple", "(", "len", "(", "getattr", "(", "self", ",", "tagname", ")", ")", "-", "1", "for", "tagname", "in", "aggregate_by", ")" ]
:returns: a shape shp + (T, ...) depending on the tagnames
[ ":", "returns", ":", "a", "shape", "shp", "+", "(", "T", "...", ")", "depending", "on", "the", "tagnames" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L353-L358
gem/oq-engine
openquake/risklib/asset.py
AssetCollection.num_taxonomies_by_site
def num_taxonomies_by_site(self): """ :returns: an array with the number of assets per each site """ dic = general.group_array(self.array, 'site_id') num_taxonomies = numpy.zeros(self.tot_sites, U32) for sid, arr in dic.items(): num_taxonomies[sid] = len(numpy.unique(arr['taxonomy'])) return num_taxonomies
python
def num_taxonomies_by_site(self): dic = general.group_array(self.array, 'site_id') num_taxonomies = numpy.zeros(self.tot_sites, U32) for sid, arr in dic.items(): num_taxonomies[sid] = len(numpy.unique(arr['taxonomy'])) return num_taxonomies
[ "def", "num_taxonomies_by_site", "(", "self", ")", ":", "dic", "=", "general", ".", "group_array", "(", "self", ".", "array", ",", "'site_id'", ")", "num_taxonomies", "=", "numpy", ".", "zeros", "(", "self", ".", "tot_sites", ",", "U32", ")", "for", "sid", ",", "arr", "in", "dic", ".", "items", "(", ")", ":", "num_taxonomies", "[", "sid", "]", "=", "len", "(", "numpy", ".", "unique", "(", "arr", "[", "'taxonomy'", "]", ")", ")", "return", "num_taxonomies" ]
:returns: an array with the number of assets per each site
[ ":", "returns", ":", "an", "array", "with", "the", "number", "of", "assets", "per", "each", "site" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L426-L434
gem/oq-engine
openquake/risklib/asset.py
AssetCollection.get_aids_by_tag
def get_aids_by_tag(self): """ :returns: dict tag -> asset ordinals """ aids_by_tag = general.AccumDict(accum=set()) for aid, ass in enumerate(self): for tagname in self.tagnames: tag = self.tagcol.get_tag(tagname, ass[tagname]) aids_by_tag[tag].add(aid) return aids_by_tag
python
def get_aids_by_tag(self): aids_by_tag = general.AccumDict(accum=set()) for aid, ass in enumerate(self): for tagname in self.tagnames: tag = self.tagcol.get_tag(tagname, ass[tagname]) aids_by_tag[tag].add(aid) return aids_by_tag
[ "def", "get_aids_by_tag", "(", "self", ")", ":", "aids_by_tag", "=", "general", ".", "AccumDict", "(", "accum", "=", "set", "(", ")", ")", "for", "aid", ",", "ass", "in", "enumerate", "(", "self", ")", ":", "for", "tagname", "in", "self", ".", "tagnames", ":", "tag", "=", "self", ".", "tagcol", ".", "get_tag", "(", "tagname", ",", "ass", "[", "tagname", "]", ")", "aids_by_tag", "[", "tag", "]", ".", "add", "(", "aid", ")", "return", "aids_by_tag" ]
:returns: dict tag -> asset ordinals
[ ":", "returns", ":", "dict", "tag", "-", ">", "asset", "ordinals" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L436-L445
gem/oq-engine
openquake/risklib/asset.py
AssetCollection.assets_by_site
def assets_by_site(self): """ :returns: numpy array of lists with the assets by each site """ assets_by_site = [[] for sid in range(self.tot_sites)] for i, ass in enumerate(self.array): assets_by_site[ass['site_id']].append(self[i]) return numpy.array(assets_by_site)
python
def assets_by_site(self): assets_by_site = [[] for sid in range(self.tot_sites)] for i, ass in enumerate(self.array): assets_by_site[ass['site_id']].append(self[i]) return numpy.array(assets_by_site)
[ "def", "assets_by_site", "(", "self", ")", ":", "assets_by_site", "=", "[", "[", "]", "for", "sid", "in", "range", "(", "self", ".", "tot_sites", ")", "]", "for", "i", ",", "ass", "in", "enumerate", "(", "self", ".", "array", ")", ":", "assets_by_site", "[", "ass", "[", "'site_id'", "]", "]", ".", "append", "(", "self", "[", "i", "]", ")", "return", "numpy", ".", "array", "(", "assets_by_site", ")" ]
:returns: numpy array of lists with the assets by each site
[ ":", "returns", ":", "numpy", "array", "of", "lists", "with", "the", "assets", "by", "each", "site" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L454-L461
gem/oq-engine
openquake/risklib/asset.py
AssetCollection.aggregate_by
def aggregate_by(self, tagnames, array): """ :param tagnames: a list of valid tag names :param array: an array with the same length as the asset collection :returns: an array of aggregate values with the proper shape """ missing = set(tagnames) - set(self.tagcol.tagnames) if missing: raise ValueError('Unknown tagname(s) %s' % missing) A, *shp = array.shape if A != len(self): raise ValueError('The array must have length %d, got %d' % (len(self), A)) if not tagnames: return array.sum(axis=0) shape = [len(getattr(self.tagcol, tagname))-1 for tagname in tagnames] acc = numpy.zeros(shape, (F32, shp) if shp else F32) for asset, row in zip(self.array, array): acc[tuple(idx - 1 for idx in asset[tagnames])] += row return acc
python
def aggregate_by(self, tagnames, array): missing = set(tagnames) - set(self.tagcol.tagnames) if missing: raise ValueError('Unknown tagname(s) %s' % missing) A, *shp = array.shape if A != len(self): raise ValueError('The array must have length %d, got %d' % (len(self), A)) if not tagnames: return array.sum(axis=0) shape = [len(getattr(self.tagcol, tagname))-1 for tagname in tagnames] acc = numpy.zeros(shape, (F32, shp) if shp else F32) for asset, row in zip(self.array, array): acc[tuple(idx - 1 for idx in asset[tagnames])] += row return acc
[ "def", "aggregate_by", "(", "self", ",", "tagnames", ",", "array", ")", ":", "missing", "=", "set", "(", "tagnames", ")", "-", "set", "(", "self", ".", "tagcol", ".", "tagnames", ")", "if", "missing", ":", "raise", "ValueError", "(", "'Unknown tagname(s) %s'", "%", "missing", ")", "A", ",", "", "*", "shp", "=", "array", ".", "shape", "if", "A", "!=", "len", "(", "self", ")", ":", "raise", "ValueError", "(", "'The array must have length %d, got %d'", "%", "(", "len", "(", "self", ")", ",", "A", ")", ")", "if", "not", "tagnames", ":", "return", "array", ".", "sum", "(", "axis", "=", "0", ")", "shape", "=", "[", "len", "(", "getattr", "(", "self", ".", "tagcol", ",", "tagname", ")", ")", "-", "1", "for", "tagname", "in", "tagnames", "]", "acc", "=", "numpy", ".", "zeros", "(", "shape", ",", "(", "F32", ",", "shp", ")", "if", "shp", "else", "F32", ")", "for", "asset", ",", "row", "in", "zip", "(", "self", ".", "array", ",", "array", ")", ":", "acc", "[", "tuple", "(", "idx", "-", "1", "for", "idx", "in", "asset", "[", "tagnames", "]", ")", "]", "+=", "row", "return", "acc" ]
:param tagnames: a list of valid tag names :param array: an array with the same length as the asset collection :returns: an array of aggregate values with the proper shape
[ ":", "param", "tagnames", ":", "a", "list", "of", "valid", "tag", "names", ":", "param", "array", ":", "an", "array", "with", "the", "same", "length", "as", "the", "asset", "collection", ":", "returns", ":", "an", "array", "of", "aggregate", "values", "with", "the", "proper", "shape" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L463-L482
gem/oq-engine
openquake/risklib/asset.py
AssetCollection.agg_value
def agg_value(self, *tagnames): """ :param tagnames: tagnames of lengths T1, T2, ... respectively :returns: the values of the exposure aggregated by tagnames as an array of shape (T1, T2, ..., L) """ aval = numpy.zeros((len(self), len(self.loss_types)), F32) # (A, L) for asset in self: for lti, lt in enumerate(self.loss_types): if lt == 'occupants': aval[asset['ordinal'], lti] = asset[lt + '_None'] else: aval[asset['ordinal'], lti] = asset['value-' + lt] return self.aggregate_by(list(tagnames), aval)
python
def agg_value(self, *tagnames): aval = numpy.zeros((len(self), len(self.loss_types)), F32) for asset in self: for lti, lt in enumerate(self.loss_types): if lt == 'occupants': aval[asset['ordinal'], lti] = asset[lt + '_None'] else: aval[asset['ordinal'], lti] = asset['value-' + lt] return self.aggregate_by(list(tagnames), aval)
[ "def", "agg_value", "(", "self", ",", "*", "tagnames", ")", ":", "aval", "=", "numpy", ".", "zeros", "(", "(", "len", "(", "self", ")", ",", "len", "(", "self", ".", "loss_types", ")", ")", ",", "F32", ")", "# (A, L)", "for", "asset", "in", "self", ":", "for", "lti", ",", "lt", "in", "enumerate", "(", "self", ".", "loss_types", ")", ":", "if", "lt", "==", "'occupants'", ":", "aval", "[", "asset", "[", "'ordinal'", "]", ",", "lti", "]", "=", "asset", "[", "lt", "+", "'_None'", "]", "else", ":", "aval", "[", "asset", "[", "'ordinal'", "]", ",", "lti", "]", "=", "asset", "[", "'value-'", "+", "lt", "]", "return", "self", ".", "aggregate_by", "(", "list", "(", "tagnames", ")", ",", "aval", ")" ]
:param tagnames: tagnames of lengths T1, T2, ... respectively :returns: the values of the exposure aggregated by tagnames as an array of shape (T1, T2, ..., L)
[ ":", "param", "tagnames", ":", "tagnames", "of", "lengths", "T1", "T2", "...", "respectively", ":", "returns", ":", "the", "values", "of", "the", "exposure", "aggregated", "by", "tagnames", "as", "an", "array", "of", "shape", "(", "T1", "T2", "...", "L", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L484-L499
gem/oq-engine
openquake/risklib/asset.py
AssetCollection.reduce
def reduce(self, sitecol): """ :returns: a reduced AssetCollection on the given sitecol """ ok_indices = numpy.sum( [self.array['site_id'] == sid for sid in sitecol.sids], axis=0, dtype=bool) new = object.__new__(self.__class__) vars(new).update(vars(self)) new.array = self.array[ok_indices] new.array['ordinal'] = numpy.arange(len(new.array)) new.asset_refs = self.asset_refs[ok_indices] return new
python
def reduce(self, sitecol): ok_indices = numpy.sum( [self.array['site_id'] == sid for sid in sitecol.sids], axis=0, dtype=bool) new = object.__new__(self.__class__) vars(new).update(vars(self)) new.array = self.array[ok_indices] new.array['ordinal'] = numpy.arange(len(new.array)) new.asset_refs = self.asset_refs[ok_indices] return new
[ "def", "reduce", "(", "self", ",", "sitecol", ")", ":", "ok_indices", "=", "numpy", ".", "sum", "(", "[", "self", ".", "array", "[", "'site_id'", "]", "==", "sid", "for", "sid", "in", "sitecol", ".", "sids", "]", ",", "axis", "=", "0", ",", "dtype", "=", "bool", ")", "new", "=", "object", ".", "__new__", "(", "self", ".", "__class__", ")", "vars", "(", "new", ")", ".", "update", "(", "vars", "(", "self", ")", ")", "new", ".", "array", "=", "self", ".", "array", "[", "ok_indices", "]", "new", ".", "array", "[", "'ordinal'", "]", "=", "numpy", ".", "arange", "(", "len", "(", "new", ".", "array", ")", ")", "new", ".", "asset_refs", "=", "self", ".", "asset_refs", "[", "ok_indices", "]", "return", "new" ]
:returns: a reduced AssetCollection on the given sitecol
[ ":", "returns", ":", "a", "reduced", "AssetCollection", "on", "the", "given", "sitecol" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L501-L513
gem/oq-engine
openquake/risklib/asset.py
AssetCollection.reduce_also
def reduce_also(self, sitecol): """ :returns: a reduced AssetCollection on the given sitecol NB: diffently from .reduce, also the SiteCollection is reduced and turned into a complete site collection. """ array = [] asset_refs = [] for idx, sid in enumerate(sitecol.sids): mask = self.array['site_id'] == sid arr = self.array[mask] arr['site_id'] = idx array.append(arr) asset_refs.append(self.asset_refs[mask]) new = object.__new__(self.__class__) vars(new).update(vars(self)) new.tot_sites = len(sitecol) new.array = numpy.concatenate(array) new.array['ordinal'] = numpy.arange(len(new.array)) new.asset_refs = numpy.concatenate(asset_refs) sitecol.make_complete() return new
python
def reduce_also(self, sitecol): array = [] asset_refs = [] for idx, sid in enumerate(sitecol.sids): mask = self.array['site_id'] == sid arr = self.array[mask] arr['site_id'] = idx array.append(arr) asset_refs.append(self.asset_refs[mask]) new = object.__new__(self.__class__) vars(new).update(vars(self)) new.tot_sites = len(sitecol) new.array = numpy.concatenate(array) new.array['ordinal'] = numpy.arange(len(new.array)) new.asset_refs = numpy.concatenate(asset_refs) sitecol.make_complete() return new
[ "def", "reduce_also", "(", "self", ",", "sitecol", ")", ":", "array", "=", "[", "]", "asset_refs", "=", "[", "]", "for", "idx", ",", "sid", "in", "enumerate", "(", "sitecol", ".", "sids", ")", ":", "mask", "=", "self", ".", "array", "[", "'site_id'", "]", "==", "sid", "arr", "=", "self", ".", "array", "[", "mask", "]", "arr", "[", "'site_id'", "]", "=", "idx", "array", ".", "append", "(", "arr", ")", "asset_refs", ".", "append", "(", "self", ".", "asset_refs", "[", "mask", "]", ")", "new", "=", "object", ".", "__new__", "(", "self", ".", "__class__", ")", "vars", "(", "new", ")", ".", "update", "(", "vars", "(", "self", ")", ")", "new", ".", "tot_sites", "=", "len", "(", "sitecol", ")", "new", ".", "array", "=", "numpy", ".", "concatenate", "(", "array", ")", "new", ".", "array", "[", "'ordinal'", "]", "=", "numpy", ".", "arange", "(", "len", "(", "new", ".", "array", ")", ")", "new", ".", "asset_refs", "=", "numpy", ".", "concatenate", "(", "asset_refs", ")", "sitecol", ".", "make_complete", "(", ")", "return", "new" ]
:returns: a reduced AssetCollection on the given sitecol NB: diffently from .reduce, also the SiteCollection is reduced and turned into a complete site collection.
[ ":", "returns", ":", "a", "reduced", "AssetCollection", "on", "the", "given", "sitecol", "NB", ":", "diffently", "from", ".", "reduce", "also", "the", "SiteCollection", "is", "reduced", "and", "turned", "into", "a", "complete", "site", "collection", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L515-L536
gem/oq-engine
openquake/risklib/asset.py
Exposure.read
def read(fnames, calculation_mode='', region_constraint='', ignore_missing_costs=(), asset_nodes=False, check_dupl=True, tagcol=None, by_country=False): """ Call `Exposure.read(fname)` to get an :class:`Exposure` instance keeping all the assets in memory or `Exposure.read(fname, asset_nodes=True)` to get an iterator over Node objects (one Node for each asset). """ if by_country: # E??_ -> countrycode prefix2cc = countries.from_exposures( os.path.basename(f) for f in fnames) else: prefix = '' allargs = [] tagcol = _minimal_tagcol(fnames, by_country) for i, fname in enumerate(fnames, 1): if by_country and len(fnames) > 1: prefix = prefix2cc['E%02d_' % i] + '_' elif len(fnames) > 1: prefix = 'E%02d_' % i else: prefix = '' allargs.append((fname, calculation_mode, region_constraint, ignore_missing_costs, asset_nodes, check_dupl, prefix, tagcol)) exp = None for exposure in parallel.Starmap( Exposure.read_exp, allargs, distribute='no'): if exp is None: # first time exp = exposure exp.description = 'Composite exposure[%d]' % len(fnames) else: assert exposure.cost_types == exp.cost_types assert exposure.occupancy_periods == exp.occupancy_periods assert (exposure.insurance_limit_is_absolute == exp.insurance_limit_is_absolute) assert exposure.retrofitted == exp.retrofitted assert exposure.area == exp.area exp.assets.extend(exposure.assets) exp.asset_refs.extend(exposure.asset_refs) exp.tagcol.extend(exposure.tagcol) exp.exposures = [os.path.splitext(os.path.basename(f))[0] for f in fnames] return exp
python
def read(fnames, calculation_mode='', region_constraint='', ignore_missing_costs=(), asset_nodes=False, check_dupl=True, tagcol=None, by_country=False): if by_country: prefix2cc = countries.from_exposures( os.path.basename(f) for f in fnames) else: prefix = '' allargs = [] tagcol = _minimal_tagcol(fnames, by_country) for i, fname in enumerate(fnames, 1): if by_country and len(fnames) > 1: prefix = prefix2cc['E%02d_' % i] + '_' elif len(fnames) > 1: prefix = 'E%02d_' % i else: prefix = '' allargs.append((fname, calculation_mode, region_constraint, ignore_missing_costs, asset_nodes, check_dupl, prefix, tagcol)) exp = None for exposure in parallel.Starmap( Exposure.read_exp, allargs, distribute='no'): if exp is None: exp = exposure exp.description = 'Composite exposure[%d]' % len(fnames) else: assert exposure.cost_types == exp.cost_types assert exposure.occupancy_periods == exp.occupancy_periods assert (exposure.insurance_limit_is_absolute == exp.insurance_limit_is_absolute) assert exposure.retrofitted == exp.retrofitted assert exposure.area == exp.area exp.assets.extend(exposure.assets) exp.asset_refs.extend(exposure.asset_refs) exp.tagcol.extend(exposure.tagcol) exp.exposures = [os.path.splitext(os.path.basename(f))[0] for f in fnames] return exp
[ "def", "read", "(", "fnames", ",", "calculation_mode", "=", "''", ",", "region_constraint", "=", "''", ",", "ignore_missing_costs", "=", "(", ")", ",", "asset_nodes", "=", "False", ",", "check_dupl", "=", "True", ",", "tagcol", "=", "None", ",", "by_country", "=", "False", ")", ":", "if", "by_country", ":", "# E??_ -> countrycode", "prefix2cc", "=", "countries", ".", "from_exposures", "(", "os", ".", "path", ".", "basename", "(", "f", ")", "for", "f", "in", "fnames", ")", "else", ":", "prefix", "=", "''", "allargs", "=", "[", "]", "tagcol", "=", "_minimal_tagcol", "(", "fnames", ",", "by_country", ")", "for", "i", ",", "fname", "in", "enumerate", "(", "fnames", ",", "1", ")", ":", "if", "by_country", "and", "len", "(", "fnames", ")", ">", "1", ":", "prefix", "=", "prefix2cc", "[", "'E%02d_'", "%", "i", "]", "+", "'_'", "elif", "len", "(", "fnames", ")", ">", "1", ":", "prefix", "=", "'E%02d_'", "%", "i", "else", ":", "prefix", "=", "''", "allargs", ".", "append", "(", "(", "fname", ",", "calculation_mode", ",", "region_constraint", ",", "ignore_missing_costs", ",", "asset_nodes", ",", "check_dupl", ",", "prefix", ",", "tagcol", ")", ")", "exp", "=", "None", "for", "exposure", "in", "parallel", ".", "Starmap", "(", "Exposure", ".", "read_exp", ",", "allargs", ",", "distribute", "=", "'no'", ")", ":", "if", "exp", "is", "None", ":", "# first time", "exp", "=", "exposure", "exp", ".", "description", "=", "'Composite exposure[%d]'", "%", "len", "(", "fnames", ")", "else", ":", "assert", "exposure", ".", "cost_types", "==", "exp", ".", "cost_types", "assert", "exposure", ".", "occupancy_periods", "==", "exp", ".", "occupancy_periods", "assert", "(", "exposure", ".", "insurance_limit_is_absolute", "==", "exp", ".", "insurance_limit_is_absolute", ")", "assert", "exposure", ".", "retrofitted", "==", "exp", ".", "retrofitted", "assert", "exposure", ".", "area", "==", "exp", ".", "area", "exp", ".", "assets", ".", "extend", "(", "exposure", ".", "assets", ")", "exp", ".", "asset_refs", ".", "extend", "(", "exposure", ".", "asset_refs", ")", "exp", ".", "tagcol", ".", "extend", "(", "exposure", ".", "tagcol", ")", "exp", ".", "exposures", "=", "[", "os", ".", "path", ".", "splitext", "(", "os", ".", "path", ".", "basename", "(", "f", ")", ")", "[", "0", "]", "for", "f", "in", "fnames", "]", "return", "exp" ]
Call `Exposure.read(fname)` to get an :class:`Exposure` instance keeping all the assets in memory or `Exposure.read(fname, asset_nodes=True)` to get an iterator over Node objects (one Node for each asset).
[ "Call", "Exposure", ".", "read", "(", "fname", ")", "to", "get", "an", ":", "class", ":", "Exposure", "instance", "keeping", "all", "the", "assets", "in", "memory", "or", "Exposure", ".", "read", "(", "fname", "asset_nodes", "=", "True", ")", "to", "get", "an", "iterator", "over", "Node", "objects", "(", "one", "Node", "for", "each", "asset", ")", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L792-L836
gem/oq-engine
openquake/risklib/asset.py
Exposure._csv_header
def _csv_header(self): """ Extract the expected CSV header from the exposure metadata """ fields = ['id', 'number', 'taxonomy', 'lon', 'lat'] for name in self.cost_types['name']: fields.append(name) if 'per_area' in self.cost_types['type']: fields.append('area') if self.occupancy_periods: fields.extend(self.occupancy_periods.split()) fields.extend(self.tagcol.tagnames) return set(fields)
python
def _csv_header(self): fields = ['id', 'number', 'taxonomy', 'lon', 'lat'] for name in self.cost_types['name']: fields.append(name) if 'per_area' in self.cost_types['type']: fields.append('area') if self.occupancy_periods: fields.extend(self.occupancy_periods.split()) fields.extend(self.tagcol.tagnames) return set(fields)
[ "def", "_csv_header", "(", "self", ")", ":", "fields", "=", "[", "'id'", ",", "'number'", ",", "'taxonomy'", ",", "'lon'", ",", "'lat'", "]", "for", "name", "in", "self", ".", "cost_types", "[", "'name'", "]", ":", "fields", ".", "append", "(", "name", ")", "if", "'per_area'", "in", "self", ".", "cost_types", "[", "'type'", "]", ":", "fields", ".", "append", "(", "'area'", ")", "if", "self", ".", "occupancy_periods", ":", "fields", ".", "extend", "(", "self", ".", "occupancy_periods", ".", "split", "(", ")", ")", "fields", ".", "extend", "(", "self", ".", "tagcol", ".", "tagnames", ")", "return", "set", "(", "fields", ")" ]
Extract the expected CSV header from the exposure metadata
[ "Extract", "the", "expected", "CSV", "header", "from", "the", "exposure", "metadata" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L885-L897
gem/oq-engine
openquake/risklib/asset.py
Exposure._read_csv
def _read_csv(self): """ :yields: asset nodes """ expected_header = self._csv_header() for fname in self.datafiles: with open(fname, encoding='utf-8') as f: fields = next(csv.reader(f)) header = set(fields) if len(header) < len(fields): raise InvalidFile( '%s: The header %s contains a duplicated field' % (fname, header)) elif expected_header - header - {'exposure', 'country'}: raise InvalidFile( 'Unexpected header in %s\nExpected: %s\nGot: %s' % (fname, sorted(expected_header), sorted(header))) occupancy_periods = self.occupancy_periods.split() for fname in self.datafiles: with open(fname, encoding='utf-8') as f: for i, dic in enumerate(csv.DictReader(f), 1): asset = Node('asset', lineno=i) with context(fname, asset): asset['id'] = dic['id'] asset['number'] = valid.positivefloat(dic['number']) asset['taxonomy'] = dic['taxonomy'] if 'area' in dic: # optional attribute asset['area'] = dic['area'] loc = Node('location', dict(lon=valid.longitude(dic['lon']), lat=valid.latitude(dic['lat']))) costs = Node('costs') for cost in self.cost_types['name']: a = dict(type=cost, value=dic[cost]) if 'retrofitted' in dic: a['retrofitted'] = dic['retrofitted'] costs.append(Node('cost', a)) occupancies = Node('occupancies') for period in occupancy_periods: a = dict(occupants=float(dic[period]), period=period) occupancies.append(Node('occupancy', a)) tags = Node('tags') for tagname in self.tagcol.tagnames: if tagname not in ( 'taxonomy', 'exposure', 'country'): tags.attrib[tagname] = dic[tagname] asset.nodes.extend([loc, costs, occupancies, tags]) yield asset
python
def _read_csv(self): expected_header = self._csv_header() for fname in self.datafiles: with open(fname, encoding='utf-8') as f: fields = next(csv.reader(f)) header = set(fields) if len(header) < len(fields): raise InvalidFile( '%s: The header %s contains a duplicated field' % (fname, header)) elif expected_header - header - {'exposure', 'country'}: raise InvalidFile( 'Unexpected header in %s\nExpected: %s\nGot: %s' % (fname, sorted(expected_header), sorted(header))) occupancy_periods = self.occupancy_periods.split() for fname in self.datafiles: with open(fname, encoding='utf-8') as f: for i, dic in enumerate(csv.DictReader(f), 1): asset = Node('asset', lineno=i) with context(fname, asset): asset['id'] = dic['id'] asset['number'] = valid.positivefloat(dic['number']) asset['taxonomy'] = dic['taxonomy'] if 'area' in dic: asset['area'] = dic['area'] loc = Node('location', dict(lon=valid.longitude(dic['lon']), lat=valid.latitude(dic['lat']))) costs = Node('costs') for cost in self.cost_types['name']: a = dict(type=cost, value=dic[cost]) if 'retrofitted' in dic: a['retrofitted'] = dic['retrofitted'] costs.append(Node('cost', a)) occupancies = Node('occupancies') for period in occupancy_periods: a = dict(occupants=float(dic[period]), period=period) occupancies.append(Node('occupancy', a)) tags = Node('tags') for tagname in self.tagcol.tagnames: if tagname not in ( 'taxonomy', 'exposure', 'country'): tags.attrib[tagname] = dic[tagname] asset.nodes.extend([loc, costs, occupancies, tags]) yield asset
[ "def", "_read_csv", "(", "self", ")", ":", "expected_header", "=", "self", ".", "_csv_header", "(", ")", "for", "fname", "in", "self", ".", "datafiles", ":", "with", "open", "(", "fname", ",", "encoding", "=", "'utf-8'", ")", "as", "f", ":", "fields", "=", "next", "(", "csv", ".", "reader", "(", "f", ")", ")", "header", "=", "set", "(", "fields", ")", "if", "len", "(", "header", ")", "<", "len", "(", "fields", ")", ":", "raise", "InvalidFile", "(", "'%s: The header %s contains a duplicated field'", "%", "(", "fname", ",", "header", ")", ")", "elif", "expected_header", "-", "header", "-", "{", "'exposure'", ",", "'country'", "}", ":", "raise", "InvalidFile", "(", "'Unexpected header in %s\\nExpected: %s\\nGot: %s'", "%", "(", "fname", ",", "sorted", "(", "expected_header", ")", ",", "sorted", "(", "header", ")", ")", ")", "occupancy_periods", "=", "self", ".", "occupancy_periods", ".", "split", "(", ")", "for", "fname", "in", "self", ".", "datafiles", ":", "with", "open", "(", "fname", ",", "encoding", "=", "'utf-8'", ")", "as", "f", ":", "for", "i", ",", "dic", "in", "enumerate", "(", "csv", ".", "DictReader", "(", "f", ")", ",", "1", ")", ":", "asset", "=", "Node", "(", "'asset'", ",", "lineno", "=", "i", ")", "with", "context", "(", "fname", ",", "asset", ")", ":", "asset", "[", "'id'", "]", "=", "dic", "[", "'id'", "]", "asset", "[", "'number'", "]", "=", "valid", ".", "positivefloat", "(", "dic", "[", "'number'", "]", ")", "asset", "[", "'taxonomy'", "]", "=", "dic", "[", "'taxonomy'", "]", "if", "'area'", "in", "dic", ":", "# optional attribute", "asset", "[", "'area'", "]", "=", "dic", "[", "'area'", "]", "loc", "=", "Node", "(", "'location'", ",", "dict", "(", "lon", "=", "valid", ".", "longitude", "(", "dic", "[", "'lon'", "]", ")", ",", "lat", "=", "valid", ".", "latitude", "(", "dic", "[", "'lat'", "]", ")", ")", ")", "costs", "=", "Node", "(", "'costs'", ")", "for", "cost", "in", "self", ".", "cost_types", "[", "'name'", "]", ":", "a", "=", "dict", "(", "type", "=", "cost", ",", "value", "=", "dic", "[", "cost", "]", ")", "if", "'retrofitted'", "in", "dic", ":", "a", "[", "'retrofitted'", "]", "=", "dic", "[", "'retrofitted'", "]", "costs", ".", "append", "(", "Node", "(", "'cost'", ",", "a", ")", ")", "occupancies", "=", "Node", "(", "'occupancies'", ")", "for", "period", "in", "occupancy_periods", ":", "a", "=", "dict", "(", "occupants", "=", "float", "(", "dic", "[", "period", "]", ")", ",", "period", "=", "period", ")", "occupancies", ".", "append", "(", "Node", "(", "'occupancy'", ",", "a", ")", ")", "tags", "=", "Node", "(", "'tags'", ")", "for", "tagname", "in", "self", ".", "tagcol", ".", "tagnames", ":", "if", "tagname", "not", "in", "(", "'taxonomy'", ",", "'exposure'", ",", "'country'", ")", ":", "tags", ".", "attrib", "[", "tagname", "]", "=", "dic", "[", "tagname", "]", "asset", ".", "nodes", ".", "extend", "(", "[", "loc", ",", "costs", ",", "occupancies", ",", "tags", "]", ")", "yield", "asset" ]
:yields: asset nodes
[ ":", "yields", ":", "asset", "nodes" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L899-L947
gem/oq-engine
openquake/risklib/asset.py
Exposure.get_mesh_assets_by_site
def get_mesh_assets_by_site(self): """ :returns: (Mesh instance, assets_by_site list) """ assets_by_loc = general.groupby(self, key=lambda a: a.location) mesh = geo.Mesh.from_coords(list(assets_by_loc)) assets_by_site = [ assets_by_loc[lonlat] for lonlat in zip(mesh.lons, mesh.lats)] return mesh, assets_by_site
python
def get_mesh_assets_by_site(self): assets_by_loc = general.groupby(self, key=lambda a: a.location) mesh = geo.Mesh.from_coords(list(assets_by_loc)) assets_by_site = [ assets_by_loc[lonlat] for lonlat in zip(mesh.lons, mesh.lats)] return mesh, assets_by_site
[ "def", "get_mesh_assets_by_site", "(", "self", ")", ":", "assets_by_loc", "=", "general", ".", "groupby", "(", "self", ",", "key", "=", "lambda", "a", ":", "a", ".", "location", ")", "mesh", "=", "geo", ".", "Mesh", ".", "from_coords", "(", "list", "(", "assets_by_loc", ")", ")", "assets_by_site", "=", "[", "assets_by_loc", "[", "lonlat", "]", "for", "lonlat", "in", "zip", "(", "mesh", ".", "lons", ",", "mesh", ".", "lats", ")", "]", "return", "mesh", ",", "assets_by_site" ]
:returns: (Mesh instance, assets_by_site list)
[ ":", "returns", ":", "(", "Mesh", "instance", "assets_by_site", "list", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/asset.py#L1049-L1057
gem/oq-engine
openquake/risklib/riskmodels.py
get_risk_files
def get_risk_files(inputs): """ :param inputs: a dictionary key -> path name :returns: a pair (file_type, {risk_type: path}) """ rfs = {} job_ini = inputs['job_ini'] for key in inputs: if key == 'fragility': # backward compatibily for .ini files with key fragility_file # instead of structural_fragility_file rfs['fragility/structural'] = inputs[ 'structural_fragility'] = inputs[key] del inputs['fragility'] elif key.endswith(('_fragility', '_vulnerability', '_consequence')): match = RISK_TYPE_REGEX.match(key) if match and 'retrofitted' not in key and 'consequence' not in key: rfs['%s/%s' % (match.group(2), match.group(1))] = inputs[key] elif match is None: raise ValueError('Invalid key in %s: %s_file' % (job_ini, key)) return rfs
python
def get_risk_files(inputs): rfs = {} job_ini = inputs['job_ini'] for key in inputs: if key == 'fragility': rfs['fragility/structural'] = inputs[ 'structural_fragility'] = inputs[key] del inputs['fragility'] elif key.endswith(('_fragility', '_vulnerability', '_consequence')): match = RISK_TYPE_REGEX.match(key) if match and 'retrofitted' not in key and 'consequence' not in key: rfs['%s/%s' % (match.group(2), match.group(1))] = inputs[key] elif match is None: raise ValueError('Invalid key in %s: %s_file' % (job_ini, key)) return rfs
[ "def", "get_risk_files", "(", "inputs", ")", ":", "rfs", "=", "{", "}", "job_ini", "=", "inputs", "[", "'job_ini'", "]", "for", "key", "in", "inputs", ":", "if", "key", "==", "'fragility'", ":", "# backward compatibily for .ini files with key fragility_file", "# instead of structural_fragility_file", "rfs", "[", "'fragility/structural'", "]", "=", "inputs", "[", "'structural_fragility'", "]", "=", "inputs", "[", "key", "]", "del", "inputs", "[", "'fragility'", "]", "elif", "key", ".", "endswith", "(", "(", "'_fragility'", ",", "'_vulnerability'", ",", "'_consequence'", ")", ")", ":", "match", "=", "RISK_TYPE_REGEX", ".", "match", "(", "key", ")", "if", "match", "and", "'retrofitted'", "not", "in", "key", "and", "'consequence'", "not", "in", "key", ":", "rfs", "[", "'%s/%s'", "%", "(", "match", ".", "group", "(", "2", ")", ",", "match", ".", "group", "(", "1", ")", ")", "]", "=", "inputs", "[", "key", "]", "elif", "match", "is", "None", ":", "raise", "ValueError", "(", "'Invalid key in %s: %s_file'", "%", "(", "job_ini", ",", "key", ")", ")", "return", "rfs" ]
:param inputs: a dictionary key -> path name :returns: a pair (file_type, {risk_type: path})
[ ":", "param", "inputs", ":", "a", "dictionary", "key", "-", ">", "path", "name", ":", "returns", ":", "a", "pair", "(", "file_type", "{", "risk_type", ":", "path", "}", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/riskmodels.py#L39-L59
gem/oq-engine
openquake/risklib/riskmodels.py
build_vf_node
def build_vf_node(vf): """ Convert a VulnerabilityFunction object into a Node suitable for XML conversion. """ nodes = [Node('imls', {'imt': vf.imt}, vf.imls), Node('meanLRs', {}, vf.mean_loss_ratios), Node('covLRs', {}, vf.covs)] return Node( 'vulnerabilityFunction', {'id': vf.id, 'dist': vf.distribution_name}, nodes=nodes)
python
def build_vf_node(vf): nodes = [Node('imls', {'imt': vf.imt}, vf.imls), Node('meanLRs', {}, vf.mean_loss_ratios), Node('covLRs', {}, vf.covs)] return Node( 'vulnerabilityFunction', {'id': vf.id, 'dist': vf.distribution_name}, nodes=nodes)
[ "def", "build_vf_node", "(", "vf", ")", ":", "nodes", "=", "[", "Node", "(", "'imls'", ",", "{", "'imt'", ":", "vf", ".", "imt", "}", ",", "vf", ".", "imls", ")", ",", "Node", "(", "'meanLRs'", ",", "{", "}", ",", "vf", ".", "mean_loss_ratios", ")", ",", "Node", "(", "'covLRs'", ",", "{", "}", ",", "vf", ".", "covs", ")", "]", "return", "Node", "(", "'vulnerabilityFunction'", ",", "{", "'id'", ":", "vf", ".", "id", ",", "'dist'", ":", "vf", ".", "distribution_name", "}", ",", "nodes", "=", "nodes", ")" ]
Convert a VulnerabilityFunction object into a Node suitable for XML conversion.
[ "Convert", "a", "VulnerabilityFunction", "object", "into", "a", "Node", "suitable", "for", "XML", "conversion", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/riskmodels.py#L69-L79
gem/oq-engine
openquake/risklib/riskmodels.py
get_risk_models
def get_risk_models(oqparam, kind='vulnerability vulnerability_retrofitted ' 'fragility consequence'): """ :param oqparam: an OqParam instance :param kind: a space-separated string with the kinds of risk models to read :returns: a dictionary riskid -> loss_type, kind -> function """ kinds = kind.split() rmodels = AccumDict() for kind in kinds: for key in sorted(oqparam.inputs): mo = re.match('(occupants|%s)_%s$' % (COST_TYPE_REGEX, kind), key) if mo: loss_type = mo.group(1) # the cost_type in the key # can be occupants, structural, nonstructural, ... rmodel = nrml.to_python(oqparam.inputs[key]) if len(rmodel) == 0: raise InvalidFile('%s is empty!' % oqparam.inputs[key]) rmodels[loss_type, kind] = rmodel if rmodel.lossCategory is None: # NRML 0.4 continue cost_type = str(rmodel.lossCategory) rmodel_kind = rmodel.__class__.__name__ kind_ = kind.replace('_retrofitted', '') # strip retrofitted if not rmodel_kind.lower().startswith(kind_): raise ValueError( 'Error in the file "%s_file=%s": is ' 'of kind %s, expected %s' % ( key, oqparam.inputs[key], rmodel_kind, kind.capitalize() + 'Model')) if cost_type != loss_type: raise ValueError( 'Error in the file "%s_file=%s": lossCategory is of ' 'type "%s", expected "%s"' % (key, oqparam.inputs[key], rmodel.lossCategory, loss_type)) rdict = AccumDict(accum={}) rdict.limit_states = [] for (loss_type, kind), rm in sorted(rmodels.items()): if kind == 'fragility': # build a copy of the FragilityModel with different IM levels newfm = rm.build(oqparam.continuous_fragility_discretization, oqparam.steps_per_interval) for (imt, riskid), ffl in newfm.items(): if not rdict.limit_states: rdict.limit_states.extend(rm.limitStates) # we are rejecting the case of loss types with different # limit states; this may change in the future assert rdict.limit_states == rm.limitStates, ( rdict.limit_states, rm.limitStates) rdict[riskid][loss_type, kind] = ffl # TODO: see if it is possible to remove the attribute # below, used in classical_damage ffl.steps_per_interval = oqparam.steps_per_interval elif kind == 'consequence': for riskid, cf in rm.items(): rdict[riskid][loss_type, kind] = cf else: # vulnerability cl_risk = oqparam.calculation_mode in ( 'classical', 'classical_risk') # only for classical_risk reduce the loss_ratios # to make sure they are strictly increasing for (imt, riskid), rf in rm.items(): rdict[riskid][loss_type, kind] = ( rf.strictly_increasing() if cl_risk else rf) return rdict
python
def get_risk_models(oqparam, kind='vulnerability vulnerability_retrofitted ' 'fragility consequence'): kinds = kind.split() rmodels = AccumDict() for kind in kinds: for key in sorted(oqparam.inputs): mo = re.match('(occupants|%s)_%s$' % (COST_TYPE_REGEX, kind), key) if mo: loss_type = mo.group(1) rmodel = nrml.to_python(oqparam.inputs[key]) if len(rmodel) == 0: raise InvalidFile('%s is empty!' % oqparam.inputs[key]) rmodels[loss_type, kind] = rmodel if rmodel.lossCategory is None: continue cost_type = str(rmodel.lossCategory) rmodel_kind = rmodel.__class__.__name__ kind_ = kind.replace('_retrofitted', '') if not rmodel_kind.lower().startswith(kind_): raise ValueError( 'Error in the file "%s_file=%s": is ' 'of kind %s, expected %s' % ( key, oqparam.inputs[key], rmodel_kind, kind.capitalize() + 'Model')) if cost_type != loss_type: raise ValueError( 'Error in the file "%s_file=%s": lossCategory is of ' 'type "%s", expected "%s"' % (key, oqparam.inputs[key], rmodel.lossCategory, loss_type)) rdict = AccumDict(accum={}) rdict.limit_states = [] for (loss_type, kind), rm in sorted(rmodels.items()): if kind == 'fragility': newfm = rm.build(oqparam.continuous_fragility_discretization, oqparam.steps_per_interval) for (imt, riskid), ffl in newfm.items(): if not rdict.limit_states: rdict.limit_states.extend(rm.limitStates) assert rdict.limit_states == rm.limitStates, ( rdict.limit_states, rm.limitStates) rdict[riskid][loss_type, kind] = ffl ffl.steps_per_interval = oqparam.steps_per_interval elif kind == 'consequence': for riskid, cf in rm.items(): rdict[riskid][loss_type, kind] = cf else: cl_risk = oqparam.calculation_mode in ( 'classical', 'classical_risk') for (imt, riskid), rf in rm.items(): rdict[riskid][loss_type, kind] = ( rf.strictly_increasing() if cl_risk else rf) return rdict
[ "def", "get_risk_models", "(", "oqparam", ",", "kind", "=", "'vulnerability vulnerability_retrofitted '", "'fragility consequence'", ")", ":", "kinds", "=", "kind", ".", "split", "(", ")", "rmodels", "=", "AccumDict", "(", ")", "for", "kind", "in", "kinds", ":", "for", "key", "in", "sorted", "(", "oqparam", ".", "inputs", ")", ":", "mo", "=", "re", ".", "match", "(", "'(occupants|%s)_%s$'", "%", "(", "COST_TYPE_REGEX", ",", "kind", ")", ",", "key", ")", "if", "mo", ":", "loss_type", "=", "mo", ".", "group", "(", "1", ")", "# the cost_type in the key", "# can be occupants, structural, nonstructural, ...", "rmodel", "=", "nrml", ".", "to_python", "(", "oqparam", ".", "inputs", "[", "key", "]", ")", "if", "len", "(", "rmodel", ")", "==", "0", ":", "raise", "InvalidFile", "(", "'%s is empty!'", "%", "oqparam", ".", "inputs", "[", "key", "]", ")", "rmodels", "[", "loss_type", ",", "kind", "]", "=", "rmodel", "if", "rmodel", ".", "lossCategory", "is", "None", ":", "# NRML 0.4", "continue", "cost_type", "=", "str", "(", "rmodel", ".", "lossCategory", ")", "rmodel_kind", "=", "rmodel", ".", "__class__", ".", "__name__", "kind_", "=", "kind", ".", "replace", "(", "'_retrofitted'", ",", "''", ")", "# strip retrofitted", "if", "not", "rmodel_kind", ".", "lower", "(", ")", ".", "startswith", "(", "kind_", ")", ":", "raise", "ValueError", "(", "'Error in the file \"%s_file=%s\": is '", "'of kind %s, expected %s'", "%", "(", "key", ",", "oqparam", ".", "inputs", "[", "key", "]", ",", "rmodel_kind", ",", "kind", ".", "capitalize", "(", ")", "+", "'Model'", ")", ")", "if", "cost_type", "!=", "loss_type", ":", "raise", "ValueError", "(", "'Error in the file \"%s_file=%s\": lossCategory is of '", "'type \"%s\", expected \"%s\"'", "%", "(", "key", ",", "oqparam", ".", "inputs", "[", "key", "]", ",", "rmodel", ".", "lossCategory", ",", "loss_type", ")", ")", "rdict", "=", "AccumDict", "(", "accum", "=", "{", "}", ")", "rdict", ".", "limit_states", "=", "[", "]", "for", "(", "loss_type", ",", "kind", ")", ",", "rm", "in", "sorted", "(", "rmodels", ".", "items", "(", ")", ")", ":", "if", "kind", "==", "'fragility'", ":", "# build a copy of the FragilityModel with different IM levels", "newfm", "=", "rm", ".", "build", "(", "oqparam", ".", "continuous_fragility_discretization", ",", "oqparam", ".", "steps_per_interval", ")", "for", "(", "imt", ",", "riskid", ")", ",", "ffl", "in", "newfm", ".", "items", "(", ")", ":", "if", "not", "rdict", ".", "limit_states", ":", "rdict", ".", "limit_states", ".", "extend", "(", "rm", ".", "limitStates", ")", "# we are rejecting the case of loss types with different", "# limit states; this may change in the future", "assert", "rdict", ".", "limit_states", "==", "rm", ".", "limitStates", ",", "(", "rdict", ".", "limit_states", ",", "rm", ".", "limitStates", ")", "rdict", "[", "riskid", "]", "[", "loss_type", ",", "kind", "]", "=", "ffl", "# TODO: see if it is possible to remove the attribute", "# below, used in classical_damage", "ffl", ".", "steps_per_interval", "=", "oqparam", ".", "steps_per_interval", "elif", "kind", "==", "'consequence'", ":", "for", "riskid", ",", "cf", "in", "rm", ".", "items", "(", ")", ":", "rdict", "[", "riskid", "]", "[", "loss_type", ",", "kind", "]", "=", "cf", "else", ":", "# vulnerability", "cl_risk", "=", "oqparam", ".", "calculation_mode", "in", "(", "'classical'", ",", "'classical_risk'", ")", "# only for classical_risk reduce the loss_ratios", "# to make sure they are strictly increasing", "for", "(", "imt", ",", "riskid", ")", ",", "rf", "in", "rm", ".", "items", "(", ")", ":", "rdict", "[", "riskid", "]", "[", "loss_type", ",", "kind", "]", "=", "(", "rf", ".", "strictly_increasing", "(", ")", "if", "cl_risk", "else", "rf", ")", "return", "rdict" ]
:param oqparam: an OqParam instance :param kind: a space-separated string with the kinds of risk models to read :returns: a dictionary riskid -> loss_type, kind -> function
[ ":", "param", "oqparam", ":", "an", "OqParam", "instance", ":", "param", "kind", ":", "a", "space", "-", "separated", "string", "with", "the", "kinds", "of", "risk", "models", "to", "read", ":", "returns", ":", "a", "dictionary", "riskid", "-", ">", "loss_type", "kind", "-", ">", "function" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/riskmodels.py#L82-L150
gem/oq-engine
openquake/risklib/riskmodels.py
rescale
def rescale(curves, values): """ Multiply the losses in each curve of kind (losses, poes) by the corresponding value. :param curves: an array of shape (A, 2, C) :param values: an array of shape (A,) """ A, _, C = curves.shape assert A == len(values), (A, len(values)) array = numpy.zeros((A, C), loss_poe_dt) array['loss'] = [c * v for c, v in zip(curves[:, 0], values)] array['poe'] = curves[:, 1] return array
python
def rescale(curves, values): A, _, C = curves.shape assert A == len(values), (A, len(values)) array = numpy.zeros((A, C), loss_poe_dt) array['loss'] = [c * v for c, v in zip(curves[:, 0], values)] array['poe'] = curves[:, 1] return array
[ "def", "rescale", "(", "curves", ",", "values", ")", ":", "A", ",", "_", ",", "C", "=", "curves", ".", "shape", "assert", "A", "==", "len", "(", "values", ")", ",", "(", "A", ",", "len", "(", "values", ")", ")", "array", "=", "numpy", ".", "zeros", "(", "(", "A", ",", "C", ")", ",", "loss_poe_dt", ")", "array", "[", "'loss'", "]", "=", "[", "c", "*", "v", "for", "c", ",", "v", "in", "zip", "(", "curves", "[", ":", ",", "0", "]", ",", "values", ")", "]", "array", "[", "'poe'", "]", "=", "curves", "[", ":", ",", "1", "]", "return", "array" ]
Multiply the losses in each curve of kind (losses, poes) by the corresponding value. :param curves: an array of shape (A, 2, C) :param values: an array of shape (A,)
[ "Multiply", "the", "losses", "in", "each", "curve", "of", "kind", "(", "losses", "poes", ")", "by", "the", "corresponding", "value", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/riskmodels.py#L220-L233
gem/oq-engine
openquake/risklib/riskmodels.py
get_riskmodel
def get_riskmodel(taxonomy, oqparam, **extra): """ Return an instance of the correct riskmodel class, depending on the attribute `calculation_mode` of the object `oqparam`. :param taxonomy: a taxonomy string :param oqparam: an object containing the parameters needed by the riskmodel class :param extra: extra parameters to pass to the riskmodel class """ riskmodel_class = registry[oqparam.calculation_mode] # arguments needed to instantiate the riskmodel class argnames = inspect.getfullargspec(riskmodel_class.__init__).args[3:] # arguments extracted from oqparam known_args = set(name for name, value in inspect.getmembers(oqparam.__class__) if isinstance(value, valid.Param)) all_args = {} for argname in argnames: if argname in known_args: all_args[argname] = getattr(oqparam, argname) if 'hazard_imtls' in argnames: # special case all_args['hazard_imtls'] = oqparam.imtls all_args.update(extra) missing = set(argnames) - set(all_args) if missing: raise TypeError('Missing parameter: %s' % ', '.join(missing)) return riskmodel_class(taxonomy, **all_args)
python
def get_riskmodel(taxonomy, oqparam, **extra): riskmodel_class = registry[oqparam.calculation_mode] argnames = inspect.getfullargspec(riskmodel_class.__init__).args[3:] known_args = set(name for name, value in inspect.getmembers(oqparam.__class__) if isinstance(value, valid.Param)) all_args = {} for argname in argnames: if argname in known_args: all_args[argname] = getattr(oqparam, argname) if 'hazard_imtls' in argnames: all_args['hazard_imtls'] = oqparam.imtls all_args.update(extra) missing = set(argnames) - set(all_args) if missing: raise TypeError('Missing parameter: %s' % ', '.join(missing)) return riskmodel_class(taxonomy, **all_args)
[ "def", "get_riskmodel", "(", "taxonomy", ",", "oqparam", ",", "*", "*", "extra", ")", ":", "riskmodel_class", "=", "registry", "[", "oqparam", ".", "calculation_mode", "]", "# arguments needed to instantiate the riskmodel class", "argnames", "=", "inspect", ".", "getfullargspec", "(", "riskmodel_class", ".", "__init__", ")", ".", "args", "[", "3", ":", "]", "# arguments extracted from oqparam", "known_args", "=", "set", "(", "name", "for", "name", ",", "value", "in", "inspect", ".", "getmembers", "(", "oqparam", ".", "__class__", ")", "if", "isinstance", "(", "value", ",", "valid", ".", "Param", ")", ")", "all_args", "=", "{", "}", "for", "argname", "in", "argnames", ":", "if", "argname", "in", "known_args", ":", "all_args", "[", "argname", "]", "=", "getattr", "(", "oqparam", ",", "argname", ")", "if", "'hazard_imtls'", "in", "argnames", ":", "# special case", "all_args", "[", "'hazard_imtls'", "]", "=", "oqparam", ".", "imtls", "all_args", ".", "update", "(", "extra", ")", "missing", "=", "set", "(", "argnames", ")", "-", "set", "(", "all_args", ")", "if", "missing", ":", "raise", "TypeError", "(", "'Missing parameter: %s'", "%", "', '", ".", "join", "(", "missing", ")", ")", "return", "riskmodel_class", "(", "taxonomy", ",", "*", "*", "all_args", ")" ]
Return an instance of the correct riskmodel class, depending on the attribute `calculation_mode` of the object `oqparam`. :param taxonomy: a taxonomy string :param oqparam: an object containing the parameters needed by the riskmodel class :param extra: extra parameters to pass to the riskmodel class
[ "Return", "an", "instance", "of", "the", "correct", "riskmodel", "class", "depending", "on", "the", "attribute", "calculation_mode", "of", "the", "object", "oqparam", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/riskmodels.py#L548-L580
gem/oq-engine
openquake/risklib/riskmodels.py
RiskModel.get_loss_types
def get_loss_types(self, imt): """ :param imt: Intensity Measure Type string :returns: loss types with risk functions of the given imt """ return [lt for lt in self.loss_types if self.risk_functions[lt].imt == imt]
python
def get_loss_types(self, imt): return [lt for lt in self.loss_types if self.risk_functions[lt].imt == imt]
[ "def", "get_loss_types", "(", "self", ",", "imt", ")", ":", "return", "[", "lt", "for", "lt", "in", "self", ".", "loss_types", "if", "self", ".", "risk_functions", "[", "lt", "]", ".", "imt", "==", "imt", "]" ]
:param imt: Intensity Measure Type string :returns: loss types with risk functions of the given imt
[ ":", "param", "imt", ":", "Intensity", "Measure", "Type", "string", ":", "returns", ":", "loss", "types", "with", "risk", "functions", "of", "the", "given", "imt" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/risklib/riskmodels.py#L193-L199
gem/oq-engine
openquake/hmtk/plotting/beachball.py
Beach
def Beach(fm, linewidth=2, facecolor='b', bgcolor='w', edgecolor='k', alpha=1.0, xy=(0, 0), width=200, size=100, nofill=False, zorder=100, axes=None): """ Return a beach ball as a collection which can be connected to an current matplotlib axes instance (ax.add_collection). S1, D1, and R1, the strike, dip and rake of one of the focal planes, can be vectors of multiple focal mechanisms. :param fm: Focal mechanism that is either number of mechanisms (NM) by 3 (strike, dip, and rake) or NM x 6 (M11, M22, M33, M12, M13, M23 - the six independent components of the moment tensor, where the coordinate system is 1,2,3 = Up,South,East which equals r,theta,phi). The strike is of the first plane, clockwise relative to north. The dip is of the first plane, defined clockwise and perpendicular to strike, relative to horizontal such that 0 is horizontal and 90 is vertical. The rake is of the first focal plane solution. 90 moves the hanging wall up-dip (thrust), 0 moves it in the strike direction (left-lateral), -90 moves it down-dip (normal), and 180 moves it opposite to strike (right-lateral). :param facecolor: Color to use for quadrants of tension; can be a string, e.g. ``'r'``, ``'b'`` or three component color vector, [R G B]. Defaults to ``'b'`` (blue). :param bgcolor: The background color. Defaults to ``'w'`` (white). :param edgecolor: Color of the edges. Defaults to ``'k'`` (black). :param alpha: The alpha level of the beach ball. Defaults to ``1.0`` (opaque). :param xy: Origin position of the beach ball as tuple. Defaults to ``(0, 0)``. :type width: int or tuple :param width: Symbol size of beach ball, or tuple for elliptically shaped patches. Defaults to size ``200``. :param size: Controls the number of interpolation points for the curves. Minimum is automatically set to ``100``. :param nofill: Do not fill the beach ball, but only plot the planes. :param zorder: Set zorder. Artists with lower zorder values are drawn first. :type axes: :class:`matplotlib.axes.Axes` :param axes: Used to make beach balls circular on non-scaled axes. Also maintains the aspect ratio when resizing the figure. Will not add the returned collection to the axes instance. """ # check if one or two widths are specified (Circle or Ellipse) try: assert(len(width) == 2) except TypeError: width = (width, width) mt = None np1 = None if isinstance(fm, MomentTensor): mt = fm np1 = MT2Plane(mt) elif isinstance(fm, NodalPlane): np1 = fm elif len(fm) == 6: mt = MomentTensor(fm[0], fm[1], fm[2], fm[3], fm[4], fm[5], 0) np1 = MT2Plane(mt) elif len(fm) == 3: np1 = NodalPlane(fm[0], fm[1], fm[2]) else: raise TypeError("Wrong input value for 'fm'.") # Only at least size 100, i.e. 100 points in the matrix are allowed if size < 100: size = 100 # Return as collection if mt: (T, N, P) = MT2Axes(mt) if np.fabs(N.val) < EPSILON and np.fabs(T.val + P.val) < EPSILON: colors, p = plotDC(np1, size, xy=xy, width=width) else: colors, p = plotMT(T, N, P, size, plot_zerotrace=True, xy=xy, width=width) else: colors, p = plotDC(np1, size=size, xy=xy, width=width) if nofill: # XXX: not tested with plotMT col = collections.PatchCollection([p[1]], match_original=False) col.set_facecolor('none') else: col = collections.PatchCollection(p, match_original=False) # Replace color dummies 'b' and 'w' by face and bgcolor fc = [facecolor if c == 'b' else bgcolor for c in colors] col.set_facecolors(fc) # Use the given axes to maintain the aspect ratio of beachballs on figure # resize. if axes is not None: # This is what holds the aspect ratio (but breaks the positioning) col.set_transform(transforms.IdentityTransform()) # Next is a dirty hack to fix the positioning: # 1. Need to bring the all patches to the origin (0, 0). for p in col._paths: p.vertices -= xy # 2. Then use the offset property of the collection to position the # patches col.set_offsets(xy) col._transOffset = axes.transData col.set_edgecolor(edgecolor) col.set_alpha(alpha) col.set_linewidth(linewidth) col.set_zorder(zorder) return col
python
def Beach(fm, linewidth=2, facecolor='b', bgcolor='w', edgecolor='k', alpha=1.0, xy=(0, 0), width=200, size=100, nofill=False, zorder=100, axes=None): try: assert(len(width) == 2) except TypeError: width = (width, width) mt = None np1 = None if isinstance(fm, MomentTensor): mt = fm np1 = MT2Plane(mt) elif isinstance(fm, NodalPlane): np1 = fm elif len(fm) == 6: mt = MomentTensor(fm[0], fm[1], fm[2], fm[3], fm[4], fm[5], 0) np1 = MT2Plane(mt) elif len(fm) == 3: np1 = NodalPlane(fm[0], fm[1], fm[2]) else: raise TypeError("Wrong input value for 'fm'.") if size < 100: size = 100 if mt: (T, N, P) = MT2Axes(mt) if np.fabs(N.val) < EPSILON and np.fabs(T.val + P.val) < EPSILON: colors, p = plotDC(np1, size, xy=xy, width=width) else: colors, p = plotMT(T, N, P, size, plot_zerotrace=True, xy=xy, width=width) else: colors, p = plotDC(np1, size=size, xy=xy, width=width) if nofill: col = collections.PatchCollection([p[1]], match_original=False) col.set_facecolor('none') else: col = collections.PatchCollection(p, match_original=False) fc = [facecolor if c == 'b' else bgcolor for c in colors] col.set_facecolors(fc) if axes is not None: col.set_transform(transforms.IdentityTransform()) for p in col._paths: p.vertices -= xy col.set_offsets(xy) col._transOffset = axes.transData col.set_edgecolor(edgecolor) col.set_alpha(alpha) col.set_linewidth(linewidth) col.set_zorder(zorder) return col
[ "def", "Beach", "(", "fm", ",", "linewidth", "=", "2", ",", "facecolor", "=", "'b'", ",", "bgcolor", "=", "'w'", ",", "edgecolor", "=", "'k'", ",", "alpha", "=", "1.0", ",", "xy", "=", "(", "0", ",", "0", ")", ",", "width", "=", "200", ",", "size", "=", "100", ",", "nofill", "=", "False", ",", "zorder", "=", "100", ",", "axes", "=", "None", ")", ":", "# check if one or two widths are specified (Circle or Ellipse)", "try", ":", "assert", "(", "len", "(", "width", ")", "==", "2", ")", "except", "TypeError", ":", "width", "=", "(", "width", ",", "width", ")", "mt", "=", "None", "np1", "=", "None", "if", "isinstance", "(", "fm", ",", "MomentTensor", ")", ":", "mt", "=", "fm", "np1", "=", "MT2Plane", "(", "mt", ")", "elif", "isinstance", "(", "fm", ",", "NodalPlane", ")", ":", "np1", "=", "fm", "elif", "len", "(", "fm", ")", "==", "6", ":", "mt", "=", "MomentTensor", "(", "fm", "[", "0", "]", ",", "fm", "[", "1", "]", ",", "fm", "[", "2", "]", ",", "fm", "[", "3", "]", ",", "fm", "[", "4", "]", ",", "fm", "[", "5", "]", ",", "0", ")", "np1", "=", "MT2Plane", "(", "mt", ")", "elif", "len", "(", "fm", ")", "==", "3", ":", "np1", "=", "NodalPlane", "(", "fm", "[", "0", "]", ",", "fm", "[", "1", "]", ",", "fm", "[", "2", "]", ")", "else", ":", "raise", "TypeError", "(", "\"Wrong input value for 'fm'.\"", ")", "# Only at least size 100, i.e. 100 points in the matrix are allowed", "if", "size", "<", "100", ":", "size", "=", "100", "# Return as collection", "if", "mt", ":", "(", "T", ",", "N", ",", "P", ")", "=", "MT2Axes", "(", "mt", ")", "if", "np", ".", "fabs", "(", "N", ".", "val", ")", "<", "EPSILON", "and", "np", ".", "fabs", "(", "T", ".", "val", "+", "P", ".", "val", ")", "<", "EPSILON", ":", "colors", ",", "p", "=", "plotDC", "(", "np1", ",", "size", ",", "xy", "=", "xy", ",", "width", "=", "width", ")", "else", ":", "colors", ",", "p", "=", "plotMT", "(", "T", ",", "N", ",", "P", ",", "size", ",", "plot_zerotrace", "=", "True", ",", "xy", "=", "xy", ",", "width", "=", "width", ")", "else", ":", "colors", ",", "p", "=", "plotDC", "(", "np1", ",", "size", "=", "size", ",", "xy", "=", "xy", ",", "width", "=", "width", ")", "if", "nofill", ":", "# XXX: not tested with plotMT", "col", "=", "collections", ".", "PatchCollection", "(", "[", "p", "[", "1", "]", "]", ",", "match_original", "=", "False", ")", "col", ".", "set_facecolor", "(", "'none'", ")", "else", ":", "col", "=", "collections", ".", "PatchCollection", "(", "p", ",", "match_original", "=", "False", ")", "# Replace color dummies 'b' and 'w' by face and bgcolor", "fc", "=", "[", "facecolor", "if", "c", "==", "'b'", "else", "bgcolor", "for", "c", "in", "colors", "]", "col", ".", "set_facecolors", "(", "fc", ")", "# Use the given axes to maintain the aspect ratio of beachballs on figure", "# resize.", "if", "axes", "is", "not", "None", ":", "# This is what holds the aspect ratio (but breaks the positioning)", "col", ".", "set_transform", "(", "transforms", ".", "IdentityTransform", "(", ")", ")", "# Next is a dirty hack to fix the positioning:", "# 1. Need to bring the all patches to the origin (0, 0).", "for", "p", "in", "col", ".", "_paths", ":", "p", ".", "vertices", "-=", "xy", "# 2. Then use the offset property of the collection to position the", "# patches", "col", ".", "set_offsets", "(", "xy", ")", "col", ".", "_transOffset", "=", "axes", ".", "transData", "col", ".", "set_edgecolor", "(", "edgecolor", ")", "col", ".", "set_alpha", "(", "alpha", ")", "col", ".", "set_linewidth", "(", "linewidth", ")", "col", ".", "set_zorder", "(", "zorder", ")", "return", "col" ]
Return a beach ball as a collection which can be connected to an current matplotlib axes instance (ax.add_collection). S1, D1, and R1, the strike, dip and rake of one of the focal planes, can be vectors of multiple focal mechanisms. :param fm: Focal mechanism that is either number of mechanisms (NM) by 3 (strike, dip, and rake) or NM x 6 (M11, M22, M33, M12, M13, M23 - the six independent components of the moment tensor, where the coordinate system is 1,2,3 = Up,South,East which equals r,theta,phi). The strike is of the first plane, clockwise relative to north. The dip is of the first plane, defined clockwise and perpendicular to strike, relative to horizontal such that 0 is horizontal and 90 is vertical. The rake is of the first focal plane solution. 90 moves the hanging wall up-dip (thrust), 0 moves it in the strike direction (left-lateral), -90 moves it down-dip (normal), and 180 moves it opposite to strike (right-lateral). :param facecolor: Color to use for quadrants of tension; can be a string, e.g. ``'r'``, ``'b'`` or three component color vector, [R G B]. Defaults to ``'b'`` (blue). :param bgcolor: The background color. Defaults to ``'w'`` (white). :param edgecolor: Color of the edges. Defaults to ``'k'`` (black). :param alpha: The alpha level of the beach ball. Defaults to ``1.0`` (opaque). :param xy: Origin position of the beach ball as tuple. Defaults to ``(0, 0)``. :type width: int or tuple :param width: Symbol size of beach ball, or tuple for elliptically shaped patches. Defaults to size ``200``. :param size: Controls the number of interpolation points for the curves. Minimum is automatically set to ``100``. :param nofill: Do not fill the beach ball, but only plot the planes. :param zorder: Set zorder. Artists with lower zorder values are drawn first. :type axes: :class:`matplotlib.axes.Axes` :param axes: Used to make beach balls circular on non-scaled axes. Also maintains the aspect ratio when resizing the figure. Will not add the returned collection to the axes instance.
[ "Return", "a", "beach", "ball", "as", "a", "collection", "which", "can", "be", "connected", "to", "an", "current", "matplotlib", "axes", "instance", "(", "ax", ".", "add_collection", ")", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L38-L144
gem/oq-engine
openquake/hmtk/plotting/beachball.py
Beachball
def Beachball(fm, linewidth=2, facecolor='b', bgcolor='w', edgecolor='k', alpha=1.0, xy=(0, 0), width=200, size=100, nofill=False, zorder=100, outfile=None, format=None, fig=None): """ Draws a beach ball diagram of an earthquake focal mechanism. S1, D1, and R1, the strike, dip and rake of one of the focal planes, can be vectors of multiple focal mechanisms. :param fm: Focal mechanism that is either number of mechanisms (NM) by 3 (strike, dip, and rake) or NM x 6 (M11, M22, M33, M12, M13, M23 - the six independent components of the moment tensor, where the coordinate system is 1,2,3 = Up,South,East which equals r,theta,phi). The strike is of the first plane, clockwise relative to north. The dip is of the first plane, defined clockwise and perpendicular to strike, relative to horizontal such that 0 is horizontal and 90 is vertical. The rake is of the first focal plane solution. 90 moves the hanging wall up-dip (thrust), 0 moves it in the strike direction (left-lateral), -90 moves it down-dip (normal), and 180 moves it opposite to strike (right-lateral). :param facecolor: Color to use for quadrants of tension; can be a string, e.g. ``'r'``, ``'b'`` or three component color vector, [R G B]. Defaults to ``'b'`` (blue). :param bgcolor: The background color. Defaults to ``'w'`` (white). :param edgecolor: Color of the edges. Defaults to ``'k'`` (black). :param alpha: The alpha level of the beach ball. Defaults to ``1.0`` (opaque). :param xy: Origin position of the beach ball as tuple. Defaults to ``(0, 0)``. :type width: int :param width: Symbol size of beach ball. Defaults to ``200``. :param size: Controls the number of interpolation points for the curves. Minimum is automatically set to ``100``. :param nofill: Do not fill the beach ball, but only plot the planes. :param zorder: Set zorder. Artists with lower zorder values are drawn first. :param outfile: Output file string. Also used to automatically determine the output format. Supported file formats depend on your matplotlib backend. Most backends support png, pdf, ps, eps and svg. Defaults to ``None``. :param format: Format of the graph picture. If no format is given the outfile parameter will be used to try to automatically determine the output format. If no format is found it defaults to png output. If no outfile is specified but a format is, than a binary imagestring will be returned. Defaults to ``None``. :param fig: Give an existing figure instance to plot into. New Figure if set to ``None``. """ plot_width = width * 0.95 # plot the figure if not fig: fig = plt.figure(figsize=(3, 3), dpi=100) fig.subplots_adjust(left=0, bottom=0, right=1, top=1) fig.set_figheight(width // 100) fig.set_figwidth(width // 100) ax = fig.add_subplot(111, aspect='equal') # hide axes + ticks ax.axison = False # plot the collection collection = Beach(fm, linewidth=linewidth, facecolor=facecolor, edgecolor=edgecolor, bgcolor=bgcolor, alpha=alpha, nofill=nofill, xy=xy, width=plot_width, size=size, zorder=zorder) ax.add_collection(collection) ax.autoscale_view(tight=False, scalex=True, scaley=True) # export if outfile: if format: fig.savefig(outfile, dpi=100, transparent=True, format=format) else: fig.savefig(outfile, dpi=100, transparent=True) elif format and not outfile: imgdata = compatibility.BytesIO() fig.savefig(imgdata, format=format, dpi=100, transparent=True) imgdata.seek(0) return imgdata.read() else: plt.show() return fig
python
def Beachball(fm, linewidth=2, facecolor='b', bgcolor='w', edgecolor='k', alpha=1.0, xy=(0, 0), width=200, size=100, nofill=False, zorder=100, outfile=None, format=None, fig=None): plot_width = width * 0.95 if not fig: fig = plt.figure(figsize=(3, 3), dpi=100) fig.subplots_adjust(left=0, bottom=0, right=1, top=1) fig.set_figheight(width // 100) fig.set_figwidth(width // 100) ax = fig.add_subplot(111, aspect='equal') ax.axison = False collection = Beach(fm, linewidth=linewidth, facecolor=facecolor, edgecolor=edgecolor, bgcolor=bgcolor, alpha=alpha, nofill=nofill, xy=xy, width=plot_width, size=size, zorder=zorder) ax.add_collection(collection) ax.autoscale_view(tight=False, scalex=True, scaley=True) if outfile: if format: fig.savefig(outfile, dpi=100, transparent=True, format=format) else: fig.savefig(outfile, dpi=100, transparent=True) elif format and not outfile: imgdata = compatibility.BytesIO() fig.savefig(imgdata, format=format, dpi=100, transparent=True) imgdata.seek(0) return imgdata.read() else: plt.show() return fig
[ "def", "Beachball", "(", "fm", ",", "linewidth", "=", "2", ",", "facecolor", "=", "'b'", ",", "bgcolor", "=", "'w'", ",", "edgecolor", "=", "'k'", ",", "alpha", "=", "1.0", ",", "xy", "=", "(", "0", ",", "0", ")", ",", "width", "=", "200", ",", "size", "=", "100", ",", "nofill", "=", "False", ",", "zorder", "=", "100", ",", "outfile", "=", "None", ",", "format", "=", "None", ",", "fig", "=", "None", ")", ":", "plot_width", "=", "width", "*", "0.95", "# plot the figure", "if", "not", "fig", ":", "fig", "=", "plt", ".", "figure", "(", "figsize", "=", "(", "3", ",", "3", ")", ",", "dpi", "=", "100", ")", "fig", ".", "subplots_adjust", "(", "left", "=", "0", ",", "bottom", "=", "0", ",", "right", "=", "1", ",", "top", "=", "1", ")", "fig", ".", "set_figheight", "(", "width", "//", "100", ")", "fig", ".", "set_figwidth", "(", "width", "//", "100", ")", "ax", "=", "fig", ".", "add_subplot", "(", "111", ",", "aspect", "=", "'equal'", ")", "# hide axes + ticks", "ax", ".", "axison", "=", "False", "# plot the collection", "collection", "=", "Beach", "(", "fm", ",", "linewidth", "=", "linewidth", ",", "facecolor", "=", "facecolor", ",", "edgecolor", "=", "edgecolor", ",", "bgcolor", "=", "bgcolor", ",", "alpha", "=", "alpha", ",", "nofill", "=", "nofill", ",", "xy", "=", "xy", ",", "width", "=", "plot_width", ",", "size", "=", "size", ",", "zorder", "=", "zorder", ")", "ax", ".", "add_collection", "(", "collection", ")", "ax", ".", "autoscale_view", "(", "tight", "=", "False", ",", "scalex", "=", "True", ",", "scaley", "=", "True", ")", "# export", "if", "outfile", ":", "if", "format", ":", "fig", ".", "savefig", "(", "outfile", ",", "dpi", "=", "100", ",", "transparent", "=", "True", ",", "format", "=", "format", ")", "else", ":", "fig", ".", "savefig", "(", "outfile", ",", "dpi", "=", "100", ",", "transparent", "=", "True", ")", "elif", "format", "and", "not", "outfile", ":", "imgdata", "=", "compatibility", ".", "BytesIO", "(", ")", "fig", ".", "savefig", "(", "imgdata", ",", "format", "=", "format", ",", "dpi", "=", "100", ",", "transparent", "=", "True", ")", "imgdata", ".", "seek", "(", "0", ")", "return", "imgdata", ".", "read", "(", ")", "else", ":", "plt", ".", "show", "(", ")", "return", "fig" ]
Draws a beach ball diagram of an earthquake focal mechanism. S1, D1, and R1, the strike, dip and rake of one of the focal planes, can be vectors of multiple focal mechanisms. :param fm: Focal mechanism that is either number of mechanisms (NM) by 3 (strike, dip, and rake) or NM x 6 (M11, M22, M33, M12, M13, M23 - the six independent components of the moment tensor, where the coordinate system is 1,2,3 = Up,South,East which equals r,theta,phi). The strike is of the first plane, clockwise relative to north. The dip is of the first plane, defined clockwise and perpendicular to strike, relative to horizontal such that 0 is horizontal and 90 is vertical. The rake is of the first focal plane solution. 90 moves the hanging wall up-dip (thrust), 0 moves it in the strike direction (left-lateral), -90 moves it down-dip (normal), and 180 moves it opposite to strike (right-lateral). :param facecolor: Color to use for quadrants of tension; can be a string, e.g. ``'r'``, ``'b'`` or three component color vector, [R G B]. Defaults to ``'b'`` (blue). :param bgcolor: The background color. Defaults to ``'w'`` (white). :param edgecolor: Color of the edges. Defaults to ``'k'`` (black). :param alpha: The alpha level of the beach ball. Defaults to ``1.0`` (opaque). :param xy: Origin position of the beach ball as tuple. Defaults to ``(0, 0)``. :type width: int :param width: Symbol size of beach ball. Defaults to ``200``. :param size: Controls the number of interpolation points for the curves. Minimum is automatically set to ``100``. :param nofill: Do not fill the beach ball, but only plot the planes. :param zorder: Set zorder. Artists with lower zorder values are drawn first. :param outfile: Output file string. Also used to automatically determine the output format. Supported file formats depend on your matplotlib backend. Most backends support png, pdf, ps, eps and svg. Defaults to ``None``. :param format: Format of the graph picture. If no format is given the outfile parameter will be used to try to automatically determine the output format. If no format is found it defaults to png output. If no outfile is specified but a format is, than a binary imagestring will be returned. Defaults to ``None``. :param fig: Give an existing figure instance to plot into. New Figure if set to ``None``.
[ "Draws", "a", "beach", "ball", "diagram", "of", "an", "earthquake", "focal", "mechanism", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L147-L230
gem/oq-engine
openquake/hmtk/plotting/beachball.py
plotMT
def plotMT(T, N, P, size=200, plot_zerotrace=True, x0=0, y0=0, xy=(0, 0), width=200): """ Uses a principal axis T, N and P to draw a beach ball plot. :param ax: axis object of a matplotlib figure :param T: :class:`~PrincipalAxis` :param N: :class:`~PrincipalAxis` :param P: :class:`~PrincipalAxis` Adapted from ps_tensor / utilmeca.c / `Generic Mapping Tools (GMT)`_. .. _`Generic Mapping Tools (GMT)`: http://gmt.soest.hawaii.edu """ # check if one or two widths are specified (Circle or Ellipse) try: assert(len(width) == 2) except TypeError: width = (width, width) collect = [] colors = [] res = [value / float(size) for value in width] b = 1 big_iso = 0 j = 1 j2 = 0 j3 = 0 n = 0 azi = np.zeros((3, 2)) x = np.zeros(400) y = np.zeros(400) x2 = np.zeros(400) y2 = np.zeros(400) x3 = np.zeros(400) y3 = np.zeros(400) xp1 = np.zeros(800) yp1 = np.zeros(800) xp2 = np.zeros(400) yp2 = np.zeros(400) a = np.zeros(3) p = np.zeros(3) v = np.zeros(3) a[0] = T.strike a[1] = N.strike a[2] = P.strike p[0] = T.dip p[1] = N.dip p[2] = P.dip v[0] = T.val v[1] = N.val v[2] = P.val vi = (v[0] + v[1] + v[2]) / 3. for i in range(0, 3): v[i] = v[i] - vi radius_size = size * 0.5 if np.fabs(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) < EPSILON: # pure implosion-explosion if vi > 0.: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('b') if vi < 0.: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('w') return colors, collect if np.fabs(v[0]) >= np.fabs(v[2]): d = 0 m = 2 else: d = 2 m = 0 if (plot_zerotrace): vi = 0. f = -v[1] / float(v[d]) iso = vi / float(v[d]) # Cliff Frohlich, Seismological Research letters, # Vol 7, Number 1, January-February, 1996 # Unless the isotropic parameter lies in the range # between -1 and 1 - f there will be no nodes whatsoever if iso < -1: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('w') return colors, collect elif iso > 1 - f: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('b') return colors, collect spd = np.sin(p[d] * D2R) cpd = np.cos(p[d] * D2R) spb = np.sin(p[b] * D2R) cpb = np.cos(p[b] * D2R) spm = np.sin(p[m] * D2R) cpm = np.cos(p[m] * D2R) sad = np.sin(a[d] * D2R) cad = np.cos(a[d] * D2R) sab = np.sin(a[b] * D2R) cab = np.cos(a[b] * D2R) sam = np.sin(a[m] * D2R) cam = np.cos(a[m] * D2R) for i in range(0, 360): fir = i * D2R s2alphan = (2. + 2. * iso) / \ float(3. + (1. - 2. * f) * np.cos(2. * fir)) if s2alphan > 1.: big_iso += 1 else: alphan = np.arcsin(np.sqrt(s2alphan)) sfi = np.sin(fir) cfi = np.cos(fir) san = np.sin(alphan) can = np.cos(alphan) xz = can * spd + san * sfi * spb + san * cfi * spm xn = can * cpd * cad + san * sfi * cpb * cab + \ san * cfi * cpm * cam xe = can * cpd * sad + san * sfi * cpb * sab + \ san * cfi * cpm * sam if np.fabs(xn) < EPSILON and np.fabs(xe) < EPSILON: takeoff = 0. az = 0. else: az = np.arctan2(xe, xn) if az < 0.: az += np.pi * 2. takeoff = np.arccos(xz / float(np.sqrt(xz * xz + xn * xn + xe * xe))) if takeoff > np.pi / 2.: takeoff = np.pi - takeoff az += np.pi if az > np.pi * 2.: az -= np.pi * 2. r = np.sqrt(2) * np.sin(takeoff / 2.) si = np.sin(az) co = np.cos(az) if i == 0: azi[i][0] = az x[i] = x0 + radius_size * r * si y[i] = y0 + radius_size * r * co azp = az else: if np.fabs(np.fabs(az - azp) - np.pi) < D2R * 10.: azi[n][1] = azp n += 1 azi[n][0] = az if np.fabs(np.fabs(az - azp) - np.pi * 2.) < D2R * 2.: if azp < az: azi[n][0] += np.pi * 2. else: azi[n][0] -= np.pi * 2. if n == 0: x[j] = x0 + radius_size * r * si y[j] = y0 + radius_size * r * co j += 1 elif n == 1: x2[j2] = x0 + radius_size * r * si y2[j2] = y0 + radius_size * r * co j2 += 1 elif n == 2: x3[j3] = x0 + radius_size * r * si y3[j3] = y0 + radius_size * r * co j3 += 1 azp = az azi[n][1] = az if v[1] < 0.: rgb1 = 'b' rgb2 = 'w' else: rgb1 = 'w' rgb2 = 'b' cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append(rgb2) if n == 0: collect.append(xy2patch(x[0:360], y[0:360], res, xy)) colors.append(rgb1) return colors, collect elif n == 1: for i in range(0, j): xp1[i] = x[i] yp1[i] = y[i] if azi[0][0] - azi[0][1] > np.pi: azi[0][0] -= np.pi * 2. elif azi[0][1] - azi[0][0] > np.pi: azi[0][0] += np.pi * 2. if azi[0][0] < azi[0][1]: az = azi[0][1] - D2R while az > azi[0][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si yp1[i] = y0 + radius_size * co i += 1 az -= D2R else: az = azi[0][1] + D2R while az < azi[0][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si yp1[i] = y0 + radius_size * co i += 1 az += D2R collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) for i in range(0, j2): xp2[i] = x2[i] yp2[i] = y2[i] if azi[1][0] - azi[1][1] > np.pi: azi[1][0] -= np.pi * 2. elif azi[1][1] - azi[1][0] > np.pi: azi[1][0] += np.pi * 2. if azi[1][0] < azi[1][1]: az = azi[1][1] - D2R while az > azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az -= D2R else: az = azi[1][1] + D2R while az < azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp2[0:i], yp2[0:i], res, xy)) colors.append(rgb1) return colors, collect elif n == 2: for i in range(0, j3): xp1[i] = x3[i] yp1[i] = y3[i] for ii in range(0, j): xp1[i] = x[ii] i += 1 yp1[i] = y[ii] if big_iso: ii = j2 - 1 while ii >= 0: xp1[i] = x2[ii] i += 1 yp1[i] = y2[ii] ii -= 1 collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) return colors, collect if azi[2][0] - azi[0][1] > np.pi: azi[2][0] -= np.pi * 2. elif azi[0][1] - azi[2][0] > np.pi: azi[2][0] += np.pi * 2. if azi[2][0] < azi[0][1]: az = azi[0][1] - D2R while az > azi[2][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si i += 1 yp1[i] = y0 + radius_size * co az -= D2R else: az = azi[0][1] + D2R while az < azi[2][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si i += 1 yp1[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) for i in range(0, j2): xp2[i] = x2[i] yp2[i] = y2[i] if azi[1][0] - azi[1][1] > np.pi: azi[1][0] -= np.pi * 2. elif azi[1][1] - azi[1][0] > np.pi: azi[1][0] += np.pi * 2. if azi[1][0] < azi[1][1]: az = azi[1][1] - D2R while az > azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az -= D2R else: az = azi[1][1] + D2R while az < azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp2[0:i], yp2[0:i], res, xy)) colors.append(rgb1) return colors, collect
python
def plotMT(T, N, P, size=200, plot_zerotrace=True, x0=0, y0=0, xy=(0, 0), width=200): try: assert(len(width) == 2) except TypeError: width = (width, width) collect = [] colors = [] res = [value / float(size) for value in width] b = 1 big_iso = 0 j = 1 j2 = 0 j3 = 0 n = 0 azi = np.zeros((3, 2)) x = np.zeros(400) y = np.zeros(400) x2 = np.zeros(400) y2 = np.zeros(400) x3 = np.zeros(400) y3 = np.zeros(400) xp1 = np.zeros(800) yp1 = np.zeros(800) xp2 = np.zeros(400) yp2 = np.zeros(400) a = np.zeros(3) p = np.zeros(3) v = np.zeros(3) a[0] = T.strike a[1] = N.strike a[2] = P.strike p[0] = T.dip p[1] = N.dip p[2] = P.dip v[0] = T.val v[1] = N.val v[2] = P.val vi = (v[0] + v[1] + v[2]) / 3. for i in range(0, 3): v[i] = v[i] - vi radius_size = size * 0.5 if np.fabs(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) < EPSILON: if vi > 0.: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('b') if vi < 0.: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('w') return colors, collect if np.fabs(v[0]) >= np.fabs(v[2]): d = 0 m = 2 else: d = 2 m = 0 if (plot_zerotrace): vi = 0. f = -v[1] / float(v[d]) iso = vi / float(v[d]) if iso < -1: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('w') return colors, collect elif iso > 1 - f: cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append('b') return colors, collect spd = np.sin(p[d] * D2R) cpd = np.cos(p[d] * D2R) spb = np.sin(p[b] * D2R) cpb = np.cos(p[b] * D2R) spm = np.sin(p[m] * D2R) cpm = np.cos(p[m] * D2R) sad = np.sin(a[d] * D2R) cad = np.cos(a[d] * D2R) sab = np.sin(a[b] * D2R) cab = np.cos(a[b] * D2R) sam = np.sin(a[m] * D2R) cam = np.cos(a[m] * D2R) for i in range(0, 360): fir = i * D2R s2alphan = (2. + 2. * iso) / \ float(3. + (1. - 2. * f) * np.cos(2. * fir)) if s2alphan > 1.: big_iso += 1 else: alphan = np.arcsin(np.sqrt(s2alphan)) sfi = np.sin(fir) cfi = np.cos(fir) san = np.sin(alphan) can = np.cos(alphan) xz = can * spd + san * sfi * spb + san * cfi * spm xn = can * cpd * cad + san * sfi * cpb * cab + \ san * cfi * cpm * cam xe = can * cpd * sad + san * sfi * cpb * sab + \ san * cfi * cpm * sam if np.fabs(xn) < EPSILON and np.fabs(xe) < EPSILON: takeoff = 0. az = 0. else: az = np.arctan2(xe, xn) if az < 0.: az += np.pi * 2. takeoff = np.arccos(xz / float(np.sqrt(xz * xz + xn * xn + xe * xe))) if takeoff > np.pi / 2.: takeoff = np.pi - takeoff az += np.pi if az > np.pi * 2.: az -= np.pi * 2. r = np.sqrt(2) * np.sin(takeoff / 2.) si = np.sin(az) co = np.cos(az) if i == 0: azi[i][0] = az x[i] = x0 + radius_size * r * si y[i] = y0 + radius_size * r * co azp = az else: if np.fabs(np.fabs(az - azp) - np.pi) < D2R * 10.: azi[n][1] = azp n += 1 azi[n][0] = az if np.fabs(np.fabs(az - azp) - np.pi * 2.) < D2R * 2.: if azp < az: azi[n][0] += np.pi * 2. else: azi[n][0] -= np.pi * 2. if n == 0: x[j] = x0 + radius_size * r * si y[j] = y0 + radius_size * r * co j += 1 elif n == 1: x2[j2] = x0 + radius_size * r * si y2[j2] = y0 + radius_size * r * co j2 += 1 elif n == 2: x3[j3] = x0 + radius_size * r * si y3[j3] = y0 + radius_size * r * co j3 += 1 azp = az azi[n][1] = az if v[1] < 0.: rgb1 = 'b' rgb2 = 'w' else: rgb1 = 'w' rgb2 = 'b' cir = patches.Ellipse(xy, width=width[0], height=width[1]) collect.append(cir) colors.append(rgb2) if n == 0: collect.append(xy2patch(x[0:360], y[0:360], res, xy)) colors.append(rgb1) return colors, collect elif n == 1: for i in range(0, j): xp1[i] = x[i] yp1[i] = y[i] if azi[0][0] - azi[0][1] > np.pi: azi[0][0] -= np.pi * 2. elif azi[0][1] - azi[0][0] > np.pi: azi[0][0] += np.pi * 2. if azi[0][0] < azi[0][1]: az = azi[0][1] - D2R while az > azi[0][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si yp1[i] = y0 + radius_size * co i += 1 az -= D2R else: az = azi[0][1] + D2R while az < azi[0][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si yp1[i] = y0 + radius_size * co i += 1 az += D2R collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) for i in range(0, j2): xp2[i] = x2[i] yp2[i] = y2[i] if azi[1][0] - azi[1][1] > np.pi: azi[1][0] -= np.pi * 2. elif azi[1][1] - azi[1][0] > np.pi: azi[1][0] += np.pi * 2. if azi[1][0] < azi[1][1]: az = azi[1][1] - D2R while az > azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az -= D2R else: az = azi[1][1] + D2R while az < azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp2[0:i], yp2[0:i], res, xy)) colors.append(rgb1) return colors, collect elif n == 2: for i in range(0, j3): xp1[i] = x3[i] yp1[i] = y3[i] for ii in range(0, j): xp1[i] = x[ii] i += 1 yp1[i] = y[ii] if big_iso: ii = j2 - 1 while ii >= 0: xp1[i] = x2[ii] i += 1 yp1[i] = y2[ii] ii -= 1 collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) return colors, collect if azi[2][0] - azi[0][1] > np.pi: azi[2][0] -= np.pi * 2. elif azi[0][1] - azi[2][0] > np.pi: azi[2][0] += np.pi * 2. if azi[2][0] < azi[0][1]: az = azi[0][1] - D2R while az > azi[2][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si i += 1 yp1[i] = y0 + radius_size * co az -= D2R else: az = azi[0][1] + D2R while az < azi[2][0]: si = np.sin(az) co = np.cos(az) xp1[i] = x0 + radius_size * si i += 1 yp1[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp1[0:i], yp1[0:i], res, xy)) colors.append(rgb1) for i in range(0, j2): xp2[i] = x2[i] yp2[i] = y2[i] if azi[1][0] - azi[1][1] > np.pi: azi[1][0] -= np.pi * 2. elif azi[1][1] - azi[1][0] > np.pi: azi[1][0] += np.pi * 2. if azi[1][0] < azi[1][1]: az = azi[1][1] - D2R while az > azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az -= D2R else: az = azi[1][1] + D2R while az < azi[1][0]: si = np.sin(az) co = np.cos(az) xp2[i] = x0 + radius_size * si i += 1 yp2[i] = y0 + radius_size * co az += D2R collect.append(xy2patch(xp2[0:i], yp2[0:i], res, xy)) colors.append(rgb1) return colors, collect
[ "def", "plotMT", "(", "T", ",", "N", ",", "P", ",", "size", "=", "200", ",", "plot_zerotrace", "=", "True", ",", "x0", "=", "0", ",", "y0", "=", "0", ",", "xy", "=", "(", "0", ",", "0", ")", ",", "width", "=", "200", ")", ":", "# check if one or two widths are specified (Circle or Ellipse)", "try", ":", "assert", "(", "len", "(", "width", ")", "==", "2", ")", "except", "TypeError", ":", "width", "=", "(", "width", ",", "width", ")", "collect", "=", "[", "]", "colors", "=", "[", "]", "res", "=", "[", "value", "/", "float", "(", "size", ")", "for", "value", "in", "width", "]", "b", "=", "1", "big_iso", "=", "0", "j", "=", "1", "j2", "=", "0", "j3", "=", "0", "n", "=", "0", "azi", "=", "np", ".", "zeros", "(", "(", "3", ",", "2", ")", ")", "x", "=", "np", ".", "zeros", "(", "400", ")", "y", "=", "np", ".", "zeros", "(", "400", ")", "x2", "=", "np", ".", "zeros", "(", "400", ")", "y2", "=", "np", ".", "zeros", "(", "400", ")", "x3", "=", "np", ".", "zeros", "(", "400", ")", "y3", "=", "np", ".", "zeros", "(", "400", ")", "xp1", "=", "np", ".", "zeros", "(", "800", ")", "yp1", "=", "np", ".", "zeros", "(", "800", ")", "xp2", "=", "np", ".", "zeros", "(", "400", ")", "yp2", "=", "np", ".", "zeros", "(", "400", ")", "a", "=", "np", ".", "zeros", "(", "3", ")", "p", "=", "np", ".", "zeros", "(", "3", ")", "v", "=", "np", ".", "zeros", "(", "3", ")", "a", "[", "0", "]", "=", "T", ".", "strike", "a", "[", "1", "]", "=", "N", ".", "strike", "a", "[", "2", "]", "=", "P", ".", "strike", "p", "[", "0", "]", "=", "T", ".", "dip", "p", "[", "1", "]", "=", "N", ".", "dip", "p", "[", "2", "]", "=", "P", ".", "dip", "v", "[", "0", "]", "=", "T", ".", "val", "v", "[", "1", "]", "=", "N", ".", "val", "v", "[", "2", "]", "=", "P", ".", "val", "vi", "=", "(", "v", "[", "0", "]", "+", "v", "[", "1", "]", "+", "v", "[", "2", "]", ")", "/", "3.", "for", "i", "in", "range", "(", "0", ",", "3", ")", ":", "v", "[", "i", "]", "=", "v", "[", "i", "]", "-", "vi", "radius_size", "=", "size", "*", "0.5", "if", "np", ".", "fabs", "(", "v", "[", "0", "]", "*", "v", "[", "0", "]", "+", "v", "[", "1", "]", "*", "v", "[", "1", "]", "+", "v", "[", "2", "]", "*", "v", "[", "2", "]", ")", "<", "EPSILON", ":", "# pure implosion-explosion", "if", "vi", ">", "0.", ":", "cir", "=", "patches", ".", "Ellipse", "(", "xy", ",", "width", "=", "width", "[", "0", "]", ",", "height", "=", "width", "[", "1", "]", ")", "collect", ".", "append", "(", "cir", ")", "colors", ".", "append", "(", "'b'", ")", "if", "vi", "<", "0.", ":", "cir", "=", "patches", ".", "Ellipse", "(", "xy", ",", "width", "=", "width", "[", "0", "]", ",", "height", "=", "width", "[", "1", "]", ")", "collect", ".", "append", "(", "cir", ")", "colors", ".", "append", "(", "'w'", ")", "return", "colors", ",", "collect", "if", "np", ".", "fabs", "(", "v", "[", "0", "]", ")", ">=", "np", ".", "fabs", "(", "v", "[", "2", "]", ")", ":", "d", "=", "0", "m", "=", "2", "else", ":", "d", "=", "2", "m", "=", "0", "if", "(", "plot_zerotrace", ")", ":", "vi", "=", "0.", "f", "=", "-", "v", "[", "1", "]", "/", "float", "(", "v", "[", "d", "]", ")", "iso", "=", "vi", "/", "float", "(", "v", "[", "d", "]", ")", "# Cliff Frohlich, Seismological Research letters,", "# Vol 7, Number 1, January-February, 1996", "# Unless the isotropic parameter lies in the range", "# between -1 and 1 - f there will be no nodes whatsoever", "if", "iso", "<", "-", "1", ":", "cir", "=", "patches", ".", "Ellipse", "(", "xy", ",", "width", "=", "width", "[", "0", "]", ",", "height", "=", "width", "[", "1", "]", ")", "collect", ".", "append", "(", "cir", ")", "colors", ".", "append", "(", "'w'", ")", "return", "colors", ",", "collect", "elif", "iso", ">", "1", "-", "f", ":", "cir", "=", "patches", ".", "Ellipse", "(", "xy", ",", "width", "=", "width", "[", "0", "]", ",", "height", "=", "width", "[", "1", "]", ")", "collect", ".", "append", "(", "cir", ")", "colors", ".", "append", "(", "'b'", ")", "return", "colors", ",", "collect", "spd", "=", "np", ".", "sin", "(", "p", "[", "d", "]", "*", "D2R", ")", "cpd", "=", "np", ".", "cos", "(", "p", "[", "d", "]", "*", "D2R", ")", "spb", "=", "np", ".", "sin", "(", "p", "[", "b", "]", "*", "D2R", ")", "cpb", "=", "np", ".", "cos", "(", "p", "[", "b", "]", "*", "D2R", ")", "spm", "=", "np", ".", "sin", "(", "p", "[", "m", "]", "*", "D2R", ")", "cpm", "=", "np", ".", "cos", "(", "p", "[", "m", "]", "*", "D2R", ")", "sad", "=", "np", ".", "sin", "(", "a", "[", "d", "]", "*", "D2R", ")", "cad", "=", "np", ".", "cos", "(", "a", "[", "d", "]", "*", "D2R", ")", "sab", "=", "np", ".", "sin", "(", "a", "[", "b", "]", "*", "D2R", ")", "cab", "=", "np", ".", "cos", "(", "a", "[", "b", "]", "*", "D2R", ")", "sam", "=", "np", ".", "sin", "(", "a", "[", "m", "]", "*", "D2R", ")", "cam", "=", "np", ".", "cos", "(", "a", "[", "m", "]", "*", "D2R", ")", "for", "i", "in", "range", "(", "0", ",", "360", ")", ":", "fir", "=", "i", "*", "D2R", "s2alphan", "=", "(", "2.", "+", "2.", "*", "iso", ")", "/", "float", "(", "3.", "+", "(", "1.", "-", "2.", "*", "f", ")", "*", "np", ".", "cos", "(", "2.", "*", "fir", ")", ")", "if", "s2alphan", ">", "1.", ":", "big_iso", "+=", "1", "else", ":", "alphan", "=", "np", ".", "arcsin", "(", "np", ".", "sqrt", "(", "s2alphan", ")", ")", "sfi", "=", "np", ".", "sin", "(", "fir", ")", "cfi", "=", "np", ".", "cos", "(", "fir", ")", "san", "=", "np", ".", "sin", "(", "alphan", ")", "can", "=", "np", ".", "cos", "(", "alphan", ")", "xz", "=", "can", "*", "spd", "+", "san", "*", "sfi", "*", "spb", "+", "san", "*", "cfi", "*", "spm", "xn", "=", "can", "*", "cpd", "*", "cad", "+", "san", "*", "sfi", "*", "cpb", "*", "cab", "+", "san", "*", "cfi", "*", "cpm", "*", "cam", "xe", "=", "can", "*", "cpd", "*", "sad", "+", "san", "*", "sfi", "*", "cpb", "*", "sab", "+", "san", "*", "cfi", "*", "cpm", "*", "sam", "if", "np", ".", "fabs", "(", "xn", ")", "<", "EPSILON", "and", "np", ".", "fabs", "(", "xe", ")", "<", "EPSILON", ":", "takeoff", "=", "0.", "az", "=", "0.", "else", ":", "az", "=", "np", ".", "arctan2", "(", "xe", ",", "xn", ")", "if", "az", "<", "0.", ":", "az", "+=", "np", ".", "pi", "*", "2.", "takeoff", "=", "np", ".", "arccos", "(", "xz", "/", "float", "(", "np", ".", "sqrt", "(", "xz", "*", "xz", "+", "xn", "*", "xn", "+", "xe", "*", "xe", ")", ")", ")", "if", "takeoff", ">", "np", ".", "pi", "/", "2.", ":", "takeoff", "=", "np", ".", "pi", "-", "takeoff", "az", "+=", "np", ".", "pi", "if", "az", ">", "np", ".", "pi", "*", "2.", ":", "az", "-=", "np", ".", "pi", "*", "2.", "r", "=", "np", ".", "sqrt", "(", "2", ")", "*", "np", ".", "sin", "(", "takeoff", "/", "2.", ")", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "if", "i", "==", "0", ":", "azi", "[", "i", "]", "[", "0", "]", "=", "az", "x", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "r", "*", "si", "y", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "r", "*", "co", "azp", "=", "az", "else", ":", "if", "np", ".", "fabs", "(", "np", ".", "fabs", "(", "az", "-", "azp", ")", "-", "np", ".", "pi", ")", "<", "D2R", "*", "10.", ":", "azi", "[", "n", "]", "[", "1", "]", "=", "azp", "n", "+=", "1", "azi", "[", "n", "]", "[", "0", "]", "=", "az", "if", "np", ".", "fabs", "(", "np", ".", "fabs", "(", "az", "-", "azp", ")", "-", "np", ".", "pi", "*", "2.", ")", "<", "D2R", "*", "2.", ":", "if", "azp", "<", "az", ":", "azi", "[", "n", "]", "[", "0", "]", "+=", "np", ".", "pi", "*", "2.", "else", ":", "azi", "[", "n", "]", "[", "0", "]", "-=", "np", ".", "pi", "*", "2.", "if", "n", "==", "0", ":", "x", "[", "j", "]", "=", "x0", "+", "radius_size", "*", "r", "*", "si", "y", "[", "j", "]", "=", "y0", "+", "radius_size", "*", "r", "*", "co", "j", "+=", "1", "elif", "n", "==", "1", ":", "x2", "[", "j2", "]", "=", "x0", "+", "radius_size", "*", "r", "*", "si", "y2", "[", "j2", "]", "=", "y0", "+", "radius_size", "*", "r", "*", "co", "j2", "+=", "1", "elif", "n", "==", "2", ":", "x3", "[", "j3", "]", "=", "x0", "+", "radius_size", "*", "r", "*", "si", "y3", "[", "j3", "]", "=", "y0", "+", "radius_size", "*", "r", "*", "co", "j3", "+=", "1", "azp", "=", "az", "azi", "[", "n", "]", "[", "1", "]", "=", "az", "if", "v", "[", "1", "]", "<", "0.", ":", "rgb1", "=", "'b'", "rgb2", "=", "'w'", "else", ":", "rgb1", "=", "'w'", "rgb2", "=", "'b'", "cir", "=", "patches", ".", "Ellipse", "(", "xy", ",", "width", "=", "width", "[", "0", "]", ",", "height", "=", "width", "[", "1", "]", ")", "collect", ".", "append", "(", "cir", ")", "colors", ".", "append", "(", "rgb2", ")", "if", "n", "==", "0", ":", "collect", ".", "append", "(", "xy2patch", "(", "x", "[", "0", ":", "360", "]", ",", "y", "[", "0", ":", "360", "]", ",", "res", ",", "xy", ")", ")", "colors", ".", "append", "(", "rgb1", ")", "return", "colors", ",", "collect", "elif", "n", "==", "1", ":", "for", "i", "in", "range", "(", "0", ",", "j", ")", ":", "xp1", "[", "i", "]", "=", "x", "[", "i", "]", "yp1", "[", "i", "]", "=", "y", "[", "i", "]", "if", "azi", "[", "0", "]", "[", "0", "]", "-", "azi", "[", "0", "]", "[", "1", "]", ">", "np", ".", "pi", ":", "azi", "[", "0", "]", "[", "0", "]", "-=", "np", ".", "pi", "*", "2.", "elif", "azi", "[", "0", "]", "[", "1", "]", "-", "azi", "[", "0", "]", "[", "0", "]", ">", "np", ".", "pi", ":", "azi", "[", "0", "]", "[", "0", "]", "+=", "np", ".", "pi", "*", "2.", "if", "azi", "[", "0", "]", "[", "0", "]", "<", "azi", "[", "0", "]", "[", "1", "]", ":", "az", "=", "azi", "[", "0", "]", "[", "1", "]", "-", "D2R", "while", "az", ">", "azi", "[", "0", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp1", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "yp1", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "i", "+=", "1", "az", "-=", "D2R", "else", ":", "az", "=", "azi", "[", "0", "]", "[", "1", "]", "+", "D2R", "while", "az", "<", "azi", "[", "0", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp1", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "yp1", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "i", "+=", "1", "az", "+=", "D2R", "collect", ".", "append", "(", "xy2patch", "(", "xp1", "[", "0", ":", "i", "]", ",", "yp1", "[", "0", ":", "i", "]", ",", "res", ",", "xy", ")", ")", "colors", ".", "append", "(", "rgb1", ")", "for", "i", "in", "range", "(", "0", ",", "j2", ")", ":", "xp2", "[", "i", "]", "=", "x2", "[", "i", "]", "yp2", "[", "i", "]", "=", "y2", "[", "i", "]", "if", "azi", "[", "1", "]", "[", "0", "]", "-", "azi", "[", "1", "]", "[", "1", "]", ">", "np", ".", "pi", ":", "azi", "[", "1", "]", "[", "0", "]", "-=", "np", ".", "pi", "*", "2.", "elif", "azi", "[", "1", "]", "[", "1", "]", "-", "azi", "[", "1", "]", "[", "0", "]", ">", "np", ".", "pi", ":", "azi", "[", "1", "]", "[", "0", "]", "+=", "np", ".", "pi", "*", "2.", "if", "azi", "[", "1", "]", "[", "0", "]", "<", "azi", "[", "1", "]", "[", "1", "]", ":", "az", "=", "azi", "[", "1", "]", "[", "1", "]", "-", "D2R", "while", "az", ">", "azi", "[", "1", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp2", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "i", "+=", "1", "yp2", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "az", "-=", "D2R", "else", ":", "az", "=", "azi", "[", "1", "]", "[", "1", "]", "+", "D2R", "while", "az", "<", "azi", "[", "1", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp2", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "i", "+=", "1", "yp2", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "az", "+=", "D2R", "collect", ".", "append", "(", "xy2patch", "(", "xp2", "[", "0", ":", "i", "]", ",", "yp2", "[", "0", ":", "i", "]", ",", "res", ",", "xy", ")", ")", "colors", ".", "append", "(", "rgb1", ")", "return", "colors", ",", "collect", "elif", "n", "==", "2", ":", "for", "i", "in", "range", "(", "0", ",", "j3", ")", ":", "xp1", "[", "i", "]", "=", "x3", "[", "i", "]", "yp1", "[", "i", "]", "=", "y3", "[", "i", "]", "for", "ii", "in", "range", "(", "0", ",", "j", ")", ":", "xp1", "[", "i", "]", "=", "x", "[", "ii", "]", "i", "+=", "1", "yp1", "[", "i", "]", "=", "y", "[", "ii", "]", "if", "big_iso", ":", "ii", "=", "j2", "-", "1", "while", "ii", ">=", "0", ":", "xp1", "[", "i", "]", "=", "x2", "[", "ii", "]", "i", "+=", "1", "yp1", "[", "i", "]", "=", "y2", "[", "ii", "]", "ii", "-=", "1", "collect", ".", "append", "(", "xy2patch", "(", "xp1", "[", "0", ":", "i", "]", ",", "yp1", "[", "0", ":", "i", "]", ",", "res", ",", "xy", ")", ")", "colors", ".", "append", "(", "rgb1", ")", "return", "colors", ",", "collect", "if", "azi", "[", "2", "]", "[", "0", "]", "-", "azi", "[", "0", "]", "[", "1", "]", ">", "np", ".", "pi", ":", "azi", "[", "2", "]", "[", "0", "]", "-=", "np", ".", "pi", "*", "2.", "elif", "azi", "[", "0", "]", "[", "1", "]", "-", "azi", "[", "2", "]", "[", "0", "]", ">", "np", ".", "pi", ":", "azi", "[", "2", "]", "[", "0", "]", "+=", "np", ".", "pi", "*", "2.", "if", "azi", "[", "2", "]", "[", "0", "]", "<", "azi", "[", "0", "]", "[", "1", "]", ":", "az", "=", "azi", "[", "0", "]", "[", "1", "]", "-", "D2R", "while", "az", ">", "azi", "[", "2", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp1", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "i", "+=", "1", "yp1", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "az", "-=", "D2R", "else", ":", "az", "=", "azi", "[", "0", "]", "[", "1", "]", "+", "D2R", "while", "az", "<", "azi", "[", "2", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp1", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "i", "+=", "1", "yp1", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "az", "+=", "D2R", "collect", ".", "append", "(", "xy2patch", "(", "xp1", "[", "0", ":", "i", "]", ",", "yp1", "[", "0", ":", "i", "]", ",", "res", ",", "xy", ")", ")", "colors", ".", "append", "(", "rgb1", ")", "for", "i", "in", "range", "(", "0", ",", "j2", ")", ":", "xp2", "[", "i", "]", "=", "x2", "[", "i", "]", "yp2", "[", "i", "]", "=", "y2", "[", "i", "]", "if", "azi", "[", "1", "]", "[", "0", "]", "-", "azi", "[", "1", "]", "[", "1", "]", ">", "np", ".", "pi", ":", "azi", "[", "1", "]", "[", "0", "]", "-=", "np", ".", "pi", "*", "2.", "elif", "azi", "[", "1", "]", "[", "1", "]", "-", "azi", "[", "1", "]", "[", "0", "]", ">", "np", ".", "pi", ":", "azi", "[", "1", "]", "[", "0", "]", "+=", "np", ".", "pi", "*", "2.", "if", "azi", "[", "1", "]", "[", "0", "]", "<", "azi", "[", "1", "]", "[", "1", "]", ":", "az", "=", "azi", "[", "1", "]", "[", "1", "]", "-", "D2R", "while", "az", ">", "azi", "[", "1", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp2", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "i", "+=", "1", "yp2", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "az", "-=", "D2R", "else", ":", "az", "=", "azi", "[", "1", "]", "[", "1", "]", "+", "D2R", "while", "az", "<", "azi", "[", "1", "]", "[", "0", "]", ":", "si", "=", "np", ".", "sin", "(", "az", ")", "co", "=", "np", ".", "cos", "(", "az", ")", "xp2", "[", "i", "]", "=", "x0", "+", "radius_size", "*", "si", "i", "+=", "1", "yp2", "[", "i", "]", "=", "y0", "+", "radius_size", "*", "co", "az", "+=", "D2R", "collect", ".", "append", "(", "xy2patch", "(", "xp2", "[", "0", ":", "i", "]", ",", "yp2", "[", "0", ":", "i", "]", ",", "res", ",", "xy", ")", ")", "colors", ".", "append", "(", "rgb1", ")", "return", "colors", ",", "collect" ]
Uses a principal axis T, N and P to draw a beach ball plot. :param ax: axis object of a matplotlib figure :param T: :class:`~PrincipalAxis` :param N: :class:`~PrincipalAxis` :param P: :class:`~PrincipalAxis` Adapted from ps_tensor / utilmeca.c / `Generic Mapping Tools (GMT)`_. .. _`Generic Mapping Tools (GMT)`: http://gmt.soest.hawaii.edu
[ "Uses", "a", "principal", "axis", "T", "N", "and", "P", "to", "draw", "a", "beach", "ball", "plot", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L233-L553
gem/oq-engine
openquake/hmtk/plotting/beachball.py
plotDC
def plotDC(np1, size=200, xy=(0, 0), width=200): """ Uses one nodal plane of a double couple to draw a beach ball plot. :param ax: axis object of a matplotlib figure :param np1: :class:`~NodalPlane` Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ # check if one or two widths are specified (Circle or Ellipse) try: assert(len(width) == 2) except TypeError: width = (width, width) S1 = np1.strike D1 = np1.dip R1 = np1.rake M = 0 if R1 > 180: R1 -= 180 M = 1 if R1 < 0: R1 += 180 M = 1 # Get azimuth and dip of second plane (S2, D2, _R2) = AuxPlane(S1, D1, R1) D = size / 2 if D1 >= 90: D1 = 89.9999 if D2 >= 90: D2 = 89.9999 # arange checked for numerical stablility, np.pi is not multiple of 0.1 phi = np.arange(0, np.pi, .01) l1 = np.sqrt( np.power(90 - D1, 2) / ( np.power(np.sin(phi), 2) + np.power(np.cos(phi), 2) * np.power(90 - D1, 2) / np.power(90, 2))) l2 = np.sqrt( np.power(90 - D2, 2) / ( np.power(np.sin(phi), 2) + np.power(np.cos(phi), 2) * np.power(90 - D2, 2) / np.power(90, 2))) inc = 1 (X1, Y1) = Pol2Cart(phi + S1 * D2R, l1) if M == 1: lo = S1 - 180 hi = S2 if lo > hi: inc = -1 th1 = np.arange(S1 - 180, S2, inc) (Xs1, Ys1) = Pol2Cart(th1 * D2R, 90 * np.ones((1, len(th1)))) (X2, Y2) = Pol2Cart(phi + S2 * D2R, l2) th2 = np.arange(S2 + 180, S1, -inc) else: hi = S1 - 180 lo = S2 - 180 if lo > hi: inc = -1 th1 = np.arange(hi, lo, -inc) (Xs1, Ys1) = Pol2Cart(th1 * D2R, 90 * np.ones((1, len(th1)))) (X2, Y2) = Pol2Cart(phi + S2 * D2R, l2) X2 = X2[::-1] Y2 = Y2[::-1] th2 = np.arange(S2, S1, inc) (Xs2, Ys2) = Pol2Cart(th2 * D2R, 90 * np.ones((1, len(th2)))) X = np.concatenate((X1, Xs1[0], X2, Xs2[0])) Y = np.concatenate((Y1, Ys1[0], Y2, Ys2[0])) X = X * D / 90 Y = Y * D / 90 # calculate resolution res = [value / float(size) for value in width] # construct the patches collect = [patches.Ellipse(xy, width=width[0], height=width[1])] collect.append(xy2patch(Y, X, res, xy)) return ['b', 'w'], collect
python
def plotDC(np1, size=200, xy=(0, 0), width=200): try: assert(len(width) == 2) except TypeError: width = (width, width) S1 = np1.strike D1 = np1.dip R1 = np1.rake M = 0 if R1 > 180: R1 -= 180 M = 1 if R1 < 0: R1 += 180 M = 1 (S2, D2, _R2) = AuxPlane(S1, D1, R1) D = size / 2 if D1 >= 90: D1 = 89.9999 if D2 >= 90: D2 = 89.9999 phi = np.arange(0, np.pi, .01) l1 = np.sqrt( np.power(90 - D1, 2) / ( np.power(np.sin(phi), 2) + np.power(np.cos(phi), 2) * np.power(90 - D1, 2) / np.power(90, 2))) l2 = np.sqrt( np.power(90 - D2, 2) / ( np.power(np.sin(phi), 2) + np.power(np.cos(phi), 2) * np.power(90 - D2, 2) / np.power(90, 2))) inc = 1 (X1, Y1) = Pol2Cart(phi + S1 * D2R, l1) if M == 1: lo = S1 - 180 hi = S2 if lo > hi: inc = -1 th1 = np.arange(S1 - 180, S2, inc) (Xs1, Ys1) = Pol2Cart(th1 * D2R, 90 * np.ones((1, len(th1)))) (X2, Y2) = Pol2Cart(phi + S2 * D2R, l2) th2 = np.arange(S2 + 180, S1, -inc) else: hi = S1 - 180 lo = S2 - 180 if lo > hi: inc = -1 th1 = np.arange(hi, lo, -inc) (Xs1, Ys1) = Pol2Cart(th1 * D2R, 90 * np.ones((1, len(th1)))) (X2, Y2) = Pol2Cart(phi + S2 * D2R, l2) X2 = X2[::-1] Y2 = Y2[::-1] th2 = np.arange(S2, S1, inc) (Xs2, Ys2) = Pol2Cart(th2 * D2R, 90 * np.ones((1, len(th2)))) X = np.concatenate((X1, Xs1[0], X2, Xs2[0])) Y = np.concatenate((Y1, Ys1[0], Y2, Ys2[0])) X = X * D / 90 Y = Y * D / 90 res = [value / float(size) for value in width] collect = [patches.Ellipse(xy, width=width[0], height=width[1])] collect.append(xy2patch(Y, X, res, xy)) return ['b', 'w'], collect
[ "def", "plotDC", "(", "np1", ",", "size", "=", "200", ",", "xy", "=", "(", "0", ",", "0", ")", ",", "width", "=", "200", ")", ":", "# check if one or two widths are specified (Circle or Ellipse)", "try", ":", "assert", "(", "len", "(", "width", ")", "==", "2", ")", "except", "TypeError", ":", "width", "=", "(", "width", ",", "width", ")", "S1", "=", "np1", ".", "strike", "D1", "=", "np1", ".", "dip", "R1", "=", "np1", ".", "rake", "M", "=", "0", "if", "R1", ">", "180", ":", "R1", "-=", "180", "M", "=", "1", "if", "R1", "<", "0", ":", "R1", "+=", "180", "M", "=", "1", "# Get azimuth and dip of second plane", "(", "S2", ",", "D2", ",", "_R2", ")", "=", "AuxPlane", "(", "S1", ",", "D1", ",", "R1", ")", "D", "=", "size", "/", "2", "if", "D1", ">=", "90", ":", "D1", "=", "89.9999", "if", "D2", ">=", "90", ":", "D2", "=", "89.9999", "# arange checked for numerical stablility, np.pi is not multiple of 0.1", "phi", "=", "np", ".", "arange", "(", "0", ",", "np", ".", "pi", ",", ".01", ")", "l1", "=", "np", ".", "sqrt", "(", "np", ".", "power", "(", "90", "-", "D1", ",", "2", ")", "/", "(", "np", ".", "power", "(", "np", ".", "sin", "(", "phi", ")", ",", "2", ")", "+", "np", ".", "power", "(", "np", ".", "cos", "(", "phi", ")", ",", "2", ")", "*", "np", ".", "power", "(", "90", "-", "D1", ",", "2", ")", "/", "np", ".", "power", "(", "90", ",", "2", ")", ")", ")", "l2", "=", "np", ".", "sqrt", "(", "np", ".", "power", "(", "90", "-", "D2", ",", "2", ")", "/", "(", "np", ".", "power", "(", "np", ".", "sin", "(", "phi", ")", ",", "2", ")", "+", "np", ".", "power", "(", "np", ".", "cos", "(", "phi", ")", ",", "2", ")", "*", "np", ".", "power", "(", "90", "-", "D2", ",", "2", ")", "/", "np", ".", "power", "(", "90", ",", "2", ")", ")", ")", "inc", "=", "1", "(", "X1", ",", "Y1", ")", "=", "Pol2Cart", "(", "phi", "+", "S1", "*", "D2R", ",", "l1", ")", "if", "M", "==", "1", ":", "lo", "=", "S1", "-", "180", "hi", "=", "S2", "if", "lo", ">", "hi", ":", "inc", "=", "-", "1", "th1", "=", "np", ".", "arange", "(", "S1", "-", "180", ",", "S2", ",", "inc", ")", "(", "Xs1", ",", "Ys1", ")", "=", "Pol2Cart", "(", "th1", "*", "D2R", ",", "90", "*", "np", ".", "ones", "(", "(", "1", ",", "len", "(", "th1", ")", ")", ")", ")", "(", "X2", ",", "Y2", ")", "=", "Pol2Cart", "(", "phi", "+", "S2", "*", "D2R", ",", "l2", ")", "th2", "=", "np", ".", "arange", "(", "S2", "+", "180", ",", "S1", ",", "-", "inc", ")", "else", ":", "hi", "=", "S1", "-", "180", "lo", "=", "S2", "-", "180", "if", "lo", ">", "hi", ":", "inc", "=", "-", "1", "th1", "=", "np", ".", "arange", "(", "hi", ",", "lo", ",", "-", "inc", ")", "(", "Xs1", ",", "Ys1", ")", "=", "Pol2Cart", "(", "th1", "*", "D2R", ",", "90", "*", "np", ".", "ones", "(", "(", "1", ",", "len", "(", "th1", ")", ")", ")", ")", "(", "X2", ",", "Y2", ")", "=", "Pol2Cart", "(", "phi", "+", "S2", "*", "D2R", ",", "l2", ")", "X2", "=", "X2", "[", ":", ":", "-", "1", "]", "Y2", "=", "Y2", "[", ":", ":", "-", "1", "]", "th2", "=", "np", ".", "arange", "(", "S2", ",", "S1", ",", "inc", ")", "(", "Xs2", ",", "Ys2", ")", "=", "Pol2Cart", "(", "th2", "*", "D2R", ",", "90", "*", "np", ".", "ones", "(", "(", "1", ",", "len", "(", "th2", ")", ")", ")", ")", "X", "=", "np", ".", "concatenate", "(", "(", "X1", ",", "Xs1", "[", "0", "]", ",", "X2", ",", "Xs2", "[", "0", "]", ")", ")", "Y", "=", "np", ".", "concatenate", "(", "(", "Y1", ",", "Ys1", "[", "0", "]", ",", "Y2", ",", "Ys2", "[", "0", "]", ")", ")", "X", "=", "X", "*", "D", "/", "90", "Y", "=", "Y", "*", "D", "/", "90", "# calculate resolution", "res", "=", "[", "value", "/", "float", "(", "size", ")", "for", "value", "in", "width", "]", "# construct the patches", "collect", "=", "[", "patches", ".", "Ellipse", "(", "xy", ",", "width", "=", "width", "[", "0", "]", ",", "height", "=", "width", "[", "1", "]", ")", "]", "collect", ".", "append", "(", "xy2patch", "(", "Y", ",", "X", ",", "res", ",", "xy", ")", ")", "return", "[", "'b'", ",", "'w'", "]", ",", "collect" ]
Uses one nodal plane of a double couple to draw a beach ball plot. :param ax: axis object of a matplotlib figure :param np1: :class:`~NodalPlane` Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd.
[ "Uses", "one", "nodal", "plane", "of", "a", "double", "couple", "to", "draw", "a", "beach", "ball", "plot", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L556-L641
gem/oq-engine
openquake/hmtk/plotting/beachball.py
StrikeDip
def StrikeDip(n, e, u): """ Finds strike and dip of plane given normal vector having components n, e, and u. Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ r2d = 180 / np.pi if u < 0: n = -n e = -e u = -u strike = np.arctan2(e, n) * r2d strike = strike - 90 while strike >= 360: strike = strike - 360 while strike < 0: strike = strike + 360 x = np.sqrt(np.power(n, 2) + np.power(e, 2)) dip = np.arctan2(x, u) * r2d return (strike, dip)
python
def StrikeDip(n, e, u): r2d = 180 / np.pi if u < 0: n = -n e = -e u = -u strike = np.arctan2(e, n) * r2d strike = strike - 90 while strike >= 360: strike = strike - 360 while strike < 0: strike = strike + 360 x = np.sqrt(np.power(n, 2) + np.power(e, 2)) dip = np.arctan2(x, u) * r2d return (strike, dip)
[ "def", "StrikeDip", "(", "n", ",", "e", ",", "u", ")", ":", "r2d", "=", "180", "/", "np", ".", "pi", "if", "u", "<", "0", ":", "n", "=", "-", "n", "e", "=", "-", "e", "u", "=", "-", "u", "strike", "=", "np", ".", "arctan2", "(", "e", ",", "n", ")", "*", "r2d", "strike", "=", "strike", "-", "90", "while", "strike", ">=", "360", ":", "strike", "=", "strike", "-", "360", "while", "strike", "<", "0", ":", "strike", "=", "strike", "+", "360", "x", "=", "np", ".", "sqrt", "(", "np", ".", "power", "(", "n", ",", "2", ")", "+", "np", ".", "power", "(", "e", ",", "2", ")", ")", "dip", "=", "np", ".", "arctan2", "(", "x", ",", "u", ")", "*", "r2d", "return", "(", "strike", ",", "dip", ")" ]
Finds strike and dip of plane given normal vector having components n, e, and u. Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd.
[ "Finds", "strike", "and", "dip", "of", "plane", "given", "normal", "vector", "having", "components", "n", "e", "and", "u", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L669-L692
gem/oq-engine
openquake/hmtk/plotting/beachball.py
AuxPlane
def AuxPlane(s1, d1, r1): """ Get Strike and dip of second plane. Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ r2d = 180 / np.pi z = (s1 + 90) / r2d z2 = d1 / r2d z3 = r1 / r2d # slick vector in plane 1 sl1 = -np.cos(z3) * np.cos(z) - np.sin(z3) * np.sin(z) * np.cos(z2) sl2 = np.cos(z3) * np.sin(z) - np.sin(z3) * np.cos(z) * np.cos(z2) sl3 = np.sin(z3) * np.sin(z2) (strike, dip) = StrikeDip(sl2, sl1, sl3) n1 = np.sin(z) * np.sin(z2) # normal vector to plane 1 n2 = np.cos(z) * np.sin(z2) h1 = -sl2 # strike vector of plane 2 h2 = sl1 # note h3=0 always so we leave it out # n3 = np.cos(z2) z = h1 * n1 + h2 * n2 z = z / np.sqrt(h1 * h1 + h2 * h2) z = np.arccos(z) rake = 0 if sl3 > 0: rake = z * r2d if sl3 <= 0: rake = -z * r2d return (strike, dip, rake)
python
def AuxPlane(s1, d1, r1): r2d = 180 / np.pi z = (s1 + 90) / r2d z2 = d1 / r2d z3 = r1 / r2d sl1 = -np.cos(z3) * np.cos(z) - np.sin(z3) * np.sin(z) * np.cos(z2) sl2 = np.cos(z3) * np.sin(z) - np.sin(z3) * np.cos(z) * np.cos(z2) sl3 = np.sin(z3) * np.sin(z2) (strike, dip) = StrikeDip(sl2, sl1, sl3) n1 = np.sin(z) * np.sin(z2) n2 = np.cos(z) * np.sin(z2) h1 = -sl2 h2 = sl1 z = h1 * n1 + h2 * n2 z = z / np.sqrt(h1 * h1 + h2 * h2) z = np.arccos(z) rake = 0 if sl3 > 0: rake = z * r2d if sl3 <= 0: rake = -z * r2d return (strike, dip, rake)
[ "def", "AuxPlane", "(", "s1", ",", "d1", ",", "r1", ")", ":", "r2d", "=", "180", "/", "np", ".", "pi", "z", "=", "(", "s1", "+", "90", ")", "/", "r2d", "z2", "=", "d1", "/", "r2d", "z3", "=", "r1", "/", "r2d", "# slick vector in plane 1", "sl1", "=", "-", "np", ".", "cos", "(", "z3", ")", "*", "np", ".", "cos", "(", "z", ")", "-", "np", ".", "sin", "(", "z3", ")", "*", "np", ".", "sin", "(", "z", ")", "*", "np", ".", "cos", "(", "z2", ")", "sl2", "=", "np", ".", "cos", "(", "z3", ")", "*", "np", ".", "sin", "(", "z", ")", "-", "np", ".", "sin", "(", "z3", ")", "*", "np", ".", "cos", "(", "z", ")", "*", "np", ".", "cos", "(", "z2", ")", "sl3", "=", "np", ".", "sin", "(", "z3", ")", "*", "np", ".", "sin", "(", "z2", ")", "(", "strike", ",", "dip", ")", "=", "StrikeDip", "(", "sl2", ",", "sl1", ",", "sl3", ")", "n1", "=", "np", ".", "sin", "(", "z", ")", "*", "np", ".", "sin", "(", "z2", ")", "# normal vector to plane 1", "n2", "=", "np", ".", "cos", "(", "z", ")", "*", "np", ".", "sin", "(", "z2", ")", "h1", "=", "-", "sl2", "# strike vector of plane 2", "h2", "=", "sl1", "# note h3=0 always so we leave it out", "# n3 = np.cos(z2)", "z", "=", "h1", "*", "n1", "+", "h2", "*", "n2", "z", "=", "z", "/", "np", ".", "sqrt", "(", "h1", "*", "h1", "+", "h2", "*", "h2", ")", "z", "=", "np", ".", "arccos", "(", "z", ")", "rake", "=", "0", "if", "sl3", ">", "0", ":", "rake", "=", "z", "*", "r2d", "if", "sl3", "<=", "0", ":", "rake", "=", "-", "z", "*", "r2d", "return", "(", "strike", ",", "dip", ",", "rake", ")" ]
Get Strike and dip of second plane. Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd.
[ "Get", "Strike", "and", "dip", "of", "second", "plane", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L695-L729
gem/oq-engine
openquake/hmtk/plotting/beachball.py
MT2Plane
def MT2Plane(mt): """ Calculates a nodal plane of a given moment tensor. :param mt: :class:`~MomentTensor` :return: :class:`~NodalPlane` Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ (d, v) = np.linalg.eig(mt.mt) D = np.array([d[1], d[0], d[2]]) V = np.array([[v[1, 1], -v[1, 0], -v[1, 2]], [v[2, 1], -v[2, 0], -v[2, 2]], [-v[0, 1], v[0, 0], v[0, 2]]]) IMAX = D.argmax() IMIN = D.argmin() AE = (V[:, IMAX] + V[:, IMIN]) / np.sqrt(2.0) AN = (V[:, IMAX] - V[:, IMIN]) / np.sqrt(2.0) AER = np.sqrt(np.power(AE[0], 2) + np.power(AE[1], 2) + np.power(AE[2], 2)) ANR = np.sqrt(np.power(AN[0], 2) + np.power(AN[1], 2) + np.power(AN[2], 2)) AE = AE / AER if not ANR: AN = np.array([np.nan, np.nan, np.nan]) else: AN = AN / ANR if AN[2] <= 0.: AN1 = AN AE1 = AE else: AN1 = -AN AE1 = -AE (ft, fd, fl) = TDL(AN1, AE1) return NodalPlane(360 - ft, fd, 180 - fl)
python
def MT2Plane(mt): (d, v) = np.linalg.eig(mt.mt) D = np.array([d[1], d[0], d[2]]) V = np.array([[v[1, 1], -v[1, 0], -v[1, 2]], [v[2, 1], -v[2, 0], -v[2, 2]], [-v[0, 1], v[0, 0], v[0, 2]]]) IMAX = D.argmax() IMIN = D.argmin() AE = (V[:, IMAX] + V[:, IMIN]) / np.sqrt(2.0) AN = (V[:, IMAX] - V[:, IMIN]) / np.sqrt(2.0) AER = np.sqrt(np.power(AE[0], 2) + np.power(AE[1], 2) + np.power(AE[2], 2)) ANR = np.sqrt(np.power(AN[0], 2) + np.power(AN[1], 2) + np.power(AN[2], 2)) AE = AE / AER if not ANR: AN = np.array([np.nan, np.nan, np.nan]) else: AN = AN / ANR if AN[2] <= 0.: AN1 = AN AE1 = AE else: AN1 = -AN AE1 = -AE (ft, fd, fl) = TDL(AN1, AE1) return NodalPlane(360 - ft, fd, 180 - fl)
[ "def", "MT2Plane", "(", "mt", ")", ":", "(", "d", ",", "v", ")", "=", "np", ".", "linalg", ".", "eig", "(", "mt", ".", "mt", ")", "D", "=", "np", ".", "array", "(", "[", "d", "[", "1", "]", ",", "d", "[", "0", "]", ",", "d", "[", "2", "]", "]", ")", "V", "=", "np", ".", "array", "(", "[", "[", "v", "[", "1", ",", "1", "]", ",", "-", "v", "[", "1", ",", "0", "]", ",", "-", "v", "[", "1", ",", "2", "]", "]", ",", "[", "v", "[", "2", ",", "1", "]", ",", "-", "v", "[", "2", ",", "0", "]", ",", "-", "v", "[", "2", ",", "2", "]", "]", ",", "[", "-", "v", "[", "0", ",", "1", "]", ",", "v", "[", "0", ",", "0", "]", ",", "v", "[", "0", ",", "2", "]", "]", "]", ")", "IMAX", "=", "D", ".", "argmax", "(", ")", "IMIN", "=", "D", ".", "argmin", "(", ")", "AE", "=", "(", "V", "[", ":", ",", "IMAX", "]", "+", "V", "[", ":", ",", "IMIN", "]", ")", "/", "np", ".", "sqrt", "(", "2.0", ")", "AN", "=", "(", "V", "[", ":", ",", "IMAX", "]", "-", "V", "[", ":", ",", "IMIN", "]", ")", "/", "np", ".", "sqrt", "(", "2.0", ")", "AER", "=", "np", ".", "sqrt", "(", "np", ".", "power", "(", "AE", "[", "0", "]", ",", "2", ")", "+", "np", ".", "power", "(", "AE", "[", "1", "]", ",", "2", ")", "+", "np", ".", "power", "(", "AE", "[", "2", "]", ",", "2", ")", ")", "ANR", "=", "np", ".", "sqrt", "(", "np", ".", "power", "(", "AN", "[", "0", "]", ",", "2", ")", "+", "np", ".", "power", "(", "AN", "[", "1", "]", ",", "2", ")", "+", "np", ".", "power", "(", "AN", "[", "2", "]", ",", "2", ")", ")", "AE", "=", "AE", "/", "AER", "if", "not", "ANR", ":", "AN", "=", "np", ".", "array", "(", "[", "np", ".", "nan", ",", "np", ".", "nan", ",", "np", ".", "nan", "]", ")", "else", ":", "AN", "=", "AN", "/", "ANR", "if", "AN", "[", "2", "]", "<=", "0.", ":", "AN1", "=", "AN", "AE1", "=", "AE", "else", ":", "AN1", "=", "-", "AN", "AE1", "=", "-", "AE", "(", "ft", ",", "fd", ",", "fl", ")", "=", "TDL", "(", "AN1", ",", "AE1", ")", "return", "NodalPlane", "(", "360", "-", "ft", ",", "fd", ",", "180", "-", "fl", ")" ]
Calculates a nodal plane of a given moment tensor. :param mt: :class:`~MomentTensor` :return: :class:`~NodalPlane` Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd.
[ "Calculates", "a", "nodal", "plane", "of", "a", "given", "moment", "tensor", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L732-L766
gem/oq-engine
openquake/hmtk/plotting/beachball.py
TDL
def TDL(AN, BN): """ Helper function for MT2Plane. Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd. """ XN = AN[0] YN = AN[1] ZN = AN[2] XE = BN[0] YE = BN[1] ZE = BN[2] AAA = 1.0 / (1000000) CON = 57.2957795 if np.fabs(ZN) < AAA: FD = 90. AXN = np.fabs(XN) if AXN > 1.0: AXN = 1.0 FT = np.arcsin(AXN) * CON ST = -XN CT = YN if ST >= 0. and CT < 0: FT = 180. - FT if ST < 0. and CT <= 0: FT = 180. + FT if ST < 0. and CT > 0: FT = 360. - FT FL = np.arcsin(abs(ZE)) * CON SL = -ZE if np.fabs(XN) < AAA: CL = XE / YN else: CL = -YE / XN if SL >= 0. and CL < 0: FL = 180. - FL if SL < 0. and CL <= 0: FL = FL - 180. if SL < 0. and CL > 0: FL = -FL else: if - ZN > 1.0: ZN = -1.0 FDH = np.arccos(-ZN) FD = FDH * CON SD = np.sin(FDH) if SD == 0: return ST = -XN / SD CT = YN / SD SX = np.fabs(ST) if SX > 1.0: SX = 1.0 FT = np.arcsin(SX) * CON if ST >= 0. and CT < 0: FT = 180. - FT if ST < 0. and CT <= 0: FT = 180. + FT if ST < 0. and CT > 0: FT = 360. - FT SL = -ZE / SD SX = np.fabs(SL) if SX > 1.0: SX = 1.0 FL = np.arcsin(SX) * CON if ST == 0: CL = XE / CT else: XXX = YN * ZN * ZE / SD / SD + YE CL = -SD * XXX / XN if CT == 0: CL = YE / ST if SL >= 0. and CL < 0: FL = 180. - FL if SL < 0. and CL <= 0: FL = FL - 180. if SL < 0. and CL > 0: FL = -FL return (FT, FD, FL)
python
def TDL(AN, BN): XN = AN[0] YN = AN[1] ZN = AN[2] XE = BN[0] YE = BN[1] ZE = BN[2] AAA = 1.0 / (1000000) CON = 57.2957795 if np.fabs(ZN) < AAA: FD = 90. AXN = np.fabs(XN) if AXN > 1.0: AXN = 1.0 FT = np.arcsin(AXN) * CON ST = -XN CT = YN if ST >= 0. and CT < 0: FT = 180. - FT if ST < 0. and CT <= 0: FT = 180. + FT if ST < 0. and CT > 0: FT = 360. - FT FL = np.arcsin(abs(ZE)) * CON SL = -ZE if np.fabs(XN) < AAA: CL = XE / YN else: CL = -YE / XN if SL >= 0. and CL < 0: FL = 180. - FL if SL < 0. and CL <= 0: FL = FL - 180. if SL < 0. and CL > 0: FL = -FL else: if - ZN > 1.0: ZN = -1.0 FDH = np.arccos(-ZN) FD = FDH * CON SD = np.sin(FDH) if SD == 0: return ST = -XN / SD CT = YN / SD SX = np.fabs(ST) if SX > 1.0: SX = 1.0 FT = np.arcsin(SX) * CON if ST >= 0. and CT < 0: FT = 180. - FT if ST < 0. and CT <= 0: FT = 180. + FT if ST < 0. and CT > 0: FT = 360. - FT SL = -ZE / SD SX = np.fabs(SL) if SX > 1.0: SX = 1.0 FL = np.arcsin(SX) * CON if ST == 0: CL = XE / CT else: XXX = YN * ZN * ZE / SD / SD + YE CL = -SD * XXX / XN if CT == 0: CL = YE / ST if SL >= 0. and CL < 0: FL = 180. - FL if SL < 0. and CL <= 0: FL = FL - 180. if SL < 0. and CL > 0: FL = -FL return (FT, FD, FL)
[ "def", "TDL", "(", "AN", ",", "BN", ")", ":", "XN", "=", "AN", "[", "0", "]", "YN", "=", "AN", "[", "1", "]", "ZN", "=", "AN", "[", "2", "]", "XE", "=", "BN", "[", "0", "]", "YE", "=", "BN", "[", "1", "]", "ZE", "=", "BN", "[", "2", "]", "AAA", "=", "1.0", "/", "(", "1000000", ")", "CON", "=", "57.2957795", "if", "np", ".", "fabs", "(", "ZN", ")", "<", "AAA", ":", "FD", "=", "90.", "AXN", "=", "np", ".", "fabs", "(", "XN", ")", "if", "AXN", ">", "1.0", ":", "AXN", "=", "1.0", "FT", "=", "np", ".", "arcsin", "(", "AXN", ")", "*", "CON", "ST", "=", "-", "XN", "CT", "=", "YN", "if", "ST", ">=", "0.", "and", "CT", "<", "0", ":", "FT", "=", "180.", "-", "FT", "if", "ST", "<", "0.", "and", "CT", "<=", "0", ":", "FT", "=", "180.", "+", "FT", "if", "ST", "<", "0.", "and", "CT", ">", "0", ":", "FT", "=", "360.", "-", "FT", "FL", "=", "np", ".", "arcsin", "(", "abs", "(", "ZE", ")", ")", "*", "CON", "SL", "=", "-", "ZE", "if", "np", ".", "fabs", "(", "XN", ")", "<", "AAA", ":", "CL", "=", "XE", "/", "YN", "else", ":", "CL", "=", "-", "YE", "/", "XN", "if", "SL", ">=", "0.", "and", "CL", "<", "0", ":", "FL", "=", "180.", "-", "FL", "if", "SL", "<", "0.", "and", "CL", "<=", "0", ":", "FL", "=", "FL", "-", "180.", "if", "SL", "<", "0.", "and", "CL", ">", "0", ":", "FL", "=", "-", "FL", "else", ":", "if", "-", "ZN", ">", "1.0", ":", "ZN", "=", "-", "1.0", "FDH", "=", "np", ".", "arccos", "(", "-", "ZN", ")", "FD", "=", "FDH", "*", "CON", "SD", "=", "np", ".", "sin", "(", "FDH", ")", "if", "SD", "==", "0", ":", "return", "ST", "=", "-", "XN", "/", "SD", "CT", "=", "YN", "/", "SD", "SX", "=", "np", ".", "fabs", "(", "ST", ")", "if", "SX", ">", "1.0", ":", "SX", "=", "1.0", "FT", "=", "np", ".", "arcsin", "(", "SX", ")", "*", "CON", "if", "ST", ">=", "0.", "and", "CT", "<", "0", ":", "FT", "=", "180.", "-", "FT", "if", "ST", "<", "0.", "and", "CT", "<=", "0", ":", "FT", "=", "180.", "+", "FT", "if", "ST", "<", "0.", "and", "CT", ">", "0", ":", "FT", "=", "360.", "-", "FT", "SL", "=", "-", "ZE", "/", "SD", "SX", "=", "np", ".", "fabs", "(", "SL", ")", "if", "SX", ">", "1.0", ":", "SX", "=", "1.0", "FL", "=", "np", ".", "arcsin", "(", "SX", ")", "*", "CON", "if", "ST", "==", "0", ":", "CL", "=", "XE", "/", "CT", "else", ":", "XXX", "=", "YN", "*", "ZN", "*", "ZE", "/", "SD", "/", "SD", "+", "YE", "CL", "=", "-", "SD", "*", "XXX", "/", "XN", "if", "CT", "==", "0", ":", "CL", "=", "YE", "/", "ST", "if", "SL", ">=", "0.", "and", "CL", "<", "0", ":", "FL", "=", "180.", "-", "FL", "if", "SL", "<", "0.", "and", "CL", "<=", "0", ":", "FL", "=", "FL", "-", "180.", "if", "SL", "<", "0.", "and", "CL", ">", "0", ":", "FL", "=", "-", "FL", "return", "(", "FT", ",", "FD", ",", "FL", ")" ]
Helper function for MT2Plane. Adapted from MATLAB script `bb.m <http://www.ceri.memphis.edu/people/olboyd/Software/Software.html>`_ written by Andy Michael and Oliver Boyd.
[ "Helper", "function", "for", "MT2Plane", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L769-L849
gem/oq-engine
openquake/hmtk/plotting/beachball.py
MT2Axes
def MT2Axes(mt): """ Calculates the principal axes of a given moment tensor. :param mt: :class:`~MomentTensor` :return: tuple of :class:`~PrincipalAxis` T, N and P Adapted from ps_tensor / utilmeca.c / `Generic Mapping Tools (GMT) <http://gmt.soest.hawaii.edu>`_. """ (D, V) = np.linalg.eigh(mt.mt) pl = np.arcsin(-V[0]) az = np.arctan2(V[2], -V[1]) for i in range(0, 3): if pl[i] <= 0: pl[i] = -pl[i] az[i] += np.pi if az[i] < 0: az[i] += 2 * np.pi if az[i] > 2 * np.pi: az[i] -= 2 * np.pi pl *= R2D az *= R2D T = PrincipalAxis(D[2], az[2], pl[2]) N = PrincipalAxis(D[1], az[1], pl[1]) P = PrincipalAxis(D[0], az[0], pl[0]) return (T, N, P)
python
def MT2Axes(mt): (D, V) = np.linalg.eigh(mt.mt) pl = np.arcsin(-V[0]) az = np.arctan2(V[2], -V[1]) for i in range(0, 3): if pl[i] <= 0: pl[i] = -pl[i] az[i] += np.pi if az[i] < 0: az[i] += 2 * np.pi if az[i] > 2 * np.pi: az[i] -= 2 * np.pi pl *= R2D az *= R2D T = PrincipalAxis(D[2], az[2], pl[2]) N = PrincipalAxis(D[1], az[1], pl[1]) P = PrincipalAxis(D[0], az[0], pl[0]) return (T, N, P)
[ "def", "MT2Axes", "(", "mt", ")", ":", "(", "D", ",", "V", ")", "=", "np", ".", "linalg", ".", "eigh", "(", "mt", ".", "mt", ")", "pl", "=", "np", ".", "arcsin", "(", "-", "V", "[", "0", "]", ")", "az", "=", "np", ".", "arctan2", "(", "V", "[", "2", "]", ",", "-", "V", "[", "1", "]", ")", "for", "i", "in", "range", "(", "0", ",", "3", ")", ":", "if", "pl", "[", "i", "]", "<=", "0", ":", "pl", "[", "i", "]", "=", "-", "pl", "[", "i", "]", "az", "[", "i", "]", "+=", "np", ".", "pi", "if", "az", "[", "i", "]", "<", "0", ":", "az", "[", "i", "]", "+=", "2", "*", "np", ".", "pi", "if", "az", "[", "i", "]", ">", "2", "*", "np", ".", "pi", ":", "az", "[", "i", "]", "-=", "2", "*", "np", ".", "pi", "pl", "*=", "R2D", "az", "*=", "R2D", "T", "=", "PrincipalAxis", "(", "D", "[", "2", "]", ",", "az", "[", "2", "]", ",", "pl", "[", "2", "]", ")", "N", "=", "PrincipalAxis", "(", "D", "[", "1", "]", ",", "az", "[", "1", "]", ",", "pl", "[", "1", "]", ")", "P", "=", "PrincipalAxis", "(", "D", "[", "0", "]", ",", "az", "[", "0", "]", ",", "pl", "[", "0", "]", ")", "return", "(", "T", ",", "N", ",", "P", ")" ]
Calculates the principal axes of a given moment tensor. :param mt: :class:`~MomentTensor` :return: tuple of :class:`~PrincipalAxis` T, N and P Adapted from ps_tensor / utilmeca.c / `Generic Mapping Tools (GMT) <http://gmt.soest.hawaii.edu>`_.
[ "Calculates", "the", "principal", "axes", "of", "a", "given", "moment", "tensor", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/plotting/beachball.py#L852-L879
gem/oq-engine
openquake/hmtk/strain/strain_utils.py
calculate_taper_function
def calculate_taper_function(obs_threshold_moment, sel_threshold_moment, corner_moment, beta): ''' Calculates the tapering function of the tapered Gutenberg & Richter model: as described in Bird & Liu (2007):: taper_function = (M_0(M_T) / M_0(M_T^{CMT}))^-beta x exp((M_0(m_T^CMT) - M_0(m_T)) / M_0(m_c)) :param numpy.ndarray obs_threshold_moment: Moment of the threshold magnitude of the observed earthquake catalogue :param numpy.ndarray sel_threshold_moment: Moment of the target magnitude :param float corner_momnet: Corner moment of the Tapered Gutenberg-Richter Function :param float beta: Beta value (b * ln(10.)) of the Tapered Gutenberg-Richter Function :returns: Relative moment rate ''' argument = (obs_threshold_moment - sel_threshold_moment) /\ corner_moment if argument < -100.0: g_function = 0.0 else: g_function = ((sel_threshold_moment / obs_threshold_moment) ** -beta) * exp(argument) return g_function
python
def calculate_taper_function(obs_threshold_moment, sel_threshold_moment, corner_moment, beta): argument = (obs_threshold_moment - sel_threshold_moment) /\ corner_moment if argument < -100.0: g_function = 0.0 else: g_function = ((sel_threshold_moment / obs_threshold_moment) ** -beta) * exp(argument) return g_function
[ "def", "calculate_taper_function", "(", "obs_threshold_moment", ",", "sel_threshold_moment", ",", "corner_moment", ",", "beta", ")", ":", "argument", "=", "(", "obs_threshold_moment", "-", "sel_threshold_moment", ")", "/", "corner_moment", "if", "argument", "<", "-", "100.0", ":", "g_function", "=", "0.0", "else", ":", "g_function", "=", "(", "(", "sel_threshold_moment", "/", "obs_threshold_moment", ")", "**", "-", "beta", ")", "*", "exp", "(", "argument", ")", "return", "g_function" ]
Calculates the tapering function of the tapered Gutenberg & Richter model: as described in Bird & Liu (2007):: taper_function = (M_0(M_T) / M_0(M_T^{CMT}))^-beta x exp((M_0(m_T^CMT) - M_0(m_T)) / M_0(m_c)) :param numpy.ndarray obs_threshold_moment: Moment of the threshold magnitude of the observed earthquake catalogue :param numpy.ndarray sel_threshold_moment: Moment of the target magnitude :param float corner_momnet: Corner moment of the Tapered Gutenberg-Richter Function :param float beta: Beta value (b * ln(10.)) of the Tapered Gutenberg-Richter Function :returns: Relative moment rate
[ "Calculates", "the", "tapering", "function", "of", "the", "tapered", "Gutenberg", "&", "Richter", "model", ":", "as", "described", "in", "Bird", "&", "Liu", "(", "2007", ")", "::" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/strain/strain_utils.py#L81-L108
gem/oq-engine
openquake/hmtk/strain/strain_utils.py
tapered_gutenberg_richter_cdf
def tapered_gutenberg_richter_cdf(moment, moment_threshold, beta, corner_moment): ''' Tapered Gutenberg Richter Cumulative Density Function :param float or numpy.ndarray moment: Moment for calculation of rate :param float or numpy.ndarray moment_threshold: Threshold Moment of the distribution (moment rate essentially!) :param float beta: Beta value (b * ln(10.)) of the Tapered Gutenberg-Richter Function :param float corner_momnet: Corner moment of the Tapered Gutenberg-Richter Function :returns: Cumulative probability of moment release > moment ''' cdf = np.exp((moment_threshold - moment) / corner_moment) return ((moment / moment_threshold) ** (-beta)) * cdf
python
def tapered_gutenberg_richter_cdf(moment, moment_threshold, beta, corner_moment): cdf = np.exp((moment_threshold - moment) / corner_moment) return ((moment / moment_threshold) ** (-beta)) * cdf
[ "def", "tapered_gutenberg_richter_cdf", "(", "moment", ",", "moment_threshold", ",", "beta", ",", "corner_moment", ")", ":", "cdf", "=", "np", ".", "exp", "(", "(", "moment_threshold", "-", "moment", ")", "/", "corner_moment", ")", "return", "(", "(", "moment", "/", "moment_threshold", ")", "**", "(", "-", "beta", ")", ")", "*", "cdf" ]
Tapered Gutenberg Richter Cumulative Density Function :param float or numpy.ndarray moment: Moment for calculation of rate :param float or numpy.ndarray moment_threshold: Threshold Moment of the distribution (moment rate essentially!) :param float beta: Beta value (b * ln(10.)) of the Tapered Gutenberg-Richter Function :param float corner_momnet: Corner moment of the Tapered Gutenberg-Richter Function :returns: Cumulative probability of moment release > moment
[ "Tapered", "Gutenberg", "Richter", "Cumulative", "Density", "Function" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/strain/strain_utils.py#L111-L134
gem/oq-engine
openquake/hmtk/strain/strain_utils.py
tapered_gutenberg_richter_pdf
def tapered_gutenberg_richter_pdf(moment, moment_threshold, beta, corner_moment): ''' Tapered Gutenberg-Richter Probability Density Function :param float or numpy.ndarray moment: Moment for calculation of rate :param float or numpy.ndarray moment_threshold: Threshold Moment of the distribution (moment rate essentially!) :param float beta: Beta value (b * ln(10.)) of the Tapered Gutenberg-Richter Function :param float corner_momnet: Corner moment of the Tapered Gutenberg-Richter Function :returns: Absolute probability of moment release > moment ''' return ((beta / moment + 1. / corner_moment) * tapered_gutenberg_richter_cdf(moment, moment_threshold, beta, corner_moment))
python
def tapered_gutenberg_richter_pdf(moment, moment_threshold, beta, corner_moment): return ((beta / moment + 1. / corner_moment) * tapered_gutenberg_richter_cdf(moment, moment_threshold, beta, corner_moment))
[ "def", "tapered_gutenberg_richter_pdf", "(", "moment", ",", "moment_threshold", ",", "beta", ",", "corner_moment", ")", ":", "return", "(", "(", "beta", "/", "moment", "+", "1.", "/", "corner_moment", ")", "*", "tapered_gutenberg_richter_cdf", "(", "moment", ",", "moment_threshold", ",", "beta", ",", "corner_moment", ")", ")" ]
Tapered Gutenberg-Richter Probability Density Function :param float or numpy.ndarray moment: Moment for calculation of rate :param float or numpy.ndarray moment_threshold: Threshold Moment of the distribution (moment rate essentially!) :param float beta: Beta value (b * ln(10.)) of the Tapered Gutenberg-Richter Function :param float corner_momnet: Corner moment of the Tapered Gutenberg-Richter Function :returns: Absolute probability of moment release > moment
[ "Tapered", "Gutenberg", "-", "Richter", "Probability", "Density", "Function" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/strain/strain_utils.py#L137-L159
gem/oq-engine
openquake/engine/export/core.py
check_version
def check_version(dstore): """ :param dstore: a DataStore instance :returns: a message if the stored version is different from the current version """ ds_version = dstore.hdf5.attrs['engine_version'] if ds_version != __version__: return (': the datastore is at version %s, but the exporter at ' 'version %s' % (ds_version, __version__)) else: return ''
python
def check_version(dstore): ds_version = dstore.hdf5.attrs['engine_version'] if ds_version != __version__: return (': the datastore is at version %s, but the exporter at ' 'version %s' % (ds_version, __version__)) else: return ''
[ "def", "check_version", "(", "dstore", ")", ":", "ds_version", "=", "dstore", ".", "hdf5", ".", "attrs", "[", "'engine_version'", "]", "if", "ds_version", "!=", "__version__", ":", "return", "(", "': the datastore is at version %s, but the exporter at '", "'version %s'", "%", "(", "ds_version", ",", "__version__", ")", ")", "else", ":", "return", "''" ]
:param dstore: a DataStore instance :returns: a message if the stored version is different from the current version
[ ":", "param", "dstore", ":", "a", "DataStore", "instance", ":", "returns", ":", "a", "message", "if", "the", "stored", "version", "is", "different", "from", "the", "current", "version" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/engine/export/core.py#L37-L48
gem/oq-engine
openquake/engine/export/core.py
export_from_db
def export_from_db(output_key, calc_id, datadir, target): """ :param output_key: a pair (ds_key, fmt) :param calc_id: calculation ID :param datadir: directory containing the datastore :param target: directory, temporary when called from the engine server :returns: the list of exported path names """ makedirs(target) export.from_db = True ds_key, fmt = output_key with datastore.read(calc_id, datadir=datadir) as dstore: dstore.export_dir = target try: exported = export(output_key, dstore) except Exception: etype, err, tb = sys.exc_info() tb_str = ''.join(traceback.format_tb(tb)) version = check_version(dstore) raise DataStoreExportError( 'Could not export %s in %s%s\n%s%s' % (output_key + (version, tb_str, err))) return exported
python
def export_from_db(output_key, calc_id, datadir, target): makedirs(target) export.from_db = True ds_key, fmt = output_key with datastore.read(calc_id, datadir=datadir) as dstore: dstore.export_dir = target try: exported = export(output_key, dstore) except Exception: etype, err, tb = sys.exc_info() tb_str = ''.join(traceback.format_tb(tb)) version = check_version(dstore) raise DataStoreExportError( 'Could not export %s in %s%s\n%s%s' % (output_key + (version, tb_str, err))) return exported
[ "def", "export_from_db", "(", "output_key", ",", "calc_id", ",", "datadir", ",", "target", ")", ":", "makedirs", "(", "target", ")", "export", ".", "from_db", "=", "True", "ds_key", ",", "fmt", "=", "output_key", "with", "datastore", ".", "read", "(", "calc_id", ",", "datadir", "=", "datadir", ")", "as", "dstore", ":", "dstore", ".", "export_dir", "=", "target", "try", ":", "exported", "=", "export", "(", "output_key", ",", "dstore", ")", "except", "Exception", ":", "etype", ",", "err", ",", "tb", "=", "sys", ".", "exc_info", "(", ")", "tb_str", "=", "''", ".", "join", "(", "traceback", ".", "format_tb", "(", "tb", ")", ")", "version", "=", "check_version", "(", "dstore", ")", "raise", "DataStoreExportError", "(", "'Could not export %s in %s%s\\n%s%s'", "%", "(", "output_key", "+", "(", "version", ",", "tb_str", ",", "err", ")", ")", ")", "return", "exported" ]
:param output_key: a pair (ds_key, fmt) :param calc_id: calculation ID :param datadir: directory containing the datastore :param target: directory, temporary when called from the engine server :returns: the list of exported path names
[ ":", "param", "output_key", ":", "a", "pair", "(", "ds_key", "fmt", ")", ":", "param", "calc_id", ":", "calculation", "ID", ":", "param", "datadir", ":", "directory", "containing", "the", "datastore", ":", "param", "target", ":", "directory", "temporary", "when", "called", "from", "the", "engine", "server", ":", "returns", ":", "the", "list", "of", "exported", "path", "names" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/engine/export/core.py#L51-L73
gem/oq-engine
openquake/engine/export/core.py
makedirs
def makedirs(path): """ Make all of the directories in the ``path`` using `os.makedirs`. """ if os.path.exists(path): if not os.path.isdir(path): # If it's not a directory, we can't do anything. # This is a problem raise RuntimeError('%s already exists and is not a directory.' % path) else: os.makedirs(path)
python
def makedirs(path): if os.path.exists(path): if not os.path.isdir(path): raise RuntimeError('%s already exists and is not a directory.' % path) else: os.makedirs(path)
[ "def", "makedirs", "(", "path", ")", ":", "if", "os", ".", "path", ".", "exists", "(", "path", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "path", ")", ":", "# If it's not a directory, we can't do anything.", "# This is a problem", "raise", "RuntimeError", "(", "'%s already exists and is not a directory.'", "%", "path", ")", "else", ":", "os", ".", "makedirs", "(", "path", ")" ]
Make all of the directories in the ``path`` using `os.makedirs`.
[ "Make", "all", "of", "the", "directories", "in", "the", "path", "using", "os", ".", "makedirs", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/engine/export/core.py#L80-L91
gem/oq-engine
openquake/engine/export/core.py
get_outkey
def get_outkey(dskey, export_types): """ Extract the first pair (dskey, exptype) found in export """ for exptype in export_types: if (dskey, exptype) in export: return (dskey, exptype)
python
def get_outkey(dskey, export_types): for exptype in export_types: if (dskey, exptype) in export: return (dskey, exptype)
[ "def", "get_outkey", "(", "dskey", ",", "export_types", ")", ":", "for", "exptype", "in", "export_types", ":", "if", "(", "dskey", ",", "exptype", ")", "in", "export", ":", "return", "(", "dskey", ",", "exptype", ")" ]
Extract the first pair (dskey, exptype) found in export
[ "Extract", "the", "first", "pair", "(", "dskey", "exptype", ")", "found", "in", "export" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/engine/export/core.py#L106-L112
gem/oq-engine
openquake/engine/export/core.py
export_output
def export_output(dskey, calc_id, datadir, target_dir, export_types): """ Simple UI wrapper around :func:`openquake.engine.export.core.export_from_db` yielding a summary of files exported, if any. """ outkey = get_outkey(dskey, export_types.split(',')) if export_types and not outkey: yield 'There is no exporter for %s, %s' % (dskey, export_types) return yield from export_from_db(outkey, calc_id, datadir, target_dir)
python
def export_output(dskey, calc_id, datadir, target_dir, export_types): outkey = get_outkey(dskey, export_types.split(',')) if export_types and not outkey: yield 'There is no exporter for %s, %s' % (dskey, export_types) return yield from export_from_db(outkey, calc_id, datadir, target_dir)
[ "def", "export_output", "(", "dskey", ",", "calc_id", ",", "datadir", ",", "target_dir", ",", "export_types", ")", ":", "outkey", "=", "get_outkey", "(", "dskey", ",", "export_types", ".", "split", "(", "','", ")", ")", "if", "export_types", "and", "not", "outkey", ":", "yield", "'There is no exporter for %s, %s'", "%", "(", "dskey", ",", "export_types", ")", "return", "yield", "from", "export_from_db", "(", "outkey", ",", "calc_id", ",", "datadir", ",", "target_dir", ")" ]
Simple UI wrapper around :func:`openquake.engine.export.core.export_from_db` yielding a summary of files exported, if any.
[ "Simple", "UI", "wrapper", "around", ":", "func", ":", "openquake", ".", "engine", ".", "export", ".", "core", ".", "export_from_db", "yielding", "a", "summary", "of", "files", "exported", "if", "any", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/engine/export/core.py#L115-L125
gem/oq-engine
openquake/hmtk/seismicity/max_magnitude/base.py
_get_observed_mmax
def _get_observed_mmax(catalogue, config): '''Check see if observed mmax values are input, if not then take from the catalogue''' if config['input_mmax']: obsmax = config['input_mmax'] if config['input_mmax_uncertainty']: return config['input_mmax'], config['input_mmax_uncertainty'] else: raise ValueError('Input mmax uncertainty must be specified!') max_location = np.argmax(catalogue['magnitude']) obsmax = catalogue['magnitude'][max_location] cond = isinstance(catalogue['sigmaMagnitude'], np.ndarray) and \ len(catalogue['sigmaMagnitude']) > 0 and not \ np.all(np.isnan(catalogue['sigmaMagnitude'])) if cond: if not np.isnan(catalogue['sigmaMagnitude'][max_location]): return obsmax, catalogue['sigmaMagnitude'][max_location] else: print('Uncertainty not given on observed Mmax\n' 'Taking largest magnitude uncertainty found in catalogue') return obsmax, np.nanmax(catalogue['sigmaMagnitude']) elif config['input_mmax_uncertainty']: return obsmax, config['input_mmax_uncertainty'] else: raise ValueError('Input mmax uncertainty must be specified!')
python
def _get_observed_mmax(catalogue, config): if config['input_mmax']: obsmax = config['input_mmax'] if config['input_mmax_uncertainty']: return config['input_mmax'], config['input_mmax_uncertainty'] else: raise ValueError('Input mmax uncertainty must be specified!') max_location = np.argmax(catalogue['magnitude']) obsmax = catalogue['magnitude'][max_location] cond = isinstance(catalogue['sigmaMagnitude'], np.ndarray) and \ len(catalogue['sigmaMagnitude']) > 0 and not \ np.all(np.isnan(catalogue['sigmaMagnitude'])) if cond: if not np.isnan(catalogue['sigmaMagnitude'][max_location]): return obsmax, catalogue['sigmaMagnitude'][max_location] else: print('Uncertainty not given on observed Mmax\n' 'Taking largest magnitude uncertainty found in catalogue') return obsmax, np.nanmax(catalogue['sigmaMagnitude']) elif config['input_mmax_uncertainty']: return obsmax, config['input_mmax_uncertainty'] else: raise ValueError('Input mmax uncertainty must be specified!')
[ "def", "_get_observed_mmax", "(", "catalogue", ",", "config", ")", ":", "if", "config", "[", "'input_mmax'", "]", ":", "obsmax", "=", "config", "[", "'input_mmax'", "]", "if", "config", "[", "'input_mmax_uncertainty'", "]", ":", "return", "config", "[", "'input_mmax'", "]", ",", "config", "[", "'input_mmax_uncertainty'", "]", "else", ":", "raise", "ValueError", "(", "'Input mmax uncertainty must be specified!'", ")", "max_location", "=", "np", ".", "argmax", "(", "catalogue", "[", "'magnitude'", "]", ")", "obsmax", "=", "catalogue", "[", "'magnitude'", "]", "[", "max_location", "]", "cond", "=", "isinstance", "(", "catalogue", "[", "'sigmaMagnitude'", "]", ",", "np", ".", "ndarray", ")", "and", "len", "(", "catalogue", "[", "'sigmaMagnitude'", "]", ")", ">", "0", "and", "not", "np", ".", "all", "(", "np", ".", "isnan", "(", "catalogue", "[", "'sigmaMagnitude'", "]", ")", ")", "if", "cond", ":", "if", "not", "np", ".", "isnan", "(", "catalogue", "[", "'sigmaMagnitude'", "]", "[", "max_location", "]", ")", ":", "return", "obsmax", ",", "catalogue", "[", "'sigmaMagnitude'", "]", "[", "max_location", "]", "else", ":", "print", "(", "'Uncertainty not given on observed Mmax\\n'", "'Taking largest magnitude uncertainty found in catalogue'", ")", "return", "obsmax", ",", "np", ".", "nanmax", "(", "catalogue", "[", "'sigmaMagnitude'", "]", ")", "elif", "config", "[", "'input_mmax_uncertainty'", "]", ":", "return", "obsmax", ",", "config", "[", "'input_mmax_uncertainty'", "]", "else", ":", "raise", "ValueError", "(", "'Input mmax uncertainty must be specified!'", ")" ]
Check see if observed mmax values are input, if not then take from the catalogue
[ "Check", "see", "if", "observed", "mmax", "values", "are", "input", "if", "not", "then", "take", "from", "the", "catalogue" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/seismicity/max_magnitude/base.py#L57-L83
gem/oq-engine
openquake/hmtk/seismicity/max_magnitude/base.py
_get_magnitude_vector_properties
def _get_magnitude_vector_properties(catalogue, config): '''If an input minimum magnitude is given then consider catalogue only above the minimum magnitude - returns corresponding properties''' mmin = config.get('input_mmin', np.min(catalogue['magnitude'])) neq = np.float(np.sum(catalogue['magnitude'] >= mmin - 1.E-7)) return neq, mmin
python
def _get_magnitude_vector_properties(catalogue, config): mmin = config.get('input_mmin', np.min(catalogue['magnitude'])) neq = np.float(np.sum(catalogue['magnitude'] >= mmin - 1.E-7)) return neq, mmin
[ "def", "_get_magnitude_vector_properties", "(", "catalogue", ",", "config", ")", ":", "mmin", "=", "config", ".", "get", "(", "'input_mmin'", ",", "np", ".", "min", "(", "catalogue", "[", "'magnitude'", "]", ")", ")", "neq", "=", "np", ".", "float", "(", "np", ".", "sum", "(", "catalogue", "[", "'magnitude'", "]", ">=", "mmin", "-", "1.E-7", ")", ")", "return", "neq", ",", "mmin" ]
If an input minimum magnitude is given then consider catalogue only above the minimum magnitude - returns corresponding properties
[ "If", "an", "input", "minimum", "magnitude", "is", "given", "then", "consider", "catalogue", "only", "above", "the", "minimum", "magnitude", "-", "returns", "corresponding", "properties" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hmtk/seismicity/max_magnitude/base.py#L104-L110
gem/oq-engine
openquake/hazardlib/gsim/campbell_1997.py
Campbell1997.get_mean_and_stddevs
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values. """ R = (dists.rrup) M = rup.mag # get constants Ssr = self.get_Ssr_term(sites.vs30) Shr = self.get_Shr_term(sites.vs30) rake = rup.rake F = self.get_fault_term(rake) # compute mean mean = -3.512 + (0.904 * M) - (1.328 * np.log(np.sqrt(R**2 + (0.149 * np.exp(0.647 * M))**2))) \ + (1.125 - 0.112 * np.log(R) - 0.0957 * M) * F \ + (0.440 - 0.171 * np.log(R)) * Ssr \ + (0.405 - 0.222 * np.log(R)) * Shr stddevs = self.get_stddevs(mean, stddev_types) return mean, stddevs
python
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): R = (dists.rrup) M = rup.mag Ssr = self.get_Ssr_term(sites.vs30) Shr = self.get_Shr_term(sites.vs30) rake = rup.rake F = self.get_fault_term(rake) mean = -3.512 + (0.904 * M) - (1.328 * np.log(np.sqrt(R**2 + (0.149 * np.exp(0.647 * M))**2))) \ + (1.125 - 0.112 * np.log(R) - 0.0957 * M) * F \ + (0.440 - 0.171 * np.log(R)) * Ssr \ + (0.405 - 0.222 * np.log(R)) * Shr stddevs = self.get_stddevs(mean, stddev_types) return mean, stddevs
[ "def", "get_mean_and_stddevs", "(", "self", ",", "sites", ",", "rup", ",", "dists", ",", "imt", ",", "stddev_types", ")", ":", "R", "=", "(", "dists", ".", "rrup", ")", "M", "=", "rup", ".", "mag", "# get constants", "Ssr", "=", "self", ".", "get_Ssr_term", "(", "sites", ".", "vs30", ")", "Shr", "=", "self", ".", "get_Shr_term", "(", "sites", ".", "vs30", ")", "rake", "=", "rup", ".", "rake", "F", "=", "self", ".", "get_fault_term", "(", "rake", ")", "# compute mean", "mean", "=", "-", "3.512", "+", "(", "0.904", "*", "M", ")", "-", "(", "1.328", "*", "np", ".", "log", "(", "np", ".", "sqrt", "(", "R", "**", "2", "+", "(", "0.149", "*", "np", ".", "exp", "(", "0.647", "*", "M", ")", ")", "**", "2", ")", ")", ")", "+", "(", "1.125", "-", "0.112", "*", "np", ".", "log", "(", "R", ")", "-", "0.0957", "*", "M", ")", "*", "F", "+", "(", "0.440", "-", "0.171", "*", "np", ".", "log", "(", "R", ")", ")", "*", "Ssr", "+", "(", "0.405", "-", "0.222", "*", "np", ".", "log", "(", "R", ")", ")", "*", "Shr", "stddevs", "=", "self", ".", "get_stddevs", "(", "mean", ",", "stddev_types", ")", "return", "mean", ",", "stddevs" ]
See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values.
[ "See", ":", "meth", ":", "superclass", "method", "<", ".", "base", ".", "GroundShakingIntensityModel", ".", "get_mean_and_stddevs", ">", "for", "spec", "of", "input", "and", "result", "values", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/campbell_1997.py#L71-L92
gem/oq-engine
openquake/hazardlib/gsim/campbell_1997.py
Campbell1997.get_fault_term
def get_fault_term(self, rake): """ Returns coefficient for faulting style (pg 156) """ rake = rake + 360 if rake < 0 else rake if (rake >= 45) & (rake <= 135): f = 1. elif (rake >= 225) & (rake <= 315): f = 0.5 else: f = 0. return f
python
def get_fault_term(self, rake): rake = rake + 360 if rake < 0 else rake if (rake >= 45) & (rake <= 135): f = 1. elif (rake >= 225) & (rake <= 315): f = 0.5 else: f = 0. return f
[ "def", "get_fault_term", "(", "self", ",", "rake", ")", ":", "rake", "=", "rake", "+", "360", "if", "rake", "<", "0", "else", "rake", "if", "(", "rake", ">=", "45", ")", "&", "(", "rake", "<=", "135", ")", ":", "f", "=", "1.", "elif", "(", "rake", ">=", "225", ")", "&", "(", "rake", "<=", "315", ")", ":", "f", "=", "0.5", "else", ":", "f", "=", "0.", "return", "f" ]
Returns coefficient for faulting style (pg 156)
[ "Returns", "coefficient", "for", "faulting", "style", "(", "pg", "156", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/campbell_1997.py#L94-L106
gem/oq-engine
openquake/hazardlib/gsim/campbell_1997.py
Campbell1997.get_stddevs
def get_stddevs(self, mean, stddev_types): """ Returns the standard deviations from mean (pg 164; more robust than estimate using magnitude) """ mean = np.exp(mean) sigma = 0.39 + np.zeros(mean.shape) sigma[mean < 0.068] = 0.55 idx = np.logical_and(mean >= 0.068, mean <= 0.21) sigma[idx] = 0.173- 0.140 * np.log(mean[idx]) stddevs = [] for stddev in stddev_types: if stddev == const.StdDev.TOTAL: stddevs.append(sigma) return stddevs
python
def get_stddevs(self, mean, stddev_types): mean = np.exp(mean) sigma = 0.39 + np.zeros(mean.shape) sigma[mean < 0.068] = 0.55 idx = np.logical_and(mean >= 0.068, mean <= 0.21) sigma[idx] = 0.173- 0.140 * np.log(mean[idx]) stddevs = [] for stddev in stddev_types: if stddev == const.StdDev.TOTAL: stddevs.append(sigma) return stddevs
[ "def", "get_stddevs", "(", "self", ",", "mean", ",", "stddev_types", ")", ":", "mean", "=", "np", ".", "exp", "(", "mean", ")", "sigma", "=", "0.39", "+", "np", ".", "zeros", "(", "mean", ".", "shape", ")", "sigma", "[", "mean", "<", "0.068", "]", "=", "0.55", "idx", "=", "np", ".", "logical_and", "(", "mean", ">=", "0.068", ",", "mean", "<=", "0.21", ")", "sigma", "[", "idx", "]", "=", "0.173", "-", "0.140", "*", "np", ".", "log", "(", "mean", "[", "idx", "]", ")", "stddevs", "=", "[", "]", "for", "stddev", "in", "stddev_types", ":", "if", "stddev", "==", "const", ".", "StdDev", ".", "TOTAL", ":", "stddevs", ".", "append", "(", "sigma", ")", "return", "stddevs" ]
Returns the standard deviations from mean (pg 164; more robust than estimate using magnitude)
[ "Returns", "the", "standard", "deviations", "from", "mean", "(", "pg", "164", ";", "more", "robust", "than", "estimate", "using", "magnitude", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/campbell_1997.py#L120-L134
gem/oq-engine
openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py
TavakoliPezeshk2005.get_mean_and_stddevs
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): """ See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values. """ # extracting dictionary of coefficients C = self.COEFFS[imt] mag = self._convert_magnitude(rup.mag) # computing the magnitude term. Equation 19, page 2291 f1 = self._compute_magnitude_scaling_term(C, mag) # computing the geometrical spreading term. Equation 20, page 2291 f2 = self._compute_geometrical_spreading_term(C, dists.rrup) # computing the anelastic attenuation term. Equation 21, page 2291 f3 = self._compute_anelastic_attenuation_term(C, dists.rrup, mag) # computing the mean ln(IMT) using equation 18 at page 2290 mean = f1 + f2 + f3 mean = self._clip_mean(imt, mean) # computing the total standard deviation stddevs = self._get_stddevs(C, stddev_types, num_sites=len(dists.rrup), mag=mag) return mean, stddevs
python
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types): C = self.COEFFS[imt] mag = self._convert_magnitude(rup.mag) f1 = self._compute_magnitude_scaling_term(C, mag) f2 = self._compute_geometrical_spreading_term(C, dists.rrup) f3 = self._compute_anelastic_attenuation_term(C, dists.rrup, mag) mean = f1 + f2 + f3 mean = self._clip_mean(imt, mean) stddevs = self._get_stddevs(C, stddev_types, num_sites=len(dists.rrup), mag=mag) return mean, stddevs
[ "def", "get_mean_and_stddevs", "(", "self", ",", "sites", ",", "rup", ",", "dists", ",", "imt", ",", "stddev_types", ")", ":", "# extracting dictionary of coefficients", "C", "=", "self", ".", "COEFFS", "[", "imt", "]", "mag", "=", "self", ".", "_convert_magnitude", "(", "rup", ".", "mag", ")", "# computing the magnitude term. Equation 19, page 2291", "f1", "=", "self", ".", "_compute_magnitude_scaling_term", "(", "C", ",", "mag", ")", "# computing the geometrical spreading term. Equation 20, page 2291", "f2", "=", "self", ".", "_compute_geometrical_spreading_term", "(", "C", ",", "dists", ".", "rrup", ")", "# computing the anelastic attenuation term. Equation 21, page 2291", "f3", "=", "self", ".", "_compute_anelastic_attenuation_term", "(", "C", ",", "dists", ".", "rrup", ",", "mag", ")", "# computing the mean ln(IMT) using equation 18 at page 2290", "mean", "=", "f1", "+", "f2", "+", "f3", "mean", "=", "self", ".", "_clip_mean", "(", "imt", ",", "mean", ")", "# computing the total standard deviation", "stddevs", "=", "self", ".", "_get_stddevs", "(", "C", ",", "stddev_types", ",", "num_sites", "=", "len", "(", "dists", ".", "rrup", ")", ",", "mag", "=", "mag", ")", "return", "mean", ",", "stddevs" ]
See :meth:`superclass method <.base.GroundShakingIntensityModel.get_mean_and_stddevs>` for spec of input and result values.
[ "See", ":", "meth", ":", "superclass", "method", "<", ".", "base", ".", "GroundShakingIntensityModel", ".", "get_mean_and_stddevs", ">", "for", "spec", "of", "input", "and", "result", "values", "." ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py#L73-L102
gem/oq-engine
openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py
TavakoliPezeshk2005._get_stddevs
def _get_stddevs(self, C, stddev_types, num_sites, mag): """ Returns standard deviation as defined in equation 23, page 2291 (Tavakoli and Pezeshk, 2005) """ stddevs = [] sigma = (C['c14'] + C['c15'] * mag) if mag < 7.2 else C['c16'] vals = sigma * np.ones((num_sites)) for _ in stddev_types: stddevs.append(vals) return stddevs
python
def _get_stddevs(self, C, stddev_types, num_sites, mag): stddevs = [] sigma = (C['c14'] + C['c15'] * mag) if mag < 7.2 else C['c16'] vals = sigma * np.ones((num_sites)) for _ in stddev_types: stddevs.append(vals) return stddevs
[ "def", "_get_stddevs", "(", "self", ",", "C", ",", "stddev_types", ",", "num_sites", ",", "mag", ")", ":", "stddevs", "=", "[", "]", "sigma", "=", "(", "C", "[", "'c14'", "]", "+", "C", "[", "'c15'", "]", "*", "mag", ")", "if", "mag", "<", "7.2", "else", "C", "[", "'c16'", "]", "vals", "=", "sigma", "*", "np", ".", "ones", "(", "(", "num_sites", ")", ")", "for", "_", "in", "stddev_types", ":", "stddevs", ".", "append", "(", "vals", ")", "return", "stddevs" ]
Returns standard deviation as defined in equation 23, page 2291 (Tavakoli and Pezeshk, 2005)
[ "Returns", "standard", "deviation", "as", "defined", "in", "equation", "23", "page", "2291", "(", "Tavakoli", "and", "Pezeshk", "2005", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py#L104-L114
gem/oq-engine
openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py
TavakoliPezeshk2005._compute_magnitude_scaling_term
def _compute_magnitude_scaling_term(self, C, mag): """ Compute magnitude scaling term as defined in equation 19, page 2291 (Tavakoli and Pezeshk, 2005) """ assert mag <= 8.5 return C['c1'] + C['c2'] * mag + C['c3'] * (8.5 - mag) ** 2.5
python
def _compute_magnitude_scaling_term(self, C, mag): assert mag <= 8.5 return C['c1'] + C['c2'] * mag + C['c3'] * (8.5 - mag) ** 2.5
[ "def", "_compute_magnitude_scaling_term", "(", "self", ",", "C", ",", "mag", ")", ":", "assert", "mag", "<=", "8.5", "return", "C", "[", "'c1'", "]", "+", "C", "[", "'c2'", "]", "*", "mag", "+", "C", "[", "'c3'", "]", "*", "(", "8.5", "-", "mag", ")", "**", "2.5" ]
Compute magnitude scaling term as defined in equation 19, page 2291 (Tavakoli and Pezeshk, 2005)
[ "Compute", "magnitude", "scaling", "term", "as", "defined", "in", "equation", "19", "page", "2291", "(", "Tavakoli", "and", "Pezeshk", "2005", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py#L131-L137
gem/oq-engine
openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py
TavakoliPezeshk2005._compute_geometrical_spreading_term
def _compute_geometrical_spreading_term(self, C, rrup): """ Compute magnitude scaling term as defined in equation 19, page 2291 (Tavakoli and Pezeshk, 2005) """ f2 = np.ones_like(rrup) idx1 = np.nonzero(rrup <= 70.) idx2 = np.nonzero((rrup > 70.) & (rrup <= 130.)) idx3 = np.nonzero(rrup > 130.) f2[idx1] = (C['c9'] * np.log(rrup[idx1] + 4.5)) f2[idx2] = (C['c10'] * np.log(rrup[idx2]/70.) + C['c9'] * np.log(rrup[idx2] + 4.5)) f2[idx3] = (C['c11'] * np.log(rrup[idx3]/130.) + C['c10'] * np.log(rrup[idx3]/70.) + C['c9'] * np.log(rrup[idx3] + 4.5)) return f2
python
def _compute_geometrical_spreading_term(self, C, rrup): f2 = np.ones_like(rrup) idx1 = np.nonzero(rrup <= 70.) idx2 = np.nonzero((rrup > 70.) & (rrup <= 130.)) idx3 = np.nonzero(rrup > 130.) f2[idx1] = (C['c9'] * np.log(rrup[idx1] + 4.5)) f2[idx2] = (C['c10'] * np.log(rrup[idx2]/70.) + C['c9'] * np.log(rrup[idx2] + 4.5)) f2[idx3] = (C['c11'] * np.log(rrup[idx3]/130.) + C['c10'] * np.log(rrup[idx3]/70.) + C['c9'] * np.log(rrup[idx3] + 4.5)) return f2
[ "def", "_compute_geometrical_spreading_term", "(", "self", ",", "C", ",", "rrup", ")", ":", "f2", "=", "np", ".", "ones_like", "(", "rrup", ")", "idx1", "=", "np", ".", "nonzero", "(", "rrup", "<=", "70.", ")", "idx2", "=", "np", ".", "nonzero", "(", "(", "rrup", ">", "70.", ")", "&", "(", "rrup", "<=", "130.", ")", ")", "idx3", "=", "np", ".", "nonzero", "(", "rrup", ">", "130.", ")", "f2", "[", "idx1", "]", "=", "(", "C", "[", "'c9'", "]", "*", "np", ".", "log", "(", "rrup", "[", "idx1", "]", "+", "4.5", ")", ")", "f2", "[", "idx2", "]", "=", "(", "C", "[", "'c10'", "]", "*", "np", ".", "log", "(", "rrup", "[", "idx2", "]", "/", "70.", ")", "+", "C", "[", "'c9'", "]", "*", "np", ".", "log", "(", "rrup", "[", "idx2", "]", "+", "4.5", ")", ")", "f2", "[", "idx3", "]", "=", "(", "C", "[", "'c11'", "]", "*", "np", ".", "log", "(", "rrup", "[", "idx3", "]", "/", "130.", ")", "+", "C", "[", "'c10'", "]", "*", "np", ".", "log", "(", "rrup", "[", "idx3", "]", "/", "70.", ")", "+", "C", "[", "'c9'", "]", "*", "np", ".", "log", "(", "rrup", "[", "idx3", "]", "+", "4.5", ")", ")", "return", "f2" ]
Compute magnitude scaling term as defined in equation 19, page 2291 (Tavakoli and Pezeshk, 2005)
[ "Compute", "magnitude", "scaling", "term", "as", "defined", "in", "equation", "19", "page", "2291", "(", "Tavakoli", "and", "Pezeshk", "2005", ")" ]
train
https://github.com/gem/oq-engine/blob/8294553a0b8aba33fd96437a35065d03547d0040/openquake/hazardlib/gsim/tavakoli_pezeshk_2005.py#L139-L155