Source code for ccpn.ui.gui.lib.StripLib

"""
Strip Library functionalities
"""
#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2022"
__credits__ = ("Ed Brooksbank, Joanna Fox, Victoria A Higman, Luca Mureddu, Eliza Płoskoń",
               "Timothy J Ragan, Brian O Smith, Gary S Thompson & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
                 "CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
                 "J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Ed Brooksbank $"
__dateModified__ = "$dateModified: 2022-01-21 11:22:12 +0000 (Fri, January 21, 2022) $"
__version__ = "$Revision: 3.0.4 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: CCPN $"
__date__ = "$Date: 2017-04-07 10:28:41 +0000 (Fri, April 07, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================

import typing
from typing import List, Tuple

from ccpn.core._OldChemicalShift import _OldChemicalShift
from ccpn.core.NmrAtom import NmrAtom
from ccpn.core.lib.AxisCodeLib import getAxisCodeMatchIndices
from ccpn.core.lib.ContextManagers import undoStackBlocking
from ccpn.ui.gui.lib.GuiStrip import GuiStrip
from ccpn.util.Common import reorder
from ccpn.util.Logging import getLogger
from ccpn.util.isotopes import name2IsotopeCode


def _getCurrentZoomRatio(viewRange):
    xRange, yRange = viewRange
    xMin, xMax = xRange
    xRatio = (xMax - xMin)
    yMin, yMax = yRange
    yRatio = (yMax - yMin)
    return xRatio, yRatio





def _setStripAxisPosition(strip, axisIndex, position, update=True):
    if axisIndex < 2:
        strip.setAxisPosition(axisIndex=axisIndex, position=position, update=update)
    else:
        strip.orderedAxes[axisIndex].position = position


def _setStripAxisWidth(strip, axisIndex, width, update=True):
    if axisIndex < 2:
        strip.setAxisWidth(axisIndex=axisIndex, width=width, update=update)
    else:
        strip.orderedAxes[axisIndex].width = width


def _setStripToLimits(strip, axisIndex, update=True):
    range = strip.getAxisLimits(axisIndex)
    strip.setAxisRegion(axisIndex=axisIndex, range=range, update=update)


[docs]def copyStripAxisPositionsAndWidths(fromStrip, toStrip): """copy the strip axes to the new strip """ positions = [axis.position for axis in fromStrip.orderedAxes] widths = [axis.width for axis in fromStrip.orderedAxes] # remove non-XY widths for ii in range(2, len(widths)): widths[ii] = None positions = reorder(positions, fromStrip.axisCodes, toStrip.axisCodes) widths = reorder(widths, fromStrip.axisCodes, toStrip.axisCodes) for ii in range(2, len(widths)): widths[ii] = None navigateToPositionInStrip(toStrip, positions, toStrip.axisCodes, widths)
[docs]def matchAxesAndNmrAtoms(strip: GuiStrip, nmrAtoms: typing.List[NmrAtom]): shiftDict = {} shiftList = strip.spectra[0].chemicalShiftList for axis in strip.orderedAxes: # axis may not exist in Nd if axis: shiftDict[axis.code] = [] for nmr in nmrAtoms: if nmr.isotopeCode == name2IsotopeCode(axis.code): shift = shiftList.getChemicalShift(nmrAtom=nmr) if shift is not None and shift.value is not None and isPositionWithinfBounds(strip, shift, axis): shiftDict[axis.code].append(shift) else: raise RuntimeError('strip %s contains undefined axes' % str(strip)) return shiftDict
[docs]def isPositionWithinfBounds(strip: GuiStrip, shift: _OldChemicalShift, axis: object): """ Determines whether a given shift is within the bounds of the specified axis of the specified strip. NBNB Bug Fixed by Rasmus 13/3/2016. This was not used then. Maybe it should be? Modified to use aliasingLimits instead of spectrumLimits. Rasmus, 24/7/2016 """ minima = [] maxima = [] axisIndex = strip.axisOrder.index(axis.code) for spectrumView in strip.spectrumViews: spectrumIndices = spectrumView.dimensionIndices index = spectrumIndices[axisIndex] if index: minima.append(spectrumView.spectrum.aliasingLimits[index][0]) maxima.append(spectrumView.spectrum.aliasingLimits[index][1]) if len(maxima) < 1: return True else: return min(minima) < shift.value <= max(maxima)