本文整理汇总了Python中astropy.utils.OrderedDict类的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict类的具体用法?Python OrderedDict怎么用?Python OrderedDict使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OrderedDict类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, inp):
if not isinstance(inp, OrderedDict):
try:
inp = OrderedDict(inp)
except (TypeError,ValueError):
raise ValueError("Input to TableList must be an OrderedDict or list of (k,v) pairs")
self._dict = inp
super(TableList,self).__init__(inp.values())
示例2: __init__
def __init__(self, inp):
if not isinstance(inp, OrderedDict):
# py3 doesn't let you catch 2 types of errors.
errmsg = "Input to TableList must be an OrderedDict or list of (k,v) pairs"
try:
inp = OrderedDict(inp)
except (TypeError, ValueError):
raise ValueError("Input to TableList must be an OrderedDict or list of (k,v) pairs")
self._dict = inp
super(TableList, self).__init__(inp.values())
示例3: _parse_result
def _parse_result(self, response, get_catalog_names=False, verbose=False):
"""
Parses the HTTP response to create an `astropy.table.Table`.
Returns the raw result as a string in case of parse errors.
Parameters
----------
response : `requests.Response`
The response of the HTTP POST request
get_catalog_names : bool
If specified, return only the table names (useful for table
discovery)
Returns
-------
`astroquery.utils.commons.TableList`
An OrderedDict of `astropy.table.Table` objects.
If there are errors in the parsing, then returns the raw results as a string.
"""
if not verbose:
commons.suppress_vo_warnings()
try:
tf = tempfile.NamedTemporaryFile()
if PY3:
tf.write(response.content)
else:
tf.write(response.content.encode('utf-8'))
tf.file.flush()
vo_tree = votable.parse(tf.name, pedantic=False)
if get_catalog_names:
return dict([(R.name,R) for R in vo_tree.resources])
else:
table_dict = OrderedDict()
for t in vo_tree.iter_tables():
if len(t.array) > 0:
if t.ref is not None:
name = vo_tree.get_table_by_id(t.ref).name
else:
name = t.name
if name not in table_dict.keys():
table_dict[name] = []
table_dict[name] += [t.to_table()]
for name in table_dict.keys():
if len(table_dict[name]) > 1:
table_dict[name] = tbl.vstack(table_dict[name])
else:
table_dict[name] = table_dict[name][0]
return commons.TableList(table_dict)
except:
traceback.print_exc() # temporary for debugging
warnings.warn(
"Error in parsing result, returning raw result instead")
return response.content
示例4: VizierKeyword
class VizierKeyword(list):
"""Helper class for setting keywords for Vizier queries"""
def __init__(self, keywords):
file_name = aud.get_pkg_data_filename(
os.path.join("data", "inverse_dict.json"))
with open(file_name, 'r') as f:
kwd = json.load(f)
self.keyword_types = sorted(kwd.values())
self.keyword_dict = OrderedDict([(k, kwd[k]) for k in sorted(kwd)])
self._keywords = None
self.keywords = keywords
@property
def keywords(self):
"""List or string for keyword(s) that must be set for the Vizier object."""
return self._keywords
@keywords.setter
def keywords(self, values):
if isinstance(values, six.string_types):
values = list(values)
keys = [key.lower() for key in self.keyword_dict]
values = [val.lower() for val in values]
# warn about unknown keywords
for val in set(values) - set(keys):
warnings.warn("{val} : No such keyword".format(val=val))
valid_keys = [
key for key in self.keyword_dict.keys()
if key.lower() in list(map(str.lower, values))]
# create a dict for each type of keyword
set_keywords = OrderedDict()
for key in self.keyword_dict:
if key in valid_keys:
if self.keyword_dict[key] in set_keywords:
set_keywords[self.keyword_dict[key]].append(key)
else:
set_keywords[self.keyword_dict[key]] = [key]
self._keywords = OrderedDict(
[(k, sorted(set_keywords[k]))
for k in set_keywords]
)
@keywords.deleter
def keywords(self):
del self._keywords
def __repr__(self):
return "\n".join([x for key in self.keywords for x in self.get_keyword_str(key)])
def get_keyword_str(self, key):
"""
Helper function that returns the keywords, grouped into appropriate
categories and suitable for the Vizier votable CGI.
Comma-separated is not valid!!!
"""
keyword_name = "-kw." + key
return [keyword_name + "=" + s for s in self.keywords[key]]
示例5: _construct_ordered_GET
def _construct_ordered_GET(self):
"""
Construct a Global Edge Table (GET)
The GET is an OrderedDict. Keys are scan line numbers,
ordered from bbox.ymin to bbox.ymax, where bbox is the
bounding box of the polygon.
Values are lists of edges for which edge.ymin==scan_line_number.
Returns
-------
GET: OrderedDict
{scan_line: [edge1, edge2]}
"""
# edges is a list of Edge objects which define a polygon
# with these vertices
edges = self.get_edges()
GET = OrderedDict.fromkeys(self._scan_line_range)
ymin = np.asarray([e._ymin for e in edges])
for i in self._scan_line_range:
ymin_ind = (ymin == i).nonzero()[0]
if ymin_ind.any():
GET[i] = [edges[ymin_ind[0]]]
for j in ymin_ind[1:]:
GET[i].append(edges[j])
return GET
示例6: __init__
def __init__(self, keywords):
file_name = aud.get_pkg_data_filename(os.path.join("data", "inverse_dict.json"))
with open(file_name, "r") as f:
kwd = json.load(f)
self.keyword_types = sorted(kwd.values())
self.keyword_dict = OrderedDict([(k, kwd[k]) for k in sorted(kwd)])
self._keywords = None
self.keywords = keywords
示例7: parse_vizier_votable
def parse_vizier_votable(data, verbose=False, invalid='warn',
get_catalog_names=False):
"""
Given a votable as string, parse it into tables
"""
if not verbose:
commons.suppress_vo_warnings()
tf = BytesIO(data)
if invalid == 'mask':
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
elif invalid == 'warn':
try:
vo_tree = votable.parse(tf, pedantic=False, invalid='exception')
except Exception as ex:
warnings.warn("VOTABLE parsing raised exception: {0}".format(ex))
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
elif invalid == 'exception':
vo_tree = votable.parse(tf, pedantic=False, invalid='exception')
else:
raise ValueError("Invalid keyword for 'invalid'. "
"Must be exception, mask, or warn")
if get_catalog_names:
return dict([(R.name, R) for R in vo_tree.resources])
else:
table_dict = OrderedDict()
for t in vo_tree.iter_tables():
if len(t.array) > 0:
if t.ref is not None:
name = vo_tree.get_table_by_id(t.ref).name
else:
name = t.name
if name not in table_dict.keys():
table_dict[name] = []
table_dict[name] += [t.to_table()]
for name in table_dict.keys():
if len(table_dict[name]) > 1:
table_dict[name] = tbl.vstack(table_dict[name])
else:
table_dict[name] = table_dict[name][0]
return commons.TableList(table_dict)
示例8: _parse_result
def _parse_result(self, response, get_catalog_names=False, verbose=False, invalid='warn'):
"""
Parses the HTTP response to create a `~astropy.table.Table`.
Returns the raw result as a string in case of parse errors.
Parameters
----------
response : `requests.Response`
The response of the HTTP POST request
get_catalog_names : bool
If specified, return only the table names (useful for table
discovery)
invalid : 'warn', 'mask' or 'raise'
The behavior if a VOTABLE cannot be parsed. Default is 'warn',
which will try to parse the table, then if an exception is raised,
it will be printent but the masked table will be returned
Returns
-------
table_list : `astroquery.utils.TableList` or str
If there are errors in the parsing, then returns the raw results as a string.
"""
if not verbose:
commons.suppress_vo_warnings()
try:
tf = six.BytesIO(response.content)
if invalid == 'mask':
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
elif invalid == 'warn':
try:
vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
except Exception as ex:
warnings.warn("VOTABLE parsing raised exception: {0}".format(ex))
vo_tree = votable.parse(tf, pedantic=False, invalid='mask')
elif invalid == 'raise':
vo_tree = votable.parse(tf, pedantic=False, invalid='raise')
else:
raise ValueError("Invalid keyword 'invalid'. Must be raise, mask, or warn")
if get_catalog_names:
return dict([(R.name, R) for R in vo_tree.resources])
else:
table_dict = OrderedDict()
for t in vo_tree.iter_tables():
if len(t.array) > 0:
if t.ref is not None:
name = vo_tree.get_table_by_id(t.ref).name
else:
name = t.name
if name not in table_dict.keys():
table_dict[name] = []
table_dict[name] += [t.to_table()]
for name in table_dict.keys():
if len(table_dict[name]) > 1:
table_dict[name] = tbl.vstack(table_dict[name])
else:
table_dict[name] = table_dict[name][0]
return commons.TableList(table_dict)
except Exception as ex:
self.response = response
self.table_parse_error = ex
raise TableParseError("Failed to parse VIZIER result! The raw response can be found "
"in self.response, and the error in self.table_parse_error."
" The attempted parsed result is in self.parsed_result.\n"
"Exception: " + str(self.table_parse_error))
示例9: _args_to_payload
def _args_to_payload(self, *args, **kwargs):
"""
accepts the arguments for different query functions and
builds a script suitable for the Vizier votable CGI.
"""
body = OrderedDict()
center = kwargs.get('center')
# process: catalog
catalog = kwargs.get('catalog')
if catalog is None:
catalog = self.catalog
if catalog is not None:
if isinstance(catalog, six.string_types):
body['-source'] = catalog
elif isinstance(catalog, list):
body['-source'] = ",".join(catalog)
else:
raise TypeError("Catalog must be specified as list or string")
# process: columns
columns = kwargs.get('columns')
if columns is None:
columns = copy.copy(self.columns)
else:
columns = self.columns + columns
# keyword names that can mean 'all' need to be treated separately
alls = ['all','*']
if any(x in columns for x in alls):
for x in alls:
if x in columns:
columns.remove(x)
body['-out.all'] = 2
# process: columns - always request computed positions in degrees
if "_RAJ2000" not in columns:
columns += ["_RAJ2000"]
if "_DEJ2000" not in columns:
columns += ["_DEJ2000"]
# process: columns - identify sorting requests
columns_out = []
sorts_out = []
for column in columns:
if column[0] == '+':
columns_out += [column[1:]]
sorts_out += [column[1:]]
elif column[0] == '-':
columns_out += [column[1:]]
sorts_out += [column]
else:
columns_out += [column]
body['-out.add'] = ','.join(columns_out)
body['-out'] = columns_out
if len(sorts_out) > 0:
body['-sort'] = ','.join(sorts_out)
# process: maximum rows returned
row_limit = kwargs.get('row_limit') or self.ROW_LIMIT
if row_limit < 0:
body["-out.max"] = 'unlimited'
else:
body["-out.max"] = row_limit
# process: column filters
column_filters = self.column_filters.copy()
column_filters.update(kwargs.get('column_filters', {}))
for (key, value) in column_filters.items():
body[key] = value
# process: center
if center is not None:
for (key, value) in center.items():
body[key] = value
# add column metadata: name, unit, UCD1+, and description
body["-out.meta"] = "huUD"
# merge tables when a list is queried against a single catalog
body["-out.form"] = "mini"
# computed position should always be in decimal degrees
body["-oc.form"] = "d"
ucd = kwargs.get('ucd', "") + self.ucd
if ucd:
body['-ucd'] = ucd
# create final script
script = "\n".join(["{key}={val}".format(key=key, val=val)
for key, val in body.items()])
# add keywords
if not isinstance(self.keywords, property) and self.keywords is not None:
script += "\n" + str(self.keywords)
return script
示例10: _args_to_payload
def _args_to_payload(self, *args, **kwargs):
"""
accepts the arguments for different query functions and
builds a script suitable for the Vizier votable CGI.
"""
body = OrderedDict()
center = kwargs.get('center')
# process: catalog
catalog = kwargs.get('catalog')
if catalog is None:
catalog = self.catalog
if catalog is not None:
if isinstance(catalog, basestring):
body['-source'] = catalog
elif isinstance(catalog, list):
body['-source'] = ",".join(catalog)
else:
raise TypeError("Catalog must be specified as list or string")
# process: columns
columns = kwargs.get('columns')
if columns is None:
columns = self.columns
else:
columns = self.columns + columns
# process: columns - always request computed positions in degrees
if "_RAJ2000" not in columns:
columns += ["_RAJ2000"]
if "_DEJ2000" not in columns:
columns += ["_DEJ2000"]
# process: columns - identify sorting requests
columns_out = []
sorts_out = []
for column in columns:
if column[0] == '+':
columns_out += [column[1:]]
sorts_out += [column[1:]]
elif column[0] == '-':
columns_out += [column[1:]]
sorts_out += [column]
else:
columns_out += [column]
body['-out'] = ','.join(columns_out)
if len(sorts_out)>0:
body['-sort'] = ','.join(sorts_out)
# process: maximum rows returned
if self.ROW_LIMIT < 0:
body["-out.max"] = 'unlimited'
else:
body["-out.max"] = self.ROW_LIMIT
# process: column filters
column_filters = self.column_filters.copy()
column_filters.update(kwargs.get('column_filters', {}))
for (key, value) in column_filters.items():
body[key] = value
# process: center
if center is not None:
for (key, value) in center.items():
body[key] = value
# add column metadata: name, unit, UCD1+, and description
body["-out.meta"] = "huUD"
# computed position should always be in decimal degrees
body["-oc.form"] = "d"
# create final script
script = "\n".join(["{key}={val}".format(key=key, val=val)
for key, val in body.items()])
# add keywords
if not isinstance(self.keywords, property) and self.keywords is not None:
script += "\n" + str(self.keywords)
return script
示例11: _args_to_payload
def _args_to_payload(self, *args, **kwargs):
"""
accepts the arguments for different query functions and
builds a script suitable for the Vizier votable CGI.
"""
body = OrderedDict()
caller = kwargs['caller']
del kwargs['caller']
catalog = kwargs.get('catalog')
if catalog is not None:
if isinstance(catalog, basestring):
body['-source'] = catalog
elif isinstance(catalog, list):
body['-source'] = ",".join(catalog)
else:
raise TypeError("Catalog must be specified as list or string")
if caller == 'query_object_async':
body["-c"] = args[0]
elif caller == 'query_region_async':
c = commons.parse_coordinates(args[0])
ra = str(c.icrs.ra.degree)
dec = str(c.icrs.dec.degree)
if dec[0] not in ['+', '-']:
dec = '+' + dec
body["-c"] = "".join([ra, dec])
# decide whether box or radius
if kwargs.get('radius') is not None:
radius = kwargs['radius']
unit, value = _parse_dimension(radius)
switch = "-c.r" + unit
body[switch] = value
elif kwargs.get('width') is not None:
width = kwargs['width']
w_unit, w_value = _parse_dimension(width)
switch = "-c.b" + w_unit
height = kwargs.get('height')
# is box a rectangle or square?
if height is not None:
h_unit, h_value = _parse_dimension(height)
if h_unit != w_unit:
warnings.warn(
"Converting height to same unit as width")
h_value = u.Quantity(h_value, u.Unit
(_str_to_unit(h_unit))).to(u.Unit(_str_to_unit(w_unit)))
body[switch] = "x".join([str(w_value), str(h_value)])
else:
body[switch] = "x".join([str(w_value)] * 2)
elif kwargs.get('height'):
warnings.warn(
"No width given - shape interpreted as square (height x height)")
height = kwargs['height']
h_unit, h_value = _parse_dimension(height)
switch = "-c.b" + h_unit
body[switch] = h_value
else:
raise Exception(
"At least one of radius, width/height must be specified")
# set output parameters
if not isinstance(self.columns, property) and self.columns is not None:
if "all" in self.columns:
body["-out"] = "**"
else:
out_cols = ",".join([col for col in self.columns])
# if default then return default cols and listed cols
if "default" in self.columns:
body["-out.add"] = out_cols
# else return only the listed cols
else:
body["-out"] = out_cols
# otherwise ask to return default columns
else:
body["-out"] = "*"
# set the maximum rows returned
body["-out.max"] = Vizier.ROW_LIMIT
script = "\n".join(["{key}={val}".format(key=key, val=val)
for key, val in body.items()])
# add keywords
if not isinstance(self.keywords, property) and self.keywords is not None:
script += "\n" + str(self.keywords)
# add column filters
if not isinstance(self.column_filters, property) and self.column_filters is not None:
filter_str = "\n".join(["{key}={constraint}".format(key=key, constraint=constraint) for key, constraint in
self.column_filters.items()])
script += "\n" + filter_str
return script
示例12: read_table_fits
def read_table_fits(input, hdu=None):
"""
Read a Table object from an FITS file
Parameters
----------
input : str or file-like object or compatible `astropy.io.fits` HDU object
If a string, the filename to read the table from. If a file object, or
a compatible HDU object, the object to extract the table from. The
following `astropy.io.fits` HDU objects can be used as input:
- :class:`~astropy.io.fits.hdu.table.TableHDU`
- :class:`~astropy.io.fits.hdu.table.BinTableHDU`
- :class:`~astropy.io.fits.hdu.table.GroupsHDU`
- :class:`~astropy.io.fits.hdu.hdulist.HDUList`
hdu : int or str, optional
The HDU to read the table from.
"""
if isinstance(input, basestring):
input = fits_open(input)
to_close = input
else:
to_close = None
if hasattr(input, 'read'):
input = fits_open(input)
try:
# Parse all table objects
tables = OrderedDict()
if isinstance(input, HDUList):
for ihdu, hdu_item in enumerate(input):
if isinstance(hdu_item, (TableHDU, BinTableHDU, GroupsHDU)):
tables[ihdu] = hdu_item
if len(tables) > 1:
if hdu is None:
warnings.warn("hdu= was not specified but multiple tables"
" are present, reading in first available"
" table (hdu={0})".format(tables.keys()[0]))
hdu = tables.keys()[0]
# hdu might not be an integer, so we first need to convert it
# to the correct HDU index
hdu = input.index_of(hdu)
if hdu in tables:
table = tables[hdu]
else:
raise ValueError("No table found in hdu={0}".format(hdu))
elif len(tables) == 1:
table = tables[tables.keys()[0]]
else:
raise ValueError("No table found")
elif isinstance(input, (TableHDU, BinTableHDU, GroupsHDU)):
table = input
else:
raise ValueError("Input should be a string, a file-like object, "
"an HDUList, TableHDU, BinTableHDU, or "
"GroupsHDU instance")
# Check if table is masked
masked = False
for col in table.columns:
if col.null is not None:
masked = True
break
# Convert to an astropy.table.Table object
t = Table(table.data, masked=masked)
# Copy over null values if needed
if masked:
for col in table.columns:
t[col.name].set_fill_value(col.null)
t[col.name].mask[t[col.name] == col.null] = True
# Copy over units
for col in table.columns:
if col.unit is not None:
try:
t[col.name].units = u.Unit(col.unit, format='fits')
except ValueError:
t[col.name].units = u.UnrecognizedUnit(col.unit)
# TODO: deal properly with unsigned integers
for key, value, comment in table.header.cards:
if key in ['COMMENT', 'HISTORY']:
if key in t.meta:
t.meta[key].append(value)
else:
#.........这里部分代码省略.........
示例13: _parse_result
def _parse_result(self, response, get_catalog_names=False, verbose=False):
"""
Parses the HTTP response to create a `~astropy.table.Table`.
Returns the raw result as a string in case of parse errors.
Parameters
----------
response : `requests.Response`
The response of the HTTP POST request
get_catalog_names : bool
If specified, return only the table names (useful for table
discovery)
Returns
-------
table_list : `astroquery.utils.TableList` or str
If there are errors in the parsing, then returns the raw results as a string.
"""
if not verbose:
commons.suppress_vo_warnings()
try:
tf = tempfile.NamedTemporaryFile()
if six.PY3:
# This is an exceedingly confusing section
# It is likely to be doubly wrong, but has caused issue #185
try:
# Case 1: data is read in as unicode
tf.write(response.content.encode())
except AttributeError:
# Case 2: data is read in as a byte string
tf.write(response.content.decode().encode('utf-8'))
else:
tf.write(response.content.encode('utf-8'))
tf.file.flush()
vo_tree = votable.parse(tf, pedantic=False)
if get_catalog_names:
return dict([(R.name,R) for R in vo_tree.resources])
else:
table_dict = OrderedDict()
for t in vo_tree.iter_tables():
if len(t.array) > 0:
if t.ref is not None:
name = vo_tree.get_table_by_id(t.ref).name
else:
name = t.name
if name not in table_dict.keys():
table_dict[name] = []
table_dict[name] += [t.to_table()]
for name in table_dict.keys():
if len(table_dict[name]) > 1:
table_dict[name] = tbl.vstack(table_dict[name])
else:
table_dict[name] = table_dict[name][0]
return commons.TableList(table_dict)
except Exception as ex:
self.response = response
self.table_parse_error = ex
raise TableParseError("Failed to parse VIZIER result! The raw response can be found "
"in self.response, and the error in self.table_parse_error."
" The attempted parsed result is in self.parsed_result.\n"
"Exception: " + str(self.table_parse_error))