Skip to content

Commit

Permalink
Bug fixes for saving spectra
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgaldino committed Aug 4, 2023
1 parent 42e1b1c commit c31dde8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion brixs/backpack/arraymanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import copy
from scipy.optimize import curve_fit
from .model_functions import voigt_fwhm

from collections.abc import Iterable

def index(x, value, closest=True):
"""Returns the first index of the element in array.
Expand Down
78 changes: 51 additions & 27 deletions brixs/brixs.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def __delitem__(self, item):
def _get_user_attrs(self):
"""return attrs that are user defined."""
default_attrs = ['_x', '_y', '_filepath', '_step', '_monotonicity', '_peaks']
return [key for key in self.__dict__.keys() if key not in default_attrs]
return [key for key in self.__dict__.keys() if key not in default_attrs and key.startswith('_') == False]

def _validate_ranges(self, *args, **kwargs):
"""Check if ranges is the right format.
Expand Down Expand Up @@ -653,7 +653,11 @@ def _create_header(self, verbose=False):
def save(self, filepath=None, only_data=False, check_overwrite=False, verbose=False, **kwargs):
r"""Save data to a text file. Wrapper for `numpy.savetxt()`_.
User defined attr are saved in the header (except for dictionaries).
Attrs are saved as comments if only_data is False. Saving attrs to file
is tricky because requires converting variables to string. Only attrs
that are of type: string, number, and list of number and strings are
saved somewhat correctly. Dictionaries are not saved.
Args:
filepath (string or path object, optional): filepath or file handle.
Expand Down Expand Up @@ -2114,7 +2118,7 @@ def _get_user_attrs(self):
'_calculated_factor',
'_calculated_offset', '_calculated_calib',
'_monotonicity', '_x', '_step', '_length']
return [key for key in self.__dict__.keys() if key not in default_attrs]
return [key for key in self.__dict__.keys() if key not in default_attrs and key.startswith('_') == False]

def _validate_ranges(self, *args, **kwargs):
"""Check if ranges is the right format.
Expand Down Expand Up @@ -2163,7 +2167,9 @@ def _validate_ranges(self, *args, **kwargs):
# fix format #
##############
# empty input
if len(args) == 0:
if args is None:
return ((vmin, vmax),)
elif len(args) == 0:
return ((vmin, vmax),)

# (xi, xf)
Expand Down Expand Up @@ -2456,6 +2462,11 @@ def get_by_attr(self, attr, value, closest=True, verbose=True):
def save(self, folderpath=None, prefix='spectrum_', suffix='.dat', filenames=None, zfill=None, only_data=False, verbose=False, **kwargs):
r"""Save spectra. Wrapper for `numpy.savetxt()`_.
Attrs are saved as comments if only_data is False. Saving attrs to file
is tricky because requires converting variables to string. Only attrs
that are of type: string, number, and list of number and strings are
saved somewhat correctly. Dictionaries are not saved.
Args:
folderpath (string or pathlib.Path, optional): folderpath, folder handle.
If None, filepath can be inferred from a
Expand Down Expand Up @@ -2598,24 +2609,37 @@ def _create_header(self, verbose=False):
header = ''
attrs = self._get_user_attrs()
for name in attrs:
if attrs[name] is None:
value = self.__getattribute__(name)
if value is None:
header += f'{name}: None' + '\n'
elif isinstance(attrs[name], str):
temp2 = str(attrs[name]).replace('\n','\\n')
elif isinstance(value, str):
temp2 = str(value).replace('\n','\\n')
header += f'{name}: \"{temp2}\"' + '\n'
elif isinstance(attrs[name], Iterable):
header += f'{name}: {list(attrs[name])}' + '\n'
elif isinstance(attrs[name], dict):
elif isinstance(value, Iterable):
value = list(value)
for item in value:
if item is None:
item = 'None'
elif isinstance(item, str):
temp2 = str(item).replace('\n','\\n')
item = f'\"{temp2}\"'
elif numanip.is_number(value):
item = str(item)
else:
temp2 = str(item).replace('\n','\\n')
item = f'\"{temp2}\"'
header += f'{name}: {value}' + '\n'
elif isinstance(value, dict):
if verbose:
type_ = str(type(attrs[name]))
type_ = str(type(value))
print(r'Warning: Cannot save attr of type: ' + type_ + r'.\attr: '+ name + r'.\nTo turn off this warning, set verbose to False.')
elif numanip.is_number(attrs[name]):
tosave = str(attrs[name])
elif numanip.is_number(value):
tosave = str(value)
if tosave[-1] == '\n':
tosave = tosave[:-1]
header += f'{name}: {tosave}' + '\n'
else:
temp2 = str(attrs[name]).replace('\n','\\n')
temp2 = str(value).replace('\n','\\n')
header += f'{name}: \"{temp2}\"' + '\n'
return header[:-1]

Expand Down Expand Up @@ -2725,14 +2749,6 @@ def save_all_single_file(self, filepath=None, only_data=False, ranges=None, chec
elif kwargs['header'][-1] != '\n':
kwargs['header'] += '\n'

# gather ylabels
if self[0].xlabel != '':
if '' in self.ylabels:
if verbose:
print('Warning: Some spectra have no ylabel.\n ylabels: {self.ylabels}.\nFile is being saved without column names.')

kwargs['header'] += '\n' + self[0].xlabel + kwargs['delimiter'] + kwargs['delimiter'].join(self.ylabels)

np.savetxt(Path(filepath), final, **kwargs)

def load_from_single_file(self, filepath=None, only_data=False, verbose=False, **kwargs):
Expand Down Expand Up @@ -2774,9 +2790,7 @@ def load_from_single_file(self, filepath=None, only_data=False, verbose=False, *
if header:
for line in header:
if ':' not in line:
temp = line.split(kwargs['delimiter'])
self.xlabels = temp[kwargs['usecols'][0]].replace(kwargs['comments'], '').replace('\n', '').replace('\r', '').strip()
self.ylabels = [temp[kwargs['usecols'][i]].replace(kwargs['comments'], '').replace('\n', '').replace('\r', '').strip() for i in kwargs['usecols'][1:]]
pass
else:
# extract name and value
name = line[1:-1].split(':')[0].strip()
Expand Down Expand Up @@ -5241,7 +5255,7 @@ def _get_user_attrs(self):
"""return attrs that are user defined."""
default_attrs = ['_data', '_shape', '_calculated_shifts',
'_x_centers', '_y_centers']
return [key for key in self.__dict__.keys() if key not in default_attrs]
return [key for key in self.__dict__.keys() if key not in default_attrs and key.startswith('_') == False]

#################
# save and load #
Expand Down Expand Up @@ -5275,6 +5289,11 @@ def _create_header(self, verbose=False):
def save(self, filepath=None, only_data=False, check_overwrite=False, verbose=False, **kwargs):
r"""Save data to a text file. Wrapper for `numpy.savetxt()`_.
Attrs are saved as comments if only_data is False. Saving attrs to file
is tricky because requires converting variables to string. Only attrs
that are of type: string, number, and list of number and strings are
saved somewhat correctly. Dictionaries are not saved.
Args:
filepath (string or path object, optional): filepath or file handle.
If the filename ends in .gz, the file is automatically saved in
Expand Down Expand Up @@ -6453,7 +6472,7 @@ def __delitem__(self, item):
def _get_user_attrs(self):
"""return attrs that are user defined."""
default_attrs = ['_x', '_y', '_filepath', '_calculated_shift']
return [key for key in self.__dict__.keys() if key not in default_attrs]
return [key for key in self.__dict__.keys() if key not in default_attrs and key.startswith('_') == False]

#################
# save and load #
Expand Down Expand Up @@ -6487,6 +6506,11 @@ def _create_header(self, verbose=False):
def save(self, filepath=None, only_data=False, check_overwrite=False, verbose=False, **kwargs):
r"""Save data to a text file. Wrapper for `numpy.savetxt()`_.
Attrs are saved as comments if only_data is False. Saving attrs to file
is tricky because requires converting variables to string. Only attrs
that are of type: string, number, and list of number and strings are
saved somewhat correctly. Dictionaries are not saved.
Args:
filepath (string or path object, optional): filepath or file handle.
If the filename ends in .gz, the file is automatically saved in
Expand Down
2 changes: 1 addition & 1 deletion brixs/peaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __delitem__(self, name):
def _get_user_attrs(self):
"""return attrs that are user defined."""
default_attrs = ['_asteval', '_additional']
return [key for key in self.__dict__.keys() if key not in default_attrs]
return [key for key in self.__dict__.keys() if key not in default_attrs and key.startswith('_') == False]

def _has_i2(self):
"""return True if parameters have secondary (spectrum) index i2.
Expand Down
Binary file modified dist/brixs-0.9-py3-none-any.whl
Binary file not shown.
Binary file added dist/brixs-0.9.1-py3-none-any.whl
Binary file not shown.
Binary file added dist/brixs-0.9.1.tar.gz
Binary file not shown.
Binary file modified dist/brixs-0.9.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="brixs",
version="0.9",
version="0.9.1",
author="Carlos Galdino, Thiago Mori, Tulio Rocha, Felipe Custodio",
author_email="galdino@ifi.unicamp.br, thiago.mori@lnls.br, tulio.rocha@lnls.br, felipe.custodio@lnls.br",
description="python package for processing and analysis of XAS and RIXS spectra ",
Expand Down

0 comments on commit c31dde8

Please sign in to comment.