Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_blosc_nthreads() -> int: # pragma: no cover
"""set the blosc library to two less than the core count on the system.
If less than 2 cores are ncores-2, we set the value to two.
Returns
-------
int
ncores blosc will use on the system
"""
nCores = blosc.detect_number_of_cores()
if nCores == 1:
nUsed = 1
elif nCores == 2:
nUsed = 2
elif nCores <= 4:
nUsed = nCores - 1
else:
nUsed = nCores - 2
blosc.set_nthreads(nUsed)
return nUsed
The following compressors are supported:
'zlib'
'zstd'
'lz4'
Memory mapping is not possible in this case at present.
"""
if not bloscPresent:
print( "ioMRC: blosc not present, cannot compress files." )
return
if n_threads == None:
blosc.nthreads = blosc.detect_number_of_cores()
else:
blosc.nthreads = n_threads
image = np.empty( header['dimensions'], dtype=header['dtype'] )
# We can read MRC2014 files that don't start at 1024 bytes, but not write them
# (as they are non-standard and we don't like breaking stuff)
blosc_chunk_pos = 1024 + header['extendedBytes']
for J in np.arange(image.shape[0]):
f.seek( blosc_chunk_pos )
( (nbytes, blockSize, ctbytes ), (ver_info) ) = readBloscHeader(f)
f.seek(blosc_chunk_pos)
# blosc includes the 16 header bytes in ctbytes
image[J,:,:] = np.reshape(
np.frombuffer( blosc.decompress( f.read( ctbytes ) ), dtype=image.dtype ),
image.shape[1:] )
header['maxImage'] = np.mean( np.real( input_image ) )
header['voltage'] = voltage
if not bool( header['voltage'] ):
header['voltage'] = 0.0
header['C3'] = C3
if not bool( header['C3'] ):
header['C3'] = 0.0
header['gain'] = gain
if not bool( header['gain'] ):
header['gain'] = 1.0
header['compressor'] = compressor
header['clevel'] = clevel
if n_threads == None and bloscPresent:
n_threads = blosc.detect_number_of_cores()
header['n_threads'] = n_threads
# TODO: can we detect the number of cores without adding a heavy dependancy?
if dtype == 'uint4':
# Decimate to packed 4-bit
input_image = input_image.astype('uint8')
input_image = input_image[:,:,::2] + np.left_shift(input_image[:,:,1::2],4)
else:
# We are going to append to an already existing file:
# So we try to figure out its header with 'imod' or 'eman2' file conventions:
try:
header = readMRCHeader( MRCfilename, endian, fileConvention = 'imod', pixelunits=pixelunits )
header['compressor'] = None
header['packedBytes'] = 0
header['clevel'] = 1
header['maxImage'] = 1.0
header['minImage'] = 0.0
header['meanImage'] = 0.0
header['pixelsize'] = 0.1
header['pixelunits'] = u"nm" # Can be "\AA" for Angstroms
header['voltage'] = 300.0 # kV
header['C3'] = 2.7 # mm
header['gain'] = 1.0 # counts/electron
if bloscPresent:
header['n_threads'] = blosc.detect_number_of_cores()
return header