From 50b503b83822f53b1eee7b736004c2569fa7e9ce Mon Sep 17 00:00:00 2001 From: JanNeuendorf Date: Fri, 28 Jan 2022 10:46:02 +0100 Subject: [PATCH 1/6] Only the changes in the correlator init, GEVP and docstrings --- pyerrors/correlators.py | 88 ++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index f051855a..5a4b5170 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -28,8 +28,8 @@ class Corr: Parameters ---------- - data_input : list - list of Obs or list of arrays of Obs. + data_input : list or array + list of Obs or list of arrays of Obs or array of Corrs padding : list, optional List with two entries where the first labels the padding at the front of the correlator and the second the padding @@ -39,25 +39,53 @@ class Corr: region indentified for this correlator. """ - if not isinstance(data_input, list): - raise TypeError('Corr__init__ expects a list of timeslices.') + if isinstance(data_input, np.ndarray): # Input is an array of Corrs - if all([(isinstance(item, Obs) or isinstance(item, CObs)) or item is None for item in data_input]): - _assert_equal_properties([o for o in data_input if o is not None]) - self.content = [np.asarray([item]) if item is not None else None for item in data_input] - self.N = 1 + # This only works, if the array fulfills the conditions below + if not len(data_input.shape) == 2 and data_input.shape[0] == data_input.shape[1]: + raise Exception("Incompatible array shape") + if not all([isinstance(item, Corr) for item in data_input.flatten()]): + raise Exception("If the input is an array, its elements must be of type pe.Corr") + if not all([item.N == 1 for item in data_input.flatten()]): + raise Exception("Can only construct matrix correlator from single valued correlators") + if not len(set([item.T for item in data_input.flatten()])) == 1: + raise Exception("All input Correlators must be defined over the same timeslices.") - elif all([isinstance(item, np.ndarray) or item is None for item in data_input]) and any([isinstance(item, np.ndarray) for item in data_input]): - self.content = data_input + T = data_input[0, 0].T + N = data_input.shape[0] + input_as_list = [] + for t in range(T): + if any([(item.content[t][0] is None) for item in data_input.flatten()]): + if not all([(item.content[t][0] is None) for item in data_input.flatten()]): + warnings.warn("Input ill-defined at different timeslices. Conversion leads to data loss!", RuntimeWarning) + input_as_list.append(None) + else: + array_at_timeslace = np.empty([N, N], dtype="object") + for i in range(N): + for j in range(N): + array_at_timeslace[i, j] = data_input[i, j][t] + input_as_list.append(array_at_timeslace) + data_input = input_as_list - noNull = [a for a in self.content if not (a is None)] # To check if the matrices are correct for all undefined elements - self.N = noNull[0].shape[0] - if self.N > 1 and noNull[0].shape[0] != noNull[0].shape[1]: - raise Exception("Smearing matrices are not NxN") - if (not all([item.shape == noNull[0].shape for item in noNull])): - raise Exception("Items in data_input are not of identical shape." + str(noNull)) + if isinstance(data_input, list): + + if all([(isinstance(item, Obs) or isinstance(item, CObs)) for item in data_input]): + _assert_equal_properties(data_input) + self.content = [np.asarray([item]) for item in data_input] + self.N = 1 + + elif all([isinstance(item, np.ndarray) or item is None for item in data_input]) and any([isinstance(item, np.ndarray) for item in data_input]): + self.content = data_input + noNull = [a for a in self.content if not (a is None)] # To check if the matrices are correct for all undefined elements + self.N = noNull[0].shape[0] + if self.N > 1 and noNull[0].shape[0] != noNull[0].shape[1]: + raise Exception("Smearing matrices are not NxN") + if (not all([item.shape == noNull[0].shape for item in noNull])): + raise Exception("Items in data_input are not of identical shape." + str(noNull)) + else: + raise Exception("data_input contains item of wrong type") else: - raise Exception("data_input contains item of wrong type") + raise Exception("Data input was not given as list or correct array") self.tag = None @@ -214,8 +242,30 @@ class Corr: # There are two ways, the GEVP metod can be called. # 1. return_list=False will return a single eigenvector, normalized according to V*C(t_0)*V=1 # 2. return_list=True will return a new eigenvector for every timeslice. The time t_s is used to order the vectors according to. arXiv:2004.10472 [hep-lat] - def GEVP(self, t0, ts, state=0, sorting="Eigenvalue", return_list=False): + def GEVP(self, t0, ts=None, state=0, return_list=False, sorting="Eigenvalue"): + """Solve the general eigenvalue problem on the current correlator + + Parameters + ---------- + t0 : int + The time t0 for G(t)v= lambda G(t_0)v + ts : int + fixed time G(t_s)v= lambda G(t_0)v if return_list=False + If return_list=True and sorting=Eigenvector it gives a reference point for the sorting method. + state : int + The state one is interested in ordered by energy. The lowest state is zero. + return_list : bool + If False - The vector $v$ with G(t_s)v= lambda_state G(t_0)v is returned. + If True - The GEVP is solved once per timeslice and a list (len=T) of vectors is returned. + sorting : string + Only matters if return_list=True. Determines how the vectors returned at every timeslice are chosen. + "Eigenvalue" - The eigenvector is chosen according to which einvenvalue it belongs individually on every timeslice. + "Eigenvector" - Use the method described in arXiv:2004.10472 [hep-lat] to find the set of v(t) belonging to the state. + The referense state is identified by its eigenvalue at t=ts + """ if not return_list: + if (ts is None): + raise Exception("ts is required if return_list=False") if (self.content[t0] is None) or (self.content[ts] is None): raise Exception("Corr not defined at t0/ts") G0, Gt = np.empty([self.N, self.N], dtype="double"), np.empty([self.N, self.N], dtype="double") @@ -246,6 +296,8 @@ class Corr: except Exception: all_vecs.append(None) if sorting == "Eigenvector": + if (ts is None): + raise Exception("ts is required for the Eigenvector sorting method.") all_vecs = _sort_vectors(all_vecs, ts) all_vecs = [a[state] for a in all_vecs] From 8db648ee631df900733c0f706f225884749eac86 Mon Sep 17 00:00:00 2001 From: JanNeuendorf Date: Fri, 28 Jan 2022 10:57:01 +0100 Subject: [PATCH 2/6] Try to keep it compatible with the tests. --- pyerrors/correlators.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index 5a4b5170..baf62277 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -72,6 +72,9 @@ class Corr: if all([(isinstance(item, Obs) or isinstance(item, CObs)) for item in data_input]): _assert_equal_properties(data_input) self.content = [np.asarray([item]) for item in data_input] + if all([(isinstance(item, Obs) or isinstance(item, CObs)) or item is None for item in data_input]): + _assert_equal_properties([o for o in data_input if o is not None]) + self.content = [np.asarray([item]) if item is not None else None for item in data_input] self.N = 1 elif all([isinstance(item, np.ndarray) or item is None for item in data_input]) and any([isinstance(item, np.ndarray) for item in data_input]): From 0bac8a417a530762c8c8046e355e4b2dd7232912 Mon Sep 17 00:00:00 2001 From: JanNeuendorf Date: Fri, 28 Jan 2022 11:31:07 +0100 Subject: [PATCH 3/6] argument name changed in GEVP --- pyerrors/correlators.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index baf62277..982afb85 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -245,7 +245,7 @@ class Corr: # There are two ways, the GEVP metod can be called. # 1. return_list=False will return a single eigenvector, normalized according to V*C(t_0)*V=1 # 2. return_list=True will return a new eigenvector for every timeslice. The time t_s is used to order the vectors according to. arXiv:2004.10472 [hep-lat] - def GEVP(self, t0, ts=None, state=0, return_list=False, sorting="Eigenvalue"): + def GEVP(self, t0, ts=None, state=0, sorted_list=None): """Solve the general eigenvalue problem on the current correlator Parameters @@ -257,16 +257,13 @@ class Corr: If return_list=True and sorting=Eigenvector it gives a reference point for the sorting method. state : int The state one is interested in ordered by energy. The lowest state is zero. - return_list : bool - If False - The vector $v$ with G(t_s)v= lambda_state G(t_0)v is returned. - If True - The GEVP is solved once per timeslice and a list (len=T) of vectors is returned. - sorting : string - Only matters if return_list=True. Determines how the vectors returned at every timeslice are chosen. - "Eigenvalue" - The eigenvector is chosen according to which einvenvalue it belongs individually on every timeslice. - "Eigenvector" - Use the method described in arXiv:2004.10472 [hep-lat] to find the set of v(t) belonging to the state. + sorted list : string + if this argument is set, a list of vectors (len=self.T) is returned. If it is left as None, only one vector is returned. + "Eigenvalue" - The eigenvector is chosen according to which einvenvalue it belongs individually on every timeslice. + "Eigenvector" - Use the method described in arXiv:2004.10472 [hep-lat] to find the set of v(t) belonging to the state. The referense state is identified by its eigenvalue at t=ts """ - if not return_list: + if sorted_list is None: if (ts is None): raise Exception("ts is required if return_list=False") if (self.content[t0] is None) or (self.content[ts] is None): @@ -280,7 +277,8 @@ class Corr: sp_vecs = _GEVP_solver(Gt, G0) sp_vec = sp_vecs[state] return sp_vec - if return_list: + else: + all_vecs = [] for t in range(self.T): try: @@ -291,14 +289,14 @@ class Corr: Gt[i, j] = self.content[t][i, j].value sp_vecs = _GEVP_solver(Gt, G0) - if sorting == "Eigenvalue": + if sorted_list == "Eigenvalue": sp_vec = sp_vecs[state] all_vecs.append(sp_vec) else: all_vecs.append(sp_vecs) except Exception: all_vecs.append(None) - if sorting == "Eigenvector": + if sorted_list == "Eigenvector": if (ts is None): raise Exception("ts is required for the Eigenvector sorting method.") all_vecs = _sort_vectors(all_vecs, ts) From 0098c973af1d0fe24d5dbc78d451d9757844a73d Mon Sep 17 00:00:00 2001 From: JanNeuendorf Date: Fri, 28 Jan 2022 12:21:28 +0100 Subject: [PATCH 4/6] The json routines for corr now have a dictionary as a tag and the entry tag of this dictionary contains the old tag. This should be compatible with the old way of saving. --- pyerrors/input/json.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pyerrors/input/json.py b/pyerrors/input/json.py index c01a7f47..9704d998 100644 --- a/pyerrors/input/json.py +++ b/pyerrors/input/json.py @@ -186,6 +186,11 @@ def create_json_string(ol, description='', indent=1): dat['tag'].append(corr_meta_data) else: dat['tag'] = [corr_meta_data] + taglist = dat['tag'] + dat['tag'] = {} # tag is now a dictionary, that contains the previous taglist in the key "tag" + dat['tag']['tag'] = taglist + if my_corr.prange is not None: + dat['tag']['prange'] = my_corr.prange return dat if not isinstance(ol, list): @@ -395,7 +400,17 @@ def import_json_string(json_string, verbose=True, full_output=False): return np.reshape(ret, layout) def get_Corr_from_dict(o): - taglist = o.get('tag') + if isinstance(o.get('tag'), list): # supports the old way + taglist = o.get('tag') # This had to be modified to get the taglist from the dictionary + temp_prange = None + else: + tagdic = o.get('tag') + taglist = tagdic['tag'] + if 'prange' in tagdic: + temp_prange = tagdic['prange'] + else: + temp_prange = None + corr_tag = taglist[-1] tmp_o = o tmp_o['tag'] = taglist[:-1] @@ -405,6 +420,8 @@ def import_json_string(json_string, verbose=True, full_output=False): my_corr = Corr([None if np.isnan(o.ravel()[0].value) else o for o in list(dat)]) if corr_tag != 'None': my_corr.tag = corr_tag + + my_corr.prange = temp_prange return my_corr json_dict = json.loads(json_string) From 55804c8c1dfa65f4adf9805ed4cfc34ee0939c13 Mon Sep 17 00:00:00 2001 From: JanNeuendorf Date: Fri, 28 Jan 2022 13:19:24 +0100 Subject: [PATCH 5/6] check if the tag is a dict --- pyerrors/input/json.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyerrors/input/json.py b/pyerrors/input/json.py index 9704d998..6df1cf67 100644 --- a/pyerrors/input/json.py +++ b/pyerrors/input/json.py @@ -403,13 +403,15 @@ def import_json_string(json_string, verbose=True, full_output=False): if isinstance(o.get('tag'), list): # supports the old way taglist = o.get('tag') # This had to be modified to get the taglist from the dictionary temp_prange = None - else: + elif isinstance(o.get('tag'), dict): tagdic = o.get('tag') taglist = tagdic['tag'] if 'prange' in tagdic: temp_prange = tagdic['prange'] else: temp_prange = None + else: + raise Exception ("The tag is not a list or dict") corr_tag = taglist[-1] tmp_o = o From 696ab4503c9cc838075a49db2aaf3c6d7faf7894 Mon Sep 17 00:00:00 2001 From: JanNeuendorf Date: Fri, 28 Jan 2022 13:24:04 +0100 Subject: [PATCH 6/6] linting fixed in exception --- pyerrors/input/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyerrors/input/json.py b/pyerrors/input/json.py index 6df1cf67..6f5cf470 100644 --- a/pyerrors/input/json.py +++ b/pyerrors/input/json.py @@ -411,7 +411,7 @@ def import_json_string(json_string, verbose=True, full_output=False): else: temp_prange = None else: - raise Exception ("The tag is not a list or dict") + raise Exception("The tag is not a list or dict") corr_tag = taglist[-1] tmp_o = o