<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Scattering curve related functions used in the SCT suite of programs
"""

# Copyright 2014 University College London

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#    http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import yaml
import numpy as np
import matplotlib.pyplot as plt

from sjp_util import sjp_util

def load_scatter_curve(filename, q_min, q_max):
    """
    Load magnitude of scattering vector, q, and intensity, I, data from file and
    filter for values between q_min and q_max.
    Note: Assuming that a/ there is no header and b/ q and I are the first two
    columns here.

    @type  filename:  string
    @param filename:  Path to file containing scattering curve (q, I data).
    @type  q_min:     float
    @param q_min:     Minimum value of the magnitude of the scattering vector,
                      q, to keep in the output dataset.
    @type  q_max:     float
    @param q_max:     Minimum value of the magnitude of the scattering vector,
                      q, to keep in the output dataset.
    @rtype:           numpy array
    @return:          Two dimensional array of scattering vector, q, and
                      intensity, I, values.
    """

    scatter_data = np.loadtxt(filename)
    qrange_mask = (scatter_data[:,0] >= q_min) & (scatter_data[:,0] <= q_max)

    return scatter_data[qrange_mask]

def read_scatter_curves(curve_files, units, param):
    """
    Read in a list of scattering curve files and return a list of dictionaries
    which contain the file name ('file'), an array of Q and I(Q) vales ('data'),
    the radius of gyration ('rg') and the cross-section ('rxs1').

    @type  curve_files:  list
    @param curve_files:  List of files containing scattering curves Q, I(Q)
                         pairs
    @type  units:        string
    @param units:        String containing either 'nm' or 'a' to indicate the
                         length units used in the scattering curve
    @type  param:        dictionary
    @param param:        Dictionary containing parameters to use when creating
                         models and analysing curves.
    @return:             List of dictionaries containing the following key/value
                         pairs:
                         - data: numpy array of Q, I(Q)
                         - rg: radius of gyration calculated from input curve
                         - rxs1: cross-section calculated from input curve
                         - file: input file name
    """

    curves = []
<commit_msg>np.loadtxt was failing due to headerlines - sct.curve now tries to skip a header if load fails<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Scattering curve related functions used in the SCT suite of programs
"""

# Copyright 2014 University College London

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#    http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import yaml
import numpy as np
import matplotlib.pyplot as plt

from sjp_util import sjp_util

def load_scatter_curve(filename, q_min, q_max):
    """
    Load magnitude of scattering vector, q, and intensity, I, data from file and
    filter for values between q_min and q_max.
    Note: Assuming that a/ there is no header and b/ q and I are the first two
    columns here.

    @type  filename:  string
    @param filename:  Path to file containing scattering curve (q, I data).
    @type  q_min:     float
    @param q_min:     Minimum value of the magnitude of the scattering vector,
                      q, to keep in the output dataset.
    @type  q_max:     float
    @param q_max:     Minimum value of the magnitude of the scattering vector,
                      q, to keep in the output dataset.
    @rtype:           numpy array
    @return:          Two dimensional array of scattering vector, q, and
                      intensity, I, values.
    """

    try:
        scatter_data = np.loadtxt(filename)
    except:
        # Maybe the file has a headerline?
        scatter_data = np.loadtxt(filename, skiprows = 1)
        
    qrange_mask = (scatter_data[:,0] >= q_min) & (scatter_data[:,0] <= q_max)

    return scatter_data[qrange_mask]

def read_scatter_curves(curve_files, units, param):
    """
    Read in a list of scattering curve files and return a list of dictionaries
    which contain the file name ('file'), an array of Q and I(Q) vales ('data'),
    the radius of gyration ('rg') and the cross-section ('rxs1').

    @type  curve_files:  list
    @param curve_files:  List of files containing scattering curves Q, I(Q)
                         pairs
    @type  units:        string
    @param units:        String containing either 'nm' or 'a' to indicate the
                         length units used in the scattering curve
    @type  param:        dictionary
    @param param:        Dictionary containing parameters to use when creating
                         models and analysing curves.
    @return:             List of dictionaries containing the following key/value
                         pairs:
                         - data: numpy array of Q, I(Q)
                         - rg: radius of gyration calculated from input curve
                         - rxs1: cross-section calculated from input curve
                         - file: input file name
    """

    curves = []

{'fraction_non_alphanumeric': 0.06720686367969494, 'fraction_numerical': 0.004289799809342231, 'mean_word_length': 2.2940868655154367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2182138', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1607, 'n_words': 753}