mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-15 03:53:41 +02:00
Merge branch 'develop' into documentation
This commit is contained in:
commit
c0cce90702
2 changed files with 43 additions and 25 deletions
|
@ -1,13 +1,13 @@
|
||||||
import json
|
import json
|
||||||
import gzip
|
import gzip
|
||||||
from ..obs import Obs
|
import numpy as np
|
||||||
import getpass
|
import getpass
|
||||||
import socket
|
import socket
|
||||||
import datetime
|
import datetime
|
||||||
from .. import version as pyerrorsversion
|
|
||||||
import platform
|
import platform
|
||||||
import numpy as np
|
|
||||||
import warnings
|
import warnings
|
||||||
|
from ..obs import Obs
|
||||||
|
from .. import version as pyerrorsversion
|
||||||
|
|
||||||
|
|
||||||
def create_json_string(ol, description='', indent=1):
|
def create_json_string(ol, description='', indent=1):
|
||||||
|
@ -15,13 +15,13 @@ def create_json_string(ol, description='', indent=1):
|
||||||
to a .json(.gz) file
|
to a .json(.gz) file
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
-----------------
|
----------
|
||||||
ol : list
|
ol : list
|
||||||
List of objects that will be exported. At the moments, these objects can be
|
List of objects that will be exported. At the moments, these objects can be
|
||||||
either of: Obs, list, np.ndarray
|
either of: Obs, list, numpy.ndarray.
|
||||||
All Obs inside a structure have to be defined on the same set of configurations.
|
All Obs inside a structure have to be defined on the same set of configurations.
|
||||||
description : str
|
description : str
|
||||||
Optional string that describes the contents of the json file
|
Optional string that describes the contents of the json file.
|
||||||
indent : int
|
indent : int
|
||||||
Specify the indentation level of the json file. None or 0 is permissible and
|
Specify the indentation level of the json file. None or 0 is permissible and
|
||||||
saves disk space.
|
saves disk space.
|
||||||
|
@ -33,7 +33,7 @@ def create_json_string(ol, description='', indent=1):
|
||||||
_default.default = json.JSONEncoder().default
|
_default.default = json.JSONEncoder().default
|
||||||
my_encoder.default = _default
|
my_encoder.default = _default
|
||||||
|
|
||||||
class deltalist:
|
class Deltalist:
|
||||||
def __init__(self, li):
|
def __init__(self, li):
|
||||||
self.cnfg = li[0]
|
self.cnfg = li[0]
|
||||||
self.deltas = li[1:]
|
self.deltas = li[1:]
|
||||||
|
@ -64,7 +64,7 @@ def create_json_string(ol, description='', indent=1):
|
||||||
rd['deltas'].append([ol[0].idl[r_name][i]])
|
rd['deltas'].append([ol[0].idl[r_name][i]])
|
||||||
for o in ol:
|
for o in ol:
|
||||||
rd['deltas'][-1].append(o.deltas[r_name][i])
|
rd['deltas'][-1].append(o.deltas[r_name][i])
|
||||||
rd['deltas'][-1] = deltalist(rd['deltas'][-1])
|
rd['deltas'][-1] = Deltalist(rd['deltas'][-1])
|
||||||
ed['replica'].append(rd)
|
ed['replica'].append(rd)
|
||||||
dl.append(ed)
|
dl.append(ed)
|
||||||
return dl
|
return dl
|
||||||
|
@ -72,15 +72,16 @@ def create_json_string(ol, description='', indent=1):
|
||||||
def _assert_equal_properties(ol, otype=Obs):
|
def _assert_equal_properties(ol, otype=Obs):
|
||||||
for o in ol:
|
for o in ol:
|
||||||
if not isinstance(o, otype):
|
if not isinstance(o, otype):
|
||||||
raise Exception('Wrong data type in list!')
|
raise Exception("Wrong data type in list.")
|
||||||
for o in ol[1:]:
|
for o in ol[1:]:
|
||||||
if not ol[0].is_merged == o.is_merged:
|
if not ol[0].is_merged == o.is_merged:
|
||||||
raise Exception('All Obs in list have to be defined on the same set of configs!')
|
raise Exception("All Obs in list have to be defined on the same set of configs.")
|
||||||
if not ol[0].reweighted == o.reweighted:
|
if not ol[0].reweighted == o.reweighted:
|
||||||
raise Exception('All Obs in list have to have the same property .reweighted!')
|
raise Exception("All Obs in list have to have the same property 'reweighted'.")
|
||||||
if not ol[0].e_content == o.e_content:
|
if not ol[0].e_content == o.e_content:
|
||||||
raise Exception('All Obs in list have to be defined on the same set of configs!')
|
raise Exception("All Obs in list have to be defined on the same set of configs.")
|
||||||
# more stringend tests --> compare idl?
|
if not ol[0].idl == o.idl:
|
||||||
|
raise Exception("All Obs in list have to be defined on the same set of configurations.")
|
||||||
|
|
||||||
def write_Obs_to_dict(o):
|
def write_Obs_to_dict(o):
|
||||||
d = {}
|
d = {}
|
||||||
|
@ -106,7 +107,6 @@ def create_json_string(ol, description='', indent=1):
|
||||||
d['reweighted'] = ol[0].reweighted
|
d['reweighted'] = ol[0].reweighted
|
||||||
d['value'] = [o.value for o in ol]
|
d['value'] = [o.value for o in ol]
|
||||||
d['data'] = _gen_data_d_from_list(ol)
|
d['data'] = _gen_data_d_from_list(ol)
|
||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def write_Array_to_dict(oa):
|
def write_Array_to_dict(oa):
|
||||||
|
@ -123,13 +123,15 @@ def create_json_string(ol, description='', indent=1):
|
||||||
d['value'] = [o.value for o in ol]
|
d['value'] = [o.value for o in ol]
|
||||||
d['data'] = _gen_data_d_from_list(ol)
|
d['data'] = _gen_data_d_from_list(ol)
|
||||||
return d
|
return d
|
||||||
|
|
||||||
if not isinstance(ol, list):
|
if not isinstance(ol, list):
|
||||||
ol = [ol]
|
ol = [ol]
|
||||||
|
|
||||||
d = {}
|
d = {}
|
||||||
d['program'] = 'pyerrors %s' % (pyerrorsversion.__version__)
|
d['program'] = 'pyerrors %s' % (pyerrorsversion.__version__)
|
||||||
d['version'] = '0.1'
|
d['version'] = '0.1'
|
||||||
d['who'] = getpass.getuser()
|
d['who'] = getpass.getuser()
|
||||||
d['date'] = str(datetime.datetime.now())[:-7]
|
d['date'] = datetime.datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S %Z')
|
||||||
d['host'] = socket.gethostname() + ', ' + platform.platform()
|
d['host'] = socket.gethostname() + ', ' + platform.platform()
|
||||||
|
|
||||||
if description:
|
if description:
|
||||||
|
@ -145,9 +147,10 @@ def create_json_string(ol, description='', indent=1):
|
||||||
|
|
||||||
jsonstring = json.dumps(d, indent=indent, cls=my_encoder, ensure_ascii=False)
|
jsonstring = json.dumps(d, indent=indent, cls=my_encoder, ensure_ascii=False)
|
||||||
|
|
||||||
# workaround for un-quoting of delta lists, adds 5% of work
|
|
||||||
# but is save, compared to a simple replace that could destroy the structure
|
|
||||||
def remove_quotationmarks(s):
|
def remove_quotationmarks(s):
|
||||||
|
"""Workaround for un-quoting of delta lists, adds 5% of work
|
||||||
|
but is save, compared to a simple replace that could destroy the structure
|
||||||
|
"""
|
||||||
deltas = False
|
deltas = False
|
||||||
split = s.split('\n')
|
split = s.split('\n')
|
||||||
for i in range(len(split)):
|
for i in range(len(split)):
|
||||||
|
@ -167,15 +170,15 @@ def dump_to_json(ol, fname, description='', indent=1, gz=True):
|
||||||
"""Export a list of Obs or structures containing Obs to a .json(.gz) file
|
"""Export a list of Obs or structures containing Obs to a .json(.gz) file
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
-----------------
|
----------
|
||||||
ol : list
|
ol : list
|
||||||
List of objects that will be exported. At the moments, these objects can be
|
List of objects that will be exported. At the moments, these objects can be
|
||||||
either of: Obs, list, np.ndarray
|
either of: Obs, list, numpy.ndarray.
|
||||||
All Obs inside a structure have to be defined on the same set of configurations.
|
All Obs inside a structure have to be defined on the same set of configurations.
|
||||||
fname : str
|
fname : str
|
||||||
Filename of the output file
|
Filename of the output file.
|
||||||
description : str
|
description : str
|
||||||
Optional string that describes the contents of the json file
|
Optional string that describes the contents of the json file.
|
||||||
indent : int
|
indent : int
|
||||||
Specify the indentation level of the json file. None or 0 is permissible and
|
Specify the indentation level of the json file. None or 0 is permissible and
|
||||||
saves disk space.
|
saves disk space.
|
||||||
|
@ -202,13 +205,14 @@ def dump_to_json(ol, fname, description='', indent=1, gz=True):
|
||||||
|
|
||||||
def load_json(fname, verbose=True, gz=True, full_output=False):
|
def load_json(fname, verbose=True, gz=True, full_output=False):
|
||||||
"""Import a list of Obs or structures containing Obs from a .json.gz file.
|
"""Import a list of Obs or structures containing Obs from a .json.gz file.
|
||||||
The following structures are supported: Obs, list, np.ndarray
|
|
||||||
|
The following structures are supported: Obs, list, numpy.ndarray
|
||||||
If the list contains only one element, it is unpacked from the list.
|
If the list contains only one element, it is unpacked from the list.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
-----------------
|
----------
|
||||||
fname : str
|
fname : str
|
||||||
Filename of the input file
|
Filename of the input file.
|
||||||
verbose : bool
|
verbose : bool
|
||||||
Print additional information that was written to the file.
|
Print additional information that was written to the file.
|
||||||
gz : bool
|
gz : bool
|
||||||
|
|
|
@ -584,6 +584,20 @@ def test_covobs():
|
||||||
|
|
||||||
|
|
||||||
def test_covobs_overloading():
|
def test_covobs_overloading():
|
||||||
covobs = pe.cov_Obs([0.5, 0.5], np.array([[0.02, 0.02], [0.02, 0.02]]), 'Zfactor')
|
covobs = pe.cov_Obs([0.5, 0.5], np.array([[0.02, 0.02], [0.02, 0.02]]), 'test')
|
||||||
assert (covobs[0] / covobs[1]) == 1
|
assert (covobs[0] / covobs[1]) == 1
|
||||||
assert (covobs[0] - covobs[1]) == 0
|
assert (covobs[0] - covobs[1]) == 0
|
||||||
|
|
||||||
|
my_obs = pe.pseudo_Obs(2.3, 0.2, 'obs')
|
||||||
|
|
||||||
|
assert (my_obs * covobs[0] / covobs[1]) == my_obs
|
||||||
|
|
||||||
|
covobs = pe.cov_Obs(0.0, 0.3, 'test')
|
||||||
|
assert not covobs.is_zero()
|
||||||
|
|
||||||
|
|
||||||
|
def test_covobs_name_collision():
|
||||||
|
covobs = pe.cov_Obs(0.5, 0.002, 'test')
|
||||||
|
my_obs = pe.pseudo_Obs(2.3, 0.2, 'test')
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
summed_obs = my_obs + covobs
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue