Skip to content

Commit

Permalink
py37 support for sascalc. Refs #888 an #1233.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkienzle authored Mar 5, 2019
2 parents 0a924c6 + 4cbb2f5 commit 8c9e65c
Show file tree
Hide file tree
Showing 30 changed files with 251 additions and 245 deletions.
6 changes: 5 additions & 1 deletion src/sas/sascalc/calculator/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ def set_size(self, size=[]):
if len(size) == 0:
self.size = 0
else:
self.size = size
# TODO: Make sure detector size is number of pixels
# Could be detector dimensions in e.g., mm, but
# the resolution calculator assumes it is pixels.
# Being pixels, it has to be integers rather than float
self.size = [int(s) for s in size]
validate(size[0])

def set_pix_size(self, size=[]):
Expand Down
8 changes: 4 additions & 4 deletions src/sas/sascalc/calculator/resolution_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,8 @@ def _get_detector_qxqy_pixels(self):
detector_offset = 0
try:
detector_offset = self.sample2detector_distance[1]
except:
logger.error(sys.exc_value)
except Exception as exc:
logger.error(exc)

# detector size in [no of pix_x,no of pix_y]
detector_pix_nums_x = self.detector_size[0]
Expand Down Expand Up @@ -1093,8 +1093,8 @@ def _get_detector_qxqy_pixels(self):
output.data = inten
output.qx_data = qx_value
output.qy_data = qy_value
except:
logger.error(sys.exc_value)
except Exception as exc:
logger.error(exc)

return output

Expand Down
15 changes: 7 additions & 8 deletions src/sas/sascalc/corfunc/corfunc_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ def __init__(self, f, g, start, stop):
self.g = g
self.start = start
self.stop = stop
self._lastx = []
self._lasty = []
self._lastx = np.empty(0, dtype='d')
self._lasty = None

def __call__(self, x):
# If input is a single number, evaluate the function at that number
# and return a single number
if type(x) == float or type(x) == int:
if isinstance(x, (float, int)):
return self._smoothed_function(np.array([x]))[0]
# If input is a list, and is different to the last input, evaluate
# the function at each point. If the input is the same as last time
# the function was called, return the result that was calculated
# last time instead of explicity evaluating the function again.
elif self._lastx == [] or x.tolist() != self._lastx.tolist():
self._lasty = self._smoothed_function(x)
self._lastx = x
if not np.array_equal(x, self._lastx):
self._lastx, self._lasty = x, self._smoothed_function(x)
return self._lasty

def _smoothed_function(self,x):
Expand Down Expand Up @@ -87,7 +86,7 @@ def set_data(self, data, scale=1):
return
# Only process data of the class Data1D
if not issubclass(data.__class__, Data1D):
raise ValueError("Data must be of the type DataLoader.Data1D")
raise ValueError("Correlation function cannot be computed with 2D data.")

# Prepare the data
new_data = Data1D(x=data.x, y=data.y)
Expand Down Expand Up @@ -245,7 +244,7 @@ def _porod(self, q, K, sigma, bg):
def _fit_guinier(self, q, iq):
"""Fit the Guinier region of the curve"""
A = np.vstack([q**2, np.ones(q.shape)]).T
return lstsq(A, np.log(iq))
return lstsq(A, np.log(iq), rcond=None)

def _fit_porod(self, q, iq):
"""Fit the Porod region of the curve"""
Expand Down
27 changes: 11 additions & 16 deletions src/sas/sascalc/data_util/calcthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@
#
from __future__ import print_function

import traceback
import sys
import logging
import traceback
from time import sleep

try:
import _thread as thread
except ImportError: # CRUFT: python 2 support
import thread

if sys.platform.count("darwin") > 0:
import time
stime = time.time()

def clock():
return time.time() - stime

def sleep(t):
return time.sleep(t)
else:
from time import clock
from time import sleep
try:
from time import perf_counter as clock
except ImportError: # CRUFT: python < 3.3
if sys.platform.count("darwin") > 0:
from time import time as clock
else:
from time import clock

logger = logging.getLogger(__name__)


class CalcThread:
"""Threaded calculation class. Inherit from here and specialize
the compute() method to perform the appropriate operations for the
Expand Down Expand Up @@ -247,7 +242,7 @@ def exception(self):
try:
self.exception_handler(*sys.exc_info())
return
except Exception:
except Exception as exc:
pass
logger.error(traceback.format_exc())
#print 'CalcThread exception',
Expand Down
7 changes: 3 additions & 4 deletions src/sas/sascalc/data_util/nxsunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ def _caret_optional(s):
"""
Strip '^' from unit names.
* WARNING * this will incorrect transform 10^3 to 103.
* WARNING * this will incorrectly transform 10^3 to 103.
"""
s.update((k.replace('^',''),v)
for k, v in list(s.items())
if '^' in k)
stripped = [(k.replace('^',''),v) for k, v in s.items() if '^' in k]
s.update(stripped)

def _build_all_units():
distance = _build_metric_units('meter','m')
Expand Down
8 changes: 4 additions & 4 deletions src/sas/sascalc/data_util/ordereddicttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ def test_copying(self):
OrderedDict(od),
]):
self.assert_(dup is not od)
self.assertEquals(dup, od)
self.assertEquals(list(dup.items()), list(od.items()))
self.assertEquals(len(dup), len(od))
self.assertEquals(type(dup), type(od))
self.assertEqual(dup, od)
self.assertEqual(list(dup.items()), list(od.items()))
self.assertEqual(len(dup), len(od))
self.assertEqual(type(dup), type(od))

def test_repr(self):
od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
Expand Down
2 changes: 1 addition & 1 deletion src/sas/sascalc/data_util/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ def load(self, path, format=None):
if last_exc is not None and len(loaders) != 0:
# If file has associated loader(s) and they;ve failed
raise last_exc
raise NoKnownLoaderException(e.message) # raise generic exception
raise NoKnownLoaderException(str(message)) # raise generic exception
2 changes: 1 addition & 1 deletion src/sas/sascalc/data_util/uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import numpy as np

from .import err1d
from . import err1d
from .formatnum import format_uncertainty

__all__ = ['Uncertainty']
Expand Down
13 changes: 8 additions & 5 deletions src/sas/sascalc/dataloader/data_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from sas.sascalc.data_util.uncertainty import Uncertainty
import numpy as np
import math
from math import fabs

class plottable_1D(object):
"""
Expand Down Expand Up @@ -655,7 +656,7 @@ def operation(a, b):
return b * a
return self._perform_operation(other, operation)

def __div__(self, other):
def __truediv__(self, other):
"""
Divided a data set by another
Expand All @@ -666,8 +667,9 @@ def __div__(self, other):
def operation(a, b):
return a/b
return self._perform_operation(other, operation)
__div__ = __truediv__

def __rdiv__(self, other):
def __rtruediv__(self, other):
"""
Divided a data set by another
Expand All @@ -678,6 +680,7 @@ def __rdiv__(self, other):
def operation(a, b):
return b/a
return self._perform_operation(other, operation)
__rdiv__ = __rtruediv__

def __or__(self, other):
"""
Expand Down Expand Up @@ -799,7 +802,7 @@ def _validity_check(self, other):
# Here we could also extrapolate between data points
TOLERANCE = 0.01
for i in range(len(self.x)):
if math.fabs((self.x[i] - other.x[i])/self.x[i]) > TOLERANCE:
if fabs(self.x[i] - other.x[i]) > self.x[i]*TOLERANCE:
msg = "Incompatible data sets: x-values do not match"
raise ValueError(msg)

Expand Down Expand Up @@ -1031,10 +1034,10 @@ def _validity_check(self, other):
msg = "Unable to perform operation: data length are not equal"
raise ValueError(msg)
for ind in range(len(self.data)):
if math.fabs((self.qx_data[ind] - other.qx_data[ind])/self.qx_data[ind]) > TOLERANCE:
if fabs(self.qx_data[ind] - other.qx_data[ind]) > fabs(self.qx_data[ind])*TOLERANCE:
msg = "Incompatible data sets: qx-values do not match: %s %s" % (self.qx_data[ind], other.qx_data[ind])
raise ValueError(msg)
if math.fabs((self.qy_data[ind] - other.qy_data[ind])/self.qy_data[ind]) > TOLERANCE:
if fabs(self.qy_data[ind] - other.qy_data[ind]) > fabs(self.qy_data[ind])*TOLERANCE:
msg = "Incompatible data sets: qy-values do not match: %s %s" % (self.qy_data[ind], other.qy_data[ind])
raise ValueError(msg)

Expand Down
24 changes: 12 additions & 12 deletions src/sas/sascalc/dataloader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def find_plugins(self, dir):
module = __import__(toks[0], globals(), locals())
if self._identify_plugin(module):
readers_found += 1
except:
except Exception as exc:
msg = "Loader: Error importing "
msg += "%s\n %s" % (item, sys.exc_value)
msg += "%s\n %s" % (item, exc)
logger.error(msg)

# Process zip files
Expand All @@ -190,14 +190,14 @@ def find_plugins(self, dir):
locals(), [""])
if self._identify_plugin(module):
readers_found += 1
except:
except Exception as exc:
msg = "Loader: Error importing"
msg += " %s\n %s" % (mfile, sys.exc_value)
msg += " %s\n %s" % (mfile, exc)
logger.error(msg)

except:
except Exception as exc:
msg = "Loader: Error importing "
msg += " %s\n %s" % (item, sys.exc_value)
msg += " %s\n %s" % (item, exc)
logger.error(msg)

return readers_found
Expand Down Expand Up @@ -241,9 +241,9 @@ def associate_file_type(self, ext, module):
# Append the new writer to the list
self.writers[ext].append(loader.write)

except:
except Exception as exc:
msg = "Loader: Error accessing"
msg += " Reader in %s\n %s" % (module.__name__, sys.exc_value)
msg += " Reader in %s\n %s" % (module.__name__, exc)
logger.error(msg)
return reader_found

Expand Down Expand Up @@ -274,9 +274,9 @@ def associate_file_reader(self, ext, loader):
if wcard not in self.wildcards:
self.wildcards.append(wcard)

except:
except Exception as exc:
msg = "Loader: Error accessing Reader "
msg += "in %s\n %s" % (loader.__name__, sys.exc_value)
msg += "in %s\n %s" % (loader.__name__, exc)
logger.error(msg)
return reader_found

Expand Down Expand Up @@ -319,9 +319,9 @@ def _identify_plugin(self, module):
self.writers[ext] = []
self.writers[ext].insert(0, loader.write)

except:
except Exception as exc:
msg = "Loader: Error accessing Reader"
msg += " in %s\n %s" % (module.__name__, sys.exc_value)
msg += " in %s\n %s" % (module.__name__, exc)
logger.error(msg)
return reader_found

Expand Down
33 changes: 17 additions & 16 deletions src/sas/sascalc/dataloader/manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,24 +927,25 @@ def _agv(self, data2D, run='phi'):
y_counts[i_bin] += 1

# Organize the results
for i in range(self.nbins):
y[i] = y[i] / y_counts[i]
y_err[i] = math.sqrt(y_err[i]) / y_counts[i]

# The type of averaging: phi,q2, or q
# Calculate x[i]should be at the center of the bin
with np.errstate(divide='ignore', invalid='ignore'):
y = y/y_counts
y_err = np.sqrt(y_err)/y_counts
# The type of averaging: phi, q2, or q
# Calculate x values at the center of the bin
if run.lower() == 'phi':
x[i] = (self.phi_max - self.phi_min) / self.nbins * \
(1.0 * i + 0.5) + self.phi_min
step = (self.phi_max - self.phi_min) / self.nbins
x = (np.arange(self.nbins) + 0.5) * step + self.phi_min
else:
# We take the center of ring area, not radius.
# This is more accurate than taking the radial center of ring.
# delta_r = (self.r_max - self.r_min) / self.nbins
# r_inner = self.r_min + delta_r * i
# r_outer = r_inner + delta_r
# x[i] = math.sqrt((r_inner * r_inner + r_outer * r_outer) / 2)
x[i] = x[i] / y_counts[i]
y_err[y_err == 0] = np.average(y_err)
# set q to the average of the q values within each bin
x = x/y_counts

### Alternate algorithm
## We take the center of ring area, not radius.
## This is more accurate than taking the radial center of ring.
#step = (self.r_max - self.r_min) / self.nbins
#r_inner = self.r_min + step * np.arange(self.nbins)
#x = math.sqrt((r_inner**2 + (r_inner + step)**2) / 2)

idx = (np.isfinite(y) & np.isfinite(y_err))
if x_err is not None:
d_x = x_err[idx] / y_counts[idx]
Expand Down
4 changes: 2 additions & 2 deletions src/sas/sascalc/dataloader/readers/associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def read_associations(loader, settings=FILE_ASSOCIATIONS):
% (ext.lower(), reader))
exec("loader.associate_file_type('%s', %s)"
% (ext.upper(), reader))
except:
except Exception as exc:
msg = "read_associations: skipping association"
msg += " for %s\n %s" % (ext.lower(), sys.exc_value)
msg += " for %s\n %s" % (ext.lower(), exc)
logger.error(msg)
5 changes: 2 additions & 3 deletions src/sas/sascalc/dataloader/readers/cansas_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,11 +1238,10 @@ def _store_float(self, location, node, variable, storage, optional=True):
try:
conv = Converter(units)
setattrchain(storage, variable, conv(value, units=local_unit))
except Exception:
_, exc_value, _ = sys.exc_info()
except Exception as exc:
err_mess = "CanSAS reader: could not convert"
err_mess += " %s unit [%s]; expecting [%s]\n %s" \
% (variable, units, local_unit, exc_value)
% (variable, units, local_unit, exc)
self.errors.add(err_mess)
if optional:
logger.info(err_mess)
Expand Down
2 changes: 1 addition & 1 deletion src/sas/sascalc/fit/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def eval_expressions():
"""%("\n ".join(assignments),"\n ".join(code))

#print("Function: "+functiondef)
exec functiondef in globals,locals
exec(functiondef, globals, locals)
retfn = locals['eval_expressions']

# Remove garbage added to globals by exec
Expand Down
Loading

0 comments on commit 8c9e65c

Please sign in to comment.