11import logging
22import os
3+ import warnings
34from concurrent import futures
45
56import mrcfile
@@ -104,8 +105,34 @@ def read_mrc(self):
104105 Micrograph image.
105106 """
106107
107- with mrcfile .open (self .filename , mode = "r+" , permissive = True ) as mrc :
108- im = mrc .data .astype ("float" )
108+ # mrcfile tends to yield many warnings about EMPIAR datasets being corrupt
109+ # These warnings generally seem benign, and the message could be clearer
110+ # The following code handles the warnings via ASPIRE's logger
111+ with warnings .catch_warnings (record = True ) as ws :
112+ # Cause all warnings to always be triggered in this context
113+ warnings .simplefilter ("always" )
114+
115+ # Try to open the mrcfile
116+ with mrcfile .open (self .filename , mode = "r" , permissive = True ) as mrc :
117+ im = mrc .data .astype ("float" )
118+
119+ # Log each mrcfile warning to debug log, noting the associated file
120+ for w in ws :
121+ logger .debug (
122+ "In APPLE.picking mrcfile.open reports corruption for"
123+ f" { self .filename } warning: { w .message } "
124+ )
125+
126+ # Log a single warning to user
127+ # Give context and note assocated filename
128+ if len (ws ) > 0 :
129+ logger .warning (
130+ f"APPLE.picking mrcfile reporting { len (ws )} corruptions."
131+ " Most likely this is a problem with the header contents."
132+ " Details written to debug log."
133+ f" APPLE will attempt to continue processing { self .filename } "
134+ )
135+
109136 self .original_im = im
110137
111138 # Discard outer pixels
@@ -121,7 +148,9 @@ def read_mrc(self):
121148 size = tuple ((np .array (im .shape ) / config .apple .mrc_shrink_factor ).astype (int ))
122149
123150 # Note, float64 required for signal.correlate call accuracy.
124- im = np .array (Image .fromarray (im ).resize (size , Image .BICUBIC ), dtype = np .float64 )
151+ im = np .asarray (Image .fromarray (im ).resize (size , Image .BICUBIC )).astype (
152+ np .float64 , copy = False
153+ )
125154
126155 im = signal .correlate (
127156 im ,
0 commit comments