<commit_before>""" Tests for the fileio.bruker submodule """

from __future__ import print_function

import tempfile
import os
import shutil

import numpy as np
from numpy.testing import assert_array_equal
import nmrglue as ng

from setup import DATA_DIR


# subroutines
def dic_similar(dic1, dic2):
    """ Compared two Bruker parameter dictionaries"""
    if dic1.keys() != dic2.keys():
        print("Not same keys!")
    assert dic1.keys() == dic2.keys()
    for key in dic1.keys():
        if dic1[key] != dic2[key]:
            print(key, dic1[key], dic2[key])
        assert dic1[key] == dic2[key]
    return True


def write_readback(dic, data):
    """ Write out and readback a Bruker directory """
    # write out and readback
    td = tempfile.mkdtemp(dir=".")
    ng.bruker.write(td, dic, data, write_procs=True)
    rdic, rdata = ng.bruker.read(td)
    assert_array_equal(data, rdata)
    assert dic_similar(dic, rdic)
    shutil.rmtree(td)


def lowmem_write_readback(dic, data):
    """ Lowmemory write out and readback of a Bruker directory """
    # write out and readback
    td = tempfile.mkdtemp(dir=".")
    ng.bruker.write_lowmem(td, dic, data)
    rdic, rdata = ng.bruker.read_lowmem(td)
    tup = tuple(range(data.ndim))
    assert_array_equal(data[tup], rdata[tup])
    assert dic_similar(dic, rdic)
    shutil.rmtree(td)


# tests
def test_jcamp1():
    """ reading/writing of JCAMP file 1"""
    dic = ng.bruker.read_jcamp(os.path.join(DATA_DIR, "bruker_1d", "acqus"))
    assert dic['LFILTER'] == 200
    assert len(dic['PRECHAN']) == 16
    tf = tempfile.mktemp(dir='.')<commit_msg>added a subroutine to test writing of processed dataset<commit_after>""" Tests for the fileio.bruker submodule """

from __future__ import print_function

import tempfile
import os
import shutil

import numpy as np
from numpy.testing import assert_array_equal
import nmrglue as ng

from setup import DATA_DIR


# subroutines
def dic_similar(dic1, dic2):
    """ Compared two Bruker parameter dictionaries"""
    if dic1.keys() != dic2.keys():
        print("Not same keys!")
    assert dic1.keys() == dic2.keys()
    for key in dic1.keys():
        if dic1[key] != dic2[key]:
            print(key, dic1[key], dic2[key])
        assert dic1[key] == dic2[key]
    return True


def write_readback(dic, data):
    """ Write out and readback a Bruker directory """
    # write out and readback
    td = tempfile.mkdtemp(dir=".")
    ng.bruker.write(td, dic, data, write_procs=True)
    rdic, rdata = ng.bruker.read(td)
    assert_array_equal(data, rdata)
    assert dic_similar(dic, rdic)
    shutil.rmtree(td)


def write_readback_pdata(dic, data, pdata_folder=False):
    """ Write out and readback a Bruker processed dataset """
    # write out and readback
    td = tempfile.mkdtemp(dir=".")
    ng.bruker.write_pdata(td, dic, data, write_procs=True, 
            pdata_folder=pdata_folder)
    if pdata_folder:
        rdic, rdata = ng.bruker.read_pdata(os.path.join(td, 'pdata', str(pdata_folder)))
    else:
        rdic, rdata = ng.bruker.read_pdata(td)
    assert_array_equal(data, rdata)
    assert dic_similar(dic, rdic)
    shutil.rmtree(td)


def lowmem_write_readback(dic, data):
    """ Lowmemory write out and readback of a Bruker directory """
    # write out and readback
    td = tempfile.mkdtemp(dir=".")
    ng.bruker.write_lowmem(td, dic, data)
    rdic, rdata = ng.bruker.read_lowmem(td)
    tup = tuple(range(data.ndim))
    assert_array_equal(data[tup], rdata[tup])
    assert dic_similar(dic, rdic)
    shutil.rmtree(td)


# tests
def test_jcamp1():
    """ reading/writing of JCAMP file 1"""
    dic = ng.bruker.read_jcamp(os.path.join(DATA_DIR, "bruker_1d", "acqus"))
    assert dic['LFILTER'] == 200
    assert len(dic['PRECHAN']) == 16
    tf = tempfile.mktemp(dir='.')
{'fraction_non_alphanumeric': 0.12660887838192803, 'fraction_numerical': 0.01103230890464933, 'mean_word_length': 3.5279429250891794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7280135', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1354, 'n_words': 339}