예제 #1
0
    def test_encode_binary(self):
        """
        This will test the encode binary function.

        This is done by taking a set of test data (read in from file), running it through
        the encode_binary function, and then hashing it.  After which, we compare the
        hashes.  If the hashes are the same, then the encode binary function worked as
        expected.
        """
        print "\nTesting encode_binary"
        pg_io_hash = hashlib.md5()

        for row in self.data:
            self.pg_io.write(encode_binary(*row))

        self.pg_io.seek(0)
        pg_io_hash.update(self.pg_io.read())

        self.assertEqual(
            self.data_hash,
            pg_io_hash.digest(),
            msg="The encoded binary data's hash is different from the expected hash, encoding failed"
        )

        return
예제 #2
0
    def transform_data(self, data, channel, pg_io, pg_index):
        """
        The purpose of this method is to transform the data as a list into a binary data.  Originally it was part of
        each parse_file call, but it was refactored to here.  Not the base_ingester class, as only EMOD currently uses
        this, but the OM ingester could easily be refactored for this.

        :param data: list of data where the index of any given row is its timestep
        :type data: list
        :param channel: Channel that the data should be associated with
        :type channel: DimChannel
        :param pg_io: BytesIO to which to add data
        :type pg_io: BytesIO
        :param pg_index: Index for which the data should be inserted at
        :type pg_index: int
        """
        if not isinstance(data, list):
            raise ValueError('Data must be a list of numbers, received %s', type(data))
        if not isinstance(channel, DimChannel):
            raise ValueError('Channel must be an instance of DimChannel, received %s' % type(channel))
        if not isinstance(pg_io, BytesIO):
            raise ValueError('pg_io must be a BytesIO instance to which the data will be appended, received %s' % type(pg_io))
        if not isinstance(pg_index, int):
            raise ValueError('pg_index must be an integer, received %s' % type(pg_index))

        for ts, datum in enumerate(data):
            pg_io.write(
                encode_binary(
                    pg_id=pg_index,
                    timestep=ts,
                    value=datum,
                    channel_key=channel.id,
                    run_key=self.run.id,
                    replication_key=self.replication.id)
            )
            pg_index += 1

        return pg_index