Example #1
0
    def test_create(self):
        # Check we can create a new file from scratch, with the correct
        # cross-referencing automatically applied to the headers to
        # enable it to load again.
        with self.temp_filename() as temp_path:
            ffv = FieldsFileVariant(temp_path, FieldsFileVariant.CREATE_MODE)
            ffv.fixed_length_header = FixedLengthHeader([-1] * 256)
            ffv.fixed_length_header.data_set_format_version = 20
            ffv.fixed_length_header.sub_model = 1
            ffv.fixed_length_header.dataset_type = 3
            constants = IMDI * np.ones(46, dtype=int)
            constants[5] = 4
            constants[6] = 5
            ffv.integer_constants = constants
            ints = IMDI * np.ones(45, dtype=int)
            ints[17] = 4  # LBROW
            ints[18] = 5  # LBNPT
            ints[20] = 0  # LBPACK
            ints[21] = 2  # LBREL
            ints[38] = 1  # LBUSER(1)
            reals = list(range(19))
            src_data = np.arange(20, dtype='f4').reshape((4, 5))
            ffv.fields = [Field2(ints, reals, src_data)]
            ffv.close()

            ffv = FieldsFileVariant(temp_path)
            # Fill with -1 instead of IMDI so we can detect where IMDI
            # values are being automatically set.
            expected = -np.ones(256, dtype=int)
            expected[0] = 20
            expected[1] = 1
            expected[4] = 3
            expected[99:101] = (257, 46)  # Integer constants
            expected[104:106] = IMDI
            expected[109:112] = IMDI
            expected[114:117] = IMDI
            expected[119:122] = IMDI
            expected[124:127] = IMDI
            expected[129:131] = IMDI
            expected[134:136] = IMDI
            expected[139:145] = IMDI
            expected[149:152] = (303, 64, 1)  # 303 = 256 + 46 + 1
            expected[159:161] = (2049, 2048)
            # Compare using lists because we get more helpful error messages!
            self.assertEqual(list(ffv.fixed_length_header.raw), list(expected))
            self.assertArrayEqual(ffv.integer_constants, constants)
            self.assertIsNone(ffv.real_constants)
            self.assertEqual(len(ffv.fields), 1)
            for field in ffv.fields:
                data = field.get_data()
                self.assertArrayEqual(data, src_data)
Example #2
0
    def test_fixed_length_header_wrong_dtype(self):
        # Check that using the wrong dtype in the fixed length header
        # doesn't confuse things.
        src_path = tests.get_data_path(('FF', 'n48_multi_field'))
        with self.temp_filename() as temp_path:
            shutil.copyfile(src_path, temp_path)
            ffv = FieldsFileVariant(temp_path, FieldsFileVariant.UPDATE_MODE)
            header_values = ffv.fixed_length_header.raw
            self.assertEqual(header_values.dtype, '>i8')
            header = FixedLengthHeader(header_values.astype('<i4'))
            ffv.fixed_length_header = header
            ffv.close()

            ffv = FieldsFileVariant(temp_path)
            # If the header was written out with the wrong dtype this
            # value will go crazy - so check that it's still OK.
            self.assertEqual(ffv.fixed_length_header.sub_model, 1)
Example #3
0
 def test_equal(self):
     ffv1 = FixedLengthHeader(list(range(256)))
     ffv2 = FixedLengthHeader(np.arange(256))
     self.assertFalse(ffv1.__ne__(ffv2))
Example #4
0
 def test_invalid(self):
     ffv1 = FixedLengthHeader(list(range(256)))
     self.assertIs(ffv1.__eq__(np.arange(256)), NotImplemented)
Example #5
0
 def test_invalid_length(self):
     with self.assertRaisesRegexp(ValueError, 'Incorrect number of words'):
         FixedLengthHeader(list(range(15)))
Example #6
0
def make_header():
    return FixedLengthHeader((np.arange(256) + 1) * 10)