<commit_before>
import numpy as np

from seisflows.tools import unix
from seisflows.tools.array import loadnpy, savenpy
from seisflows.tools.code import savetxt, exists
from seisflows.tools.math import angle


class LBFGS(object):
    """ Limited-memory BFGS algorithm

        Includes optional safeguards: periodic restarting and descent
        conditions.

        To conserve memory, most vectors are read from disk rather than 
        passed from a calling routine.
    """

    def __init__(self, path='.', load=loadnpy, save=savenpy, memory=5, thresh=0., maxiter=np.inf, precond=None):
        assert exists(path)
        unix.cd(path)
        unix.mkdir('LBFGS')

        self.path = path
        self.load = load
        self.save = save
        self.thresh = thresh
        self.maxiter = maxiter
        self.precond = precond
        self.memory = memory

        self.iter = 0
        self.memory_used = 0


    def __call__(self):
        """ Returns L-BFGS search direction
        """
        self.iter += 1

        g = self.load('g_new')
        if self.iter == 1:
            return -g, 0

        elif self.iter > self.maxiter:
            print 'restarting LBFGS... [periodic restart]'
            self.restart()
            return -g, 1

        S, Y = self.update()
        q = self.apply(g, S, Y)

        status = self.check_status(g,q)
        if status != 0:
            self.restart()
            return -g, status
        else:
            return -q, status


    def update(self):
        """ Updates L-BFGS algorithm history
        """
        unix.cd(self.path)

        s = self.load('m_new') - self.load('m_old')
        y = self.load('g_new') - self.load('g_old')

        m = len(s)
        n = self.memory<commit_msg>clarify use of preconditioner on first iteration<commit_after>
import numpy as np

from seisflows.tools import unix
from seisflows.tools.array import loadnpy, savenpy
from seisflows.tools.code import savetxt, exists
from seisflows.tools.math import angle


class LBFGS(object):
    """ Limited-memory BFGS algorithm

        Includes optional safeguards: periodic restarting and descent
        conditions.

        To conserve memory, most vectors are read from disk rather than 
        passed from a calling routine.
    """

    def __init__(self, path='.', load=loadnpy, save=savenpy, memory=5, thresh=0., maxiter=np.inf, precond=None):
        assert exists(path)
        unix.cd(path)
        unix.mkdir('LBFGS')

        self.path = path
        self.load = load
        self.save = save
        self.thresh = thresh
        self.maxiter = maxiter
        self.precond = precond
        self.memory = memory

        self.iter = 0
        self.memory_used = 0


    def __call__(self):
        """ Returns L-BFGS search direction
        """
        self.iter += 1

        g = self.load('g_new')
        if self.iter == 1:
            # return -g rather than -precond(g) on the first iteration because
            # this approach is perhaps a bit safter and L-BFGS allows it
            return -g, 0

        elif self.iter > self.maxiter:
            print 'restarting LBFGS... [periodic restart]'
            self.restart()
            return -g, 1

        S, Y = self.update()
        q = self.apply(g, S, Y)

        status = self.check_status(g,q)
        if status != 0:
            self.restart()
            return -g, status
        else:
            return -q, status


    def update(self):
        """ Updates L-BFGS algorithm history
        """
        unix.cd(self.path)

        s = self.load('m_new') - self.load('m_old')
        y = self.load('g_new') - self.load('g_old')

        m = len(s)
        n = self.memory
{'fraction_non_alphanumeric': 0.10294514995947042, 'fraction_numerical': 0.004863550391786004, 'mean_word_length': 1.8411358403683806, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193523', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1107, 'n_words': 322}