Skip to content

Commit

Permalink
Refactor, fix issue with some sample workspaces
Browse files Browse the repository at this point in the history
Some workspaces have differing numbers of array elements for X, Y and E
values, just choose the samllest and trim the longer arrays

Refs #7860
  • Loading branch information
DanNixon committed Sep 3, 2014
1 parent de5e537 commit f695f9a
Showing 1 changed file with 31 additions and 19 deletions.
Expand Up @@ -36,68 +36,80 @@ def PyExec(self):

StartTime('Symmetrise')
self._setup()
num_spectra, sample_aray_len = CheckHistZero(self._sample)
num_spectra, _ = CheckHistZero(self._sample)
sample_x = mtd[self._sample].readX(0)

if math.fabs(self._x_cut) < 1e-5:
raise ValueError('XCut point is Zero')

# Find range of values to flip
# Find the smallest data array in the first spectra
len_x = len(mtd[self._sample].readX(0))
len_y = len(mtd[self._sample].readY(0))
len_e = len(mtd[self._sample].readE(0))
sample_array_len = min(len_x, len_y, len_e) - 1

delta_x = sample_x[1] - sample_x[0]

# Find array index of negative XCut
negative_diff = np.absolute(sample_x + self._x_cut)
negative_index = np.where(negative_diff < delta_x)[0][-1]
self._check_bounds(negative_index, sample_aray_len, label='Negative')
self._check_bounds(negative_index, sample_array_len, label='Negative')

# FInd array index of positive XCut
positive_diff = np.absolute(sample_x + sample_x[negative_index])
positive_index = np.where(positive_diff < delta_x)[0][-1]
self._check_bounds(positive_index, sample_aray_len, label='Positive')
self._check_bounds(positive_index, sample_array_len, label='Positive')

new_array_len = 2 * sample_aray_len - (positive_index + negative_index) + 1
# Calculate number of elements neede dfor new array (per spectra)
new_array_len = 2 * sample_array_len - (positive_index + negative_index) + 1

if self._verbose:
logger.notice('No. points = %d' % sample_aray_len)
logger.notice('Negative : at i =%d; x = %f'
logger.notice('No. points = %d' % sample_array_len)
logger.notice('Negative at i=%d, x=%f'
% (negative_index, sample_x[negative_index]))
logger.notice('Positive : at i =%d; x = %f'
logger.notice('Positive at i=%d, x=%f'
% (positive_index, sample_x[positive_index]))
logger.notice('New array size = %d' % new_array_len)

# Create an empty workspace with enough storage for the new data
zeros = np.zeros(new_array_len * num_spectra)
CreateWorkspace(OutputWorkspace=self._output_workspace,
DataX=zeros, DataY=zeros, DataE=zeros,
NSpec=num_spectra)

# Copy logs and properties from sample workspace
CopyLogs(InputWorkspace=self._sample, OutputWorkspace=self._output_workspace)
CopyInstrumentParameters(InputWorkspace=self._sample, OutputWorkspace=self._output_workspace)
# CopySample(InputWorkspace=self._sample, OutputWorkspace=self._output_workspace)

# For each spectrum copy positive values to the negative
for index in xrange(num_spectra):
x_in = mtd[self._sample].readX(index)
y_in = mtd[self._sample].readY(index)
e_in = mtd[self._sample].readE(index)
# Strip any additional array cells
x_in = mtd[self._sample].readX(index)[:sample_array_len + 1]
y_in = mtd[self._sample].readY(index)[:sample_array_len + 1]
e_in = mtd[self._sample].readE(index)[:sample_array_len + 1]

# Get some zeroed data to overwrite with copies from sample
x_out = np.zeros(new_array_len)
y_out = np.zeros(new_array_len)
e_out = np.zeros(new_array_len)

# Left hand side of cut
x_out[:sample_aray_len - positive_index] = -x_in[sample_aray_len:positive_index:-1]
y_out[:sample_aray_len - positive_index] = y_in[sample_aray_len:positive_index:-1]
e_out[:sample_aray_len - positive_index] = e_in[sample_aray_len:positive_index:-1]
x_out[:sample_array_len - positive_index] = -x_in[sample_array_len:positive_index:-1]
y_out[:sample_array_len - positive_index] = y_in[sample_array_len:positive_index:-1]
e_out[:sample_array_len - positive_index] = e_in[sample_array_len:positive_index:-1]

# Right hand side of cut
x_out[sample_aray_len - positive_index:] = x_in[negative_index:]
y_out[sample_aray_len - positive_index:] = y_in[negative_index:]
e_out[sample_aray_len - positive_index:] = e_in[negative_index:]
x_out[sample_array_len - positive_index:] = x_in[negative_index:]
y_out[sample_array_len - positive_index:] = y_in[negative_index:]
e_out[sample_array_len - positive_index:] = e_in[negative_index:]

mtd[self._output_workspace].setX(index, x_out)
mtd[self._output_workspace].setY(index, y_out)
mtd[self._output_workspace].setE(index, e_out)

logger.information('Spectra %d out of %d done'
% (index, num_spectra))
% (index + 1, num_spectra))

if self._save:
self._save_output()
Expand Down Expand Up @@ -154,7 +166,7 @@ def _plot_output(self):
"""
from IndirectImport import import_mantidplot
mtd_plot = import_mantidplot()
mtd_plot.plotSpectrum([self._output_workspace, self._sample], 0)
mtd_plot.plotSpectrum([self._sample, self._output_workspace], 0)

# Register algorithm with Mantid
AlgorithmFactory.subscribe(Symmetrise)

0 comments on commit f695f9a

Please sign in to comment.