Diffraction

Functions to load and process x-ray diffraction data.

fit_peak(xdata, ydata[, model])

Fit Bragg peak with model of choice: Gaussian, Lorentzian, PseudoVoigt.

load_info(scan_id, info[, source])

Load metadata variable value.

fit_series(scan_series[, source, output, ...])

Fit series of scans with chosen functional and returns fit parameters.

load_series(scan_series[, source, log, ...])

Load series of scans as function of variable like temperature of field which can be spaced unequally.

get_type(scan_id[, source])

get_type returns type of scan and scan parameters.

load_mesh(scan, scan_range[, source, log, ...])

Load mesh generates input array for plot_2d from mesh scans:

plot_2d(scans[, source, var_series, ...])

Plot 2d:

plot_fit(scan_series[, source, output, ...])

Fit and plot series of scans with chosen functional and returns fit parameters.

load_axes(scan[, source, positioner, ...])

Load default positioner and detector for plot

plot_data(scan_series[, source, positioner, ...])

Plot and fit data.

dbplot(scan[, source, positioner, detector, ...])

Plot and fit data.

class polartools.diffraction.Model(*values)[source]
polartools.diffraction.dbplot(scan, source=None, positioner=None, detector=None, monitor=None, normalize=False, fit=False, deriv=False, direction=[1, 1], **kwargs)[source]

Plot and fit data.

Parameters:
scan_seriesint, list

single scan or list [start, stop, step, start2, stop2, step2, … ,startn, stopn, stepn]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

positionerstring, optional

Name of the positioner, this needs to be the same as defined in Bluesky or SPEC. If None is passed, it defauts to ‘4C Theta’ motor.

detectorstring, optional

Detector to be read from this scan, again it needs to be the same name as in Bluesky. If None is passed, it defaults to the APD detector.

monitorstring, optional

Name of the monitor detector. If None is passed, it defaults to the ion chamber 3.

normalizeboolean, optional

Normalization to selected/default monitor on/off

derivboolean, optional

calculated derivative

fitboolean, optional

Fitting of peak using model on/off. In case of deriv=True, derivative is fitted

directionlist, int

multiply axes for inversion: [1,-1] inverts y-axis

kwargs
modelstring, optional
  • fit model: Gaussian, Lorentzian, PseudoVoigt

source argument:
  • csv -> possible kwargs: folder, name_format.

  • spec -> possible kwargs: folder.

  • databroker -> possible kwargs: stream, query, use_db_v1.

polartools.diffraction.fit_peak(xdata, ydata, model=Model.Gaussian)[source]

Fit Bragg peak with model of choice: Gaussian, Lorentzian, PseudoVoigt.

Uses lmfit (https://lmfit.github.io/lmfit-py/).

Parameters:
xdataiterable

List of x-axis values.

yydataiterable

List of y-axis values.

model: enumeration

fit model: model = Model.Gaussian (default), Model.Lorentzian, Model.PseudoVoigt

Returns:
fitlmfit ModelResult class

Contains the fit results. See: https://lmfit.github.io/lmfit-py/model.html#the-modelresult-class

polartools.diffraction.fit_series(scan_series, source=None, output=False, var_series=None, positioner=None, detector=None, monitor=None, normalize=False, xrange=None, **kwargs)[source]

Fit series of scans with chosen functional and returns fit parameters.

Uses lmfit.

Parameters:
scan_seriesint

start, stop, step, [start2, stop2, step2, … ,startn, stopn, stepn]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

outputboolean, optional

Output fit parameters and plot data+fit for each scan.

var_seriesstring or list

If string:

  • Varying variable for scan series to be read from scan {detector), e.g. SampK (sample temperature), optional.

  • String starting with #metadata, reads metadata from CSV baseline

If list: SPEC: Information on metadata to be read: List starting with #P, #U, #Q for motor positions, user values or Q-position or general #xx:

  • #P: [‘#P’, row, element_number], e.g. [‘#P’, 2, 0]

  • #U: [‘#U’, Variable, element_number], e.g. [‘#U’, ‘KepkoI’, 1]

  • #Q: [‘#Q’, None, element_number], e.g. [‘#Q’, None, 0]

  • #xx like #UA etc.: [‘#UA’, row, element_number].

CSV: #metadata_name If None, successive scans will be numbered starting from zero.

positionerstring, optional

Name of the positioner, this needs to be the same as defined in Bluesky or SPEC. If None is passed, it defauts to ‘4C Theta’ motor.

detectorstring, optional

Detector to be read from this scan, again it needs to be the same name as in Bluesky. If None is passed, it defaults to the APD detector.

monitorstring, optional

Name of the monitor detector. If None is passed, it defaults to the ion chamber 3.

normalizeboolean, optional

Normalization to selected/default monitor on/off

xrangelist

Set positioner range for fitting

kwargs

The necessary kwargs are passed to the loading functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format.

  • spec -> possible kwargs: folder.

  • databroker -> possible kwargs: stream, query, use_db_v1.

Note that a warning will be printed if the an unnecessary kwarg is passed.

model -> fit model, options: model = Model.Gaussian (default), Model.Lorentzian, Model.PseudoVoigt.

Returns:
fitlmfit ModelResult class

Contains the fit results. See: https://lmfit.github.io/lmfit-py/model.html#the-modelresult-class

polartools.diffraction.get_type(scan_id, source=None, **kwargs)[source]

get_type returns type of scan and scan parameters.

Parameters:
scan_idint

scan number

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

kwargs

The necessary kwargs are passed to the loading and fitting functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format, e.g. “scan_{}_primary.csv”

  • spec -> possible kwargs: folder

  • databroker -> possible kwargs: stream, query

Note that a warning will be printed if the an unnecessary kwarg is passed.

Additional parameters in kwargs: folder: location of scan file

Returns:
datatype of scan and scan parameters.
polartools.diffraction.load_axes(scan, source=None, positioner=None, detector=None, monitor=None, defaults=None, read=False, **kwargs)[source]

Load default positioner and detector for plot

Parameters:
scanint, list

single scan or list [start, stop, step, start2, stop2, step2, … ,startn, stopn, stepn]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

positionerstring, optional

Name of the positioner, this needs to be the same as defined in Bluesky or SPEC. If None is passed, it defauts to ‘4C Theta’ motor.

detectorstring, optional

Detector to be read from this scan, again it needs to be the same name as in Bluesky. If None is passed, it defaults to the APD detector.

monitorstring, optional

Name of the monitor detector. If None is passed, it defaults to the ion chamber 3.

defaultsstring, optional

Default values for positioner and detector

read: boolean, optional

Determines if positioner is read from metadata

polartools.diffraction.load_dichromesh(scan, scan_range, source=None, detector=None, **kwargs)[source]

Load dichromesh generates input array for plot_2d from mesh scans with several polarizations per scan point:

  • dichromesh (SPEC)

  • dichro_grid_scan (BlueSky)

Parameters:
scan_serieslist, int

start, stop, step, [start2, stop2, step2, … ,startn, stopn, stepn]

scan_rangelist, int

scan parameters of mesh scan [x0, x1, xinterval, y0, y1, yinterval]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

kwargs

The necessary kwargs are passed to the loading and fitting functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format, e.g. “scan_{}_primary.csv”

  • spec -> possible kwargs: folder

  • databroker -> possible kwargs: stream, query

Note that a warning will be printed if the an unnecessary kwarg is passed.

Additional parameters in kwargs:
scalelist, int

intensity limits: [z_min,z_max]

Returns:
dataarrays with x, y and z information for 2D plot for left and right polarizatio and axes names
polartools.diffraction.load_info(scan_id, info, source=None, **kwargs)[source]

Load metadata variable value.

Parameters:
scanint

Scan_id our uid. If scan_id is passed, it will load the last scan with that scan_id.

infolist

If SPEC, information on metadata to be read: List starting with #P, #U, #Q for motor positions, user values or Q-position or general #xx:

  • #P: [‘#P’, row, element_number], e.g. [‘#P’, 2, 0]

  • #U: [‘#U’, Variable, element_number], e.g. [‘#U’, ‘KepkoI’, 1]

  • #Q: [‘#Q’, None, element_number], e.g. [‘#Q’, None, 0]

  • #xx like #UA etc.: [‘#UA’, row, element_number],

If CSV, #metadata_name If db, #baseline_information (e.g. #lakeshore340_sample)

source: databroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

kwargs

The necessary kwargs are passed to the loading functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format.

  • spec -> possible kwargs: folder.

  • databroker -> possible kwargs: stream, query, use_db_v1.

Note that a warning will be printed if the an unnecessary kwarg is passed.

Returns:
valuenumber

Variable value.

polartools.diffraction.load_mesh(scan, scan_range, source=None, log=False, mrange='reduced', detector=None, **kwargs)[source]
Load mesh generates input array for plot_2d from mesh scans:

mesh, dichromesh, hklmesh (SPEC) grid_scan, rel_grid_scan (BlueSky)

Parameters:
scan_serieslist, int

start, stop, step, [start2, stop2, step2, … ,startn, stopn, stepn]

scan_rangelist, int

scan parameters of mesh scan [x0, x1, xinterval, y0, y1, yinterval]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

log: boolean

If True, z-axis plotted in logarithmic scale.

mrange: string, list

reduced, full, [xmin,ymin,xmx,ymax]

kwargs

The necessary kwargs are passed to the loading and fitting functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format, e.g. “scan_{}_primary.csv”

  • spec -> possible kwargs: folder

  • databroker -> possible kwargs: stream, query

Note that a warning will be printed if the an unnecessary kwarg is passed.

Additional parameters in kwargs:
scalelist, int

intensity limits: [z_min,z_max]

Returns:
dataarrays with x, y and z information for 2D plot and axes names
polartools.diffraction.load_series(scan_series, source=None, log=False, var_series=None, positioner=None, detector=None, monitor=None, normalize=False, scale=None, **kwargs)[source]

Load series of scans as function of variable like temperature of field which can be spaced unequally. Generates input arrays for plot_2d.

Parameters:
scan_serieslist, int

start, stop, step, [start2, stop2, step2, … ,startn, stopn, stepn]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

var_seriesstring or list

string:

  • Varying variable for scan series to be read from scan (detector), e.g. SampK (sample temperature), optional.

  • String starting with #metadata, reads metadata from CSV baseline

list: SPEC: Information on metadata to be read: List starting with #P, #U, #Q for motor positions, user values or Q-position or general #xx:

  • #P: [‘#P’, row, element_number], e.g. [‘#P’, 2, 0]

  • #U: [‘#U’, Variable, element_number], e.g. [‘#U’, ‘KepkoI’, 1]

  • #Q: [‘#Q’, None, element_number], e.g. [‘#Q’, None, 0]

  • #xx like #UA etc.: [‘#UA’, row, element_number],

CSV: #metadata_name None, successive scans will be numbered starting from zero.

positionerstring, optional

Name of the positioner, this needs to be the same as defined in Bluesky or SPEC. If None is passed, it defauts to ‘4C Theta’ motor.

detectorstring, optional

Detector to be read from this scan, again it needs to be the same name as in Bluesky. If None is passed, it defaults to the APD detector.

monitorstring, optional

Name of the monitor detector for normalization. If None is passed, data are not normalized.

logboolean

If True, z-axis plotted in logarithmic scale.

scalelist, int

intensity limits: [z_min,z_max]

kwargs

The necessary kwargs are passed to the loading and fitting functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format, e.g. “scan_{}_primary.csv”

  • spec -> possible kwargs: folder

  • databroker -> possible kwargs: stream, query

Note that a warning will be printed if the an unnecessary kwarg is passed.

Returns:
dataarrays with x, y and z information for 2D plot
polartools.diffraction.plot_2d(scans, source=None, var_series=None, positioner=None, detector=None, monitor=None, normalize=False, log=False, scale=None, mrange='reduced', direction=[1, 1], output=False, xcut=None, ycut=None, plot2=None, scale2=None, **kwargs)[source]

Plot 2d:

Creates 2D plot from individual 1D scans as function of variable parameter or plots a 2D mesh scan.

Supported mesh scans:
  • mesh, dichromesh, hklmesh (SPEC)

  • grid_scan, rel_grid_scan (BlueSky)

Parameters:
scanslist, int

1D-scans: start, stop, step, [start2, stop2, step2, … ,startn, stopn, stepn] e.g. [10,14,2,23,27,4] will use scan #10,12,14,23,27 mesh-scan: scan_number, e.g. 10 will read scan #10

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

var_series: string or list, optional

string:

  • Varying variable for scan series to be read from scan

    (detector), e.g. SampK (sample temperature), optional.

  • String starting with #metadata, reads metadata from CSV baseline

list: Information on metadata to be read: List starting with #P, #U or #Q for motor positions, user values or Q-position, or general #xx, optional:

  • #P: [‘#P’, row, element_number], e.g. [‘#P’, 2, 0]

  • #U: [‘#U’, Variable, element_number], e.g. [‘#U’, ‘KepkoI’, 1]

  • #Q: [‘#Q’, None, element_number], e.g. [‘#Q’, None, 0]

  • #xx like #UA etc.: [‘#UA’, row, element_number],

If None, successive scans will be numbered starting from zero.

positionerstring, optional

Name of the positioner, this needs to be the same as defined in Bluesky or SPEC. If None is passed, it defauts to ‘4C Theta’ motor.

detectorstring, optional

Detector to be read from this scan, again it needs to be the same name as in Bluesky. If None is passed, it defaults to the APD detector.

monitorstring, optional

Name of the monitor detector for normalization. If None is passed, data are not normalized.

log: boolean, optional

If True, z-axis plotted in logarithmic scale.

scalelist, int, optional

intensity limits: [z_min,z_max]

scale2list, int, optional

intensity limits: [z_min,z_max] for sum if 2 plots are provided

mrangelist, optional

full, reduced, [xmin,ymin,xmax,ymax]

directionlist, int, optional

multiply axes for inversion: [1,-1]

outputstring, optional

Output file for png file of plot.

xcutlist, optional

plot 1D cuts for x-axis values

ycutlist, optional

plot 1D cuts for y-axis values

plot2list, int

same as scans. Only z-values are taken. x and y used from scans

kwargs:

The necessary kwargs are passed to the loading and fitting functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format, e.g. “scan_{}_primary.csv”

  • spec -> possible kwargs: folder

  • databroker -> possible kwargs: stream, query

Returns:
2D plot, png-file
polartools.diffraction.plot_data(scan_series, source=None, positioner=None, detector=None, monitor=None, fit=False, normalize=False, log=False, deriv=False, direction=[1, 1], **kwargs)[source]

Plot and fit data.

Parameters:
scan_serieslist

list [start, stop, step, start2, stop2, step2, … ,startn, stopn, stepn]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

positionerstring, optional

Name of the positioner, this needs to be the same as defined in Bluesky or SPEC.

detectorstring, optional

Detector to be read from this scan, again it needs to be the same name as in Bluesky.

monitorstring, optional

Monitor to be read from this scan, again it needs to be the same name as in Bluesky.

normalizeboolean, optional

Normalization to selected/default monitor on/off

log: boolean

If True, y-axis plotted in logarithmic scale.

derivboolean, optional

calculated derivative

fitboolean, optional

Fitting of peak using model on/off. In case of deriv=True, derivative is fitted

directionlist, int

multiply axes for inversion: [1,-1] inverts y-axis

kwargs
model: enumeration

fit model: model = Model.Gaussian (default), Model.Lorentzian, Model.PseudoVoigt

source argument:
  • csv -> possible kwargs: folder, name_format.

  • spec -> possible kwargs: folder.

  • databroker -> possible kwargs: stream, query, use_db_v1.

polartools.diffraction.plot_fit(scan_series, source=None, output=False, var_series=None, positioner=None, detector=None, monitor=None, normalize=False, errorbar=True, xrange=None, **kwargs)[source]

Fit and plot series of scans with chosen functional and returns fit parameters.

Uses lmfit (https://lmfit.github.io/lmfit-py/).

Parameters:
scan_seriesint

start, stop, step, [start2, stop2, step2, … ,startn, stopn, stepn]

sourcedatabroker database, name of the spec file, or ‘csv’

Note that applicable kwargs depend on this selection.

outputboolean, optional

Output fit parameters and plot data+fit for each scan.

var_seriesstring or list

If string

  • Varying variable for scan series to be read from scan (detector),

    e.g. SampK (sample temperature), optional.

  • String starting with #metadata, reads metadata from CSV baseline

If list, information on metadata to be read: List starting with #P, #U or #Q for motor positions, user values or Q-position, optional:

  • #P [‘#P’, row, element_number], e.g. [‘#P’, 2, 0]

  • #U [‘#U’, Variable, element_number], e.g. [‘#U’, ‘KepkoI’, 1]

  • #Q [‘#Q’, None, element_number], e.g. [‘#Q’, None, 0]

  • #xx like #UA etc.: [‘#UA’, row, element_number]

If None, successive scans will be numbered starting from zero.

positionerstring, optional

Name of the positioner, this needs to be the same as defined in Bluesky or SPEC. If None is passed, it defauts to ‘4C Theta’ motor.

detectorstring, optional

Detector to be read from this scan, again it needs to be the same name as in Bluesky. If None is passed, it defaults to the APD detector.

monitorstring, optional

Name of the monitor detector. If None is passed, it defaults to the ion chamber 3.

normalizeboolean, optional

Normalization to selected/default monitor on/off

xrange: list

Set positioner range for fitting

noerrorboolean, optional

Plotting of errorbars on/off

kwargs

The necessary kwargs are passed to the loading functions defined by the source argument:

  • csv -> possible kwargs: folder, name_format.

  • spec -> possible kwargs: folder.

  • databroker -> possible kwargs: stream, query.

modelstring, optional

fit model: Gaussian, Lorentzian, PseudoVoigt

Note that a warning will be printed if the an unnecessary kwarg is passed.

Returns:
fitlmfit ModelResult class

Contains the fit results. See: https://lmfit.github.io/lmfit-py/model.html#the-modelresult-class

plot: plot of fitted data as function of variable changing between scans