Skip to content

Commit

Permalink
Merge pull request #60 from MJOLNIRPackage/develop
Browse files Browse the repository at this point in the history
Merged from Develop
  • Loading branch information
MJOLNIRPackage committed Mar 17, 2021
2 parents 61aaf9c + 854887e commit a3e7e9b
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 177 deletions.
32 changes: 30 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python:
- 3.6
- 3.7
- 3.8
- 3.9
matrix:
include:
- name: "Python 3.7.5 on macOS 10.14"
Expand All @@ -26,9 +27,22 @@ matrix:
- wget -UseBasicParsing 'https://www.psi.ch/en/media/60041/download'
- unzip download
script: py.test test/ -vv
- name: "Python 3.8.0 on macOS 10.14"
- name: "Python 3.8 on macOS 10.15.5"
os: osx
osx_image: xcode11.3 # Python 3.8.0 running on macOS 10.14.6
osx_image: xcode11.6 # Python 3.8 running on macOS 10.15.5
language: shell # 'language: python' is an error on Travis CI macOS
before_install:
- python3 --version
- sleep 3 # give xvfb some time to start
- pip3 install -U pip
- pip install pytest>=3.6 pycodestyle python-coveralls pytest-cov>=2.5 coveralls
- pip install .
- wget -UseBasicParsing 'https://www.psi.ch/en/media/60041/download'
- unzip download
script: py.test test/ -vv
- name: "Python 3.9 on macOS 10.15.7"
os: osx
osx_image: xcode12.2 # Python 3.9 running on macOS 10.15.7
language: shell # 'language: python' is an error on Travis CI macOS
before_install:
- python3 --version
Expand Down Expand Up @@ -96,6 +110,20 @@ matrix:
- curl -o download.zip https://www.psi.ch/en/media/60041/download
- 7z e download.zip "-osamlpedata"
env: PATH=/c/Python38:/c/Python38/Scripts:$PATH
- name: "Python 3.9.0 on Windows"
os: windows # Windows 10.0.17134 N/A Build 17134
language: shell # 'language: python' is an error on Travis CI Windows
before_install:
- choco install python --version 3.9.0
- python --version
- sleep 3 # give xvfb some time to start
- python -m pip install --upgrade pip
- pip3 install pytest>=3.6 pycodestyle python-coveralls pytest-cov>=2.5 coveralls
- python setup.py sdist
- pip3 install .
- curl -o download.zip https://www.psi.ch/en/media/60041/download
- 7z e download.zip "-osamlpedata"
env: PATH=/c/Python39:/c/Python39/Scripts:$PATH
before_install:
- pip install pytest>=3.6 pycodestyle python-coveralls pytest-cov>=2.5 coveralls
- pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion MJOLNIR/Data/DataFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import MJOLNIR
import datetime
import math
import shapely
#import shapely
# from shapely.geometry import Polygon as PolygonS, Point as PointS
from MJOLNIR import TasUBlibDEG as TasUBlib
from MJOLNIR._tools import Marray
Expand Down
14 changes: 6 additions & 8 deletions MJOLNIR/Data/DataSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3128,20 +3128,18 @@ def undoAbsolutNormalize(self):



def absolutNormalize(self,sampleMass,sampleChemicalFormula,formulaUnitsPerUnitCell=1.0,
def absolutNormalize(self,sampleMass=None,sampleMolarMass=None,sampleChemicalFormula=None,formulaUnitsPerUnitCell=1.0,
sampleGFactor=2.0, correctVanadium=False,vanadiumMass=15.25,
vanadiumMonitor=100000,vanadiumSigmaIncoherent=5.08,vanadiumChemicalFormula='V',vanadiumGFactor=2.0,
vanadiumUnitsPerUnitCell=1.0):
vanadiumUnitsPerUnitCell=1.0,vanadiumMolarMass=None):
"""Normaliza dataset to absolut units () by
Args:
Kwargs:
- sampleMass (float): Mass of sample in gram
- sampleChemicalFormula (string): Chemical formula of sample
Kwargs:
- formulaUnitsPerUnitCell (float): Number of formula units per unit cell (default 1.0)
- sampleDebyeWaller (float): Debye Waller factor (default 1.0)
Expand All @@ -3166,10 +3164,10 @@ def absolutNormalize(self,sampleMass,sampleChemicalFormula,formulaUnitsPerUnitCe


normFactor = \
_tools.calculateAbsolutNormalization(sampleChemicalFormula=sampleChemicalFormula,sampleMass=sampleMass,
_tools.calculateAbsolutNormalization(sampleChemicalFormula=sampleChemicalFormula,sampleMolarMass=sampleMolarMass,sampleMass=sampleMass,
formulaUnitsPerUnitCell=formulaUnitsPerUnitCell,sampleGFactor=sampleGFactor,
correctVanadium=correctVanadium,vanadiumMass=vanadiumMass,
vanadiumMonitor=vanadiumMonitor,vanadiumSigmaIncoherent=vanadiumSigmaIncoherent,
correctVanadium=correctVanadium,vanadiumMass=vanadiumMass,vanadiumChemicalFormula=vanadiumChemicalFormula,
vanadiumMonitor=vanadiumMonitor,vanadiumSigmaIncoherent=vanadiumSigmaIncoherent,vanadiumMolarMass=vanadiumMolarMass,
vanadiumGFactor=vanadiumGFactor,vanadiumUnitsPerUnitCell=vanadiumUnitsPerUnitCell)

if self.absolutNormalized != 0:
Expand Down
1 change: 0 additions & 1 deletion MJOLNIR/Data/Sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from MJOLNIR._tools import Marray
import MJOLNIR.Data.DataFile

factorsqrtEK = 0.694692

def cosd(x):
return np.cos(np.deg2rad(x))
Expand Down
131 changes: 83 additions & 48 deletions MJOLNIR/Geometry/Instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from MJOLNIR import _tools
from MJOLNIR import TasUBlibDEG as TasUBlib
from MJOLNIR.TasUBlibDEG import cosd,sind
from MJOLNIR.Data import Sample,RLUAxes
import warnings
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Expand Down Expand Up @@ -1304,85 +1305,119 @@ def makeMonitor(entry,Numpoints):


def prediction(A3Start,A3Stop,A3Steps,A4Positions,Ei,Cell,r1,r2,points=False):

cell = TasUBlib.calcCell(Cell)

UB = TasUBlib.calcTasUBFromTwoReflections(cell, r1, r2)
s = Sample.Sample(*Cell,projectionVector1=r1,projectionVector2=r2)

class simpleDataSet():
def __init__(self,sample):
self.sample = [sample]

pV1 = _tools.LengthOrder(np.array(r2[:3],dtype=float))
pV2 = _tools.LengthOrder(np.array(r1[:3],dtype=float))

def converterToQxQy(A3,A4,Ei,Ef):

convertionMatrix = np.dot(np.dot([[1,0,0],[0,1,0]],UB),np.array([pV1,pV2]).T)
ki = np.sqrt(Ei)*_tools.factorsqrtEK
kf = np.sqrt(Ef)*_tools.factorsqrtEK


factorsqrtEK = 0.694692

def converterToP1P2(A3,A4,Ei,Ef, convertionMatrix = convertionMatrix):

ki = np.sqrt(Ei)*factorsqrtEK
kf = np.sqrt(Ef)*factorsqrtEK

r = [0,0,0,A3,A4,0.0,0.0,Ei,Ef]
QV = TasUBlib.calcTasUVectorFromAngles(r)
q = np.sqrt(ki**2 +kf**2-
2. *ki *kf * np.cos(np.deg2rad(A4)))
2. *ki *kf * np.cos(np.deg2rad(A4)))


Qx,Qy = (QV*q.reshape(-1,1))[:,:2].T
convertionMatrixINV = np.linalg.inv(convertionMatrix)
proj1,proj2 = np.einsum('ij,j...->i...',convertionMatrixINV,[Qx,Qy])
return (proj1,proj2)

fig,Ax = plt.subplots(3,3,figsize=(26,13))
Ax = Ax.flatten()
return (Qx,Qy)

def converterToA3A4(Qx,Qy, Ei,Ef,A3Off=0.0):
Qx = np.asarray(Qx)
Qy = np.asarray(Qy)

QC = np.array([Qx,Qy])
q = np.linalg.norm(QC)

U1V = np.array([Qx.flatten(),Qy.flatten(),0.0],dtype=float)

U1V/=np.linalg.norm(U1V)
U2V = np.array([0.0,0.0,1.0],dtype=float)


TV = TasUBlib.buildTVMatrix(U1V, U2V)
R = np.linalg.inv(TV)

ss = 1.0

cossgl = np.sqrt(R[0,0]*R[0,0]+R[1,0]*R[1,0])
om = TasUBlib.arctan2d(R[1,0]/cossgl, R[0,0]/cossgl)

ki = np.sqrt(Ei)*_tools.factorsqrtEK
kf = np.sqrt(Ef)*_tools.factorsqrtEK

cos2t =(ki**2 + kf**2 - q**2) / (2. * np.abs(ki) * np.abs(kf))

A4 = ss*TasUBlib.arccosd(cos2t)
theta = TasUBlib.calcTheta(ki, kf, A4)
A3 = -om + -ss*theta + A3Off
return A3,-A4


t = simpleDataSet(s)
fig = plt.figure(figsize=(26,13))
Ax = [RLUAxes.createRLUAxes(t,figure=fig,ids=(3,3,i+1)) for i in range(9)]

MJOLNIRDir = os.path.dirname(_tools.__file__)
calibPath = os.path.join(MJOLNIRDir,'Normalization_1.calib')

calib = np.loadtxt(calibPath,delimiter=',',skiprows=3)

A4Instrument = calib[:,-1].reshape(104,8)
EfInstrument = calib[:,4].reshape(104,8)
EfInstrument[np.isclose(EfInstrument,0.0)]=np.nan
A3PointValues = np.linspace(A3Start,A3Stop,A3Steps)

A3PointValues = np.linspace(A3Start,A3Stop,A3Steps)-s.theta
steps = A3Steps
if steps>30:
steps = 30
A3Values = np.linspace(A3Start,A3Stop,steps)


startColor = np.array([0.12156863, 0.46666667, 0.70588235, 0.5])
endColor = np.array([0.83921569, 0.15294118, 0.15686275, 0.5])
A3Values = np.linspace(A3Start,A3Stop,steps)-s.theta

endColor = np.array([0.12156863, 0.46666667, 0.70588235, 0.5])
startColor = np.array([0.83921569, 0.15294118, 0.15686275, 0.5])
diff = (endColor-startColor)/7.0


def format_axes(x,y,pV1,pV2):
return '['+', '.join(['{:.3f}'.format(x) for x in np.asarray(x)*pV1+np.asarray(y)*pV2])+'] RLU'
def format_axes_func(self,x,y,Ei,Ef,A3Off=s.theta):
A3,A4 = converterToA3A4(x,y,Ei,Ef,-A3Off)
return ax.sample.format_coord(x,y)+', A3 = {:.2f} deg, A4 = {:.2f} deg, Ef = {:.2f}'.format(A3,A4,Ef)

for i,(ax,Ef,A4) in enumerate(zip(Ax,EfInstrument.reshape(104,8).T,A4Instrument.reshape(104,8).T)):
ax.Ef = np.nanmean(Ef)
for A4Position in A4Positions:
if points:
for A3 in A3PointValues:
H,L = converterToP1P2(A3=A3, A4=A4+A4Position,Ei = Ei, Ef = Ef)
H,L = converterToQxQy(A3=A3, A4=A4+A4Position,Ei = Ei, Ef = Ef)
ax.scatter(H,L,color=startColor[:-1]+diff[:-1]*i)

A4Edge = np.array([np.concatenate([a4,[a4[-1]]*steps,a4[::-1],[a4[0]]*steps]) for a4 in A4.reshape(8,13)])
EfEdge = np.array([np.concatenate([ef,[ef[-1]]*steps,ef[::-1],[ef[0]]*steps]) for ef in Ef.reshape(8,13)])
A3Edge = np.array(list(np.concatenate([[A3Values[0]]*13,A3Values,[A3Values[-1]]*13,A3Values[::-1]]))*8).reshape(8,-1)
Hedge,Ledge = np.array([converterToP1P2(A3=a3, A4=a4+A4Position,Ei = Ei, Ef = ef) for a3,a4,ef in zip(A3Edge,A4Edge,EfEdge)]).transpose([1,0,2])
Hedge,Ledge = np.array([converterToQxQy(A3=a3, A4=a4+A4Position,Ei = Ei, Ef = ef) for a3,a4,ef in zip(A3Edge,A4Edge,EfEdge)]).transpose([1,0,2])
[a.plot(hedge,ledge,color=startColor+diff*i) for hedge,ledge in zip(Hedge,Ledge) for a in [Ax[-1],ax]]


ax.set_xlabel(_tools.generateLabelDirection(pV1) + ' RLU')
ax.set_ylabel(_tools.generateLabelDirection(pV2) + ' RLU')
ax.set_title(r'$\Delta $E {:.2f}'.format(Ei-np.nanmean(Ef))+' meV')
ax.invert_yaxis()
ax.format_coord = lambda x,y: format_axes(x, y, pV1, pV2)

Ax[-1].set_xlabel(_tools.generateLabelDirection(pV1) + ' RLU')
Ax[-1].set_ylabel(_tools.generateLabelDirection(pV2) + ' RLU')
ax.set_xlabel(_tools.generateLabelDirection(s.projectionVector1) + ' RLU')
ax.set_ylabel(_tools.generateLabelDirection(s.projectionVector2) + ' RLU')
ax.set_title(r'$\Delta $E {:.2f}'.format(Ei-np.nanmean(Ef))+' meV, E$_f$ {:.2f} meV'.format(np.nanmean(Ef)))


Ax[-1].set_xlabel(_tools.generateLabelDirection(s.projectionVector1) + ' RLU')
Ax[-1].set_ylabel(_tools.generateLabelDirection(s.projectionVector2) + ' RLU')
Ax[-1].set_title('All Planes')
Ax[-1].invert_yaxis()
Ax[-1].format_coord = lambda x,y: format_axes(x, y, pV1, pV2)


fig.tight_layout()
Ax[0].format_coord = lambda x,y: format_axes_func(Ax[0],x,y,Ei,np.nanmean(EfInstrument,axis=0)[0],-s.theta)
Ax[1].format_coord = lambda x,y: format_axes_func(Ax[1],x,y,Ei,np.nanmean(EfInstrument,axis=0)[1],-s.theta)
Ax[2].format_coord = lambda x,y: format_axes_func(Ax[2],x,y,Ei,np.nanmean(EfInstrument,axis=0)[2],-s.theta)
Ax[3].format_coord = lambda x,y: format_axes_func(Ax[3],x,y,Ei,np.nanmean(EfInstrument,axis=0)[3],-s.theta)
Ax[4].format_coord = lambda x,y: format_axes_func(Ax[4],x,y,Ei,np.nanmean(EfInstrument,axis=0)[4],-s.theta)
Ax[5].format_coord = lambda x,y: format_axes_func(Ax[5],x,y,Ei,np.nanmean(EfInstrument,axis=0)[5],-s.theta)
Ax[6].format_coord = lambda x,y: format_axes_func(Ax[6],x,y,Ei,np.nanmean(EfInstrument,axis=0)[6],-s.theta)
Ax[7].format_coord = lambda x,y: format_axes_func(Ax[7],x,y,Ei,np.nanmean(EfInstrument,axis=0)[7],-s.theta)
fig.tight_layout()

return Ax
4 changes: 3 additions & 1 deletion MJOLNIR/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import sys,os
sys.path.append('.')

__version__ = '1.1.16'
__version__ = '1.1.17'
__author__ = 'Jakob Lass'

__multiFLEXXNormalization__ = os.path.join(os.path.dirname(__file__),'CalibrationMultiFLEXX.csv')
__flatConeNormalization__ = os.path.join(os.path.dirname(__file__),'CalibrationFlatCone.csv')
__CAMEANormalization__ = os.path.join(os.path.dirname(__file__),'Normalization_1.calib')

Loading

0 comments on commit a3e7e9b

Please sign in to comment.