Skip to content
sarasame00 edited this page Sep 13, 2024 · 8 revisions

FuncLab: A Data Analysis Library

This library provides functions for data import, curve fitting, regression analysis, uncertainty propagation, and data visualization using Python. The code leverages several popular libraries, including NumPy, gspread, Matplotlib, SymPy, and SciPy.

Table of Contents

Dependencies

Ensure you have the following libraries installed:

  • numpy
  • gspread
  • matplotlib
  • sympy
  • scipy

You can install these libraries using pip:

pip install numpy gspread matplotlib sympy scipy

Authentication

To use Google Sheets API, authenticate using Google Colab:

from google.colab import auth
auth.authenticate_user()  # Authenticate the user for Google Colab environment

Then, set up the credentials:

from google.auth import default
import gspread

creds, _ = default()  # Get default authentication credentials
gc = gspread.authorize(creds)  # Authorize the gspread client with credentials

Functions

importData

Imports data from a specified Google Sheet.

Parameters:

  • fileName (str): The name of the Google Sheet file.
  • sheetName (str): The name of the worksheet within the Google Sheet.
  • numCol (int or list of int): The column number(s) to import (1-based indexing).
  • firstRow (int, optional): The number of the first row to import (default is 1).
  • lastRow (int, optional): The number of the last row to import (default is None, meaning all rows).

Returns:

  • list or list of lists: A list of data from the specified columns. If multiple columns are specified, a list of lists is returned.

Example Usage:

data = importData('Sheet1', 'Data', 1, firstRow=2, lastRow=10)

save_eval

Safely converts a string to a float, handling non-numeric values.

Parameters:

  • value(str): The string to convert.

Returns:

  • float: The converted float value, or NaN if conversion fails.

Example Usage:

number = safe_eval('123,45')

curveFit

Parameters:

  • func (callable): The function to fit the data to. It should accept x values and parameters, and return y values.
  • x (array-like): The independent variable data.
  • y (array-like): The dependent variable data.

Returns:

  • dict: Contains optimized parameters, parameter uncertainties, and R-squared value.

Example Usage:

def linear_func(x, A, B):
    return A * x + B

results = curveFit(linear_func, x_data, y_data)

Variable Class

Represents a variable with a symbolic representation, value, and uncertainty.

Parameters:

  • sym (str): The name of the symbolic variable.
  • val (float): The value of the variable.
  • inc (float): The uncertainty of the variable.

Example Usage:

A = Variable('A', 2.5, 0.1)

regression

Performs linear regression on the data and returns or prints the results.

Parameters:

  • x (array-like): The independent variable data.
  • y (array-like): The dependent variable data.
  • table (bool, optional): If True, print the regression results in LaTeX table format. If False, print the results in plain text.

Returns:

  • dict: Contains slope, intercept, and R-squared value.

Example Usage:

results = regression(x_data, y_data, table=False)

propIncertesa

Propagates uncertainties through a symbolic function using partial derivatives.

Parameters:

  • fun (sympy expression): The symbolic function through which uncertainties are propagated.
  • variables (list of Variable or Variable): A list of Variable objects representing variables with their uncertainties.

Returns:

  • tuple: Contains evaluated function values and uncertainties.

Example Usage:

x = Variable('x', 1.0, 0.1)
y = Variable('y', 2.0, 0.2)
expr = sym.Symbol('x') + sym.Symbol('y')
values, uncertainties = propIncertesa(expr, [x, y])

mean_and_uncertainty

Calculates the mean and combined uncertainty of a set of values.

Parameters:

  • values (list of float): The list of measured values.
  • `instrumental_error (float): The instrumental error, which is the uncertainty associated with the measurement process.

Returns:

  • tuple: Contains the mean of the values and combined uncertainty.

Example Usage:

mean, uncertainty = mean_and_uncertainty([1.1, 1.2, 1.3], 0.05)

plotDades

Plots data with error bars on the given axis.

Parameters:

  • ax (matplotlib.axes.Axes): The Matplotlib axis object to plot on.
  • x (Variable): The independent variable data, including uncertainties.
  • y (Variable): The dependent variable data, including uncertainties.
  • label (str, optional): Label for the data series (default is None).
  • color (str, optional): Color of the data points and error bars (default is 'b' for blue).
  • marker (str, optional): Marker style for the data points (default is 'o' for circles).
  • markersize (int, optional): Size of the markers (default is 3).

Raises:

  • TypeError: If either x or y is not an instance of the Variable class.

Example Usage:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plotDades(ax, x_data, y_data, label='Data', color='red')
plt.show()

Clone this wiki locally