Ejemplo n.º 1
0
 def test_nlabl_too_large(self):
     with mrcfile.new(self.temp_mrc_name) as mrc:
         mrc.header.label[1] = "test label"
         mrc.header.nlabl = 3
     result = mrcfile.validate(self.temp_mrc_name,
                               print_file=self.print_stream)
     assert result == False
     print_output = self.print_stream.getvalue()
     assert ("Error in header labels: "
             "nlabl is 3 but 2 labels contain text" in print_output)
     assert len(sys.stdout.getvalue()) == 0
     assert len(sys.stderr.getvalue()) == 0
Ejemplo n.º 2
0
 def test_invalid_axis_mapping(self):
     with mrcfile.new(self.temp_mrc_name) as mrc:
         mrc.header.mapc = 3
         mrc.header.mapr = 4
         mrc.header.maps = -200
     result = mrcfile.validate(self.temp_mrc_name,
                               print_file=self.print_stream)
     assert result == False
     print_output = self.print_stream.getvalue()
     assert "Invalid axis mapping: found [-200, 3, 4]" in print_output
     assert len(sys.stdout.getvalue()) == 0
     assert len(sys.stderr.getvalue()) == 0
Ejemplo n.º 3
0
 def test_empty_labels_in_list(self):
     with mrcfile.new(self.temp_mrc_name) as mrc:
         mrc.header.label[2] = 'test label'
         mrc.header.nlabl = 2
     result = mrcfile.validate(self.temp_mrc_name,
                               print_file=self.print_stream)
     assert result == False
     print_output = self.print_stream.getvalue()
     assert ("Error in header labels: empty labels appear between "
             "text-containing labels" in print_output)
     assert len(sys.stdout.getvalue()) == 0
     assert len(sys.stderr.getvalue()) == 0
Ejemplo n.º 4
0
 def sniff(self, filename):
     # Handle the wierdness of mrcfile:
     # https://github.com/ccpem/mrcfile/blob/master/mrcfile/validator.py#L88
     try:
         # An exception is thrown
         # if the file is not an
         # mrc2014 file.
         if mrcfile.validate(filename, print_file=StringIO()):
             return True
     except Exception:
         return False
     return False
Ejemplo n.º 5
0
 def test_min_max_and_mean_undetermined(self):
     data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
     with mrcfile.new(self.temp_mrc_name) as mrc:
         mrc.set_data(data)
         mrc.header.dmin = 30.1
         mrc.header.dmax = 30.0
         mrc.header.dmean = 29.9
     result = mrcfile.validate(self.temp_mrc_name,
                               print_file=self.print_stream)
     assert result == True
     assert len(sys.stdout.getvalue()) == 0
     assert len(sys.stderr.getvalue()) == 0
Ejemplo n.º 6
0
 def test_incorrect_dmean(self):
     data = np.arange(-10, 20, dtype=np.float32).reshape(2, 3, 5)
     with mrcfile.new(self.temp_mrc_name) as mrc:
         mrc.set_data(data)
         mrc.header.dmean = -2.5
     result = mrcfile.validate(self.temp_mrc_name,
                               print_file=self.print_stream)
     assert result == False
     print_output = self.print_stream.getvalue()
     assert ("Error in data statistics: mean is {0} but the value "
             "in the header is -2.5".format(data.mean()) in print_output)
     assert len(sys.stdout.getvalue()) == 0
     assert len(sys.stderr.getvalue()) == 0
Ejemplo n.º 7
0
 def test_mz_incorrect_for_volume_stack(self):
     with mrcfile.new(self.temp_mrc_name) as mrc:
         mrc.set_data(np.arange(120, dtype=np.float32).reshape(3, 2, 4, 5))
         mrc.header.mz = 5
     with warnings.catch_warnings(record=True):
         result = mrcfile.validate(self.temp_mrc_name,
                                   print_file=self.print_stream)
         assert result == False
         print_output = self.print_stream.getvalue()
         assert ("Error in dimensions for volume stack: nz should be "
                 "divisible by mz. Found nz = 6, mz = 5" in print_output)
     assert len(sys.stdout.getvalue()) == 0
     assert len(sys.stderr.getvalue()) == 0
Ejemplo n.º 8
0
def check_mrc(inputfile, outmode=1):
    a = mrcfile.validate(inputfile)
    #a = mrcfile.validate('/Users/shi/Desktop/out1.mrc')
    if a == True:
        print("input file: ", file, " ok")
    else:
        print(inputfile, " is not a vaild mrc file....trying to repair")
        if outmode == 1:
            outfile = inputfile
        else:
            outfile = inputfile.rstrip(".mrc") + "_r.mrc"
        process = subprocess.Popen(["newstack", inputfile, outfile],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        #print(stdout.decode("utf-8"))
        a = mrcfile.validate(outfile)

        if a == True:
            print("file ok. saved to :", outfile)
        else:
            print("repair not succesful")
            exit()
Ejemplo n.º 9
0
 def __init__(self, filehandle, validate=False):
     '''
         This initialize function only open .mrc file with 
         mmap read-only
     '''
     super(Mrc, self).__init__()
     self.filehandle = filehandle
     self.mrc = mrcfile.mmap(filehandle, mode='r')
     if validate:
         print('*' * 20 + 'Validating start' + '*' * 20)
         if not mrcfile.validate(filehandle):
             raise ValueError('Initial Mrc File is not validated')
         print('*' * 20 + 'Validating finished' + '*' * 20)
     '''
Ejemplo n.º 10
0
 def test_short_map_id(self):
     """This tests the case of files where the map ID is almost correct.
     For example, MotionCor2 writes files with ID 'MAP\0', which is not
     valid according to the MRC2014 spec on the CCP-EM website, but could
     be considered valid according to the MRC2014 paper (which just
     specifies 'MAP', i.e. without the final byte). We should read such
     files without errors or warnings, but they should fail a strict
     validation check."""
     with mrcfile.new(self.temp_mrc_name) as mrc:
         mrc.header.map = b'MAP\0'
     result = mrcfile.validate(self.temp_mrc_name,
                               print_file=self.print_stream)
     assert result == False
     print_output = self.print_stream.getvalue()
     assert "Map ID string is incorrect" in print_output
     assert len(sys.stdout.getvalue()) == 0
     assert len(sys.stderr.getvalue()) == 0
Ejemplo n.º 11
0
def check_valid(filename):
    import io
    output = io.StringIO()
    res = mrcfile.validate(filename, print_file=output)
    print(output.getvalue().strip())
    return res
Ejemplo n.º 12
0
input_dir = define_local_input_directory(
    "D:/Google Drive/_PROJECTS_/MRC_convert/stacktest/file_source")
output_dir = define_local_output_directory(
    "D:/Google Drive/_PROJECTS_/MRC_convert/stacktest/file_source")

job_name = raw_input('Job name: name sub-directory to create: ')
file_name = raw_input(
    'Input file: without extension, enter name of tif file to process: ')
output_path = output_dir + job_name
if not os.path.exists(output_path):
    os.makedirs(output_path)
img = cv2.imread(input_dir + '/' + file_name + '.tif', 0)
with mrcfile.new(output_path + '/' + file_name + '.mrc',
                 overwrite=True) as mrc:
    mrc.set_data(img)  #, dtype=np.int8)
    mrc.data
print 'mrc validation for: ' + output_path + '/' + file_name + '.mrc \n', mrcfile.validate(
    output_path + '/' + file_name + '.mrc')
print img
print '#################################'

# for i in range(0,20):
#     img = cv2.imread('origamis'+'%04d'% i+'.tif', 0)
#     #np.savetxt(newpath+'/text_image'+'%04d'% i+'.txt',img)
#     with mrcfile.new(newpath+'/origamis'+'%04d'% i+'.mrc',overwrite=True) as mrc:
#         mrc.set_data(img)#, dtype=np.int8)
#         mrc.data
#     print 'mrc validation for: ' + newpath+'/origamis'+'%04d'% i+'.mrc \n', mrcfile.validate(newpath+'/origamis'+'%04d'% i+'.mrc')
#     print '\n origamis'+'%04d'% i, img
#     print '#################################'
Ejemplo n.º 13
0
 def test_bzip2_emdb_file(self):
     result = mrcfile.validate(self.bzip2_mrc_name, self.print_stream)
     assert result == False
     print_output = self.print_stream.getvalue()
     assert print_output.strip() == ("File does not declare MRC format "
                                     "version 20140: nversion = 0")