Input/output functions (starclass.io
)
Input/output functions.
Code author: Rasmus Handberg <rasmush@phys.au.dk>
- class starclass.io.NumpyJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
JSONEncoder
JSONEncoder class which can automatically encode numpy and Enum objects.
Can be used as input for
json.dump()
andjson.dumps()
.Code author: Rasmus Handberg <rasmush@phys.au.dk>
- __init__(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.
If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.
If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is
None
and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a
TypeError
.
- default(obj)[source]
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- encode(o)
Return a JSON string representation of a Python data structure.
>>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
- iterencode(o, _one_shot=False)
Encode the given object and yield each string representation as available.
For example:
for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
- item_separator = ', '
- key_separator = ': '
- starclass.io.loadJSON(fname)[source]
Load an object from a JSON file.
- Parameters:
fname (str) – File name to load to. If the name ends in ‘.gz’ the file will be automatically unzipped.
- Returns:
The object from the file.
- Return type:
object
Code author: Rasmus Handberg <rasmush@phys.au.dk>
- starclass.io.loadPickle(fname)[source]
Load an object from file using pickle.
- Parameters:
fname (str) – File name to load from. If the name ends in ‘.gz’ the file will be automatically unzipped.
- Returns:
The unpickled object from the file.
- Return type:
object
Code author: Rasmus Handberg <rasmush@phys.au.dk>
- starclass.io.load_lightcurve(fname, starid=None, truncate_lightcurve=False, exclude_bad_data=True)[source]
Load light curve from file.
- Parameters:
fname (str) – Path to file to be loaded.
starid (int) – Star identifier (TIC/KIC/EPIC number) to be added to lightcurve object. This is only used for file types where the number can not be determined from the file itself.
truncate_lightcurve (bool) – Truncate lightcurve to 27.4 days length, corresponding to the nominal length of a TESS observing sector. This is only applied to Kepler/K2 data.
exclude_bad_data (bool) – Exclude data based on quality flags.
- Returns:
Lightcurve object.
- Return type:
lightkurve.LightCurve
- Raises:
ValueError – On invalid file format.
Code author: Rasmus Handberg <rasmush@phys.au.dk>
- starclass.io.saveJSON(fname, obj)[source]
Save an object to JSON file.
- Parameters:
fname (str) – File name to save to. If the name ends in ‘.gz’ the file will be automatically gzipped.
obj (object) – Any pickalble object to be saved to file.
Code author: Rasmus Handberg <rasmush@phys.au.dk>
- starclass.io.savePickle(fname, obj)[source]
Save an object to file using pickle.
- Parameters:
fname (str) – File name to save to. If the name ends in ‘.gz’ the file will be automatically gzipped.
obj (object) – Any pickalble object to be saved to file.
Code author: Rasmus Handberg <rasmush@phys.au.dk>
- starclass.io.PICKLE_DEFAULT_PROTOCOL = 4
Default protocol to use for saving pickle files.