def saveContours(spectrum, contourFile, fileName, levels, firstInt, lastInt, isBigEndian=None): from memops.c.StoreHandler import StoreHandler import ccpnmr.c.ContourStyle as ContourStyle import ccpnmr.c.ContourLevels as ContourLevels # set things up if isBigEndian is None or isBigEndian == isBigEndian(): swap = 0 else: swap = 1 handler = StoreHandler(fileName, swap) # below irrelevant but mandatory argument pos_colors = neg_colors = [(0.0, 0.0, 0.0)] contourStyle = ContourStyle.ContourStyle(pos_colors, neg_colors, 0, 0) contourLevels = ContourLevels.ContourLevels(levels) contourFile.draw(handler, firstInt, lastInt, contourLevels, contourStyle)
def createDataSource(self, experiment, name, dataUrl=None): # set displayNames if available ll = [dd.get('name') for dd in self.dimParams] if [x for x in ll if x]: # we have some displayNames self.displayNames = ll extraData = self.extraData if extraData: # set/override data from input extra parameters for tag in dimParamDefs.keys() + extraDimParams: val = extraData.get(tag) if val: setattr(self, tag, val) # set referencing from carppm (carrier pm value) carppm = extraData.get('carppm') if carppm: maxIndex = len(self.numPointsOrig) - 1 for ii, refppm in enumerate(carppm): if self.refpt[ii] == 1.0: # reset index to allow for the size arrays being shorter # basically use last point by overshoot (project dimensions) # skip if refpt !- 1.0, to preserve some user-set values jj = min(ii, maxIndex) refpt = self.numPointsOrig[ jj] / 2 + 1 - self.pointOffset[jj] self.refpt[ii] = refpt self.refppm[ii] = refppm if self.ndim < 1: raise ApiError('ndim = 0 (did you choose correct format?)') # check that information compatible with experiment if experiment.numDim != self.ndim: raise ApiError( 'for now experiment numDim (%d) and dataSource numDim (%d) need to be the same' % (experiment.numDim, self.ndim)) for n in range(self.ndim): # TBD: is this correct? (e.g. do fidDimType have nuc?) if self.dimType[n] != freqDimType: if self.dimType[n] == fidDimType: raise ApiError('dimension %s is FID - not yet supported.' % (n + 1)) else: self.nuc[n] = None # match ExpDimRefs if any, to dims. expDims = experiment.sortedExpDims() for n in range(self.ndim): expDim = expDims[n] if self.dimType[n] == freqDimType: if expDim.expDimRefs: if len(expDim.expDimRefs) == 1: expDimRef = expDim.findFirstExpDimRef() if (self.nuc[n]): if expDimRef.isotopeCodes != (self.nuc[n], ): print( 'Warning: in dim %d experiment has isotopeCodes %s, spectrum has %s, must be the same' % (n + 1, expDimRef.isotopeCodes, (self.nuc[n], ))) if (abs(expDimRef.sf - self.sf[n]) > 1.0e-2): print( 'Warning: inconsistent sf in dim %d: %3.2f vs %3.2f' % (n + 1, expDimRef.sf, self.sf[n])) elif expDimRef.isotopeCodes: print( 'Warning: in dim %d experiment has isotopeCodes %s, spectrum has no nuc set' % (n + 1, expDimRef.isotopeCodes)) else: # NBNB TBD new code - may not work for all cases if (self.nuc[n]): expDimRef = None expDimRefs = [ x for x in expDim.sortedExpDimRefs() if x.isotopeCodes == (self.nuc[n], ) ] for xdr in expDimRefs: if abs(xdr.sf - self.sf[n]) <= 1.0e-2: expDimRef = xdr break elif not xdr.sf: expDimRef = xdr if expDimRef is None: if expDimRefs: expDimRef = expDimRefs[0] print( 'Warning: in dim %d no ExpDimRef fits sf %s' % (n + 1, self.nuc[n]), expDimRef.sf, self.sf[n]) else: expDimRef = expDim.findFirstExpDimRef() print( 'Warning: in dim %d no ExpDimRef fits nucleus %s' % (n + 1, self.nuc[n]), expDimRef.sf, self.sf[n]) else: expDimRef = expDim.findFirstExpDimRef() print( 'Warning: in dim %d spectrum has no nuc set' % (n + 1)) else: # non-freq dim if expDim.expDimRefs: raise ApiError('non-freq dim has experiment dim ref') if self.dataFile: # create DataStore memopsRoot = experiment.root if dataUrl: preferDataUrls = [dataUrl] else: oldDataStores = (x.dataStore for x in experiment.dataSources) preferDataUrls = set(x.dataUrl for x in oldDataStores if x) newDataUrl, filePath = getDataStoringFromFilepath( memopsRoot, self.dataFile, preferDataUrls=preferDataUrls, keepDirectories=self.keepDirectories) if dataUrl is not None and newDataUrl is not dataUrl: raise ApiError("DataUrl with path %s not valid for file %s" % (dataUrl.url.path, self.dataFile)) dataLocationStore = newDataUrl.dataLocationStore if self.big_endian is not None: bigEndian = self.big_endian elif self.swap: bigEndian = not isBigEndian() else: bigEndian = isBigEndian() if self.integer: numberType = 'int' else: numberType = 'float' isComplex = [ self.dimParams[ii].get('isComplex') for ii in range(self.ndim) ] if None in isComplex: isComplex = self.ndim * [False] # numPoints - necessary in case of projection spectra numPoints = self.npts if None in numPoints: numPoints = numPoints[:numPoints.index(None)] params = { 'dataUrl': newDataUrl, 'path': filePath, 'numPoints': numPoints, 'isBigEndian': bigEndian, 'numberType': numberType, 'isComplex': isComplex, 'headerSize': self.head, 'fileType': self.format, 'nByte': self.nbytes, } if self.format == 'Factorised': for tag in ('numShapes', 'isReconstructable', 'isResolved', 'numRecords'): if tag in self.topParams: params[tag] = self.topParams[tag] dataStore = dataLocationStore.newShapeMatrix(**params) # NBNB TBD assumes there are no 2D shapes. To be changed! if len(self.dimParams) == self.topParams['numShapes']: # all shapes are 1D for dim in range(self.topParams['numShapes']): dataStore.newComponentShape(dims=(dim, )) else: raise ApiError( "Decompositions must have one shape per dim (for now)") # add Component elements for dd in self.topParams['componentList']: dataStore.newComponent(**dd) else: # default: blocked binary matrix if self.blockHead: params['blockHeaderSize'] = self.blockHead # blockSizes - necessary in case of projection spectra blockSizes = self.block if None in blockSizes: blockSizes = blockSizes[:blockSizes.index(None)] # Bruker in Topspin 3 can have blockSize = 0 it seems for nn, blockSize in enumerate(blockSizes): if blockSize == 0: blockSizes[nn] = numPoints[nn] dataStore = dataLocationStore.newBlockedBinaryMatrix( blockSizes=blockSizes, **params) else: dataStore = None #set ndims and set up for extra (projection) dims ndims = self.ndim lastEdrs = expDims[-1].expDimRefs if (self.nAxes > ndims and self.dimType[-1] == freqDimType and (not lastEdrs or self.nAxes <= ndims + len(lastEdrs))): # We have extra (projection) ExpDimRefs, # They fit onto existing, or the last dimension still needs ExpDimRefs # Set up to add extra ExpDimRefs to last ExpDim expDims.extend((self.nAxes - ndims) * [expDims[-1]]) ndims = self.nAxes #set ExpDimRefs and isAcquisition hasNoAcqDim = ( expDims[0].experiment.findFirstExpDim(isAcquisition=True) is None) for n in range(ndims): if self.dimType[n] == freqDimType: expDim = expDims[n] lenExpDimRefs = len(expDim.expDimRefs) if (not lenExpDimRefs or (n >= self.ndim + lenExpDimRefs - 1)): # The first term ensures an ExpDimRef for every freqDim # The second adds extra expDimRefs to the last Dim to use up all # read Dims (for projection experiments if hasNoAcqDim and n == self.acquisitionDim: expDim.isAcquisition = True hasNoAcqDim = False if self.nuc[n]: xdr = expDim.newExpDimRef( sf=self.sf[n], unit='ppm', baseFrequency=self.baseFreq[n], isotopeCodes=[self.nuc[n]]) else: xdr = expDim.newExpDimRef( sf=self.sf[n], unit='None', baseFrequency=self.baseFreq[n], ) # set displayName if hasattr(self, 'displayNames'): displayNames = self.displayNames if displayNames: ii = 0 for expDim in experiment.sortedExpDims(): for expDimRef in expDim.sortedExpDimRefs(): expDimRef.displayName = displayNames[ii] ii += 1 dataSource = experiment.newDataSource(name=name, numDim=self.ndim, dataStore=dataStore, dataType='processed', scale=self.scale) scalingFactors = None if hasattr(self, 'scalingFactors'): ll = self.scalingFactors if ll: scalingFactors = ll # Set DataSource.DataDims for n in range(self.ndim): sw = self.sw[n] npts = self.npts[n] numPointsOrig = self.numPointsOrig[n] or npts expDim = expDims[n] if self.dimType[n] == freqDimType: freqDataDim = dataSource.newFreqDataDim( dim=n + 1, numPoints=npts, isComplex=False, numPointsOrig=numPointsOrig, pointOffset=self.pointOffset[n], valuePerPoint=sw / float(npts), expDim=expDim) expDimRef = expDim.findFirstExpDimRef() ddr = freqDataDim.newDataDimRef(refPoint=self.refpt[n], refValue=self.refppm[n], expDimRef=expDimRef) # reset to reference on O1 centrePoint = freqDataDim.numPointsOrig / 2 - freqDataDim.pointOffset + 1 ddr.refValue = ddr.pointToValue(centrePoint) ddr.refPoint = centrePoint if scalingFactors is not None: # NB DimensionScaling is set automatically in some cases previousDimScaling = freqDataDim.findFirstDimensionScaling( expDimRef=expDimRef) if previousDimScaling is None: freqDataDim.newDimensionScaling( expDimRef=expDimRef, scalingFactors=(scalingFactors[n], )) else: previousDimScaling.scalingFactors = ( scalingFactors[n], ) elif self.dimType[n] == fidDimType: raise ApiError('fidDimType not supported yet') elif self.dimType[n] == sampledDimType: # TBD: set conditionVaried somehow dataSource.newSampledDataDim(dim=n + 1, numPoints=npts, isComplex=False, pointValues=self.pointValues[n], expDim=expDim) else: raise ApiError('%s dimType not supported yet' % self.dimType[n]) # Add extra (projection) DataDimRefs to last dataDim # NB if ndims > self.ndim the last dimension is a freqDataDim # freqDataDim, expDim, and npts come correctly out of the preceding loop. expDimRefs = expDim.sortedExpDimRefs() ii = 0 for n in range(self.ndim, ndims): ii += 1 # NB we do want to start with expDimRefs[1] expDimRef = expDimRefs[ii] ddr = freqDataDim.newDataDimRef(refPoint=self.refpt[n], refValue=self.refppm[n], expDimRef=expDimRefs[ii], localValuePerPoint=self.sw[n] / npts) # reset to reference on O1 centrePoint = freqDataDim.numPointsOrig / 2 - freqDataDim.pointOffset + 1 ddr.refValue = ddr.pointToValue(centrePoint) ddr.refPoint = centrePoint if scalingFactors is not None: freqDataDim.newDimensionScaling( expDimRef=expDimRef, scalingFactors=(scalingFactors[n], )) # return dataSource
byte_order = [0x40, 0x16, 0x14, 0x7b] t = [ord(c) for c in s[8:12]] if (t == byte_order): big_endian = True else: t.reverse() if (t == byte_order): big_endian = False else: raise ApiError( 'bytes 8 through 11 should be [ 0x40, 0x16, 0x14, 0x7b ] or reverse' ) big_endian = big_endian swap = not (big_endian == isBigEndian()) if (swap): x.byteswap() return (s, x, big_endian, swap) def getDataFileName(template, z, a=None): """Get NMRPipe fileName given template and z and (optionally) a. Note that z and a start counting at 0, not 1. """ if a is None: name = template % (z + 1) else:
def createDataSource(self, experiment, name, dataUrl=None): # check that information compatible with experiment if experiment.numDim != self.ndim: raise ApiError('for now experiment numDim (%d) and dataSource numDim (%d) need to be the same' % (experiment.numDim, self.ndim)) for n in range(self.ndim): # TBD: is this correct? (e.g. do fidDimType have nuc?) if self.dimType[n] != self.freqDimType: self.nuc[n] = None expDims = experiment.sortedExpDims() useExpDimRefs = [None] * self.ndim for n in range(self.ndim): expDim = expDims[n] if self.dimType[n] == self.freqDimType: if expDim.expDimRefs: if len(expDim.expDimRefs) == 1: #raise ApiError('for now can only have one experiment dim ref') expDimRef = expDim.findFirstExpDimRef() useExpDimRefs[n] = expDimRef if (self.nuc[n]): if expDimRef.isotopeCodes != (self.nuc[n],): print 'Warning: in dim %d experiment has isotopeCodes %s, spectrum has %s, must be the same' % (n+1, expDimRef.isotopeCodes, (self.nuc[n],)) if (abs(expDimRef.sf-self.sf[n]) > 1.0e-2): print 'Warning: inconsistent sf in dim %d: %3.2f vs %3.2f' % (n+1, expDimRef.sf, self.sf[n]) elif expDimRef.isotopeCodes: print 'Warning: in dim %d experiment has isotopeCodes %s, spectrum has no nuc set' % (n+1, expDimRef.isotopeCodes) else: # NBNB TBD new code - may not work for all cases if (self.nuc[n]): expDimRef = None expDimRefs = expDim.findAllExpDimRefs(isotopeCodes=(self.nuc[n],)) for xdr in expDimRefs: if abs(xdr.sf-self.sf[n]) <= 1.0e-2: expDimRef = xdr break elif not xdr.sf: expDimRef = xdr if expDimRef is None: expDimRef = expDim.findFirstExpDimRef() print ( 'Warning: in dim %d no ExpDimRef fits nucleus %s' % (n+1, self.nuc[n]) , expDimRef.sf, self.sf[n] ) else: expDimRef = expDim.findFirstExpDimRef() print ( 'Warning: in dim %d spectrum has no nuc set' % (n+1) ) else: # non-freq dim if expDim.expDimRefs: raise ApiError('non-freq dim has experiment dim ref') if self.dataFile: memopsRoot = experiment.root # create DataStore if dataUrl: preferDataUrls = [dataUrl] else: oldDataStores = (x.dataStore for x in experiment.dataSources) preferDataUrls = set(x.dataUrl for x in oldDataStores if x) newDataUrl, filePath = getDataStoringFromFilepath(memopsRoot, self.dataFile, preferDataUrls=preferDataUrls, keepDirectories=self.keepDirectories) if dataUrl is not None and newDataUrl is not dataUrl: raise ApiError("DataUrl with path %s not valid for file %s" % (dataUrl.url.path, self.dataFile) ) dataLocationStore = newDataUrl.dataLocationStore if self.big_endian is not None: bigEndian = self.big_endian elif self.swap: bigEndian = not isBigEndian() else: bigEndian = isBigEndian() if self.integer: numberType = 'int' else: numberType = 'float' isComplex = [self.dimParams[ii].get('isComplex') for ii in range(self.ndim)] if None in isComplex: isComplex = self.ndim * [False] params = { 'dataUrl':newDataUrl, 'path':filePath, 'numPoints':self.npts, 'isBigEndian':bigEndian, 'numberType':numberType, 'isComplex':isComplex, 'headerSize': self.head } dataStoreType = self.topParams.get('dataStoreType') if dataStoreType == 'factorised': dataStore = dataLocationStore.newShapeMatrix( numRecords=self.topParams['numRecords'], **params) # NBNB TBD assumes there are no 2D shapes. To be changed! for dim in range(len(self.dimParams)): dataStore.newComponentShape(dims = (dim,)) else: dataStore = dataLocationStore.newBlockedBinaryMatrix( blockSizes=self.block, **params) # TBD: TEMP: use appData until blockHeaderSize is in data model if self.blockHead and hasattr(dataStore.root, 'application'): dataStore.root.application.setValue(dataStore, 'blockHeaderSize', self.blockHead) else: dataStore = None for n in range(self.ndim): expDim = expDims[n] if not expDim.expDimRefs and self.dimType[n] == self.freqDimType: if self.nuc[n]: expDim.newExpDimRef(sf=self.sf[n], unit='ppm', isotopeCodes=[self.nuc[n]]) else: expDim.newExpDimRef(sf=self.sf[n], unit='None') dataSource = experiment.newDataSource(name=name, numDim=self.ndim, dataStore=dataStore, dataType='processed') for n in range(self.ndim): sw = self.sw[n] npts = self.npts[n] expDim = expDims[n] if self.dimType[n] == self.freqDimType: freqDataDim = dataSource.newFreqDataDim(dim=n+1, numPoints=npts, isComplex=False, numPointsOrig=npts, valuePerPoint=sw/float(npts), expDim=expDim) freqDataDim.newDataDimRef(refPoint=self.refpt[n], refValue=self.refppm[n], expDimRef=expDim.findFirstExpDimRef()) elif self.dimType[n] == self.fidDimType: raise ApiError('fidDimType not supported yet') elif self.dimType[n] == self.sampledDimType: # TBD: set conditionVaried somehow dataSource.newSampledDataDim(dim=n+1, numPoints=npts, isComplex=False, pointValues=self.pointValues[n], expDim=expDim) else: raise ApiError('%s dimType not supported yet' % self.dimType[n]) return dataSource
class UcsfParams(ExternalParams): format = 'UCSF' def __init__(self, file, **kw): self.dataFile = file ExternalParams.__init__(self, **kw) # ExternalParams requires this to be defined def parseFile(self): try: fp = open(self.dataFile, 'rb') except IOError, e: raise ApiError(str(e)) s = fp.read(ucsf_file_header) if (len(s) < ucsf_file_header): raise ApiError('file shorter than expected length (%d bytes) of first part of header (never mind data)' % ucsf_file_header) if (s[:8] != 'UCSF NMR'): raise ApiError('first eight bytes of header = "%s", should be "UCSF NMR"' % s[:8]) ndim = self.ndim = ord(s[10]) self.head = ucsf_file_header + ndim*ucsf_dim_header self.big_endian = True # UCSF files always big endian self.swap = not isBigEndian() s = fp.read(ndim*ucsf_dim_header) if (len(s) < ndim*ucsf_dim_header): raise ApiError('file shorter than expected length (%d bytes) of header (never mind data)' % (ucsf_file_header+ndim*ucsf_dim_header)) fp.close() x = array.array('i') y = array.array('f') x.fromstring(s) y.fromstring(s) if (self.swap): x.byteswap() y.byteswap() self.initDims() for i in range(ndim): j = ndim - i - 1 # UCSF does dims backwards base = (j * ucsf_dim_header) / 4 self.npts[i] = x[base+2] self.block[i] = x[base+4] self.sf[i] = y[base+5] self.sw[i] = y[base+6] self.refppm[i] = y[base+7] # 27 Oct 2006: added 1.0, which seems to be the correct thing to do self.refpt[i] = 1.0 + 0.5 * float(self.npts[i]) nuc = s[4*base:4*base+6] n = nuc.find(chr(0)) if (n >= 0): nuc = nuc[:n] self.nuc[i] = self.standardNucleusName(nuc)