<commit_before>import unittest
import os
import numpy as np
from PIL import Image

import geosoft
import geosoft.gxapi as gxapi
import geosoft.gxpy.gx as gx
import geosoft.gxpy.utility as gxu
import geosoft.gxpy.system as gsys
import geosoft.gxpy.gdb as gxdb
import geosoft.gxpy.vv as gxvv
import geosoft.gxpy.va as gxva
import geosoft.gxpy.map as gxmap
import geosoft.gxpy.metadata as gxmeta
import geosoft.gxpy.geometry as gxgeo

from base import GXPYTest


class Test(GXPYTest):

    @classmethod
    def setUpClass(cls):
        cls.setUpGXPYTest()
        cls.folder, files = gsys.unzip(os.path.join(os.path.dirname(cls._test_case_py), 'test_database.zip'),
                                       folder=cls._gx.temp_folder())
        cls.gdb_name = os.path.join(cls.folder, 'test_database.gdb')

    def test_gdb(self):
        self.start()
        self.assertEqual(gxdb.__version__, geosoft.__version__)

        def setxyz(xyz):
            gdb.xyz_channels = xyz

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            try:
                self.assertRaises(gxdb.GdbException, gdb.is_line, 'nope', True)
                self.assertRaises(gxdb.GdbException, gdb.is_channel, 'nope', True)
                self.assertRaises(gxdb.GdbException, setxyz, ('x', 'y', 'crazy_cannot_exist'))
                self.assertRaises(gxdb.GdbException, setxyz, ('crazy_cannot_exist', 'y'))
                gdb.xyz_channels = ('x', 'y', 'z')
                self.assertEqual(gdb.xyz_channels[2], 'z')
                gdb.xyz_channels = ('x', 'y')
                self.assertEqual(gdb.xyz_channels[2], 'z')

            finally:
                gdb.discard()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            self.assertEqual(len(gdb.file_name), len(self.gdb_name))

        with gxdb.Geosoft_gdb.open(os.path.splitext(self.gdb_name)[0]) as gdb:
            self.assertEqual(len(gdb.file_name), len(self.gdb_name))
            gdb.commit()
            try:
                l = list(gdb.list_lines())
                c = list(gdb.list_channels())
                self.assertTrue(gdb.exist_symb_(gxdb.Line(gdb, l[0]), gxapi.DB_SYMB_LINE))
                self.assertTrue(gdb.exist_symb_(gxdb.Channel(gdb, c[0]), gxapi.DB_SYMB_CHAN))
                self.assertFalse(gdb.exist_symb_(gxdb.Channel(gdb, c[0]), gxapi.DB_SYMB_LINE))

            finally:
                gdb.discard()

        self.assertFalse(gxdb.is_valid_line_name(123))

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            self.assertEqual(gdb.max_blobs, 650)
            self.assertEqual(gdb.max_lines, 500)
            self.assertEqual(gdb.max_channels, 50)
            self.assertEqual(gdb.used_blobs, 14)
            self.assertEqual(gdb.used_lines, 5)
            self.assertEqual(gdb.used_channels, 6)
            self.assertEqual(gdb.max_compressed_channel_bytes, 67106816)
            self.assertEqual(gdb.number_of_blocks, 275)
            self.assertEqual(gdb.lost_blocks, 0)
            self.assertEqual(gdb.free_blocks, 36)
            self.assertEqual(gdb.compression, 0)
            self.assertEqual(gdb.pages_for_blobs, 0)
            self.assertEqual(gdb.db_size_kb, 638)
            self.assertEqual(gdb.index_size_kb, 303)
            self.assertEqual(gdb.max_block_size_bytes, 67106792)
            self.assertEqual(gdb.data_has_changed, 0)

        with gxdb.Geosoft_gdb.open(os.path.splitext(self.gdb_name)[0]) as gdb:
            try:
                gxdb.Channel.new(gdb, 'empty')
                data = gdb.read_line('D2','empty')[0]
                self.assertEqual(len(data), 0)
                data = gdb.read_line('D2', ('x', 'y', 'empty'))[0]
                self.assertEqual(len(data), 832)
                self.assertFalse(np.isfinite(data[:,2]).any())
            finally:
                gdb.discard()

    def test_noprops_GDB(self):
        self.start()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            self.assertEqual(len(gdb.file_name), len(self.gdb_name))
            self.assertEqual(str(gdb).lower(),os.path.basename(self.gdb_name).lower())
            self.assertTrue(len(gdb.list_channels())>=6)
            self.assertTrue('X' in gdb.list_channels())
            self.assertTrue('dx' in gdb.list_channels(chan=gxdb.CHAN_ALL))
            self.assertTrue('vector' in gdb.list_channels(chan=gxdb.CHAN_ARRAY))
            self.assertFalse('vector' in gdb.list_channels(chan=gxdb.CHAN_NORMAL))

            self.assertEqual(gdb.channel_width('vector'),3)
            self.assertEqual(gdb.channel_width('x'),1)

            gdb.discard()

    def test_empty(self):
        self.start()

        name = 'empty'
        try:
            with gxdb.Geosoft_gdb.new(name, overwrite=True) as gdb:
                name = gdb.file_name
                self.assertEqual(len(gdb.list_lines()), 0)
                gdb.new_line('some_line')
                gdb.new_line('_some_line')
                lines = gdb.list_lines()
                self.assertEqual(len(lines), 2)
                self.assertTrue('Some_line' in lines) # note leading 's' was interpreted as line type 'S'
                self.assertTrue('D_some_line' in lines)

                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.size, 0)
                self.assertEqual(len(ch), 0)

                gdb.new_channel('one')
                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.shape, (0, 1))
                self.assertEqual(len(ch), 1)
                self.assertEqual(fid, (0.0, 1.0))

                gdb.new_channel('two')
                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.shape, (0, 2))
                self.assertEqual(len(ch), 2)
                self.assertEqual(fid, (0.0, 1.0))

                ch = gdb.new_channel('three')
                line = list(lines)[0]
                gdb.write_channel(line, ch, [1, 2, 3, 4, 5])
                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.shape, (5, 3))
                self.assertEqual(len(ch), 3)
                self.assertEqual(fid, (0.0, 1.0))

                ch = gdb.new_channel('four', dtype=np.int)
                line = list(lines)[0]
                gdb.write_channel(line, ch, [10, 20, 30, 40], fid=(-1.5, 2))
                npd, ch, fid = gdb.read_line(line)
                self.assertEqual(npd.shape, (7, 4))
                self.assertEqual(len(ch), 4)
                self.assertEqual(fid, (-1.5, 1.0))
                self.assertEqual(npd[0][0], 10.)
                self.assertEqual(npd[2][2], 1.5)
                self.assertEqual(npd[5][2], 4.5)
                self.assertTrue(np.isnan(npd[0][1]))
                self.assertTrue(np.isnan(npd[1][2]))
                self.assertTrue(np.isnan(npd[6][2]))

                npd, ch, fid = gdb.read_line(line, 'four', dtype=np.int)
                self.assertEqual(npd.shape, (4, 1))
                self.assertEqual(len(ch), 1)
                self.assertEqual(fid, (-1.5, 2.0))
                self.assertEqual(npd[0][0], 10)
                self.assertEqual(npd[3][0], 40)

                npd, ch, fid = gdb.read_line(line, 'four', fid=(-0.13333, 0.000666))
                self.assertEqual(npd.shape, (6958, 1))
                self.assertEqual(len(ch), 1)
                self.assertEqual(fid, (-0.13333, 0.000666))
                self.assertAlmostEqual(npd[0][0], 16.83335)
                self.assertAlmostEqual(npd[1000][0], 20.16335)


        finally:
            gxdb.delete_files(name)


    def test_read_write_channel_vv_va(self):
        self.start()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            gdb.delete_channel('test_chan_vv')
            vv = gxvv.GXvv(np.array([1.,2.,3.]), fid=(-10, 2.5))
            gdb.write_channel_vv('T46', 'test_chan_vv', vv)
            vv = gdb.read_channel_vv('T46', 'test_chan_vv')
            self.assertEqual(vv.length, 3)
            self.assertEqual(vv.fid, (-10.0, 2.5))
            self.assertRaises(gxva.VAException, gdb.read_channel_va, 'T46', 'test_chan_vv')
            gdb.delete_channel('test_chan_vv')

            va = gxva.GXva(np.array([[1., 2., 3.],[8,9,10]]), fid=(-10, 2.5))
            gdb.write_channel_va('T46', 'test_chan_va', va)
            self.assertRaises(gxdb.GdbException, gdb.read_channel_vv, 'T46', 'test_chan_va')
            va = gdb.read_channel_va('T46', 'test_chan_va')
            self.assertEqual(va.width, 3)
            self.assertEqual(va.length, 2)
            self.assertEqual(va.fid, (-10.0, 2.5))
            gdb.delete_channel('test_chan_va')

    def test_group_VA_read_write(self):
        self.start()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:

            self.assertEqual(len(gdb.file_name), len(self.gdb_name))
            self.assertEqual(str(gdb).lower(),os.path.basename(self.gdb_name).lower())
            data, ch, fid = gdb.read_line('D578625')
            self.assertEqual(data.shape, (832, 8))

            gdb.write_line('T45', data, fid=(99, 0.5))
            data, ch, fid = gdb.read_line('T45')
            self.assertEqual(data.shape, (832, 8))
            self.assertEqual(len(ch), 8)
            self.assertEqual(ch[0], 'X')
            self.assertEqual(fid, (99.0, 0.5))

            gdb.write_channel('T46', 'wva', data, fid=(-10, 2.5))
            data, fid = gdb.read_channel('T46', 'wva')
            self.assertEqual(data.shape, (832, 8))
            self.assertEqual(fid, (-10.0, 2.5))
            gdb.delete_channel('wva')

            gdb.write_line('T46', data, channels='wideva', fid=(-10, 2.5))
            data, ch, fid = gdb.read_line('T46', 'wideva')
            self.assertEqual(data.shape, (832, 8))
            self.assertEqual(len(ch), 8)
            self.assertEqual(ch[0], 'wideva[0]')
            self.assertEqual(fid, (-10.0, 2.5))<commit_msg>test modified for name changes in gdb<commit_after>import unittest
import os
import numpy as np
from PIL import Image

import geosoft
import geosoft.gxapi as gxapi
import geosoft.gxpy.gx as gx
import geosoft.gxpy.utility as gxu
import geosoft.gxpy.system as gsys
import geosoft.gxpy.gdb as gxdb
import geosoft.gxpy.vv as gxvv
import geosoft.gxpy.va as gxva
import geosoft.gxpy.map as gxmap
import geosoft.gxpy.metadata as gxmeta
import geosoft.gxpy.geometry as gxgeo

from base import GXPYTest


class Test(GXPYTest):

    @classmethod
    def setUpClass(cls):
        cls.setUpGXPYTest()
        cls.folder, files = gsys.unzip(os.path.join(os.path.dirname(cls._test_case_py), 'test_database.zip'),
                                       folder=cls._gx.temp_folder())
        cls.gdb_name = os.path.join(cls.folder, 'test_database.gdb')

    def test_gdb(self):
        self.start()
        self.assertEqual(gxdb.__version__, geosoft.__version__)

        def setxyz(xyz):
            gdb.xyz_channels = xyz

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            try:
                self.assertRaises(gxdb.GdbException, gdb.is_line, 'nope', True)
                self.assertRaises(gxdb.GdbException, gdb.is_channel, 'nope', True)
                self.assertRaises(gxdb.GdbException, setxyz, ('x', 'y', 'crazy_cannot_exist'))
                self.assertRaises(gxdb.GdbException, setxyz, ('crazy_cannot_exist', 'y'))
                gdb.xyz_channels = ('x', 'y', 'z')
                self.assertEqual(gdb.xyz_channels[2], 'z')
                gdb.xyz_channels = ('x', 'y')
                self.assertEqual(gdb.xyz_channels[2], 'z')

            finally:
                gdb.discard()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            self.assertEqual(len(gdb.file_name), len(self.gdb_name))

        with gxdb.Geosoft_gdb.open(os.path.splitext(self.gdb_name)[0]) as gdb:
            self.assertEqual(len(gdb.file_name), len(self.gdb_name))
            gdb.commit()
            try:
                l = list(gdb.list_lines())
                c = list(gdb.list_channels())
                self.assertTrue(gdb.exist_symb_(gxdb.Line(gdb, l[0]), gxapi.DB_SYMB_LINE))
                self.assertTrue(gdb.exist_symb_(gxdb.Channel(gdb, c[0]), gxapi.DB_SYMB_CHAN))
                self.assertFalse(gdb.exist_symb_(gxdb.Channel(gdb, c[0]), gxapi.DB_SYMB_LINE))

            finally:
                gdb.discard()

        self.assertFalse(gxdb.is_valid_line_name(123))

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            self.assertEqual(gdb.max_blobs, 650)
            self.assertEqual(gdb.max_lines, 500)
            self.assertEqual(gdb.max_channels, 50)
            self.assertEqual(gdb.used_blobs, 14)
            self.assertEqual(gdb.used_lines, 5)
            self.assertEqual(gdb.used_channels, 6)
            self.assertEqual(gdb.max_compressed_channel_bytes, 67106816)
            self.assertEqual(gdb.number_of_blocks, 275)
            self.assertEqual(gdb.lost_blocks, 0)
            self.assertEqual(gdb.free_blocks, 36)
            self.assertEqual(gdb.compression, 0)
            self.assertEqual(gdb.pages_for_blobs, 0)
            self.assertEqual(gdb.db_size_kb, 638)
            self.assertEqual(gdb.index_size_kb, 303)
            self.assertEqual(gdb.max_block_size_bytes, 67106792)
            self.assertEqual(gdb.data_has_changed, 0)

        with gxdb.Geosoft_gdb.open(os.path.splitext(self.gdb_name)[0]) as gdb:
            try:
                gxdb.Channel.new(gdb, 'empty')
                data = gdb.read_line('D2','empty')[0]
                self.assertEqual(len(data), 0)
                data = gdb.read_line('D2', ('x', 'y', 'empty'))[0]
                self.assertEqual(len(data), 832)
                self.assertFalse(np.isfinite(data[:,2]).any())
            finally:
                gdb.discard()

    def test_noprops_GDB(self):
        self.start()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            self.assertEqual(len(gdb.file_name), len(self.gdb_name))
            self.assertTrue(len(gdb.list_channels()) >= 6)
            self.assertTrue('X' in gdb.list_channels())
            self.assertTrue('dx' in gdb.list_channels(chan=gxdb.CHAN_ALL))
            self.assertTrue('vector' in gdb.list_channels(chan=gxdb.CHAN_ARRAY))
            self.assertFalse('vector' in gdb.list_channels(chan=gxdb.CHAN_NORMAL))

            self.assertEqual(gdb.channel_width('vector'),3)
            self.assertEqual(gdb.channel_width('x'),1)

            gdb.discard()

    def test_empty(self):
        self.start()

        name = 'empty'
        try:
            with gxdb.Geosoft_gdb.new(name, overwrite=True) as gdb:
                name = gdb.file_name
                self.assertEqual(len(gdb.list_lines()), 0)
                gdb.new_line('some_line')
                gdb.new_line('_some_line')
                lines = gdb.list_lines()
                self.assertEqual(len(lines), 2)
                self.assertTrue('Some_line' in lines) # note leading 's' was interpreted as line type 'S'
                self.assertTrue('D_some_line' in lines)

                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.size, 0)
                self.assertEqual(len(ch), 0)

                gdb.new_channel('one')
                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.shape, (0, 1))
                self.assertEqual(len(ch), 1)
                self.assertEqual(fid, (0.0, 1.0))

                gdb.new_channel('two')
                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.shape, (0, 2))
                self.assertEqual(len(ch), 2)
                self.assertEqual(fid, (0.0, 1.0))

                ch = gdb.new_channel('three')
                line = list(lines)[0]
                gdb.write_channel(line, ch, [1, 2, 3, 4, 5])
                npd, ch, fid = gdb.read_line(list(lines)[0])
                self.assertEqual(npd.shape, (5, 3))
                self.assertEqual(len(ch), 3)
                self.assertEqual(fid, (0.0, 1.0))

                ch = gdb.new_channel('four', dtype=np.int)
                line = list(lines)[0]
                gdb.write_channel(line, ch, [10, 20, 30, 40], fid=(-1.5, 2))
                npd, ch, fid = gdb.read_line(line)
                self.assertEqual(npd.shape, (7, 4))
                self.assertEqual(len(ch), 4)
                self.assertEqual(fid, (-1.5, 1.0))
                self.assertEqual(npd[0][0], 10.)
                self.assertEqual(npd[2][2], 1.5)
                self.assertEqual(npd[5][2], 4.5)
                self.assertTrue(np.isnan(npd[0][1]))
                self.assertTrue(np.isnan(npd[1][2]))
                self.assertTrue(np.isnan(npd[6][2]))

                npd, ch, fid = gdb.read_line(line, 'four', dtype=np.int)
                self.assertEqual(npd.shape, (4, 1))
                self.assertEqual(len(ch), 1)
                self.assertEqual(fid, (-1.5, 2.0))
                self.assertEqual(npd[0][0], 10)
                self.assertEqual(npd[3][0], 40)

                npd, ch, fid = gdb.read_line(line, 'four', fid=(-0.13333, 0.000666))
                self.assertEqual(npd.shape, (6958, 1))
                self.assertEqual(len(ch), 1)
                self.assertEqual(fid, (-0.13333, 0.000666))
                self.assertAlmostEqual(npd[0][0], 16.83335)
                self.assertAlmostEqual(npd[1000][0], 20.16335)


        finally:
            gxdb.delete_files(name)


    def test_read_write_channel_vv_va(self):
        self.start()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:
            gdb.delete_channel('test_chan_vv')
            vv = gxvv.GXvv(np.array([1.,2.,3.]), fid=(-10, 2.5))
            gdb.write_channel_vv('T46', 'test_chan_vv', vv)
            vv = gdb.read_channel_vv('T46', 'test_chan_vv')
            self.assertEqual(vv.length, 3)
            self.assertEqual(vv.fid, (-10.0, 2.5))
            self.assertRaises(gxva.VAException, gdb.read_channel_va, 'T46', 'test_chan_vv')
            gdb.delete_channel('test_chan_vv')

            va = gxva.GXva(np.array([[1., 2., 3.],[8,9,10]]), fid=(-10, 2.5))
            gdb.write_channel_va('T46', 'test_chan_va', va)
            self.assertRaises(gxdb.GdbException, gdb.read_channel_vv, 'T46', 'test_chan_va')
            va = gdb.read_channel_va('T46', 'test_chan_va')
            self.assertEqual(va.width, 3)
            self.assertEqual(va.length, 2)
            self.assertEqual(va.fid, (-10.0, 2.5))
            gdb.delete_channel('test_chan_va')

    def test_group_VA_read_write(self):
        self.start()

        with gxdb.Geosoft_gdb.open(self.gdb_name) as gdb:

            self.assertEqual(len(gdb.file_name), len(self.gdb_name))
            data, ch, fid = gdb.read_line('D578625')
            self.assertEqual(data.shape, (832, 8))

            gdb.write_line('T45', data, fid=(99, 0.5))
            data, ch, fid = gdb.read_line('T45')
            self.assertEqual(data.shape, (832, 8))
            self.assertEqual(len(ch), 8)
            self.assertEqual(ch[0], 'X')
            self.assertEqual(fid, (99.0, 0.5))

            gdb.write_channel('T46', 'wva', data, fid=(-10, 2.5))
            data, fid = gdb.read_channel('T46', 'wva')
            self.assertEqual(data.shape, (832, 8))
            self.assertEqual(fid, (-10.0, 2.5))
            gdb.delete_channel('wva')

            gdb.write_line('T46', data, channels='wideva', fid=(-10, 2.5))
            data, ch, fid = gdb.read_line('T46', 'wideva')
            self.assertEqual(data.shape, (832, 8))
            self.assertEqual(len(ch), 8)
            self.assertEqual(ch[0], 'wideva[0]')
            self.assertEqual(fid, (-10.0, 2.5))
{'fraction_non_alphanumeric': 0.1402228976697062, 'fraction_numerical': 0.03181357649442756, 'mean_word_length': 2.009757584997713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5977050', 'n_tokens_mistral': 7355, 'n_tokens_neox': 6825, 'n_words': 1040}