<commit_before>#!/usr/bin/env python
#
# Copyright 2012 Knowledge Economy Developments Ltd
# 
# <NAME>
# <EMAIL>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''
Utility functions for the interfaces routines
'''

import pyfftw.builders as builders
from . import cache

def _Xfftn(a, s, axes, overwrite_input, planner_effort,
        threads, auto_align_input, auto_contiguous, 
        calling_func):

    reload_after_transform = False

    if not s.__hash__:
        s = tuple(s)

    if not axes.__hash__:
        axes = tuple(axes)

    if calling_func in ('irfft2', 'irfftn'):
        # overwrite_input is not an argument to irfft2 or irfftn
        args = (a, s, axes, planner_effort, threads, 
                auto_align_input, auto_contiguous)

        if not overwrite_input:
            # Only irfft2 and irfftn have overwriting the input
            # as the default (and so require the input array to 
            # be reloaded).
            reload_after_transform = True
    else:
        args = (a, s, axes, overwrite_input, planner_effort, threads, 
                auto_align_input, auto_contiguous)
    
    if cache.is_enabled():
        key = (calling_func, a.shape, a.strides, a.dtype, s.__hash__(), 
                axes.__hash__(), args[3:])

    if not cache.is_enabled() or key not in cache._fftw_cache:

        # If we're going to create a new FFTW object, we need to copy
        # the input array to preserve it, otherwise we can't actually
        # take the transform of the input array! (in general, we have
        # to assume that the input array will be destroyed during 
        # planning).
        a_copy = a.copy()

        FFTW_object = getattr(builders, calling_func)(*args)
    
        # Only copy if the input array is what was actually used
        # (otherwise it shouldn't be overwritten)
        if FFTW_object.get_input_array() is a:
            a[:] = a_copy

        if cache.is_enabled():
            cache._fftw_cache.insert(FFTW_object, key)
            output_array = FFTW_object().copy()
        else:
            output_array = FFTW_object()

    else:
        if reload_after_transform:
            a_copy = a.copy()

        FFTW_object = cache._fftw_cache.lookup(key)

        output_array = FFTW_object(a).copy()
    
    if reload_after_transform:
        a[:] = a_copy

    return output_array<commit_msg>interfaces now creates a new empty output array rather than copying the existing one.<commit_after>#!/usr/bin/env python
#
# Copyright 2012 Knowledge Economy Developments Ltd
# 
# <NAME>
# <EMAIL>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''
Utility functions for the interfaces routines
'''

import pyfftw.builders as builders
import pyfftw
from . import cache

def _Xfftn(a, s, axes, overwrite_input, planner_effort,
        threads, auto_align_input, auto_contiguous, 
        calling_func):

    reload_after_transform = False

    if not s.__hash__:
        s = tuple(s)

    if not axes.__hash__:
        axes = tuple(axes)

    if calling_func in ('irfft2', 'irfftn'):
        # overwrite_input is not an argument to irfft2 or irfftn
        args = (a, s, axes, planner_effort, threads, 
                auto_align_input, auto_contiguous)

        if not overwrite_input:
            # Only irfft2 and irfftn have overwriting the input
            # as the default (and so require the input array to 
            # be reloaded).
            reload_after_transform = True
    else:
        args = (a, s, axes, overwrite_input, planner_effort, threads, 
                auto_align_input, auto_contiguous)
    
    if cache.is_enabled():
        key = (calling_func, a.shape, a.strides, a.dtype, s.__hash__(), 
                axes.__hash__(), args[3:])

    if not cache.is_enabled() or key not in cache._fftw_cache:

        # If we're going to create a new FFTW object, we need to copy
        # the input array to preserve it, otherwise we can't actually
        # take the transform of the input array! (in general, we have
        # to assume that the input array will be destroyed during 
        # planning).
        a_copy = a.copy()

        FFTW_object = getattr(builders, calling_func)(*args)
    
        # Only copy if the input array is what was actually used
        # (otherwise it shouldn't be overwritten)
        if FFTW_object.get_input_array() is a:
            a[:] = a_copy

        if cache.is_enabled():
            cache._fftw_cache.insert(FFTW_object, key)
        
        output_array = FFTW_object()

    else:
        if reload_after_transform:
            a_copy = a.copy()

        FFTW_object = cache._fftw_cache.lookup(key)

        orig_output_array = FFTW_object.get_output_array()
        output_shape = orig_output_array.shape
        output_dtype = orig_output_array.dtype
        output_alignment = FFTW_object.output_alignment

        output_array = pyfftw.n_byte_align_empty(output_shape, 
                output_alignment, output_dtype)

        FFTW_object(input_array=a, output_array=output_array)
    
    if reload_after_transform:
        a[:] = a_copy

    return output_array
{'fraction_non_alphanumeric': 0.07428026085573405, 'fraction_numerical': 0.002863050739621441, 'mean_word_length': 2.593142857142857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3921750', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1794, 'n_words': 653}