PKm=H՞labreporthelper/plot.py""" 2D single plot module """ import matplotlib.pyplot as plt import collections import numpy as np class PlotSingle2D(object): """ make plots using pyplot, can be used as a parent class to manage pyplots """ def __init__(self, x, y, filepath, **kwargs): """ args: x: array_like xdata y: array_like ydata filepath: string filepath of pdf to save **kwargs: figure_options: passed to matplotlib.pyplot.figure xlabel_options: ylabel_options: suptitle_options: title_options: scilimits: errors: dictionary, array_like, scalar dictionary: {"xerr": xerr, "yerr": yerr} array_like, scalar: yerr fmt: string, default="k." bestfitfmt: string, default="k-" bestfit: BestFit child class eg. bestfit.polyfit.PolyFit, bestfit.logfit.LogFit bestfitlim: tuple, default=None xlim for bestfit line suptitle: string, default=xlim suptitle of pdf plot, formatted with outputdict suptitle_fontsize: int, default=15 font size of suptitle title: string, default=None title of the pdf plot title_fontsize: int, default=12 font size of title, formatted with outputdict xlabel: string, default=None label of string xlabel, formatted with outputdict ylabel: string, default=None label of string ylabel, formatted with outputdict xlim: tuple, default=None xlim ylim: tuple, default=None ylim outputdict: dictionary, default=None pass keys and arguments for formatting and to output """ figure_options = kwargs.get("figure_options", {}) self.figure = plt.figure(**figure_options) self.y = y self.x = x if x is not None else np.array(range(len(y))) self.filepath = filepath self.kwargs = kwargs self.subplot = None self.outputdict = kwargs.get("strformatdict", {}) self.create_subplot() def get_subplot(self): """Get subplot """ return self.subplot def create_subplot(self): """ Create subplot, Method to be overriden in more complex child classes. self.subplot can be a subplot, a list of subplot, or a dictionary of subplot """ self.subplot = self.figure.add_subplot(1, 1, 1) return self def before_plot(self): """ Method to be overriden in more complex child classes. Done before plot """ return self def after_plot(self): """ Method to be overriden in more complex child classes. Done after plot """ return self def after_label(self): """ Method to be overriden in more complex child classes Done after label """ return self def do_plot_and_bestfit(self): """ Create plot """ # Plot fmt = str(self.kwargs.get("fmt", "k.")) if "errors" in self.kwargs: errors = self.kwargs["errors"] if isinstance(errors, dict): self.subplot.errorbar(self.x, self.y, fmt=fmt, xerr=errors.get("xerr", None), yerr=errors.get("yerr", None)) elif isinstance(errors, (collections.Sequence, np.ndarray, float)): self.subplot.errorbar(self.x, self.y, fmt=fmt, yerr=errors) else: self.subplot.plot(self.x, self.y, fmt) else: self.subplot.plot(self.x, self.y, fmt) # bestfit bestfit = self.kwargs.get("bestfit", None) if bestfit is not None: bestfitlim = self.kwargs.get("bestfitlim", None) if bestfitlim is None: bestfitlim = self.kwargs.get("xlim", None) if bestfitlim is None: bestfitlim = (min(self.x), max(self.x)) fit_args = bestfit.do_bestfit() bestfit_line = bestfit.get_bestfit_line( x_min=bestfitlim[0], x_max=bestfitlim[1]) self.subplot.plot( bestfit_line[0], bestfit_line[1], self.kwargs.get("bestfitfmt", "k-") ) self.outputdict["fit_args"] = fit_args self.outputdict["rmse"] = bestfit.get_rmse() return self def do_label(self): """ Create label for x and y axis, title and suptitle """ outputdict = self.outputdict xlabel_options = self.kwargs.get("xlabel_options", {}) self.subplot.set_xlabel( self.kwargs.get("xlabel", "").format(**outputdict), **xlabel_options) ylabel_options = self.kwargs.get("ylabel_options", {}) self.subplot.set_ylabel( self.kwargs.get("ylabel", "").format(**outputdict), **ylabel_options) suptitle = self.kwargs.get("suptitle", None) if suptitle is not None: suptitle_options = self.kwargs.get("suptitle_options", {}) self.figure.suptitle( suptitle.format(**outputdict), fontsize=int(self.kwargs.get("suptitle_fontsize", 15)), **suptitle_options) title = self.kwargs.get("title", None) if title is not None: title_options = self.kwargs.get("title_options", {}) self.subplot.set_title( title.format(**outputdict), fontsize=int(self.kwargs.get("title_fontsize", 12)), **title_options) xlim = self.kwargs.get("xlim", None) ylim = self.kwargs.get("ylim", None) if xlim is not None: self.subplot.set_xlim(xlim) if ylim is not None: self.subplot.set_ylim(ylim) # axis format self.subplot.ticklabel_format( style="sci", useOffset=False, scilimits=self.kwargs.get("scilimits", (-4, 4)) ) return self def save(self): """ save plot to pdf """ self.figure.savefig(self.filepath) return self def close(self): """ close figure """ self.figure.clf() return self def get_outputdict(self): """ Get outpudict """ return self.outputdict def plot(self): """ Plot """ self.before_plot() self.do_plot_and_bestfit() self.after_plot() self.do_label() self.after_label() self.save() self.close() return self.outputdict PKHaH!igq q labreporthelper/manage.py""" Processes commands from user """ import os import sys import json import importlib ENV_VAR_SETTINGS = "LAB_SETTINGS" ENV_VAR_ROOT_DIR = "LAB_ROOT_DIR" FILEPATHSTR = "{root_dir}{os_sep}{folder}{os_sep}{name}{os_extsep}{ext}" def generate_datasets_list(settings, argv): """ generate datasets list to activate args: settings: dictionary from settings file argv: list from sys.argv """ datasets_string_list = settings["DATASETS_LIST"] datasets_list = [] if len(argv) == 2: try: datasets_items = datasets_string_list.iteritems() except AttributeError: datasets_items = datasets_string_list.items() for key, val in datasets_items: key_module = importlib.import_module( settings["PYTHON_MODULE"] + "." + key ) for element in val: datasets_list.append( (key, element, getattr(key_module, element)()) ) elif len(argv) > 2: arguments = argv[2:] for argument in arguments: argument_list = argument.split(".") key = ".".join(argument_list[:-1]) key_module = importlib.import_module( settings["PYTHON_MODULE"] + "." + key ) datasets_list.append( (key, argument_list[-1], getattr(key_module, argument_list[-1])()) ) else: print_help() return datasets_list def make_data_file(datasets_list, argv): """ generate the data file """ for datasets in datasets_list: datasets[2].create_dat_file() def parse_data(datasets_list, argv): """ parse data to pickle/hickle """ for datasets in datasets_list: datasets[2].parse_data_to_internal() def do_operations(datasets_list, argv): """ do operations """ for datasets in datasets_list: datasets[2].operations() def print_help(): """ Usage: python [options] [arguments] ... options: make-data-file: make data file for all datasets-list in the arguments, or all datasets_list in settings.json file if no arguments given parse-data: parse data to pickle/hickle folder do-operations: do operations method on the datasets arguments: filename.classname ... -- loop through all selected """ print print_help.__doc__ sys.exit(0) def manage(settingspath, root_dir, argv): """ Manage all processes """ # add settings.json to environment variables os.environ[ENV_VAR_SETTINGS] = settingspath # add root_dir os.environ[ENV_VAR_ROOT_DIR] = root_dir # get datasets list with open(settingspath) as settings_file: settings = json.load(settings_file) # manage args datasets_list = generate_datasets_list(settings, argv) if "make-data-file" == argv[1]: make_data_file(datasets_list, argv) elif "parse-data" == argv[1]: parse_data(datasets_list, argv) elif "do-operations" == argv[1]: do_operations(datasets_list, argv) else: print_help() PK ^HQdNlabreporthelper/dataset.py""" DataSets -- Main Operation Class """ import os import json from abc import ABCMeta, abstractmethod from . import table from .plot import PlotSingle2D from .manage import FILEPATHSTR, ENV_VAR_SETTINGS, ENV_VAR_ROOT_DIR from .misc import get_default_format with open(os.environ[ENV_VAR_SETTINGS], 'rb') as settings_file: SETTINGS = json.load(settings_file) ROOT_DIR = os.environ[ENV_VAR_ROOT_DIR] PURPOSE = SETTINGS.get("PURPOSE", {}) class DataSets(object): """ Main Operation Class data: dictionary "name": DataFile custom_data: dictionary "name": DataFile """ __metaclass__ = ABCMeta data = {} custom_data = {} def __init__(self): self.vardict = {} self.vardictformat = {} self.variables = {} @abstractmethod def operations(self): """ Abstract Method Main Operations """ pass def parse_data_to_internal(self, include_custom=False): """ Invoke parse_data_to_internal() for every element in self.data """ for element in self.data: self.data[element].parse_data_to_internal() if include_custom: for element in self.custom_data: self.custom_data[element].parse_data_to_internal() def create_dat_file(self, include_custom=False): """ Invoke create_dat_file() for every element in self.data """ for element in self.data: self.data[element].create_dat_file() if include_custom: for element in self.custom_data: self.custom_data[element].parse_data_to_internal() # built-in functions @staticmethod def plot_2d_single(x, y, pdffilename, **kwargs): """ Do make_2d_single_plot and pass all arguments args: x: array_like xdata y: array_like ydata filepath: string filepath of pdf to save **kwargs: errors: dictionary, array_like, scalar dictionary: {"xerr": xerr, "yerr": yerr} array_like, scalar: yerr fmt: string, default="k." bestfitfmt: string, default="k-" bestfit: BestFit child class eg. bestfit.polyfit.PolyFit, bestfit.logfit.LogFit suptitle: string, default=None suptitle of pdf plot, formatted with outputdict suptitle_fontsize: int, default=15 font size of suptitle title: string, default=None title of the pdf plot title_fontsize: int, default=12 font size of title, formatted with outputdict xlabel: string, default=None label of string xlabel, formatted with outputdict ylabel: string, default=None label of string ylabel, formatted with outputdict outputdict: dictionary, default=None pass keys and arguments for formatting and to output return: dictionary outputdict: passed from outputdict fit_args: list fitting arguments rmse: list rmse """ pdffilepath = DataSets.get_pdffilepath(pdffilename) plotsingle2d = PlotSingle2D(x, y, pdffilepath, **kwargs) return plotsingle2d.plot() @staticmethod def get_pdffilepath(pdffilename): """ Returns the path for the pdf file args: pdffilename: string returns path for the plots folder / pdffilename.pdf """ return FILEPATHSTR.format( root_dir=ROOT_DIR, os_sep=os.sep, os_extsep=os.extsep, name=pdffilename, folder=PURPOSE.get("plots").get("folder", "plots"), ext=PURPOSE.get("plots").get("extension", "pdf") ) @staticmethod def make_tex_table(inputlist, outputfilename, fmt=None, **kwargs): """ Do make_tex_table and pass all arguments args: inputlist: list outputfilename: string fmt: dictionary key: integer column index starting with 0 values: string format string. eg "{:g}" **kwargs: nonestring: string string when objecttype is None """ outputfilepath = FILEPATHSTR.format( root_dir=ROOT_DIR, os_sep=os.sep, os_extsep=os.extsep, name=outputfilename, folder=PURPOSE.get("tables").get("folder", "tables"), ext=PURPOSE.get("tables").get("extension", "tex") ) table.make_tex_table(inputlist, open(outputfilepath, 'wb'), fmt=fmt, close=kwargs.get("close", True), **kwargs) def make_compute_file(self): """ Make the compute file from the self.vardict and self.vardictformat """ string = "" try: vardict_items = self.vardict.iteritems() except AttributeError: vardict_items = self.vardict.items() for key, val in vardict_items: # get default default_format = get_default_format(val) string_format = "\\newcommand{{\\{}}}{{" + self.vardictformat.get( key, default_format) + "}}\n" string += string_format.format(key, val).replace("+", "") # get settings compute_file = open( "{root_dir}{os_sep}{name}{os_extsep}{ext}".format( root_dir=ROOT_DIR, os_sep=os.sep, os_extsep=os.extsep, name=SETTINGS["COMPUTE"]["name"], ext=SETTINGS["COMPUTE"]["extension"] ), "wb") compute_file.write(string) compute_file.close() PKHaH;Kgglabreporthelper/__init__.py""" CLASS INIT """ import sys import os sys.path.append(os.path.dirname(os.path.realpath(__file__))) PK(oHJwwlabreporthelper/table.py""" Helper module for make_tex_table """ import numpy as np def make_tex_table(inputlist, outputfile, close=False, fmt=None, **kwargs): """ Parse table from inputlist Args: inputlist: list List to parse outputfile: file .tex file to write fmt: dictionary key: integer column index starting with 0 values: string format string. eg "{:g}" **kwargs: nonestring: string string when objecttype is None Returns: None """ output_str = "" if fmt is None: fmt = {} for row in inputlist: for key, val in enumerate(row): if val is None: output_str += r'\text{{{}}}'.format( str(kwargs.get("nonestring", "None")) ) else: # get default if np.isscalar(val): temp_str_fmt = "$\\num{{" + fmt.get( key, "{:g}") + "}}$" else: temp_str_fmt = fmt.get(key, "{}") temp_str = temp_str_fmt.format(val).replace("+", "") output_str += temp_str + "&" output_str = output_str[:-1] output_str += "\\\\\n" outputfile.write(output_str) if close: outputfile.close() PKfN|H7Ynnlabreporthelper/datafile.py""" Input data file class """ import os import glob import cPickle as pickle import json import numpy as np from . import parse from .manage import FILEPATHSTR, ENV_VAR_SETTINGS, ENV_VAR_ROOT_DIR class CustomDataFile(object): """ Input data file class """ def __init__(self, name, ext=None, argnum=2, filetype="pickle", **kwargs): """ Constructor args: name: string invalid filename will be changed to valid filename in files argnum: int, default=2 number of columns **kwargs: string keyword is the variable arguments is either 'f' (float) or 'l' (list) """ self.name = str(name) keepcharacters = (" ", ".", "_") self.valid_filename = "".join( c for c in self.name if c.isalnum() or c in keepcharacters ).rstrip() self.argnum = int(argnum) self.kwargs = kwargs with open(os.environ[ENV_VAR_SETTINGS], 'rb') as settings_file: settings = json.load(settings_file) root_dir = os.environ[ENV_VAR_ROOT_DIR] purpose = settings.get("PURPOSE", {}) self.filetype = filetype if filetype is "pickle" or "hickle"\ else "pickle" self.location_dat = FILEPATHSTR.format( root_dir=root_dir, os_sep=os.sep, os_extsep=os.extsep, name=self.valid_filename, folder=purpose.get("data", {}).get("folder", "data"), ext=ext if ext is not None else purpose.get( "data", {}).get("extension", "dat"), ) self.location_internal = FILEPATHSTR.format( root_dir=root_dir, os_sep=os.sep, os_extsep=os.extsep, name=self.valid_filename, folder=purpose.get("pickle", {}).get("folder", "pickle"), ext=purpose.get("pickle", {}).get("extension", "pickle") ) def create_dat_file(self): """ Create and write empty data file in the data directory """ output = "## {}\n".format(self.name) try: kwargs_items = self.kwargs.iteritems() except AttributeError: kwargs_items = self.kwargs.items() for key, val in kwargs_items: if val is "l": output += "#l {}=\n".format(str(key)) elif val is "f" or True: output += "#f {}=\n".format(str(key)) comment = "## " + "\t".join(["col{" + str(i) + ":d}" for i in range(self.argnum)]) comment += "\n" rangeargnum = range(self.argnum) output += comment.format(*rangeargnum) if os.path.isfile(self.location_dat): files = glob.glob(self.location_dat + "*") count = 2 while ((self.location_dat + str(count) in files) ) and (count <= 10): count += 1 os.rename(self.location_dat, self.location_dat + str(count)) dat_file = open(self.location_dat, "wb") dat_file.write(output) dat_file.close() def parse_data_to_internal(self, data=None): """ Parse data and save to pickle/hickle """ if data is None: data = parse.getdata(open(self.location_dat, "rb"), argnum=self.argnum, close=True) if self.filetype is "pickle": pickle.dump(data, open(self.location_internal, "wb")) elif self.filetype is "hickle": import hickle hickle.dump(data, open(self.location_internal, "wb")) else: raise ValueError( "Invalid filetype {} (must be {} or {})".format( self.filetype, "pickle", "hickle" ) ) def get_internal_data(self): """ Get data that is saved in pickle/hickle """ if self.filetype is "pickle": return pickle.load(open(self.location_internal, "rb")) elif self.filetype is "hickle": import hickle return hickle.load(open(self.location_internal, "rb")) else: raise ValueError( "Invalue filetype {} (must be {} or {})".format( self.filetype, "pickle", "hickle" ) ) class MCADataFile(CustomDataFile): """ Data Table in the MCA """ def create_dat_file(self): """Pass """ pass def save_to_internal(self, data): """save """ if self.filetype is "pickle": pickle.dump(data, open(self.location_internal, "wb")) elif self.filetype is "hickle": import hickle hickle.dump(data, open(self.location_internal, "wb")) else: raise ValueError( "Invalid filetype {} (must be {} or {})".format( self.filetype, "pickle", "hickle" ) ) def parse_data_to_internal(self, data=None): """parse to internal """ if data is None: f = open(self.location_dat, "rb") data = { "PMCA SPECTRUM": {}, "DATA": [], "DP5 CONFIGURATION": {}, "DPP STATUS": {} } delimiter = { "PMCA SPECTRUM": " - ", "DP5 CONFIGURATION": "=", "DPP STATUS": ":" } comments = { "PMCA SPECTRUM": None, "DP5 CONFIGURATION": ";", "DPP STATUS": None } for e in f: if "<<" in e: if "<>" in e: current = None elif "<>" in e: current = "PMCA SPECTRUM" elif "<>" in e: current = "DATA" elif "<>" in e: current = "DP5 CONFIGURATION" elif "<>" in e: current = "DPP STATUS" else: if current == "DATA": data["DATA"].append(float(e)) elif current is not None: e = e.split("\r\n")[0] if comments[current] is not None: e = e.split(comments[current], 1)[0] e_list = e.split(delimiter[current], 1) data[current][e_list[0]] = e_list[1] f.close() self.save_to_internal(data) class DataFile(CustomDataFile): """ Standard Data Table using numpy to parse """ def create_dat_file(self): """Pass """ pass def parse_data_to_internal(self, data=None): """Use numpy loadtxt """ if data is None: kwargs = self.kwargs data = np.loadtxt( open(self.location_dat, "rb"), **kwargs ) if self.filetype is "pickle": pickle.dump(data, open(self.location_internal, "wb")) elif self.filetype is "hickle": import hickle hickle.dump(data, open(self.location_internal, "wb")) else: raise ValueError( "Invalid filetype {} (must be {} or {})".format( self.filetype, "pickle", "hickle" ) ) PKHaHplabreporthelper/parse.py""" Helper module to parse data """ import numpy as np def getdata(inputfile, argnum=None, close=False): """ Get data from the .dat files args: inputfile: file Input File close: bool, default=False Closes inputfile if True inputfile (File): Input file close (boolean): Closes inputfile if True (default: False) returns: dictionary: data: list of parsed data variables: dictionary of errors and other additional variables """ # get data and converts them to list # outputtype - list, dict, all output = [] add_data = {} line_num = 0 for line in inputfile: line_num += 1 if ("#" not in line) and (line != ""): linesplit = line.split() if argnum is not None and len(linesplit) != int(argnum): raise ValueError( "Line {:d} has {:d} arguments (need {:d})".format( line_num, len(linesplit), argnum)) output.append(linesplit) # additional float variable if "#f" in line: data = line.split()[1].split("=") add_data[data[0]] = float(data[1]) # additional list float variable if "#l" in line: data = line.split()[1].split("=") add_data[data[0]] = [float(e) for e in data[1].split(",")] if close: inputfile.close() output = cleandata(output) return { "data": np.array(output), "variables": add_data, } def cleandata(inputlist): """ Helper function for parse.getdata. Remove empty variables, convert strings to float args: inputlist: list List of Variables Returns: ouput: Cleaned list """ output = [] for e in inputlist: new = [] for f in e: if f == "--": new.append(None) else: new.append(float(f)) output.append(new) return output PK]HԋWWlabreporthelper/misc.py""" Misc Functions """ import numpy as np def get_default_format(val): """ Get default format Args: val: String/Scalar if np.isscalar(val) is true, returns "{:.3e}" else "{}" """ if np.isscalar(val): default_format = "{:.3e}" else: default_format = "{}" return default_format PKxwHzqoo"labreporthelper/bestfit/polyfit.py""" PolyFit """ import numpy as np from .bestfit import BestFit class PolyFit(BestFit): """ Polyfit class """ def __init__(self, **kwargs): """ Constructor args: **kwargs: degree: int, default=1 degree of polynomial fitting resolution: int, default=1000 """ BestFit.__init__(self, **kwargs) # set defaults self.set_defaults(degree=1, resolution=1000) def do_bestfit(self): """ Method to get fit_args after getting necessary variables """ self.check_important_variables() self.fit_args = np.polyfit( self.args["x"], self.args["y"], self.args.get("degree", 1)) self.done_bestfit = True return self.fit_args def bestfit_func(self, bestfit_x): """ Returns bestfit_y value args: bestfit_x: scalar, array_like x value return: scalar, array_like bestfit y value """ bestfit_x = np.array(bestfit_x) if not self.done_bestfit: raise KeyError("Do do_bestfit first") bestfit_y = 0 for idx, val in enumerate(self.fit_args): bestfit_y += val * (bestfit_x ** (self.args.get("degree", 1) - idx)) return bestfit_y PK#Hּ%9 9 #labreporthelper/bestfit/curvefit.py""" Curve Fit with scipy optimize and ODR """ from .bestfit import BestFit import scipy.optimize as opt import numpy as np from scipy.odr import ODR, RealData, Model, Data class CurveFit(BestFit): """ Curve Fit Important Variables: x, y, func, num_vars """ important_variables = set(["x", "y", "func", "num_vars"]) def __init__(self, **kwargs): """ Constructor """ BestFit.__init__(self, **kwargs) self.cov = None def bestfit_func(self, bestfit_x): """ Returns bestfit_func """ if not self.bestfit_func: raise KeyError("Do do_bestfit first") return self.args["func"](bestfit_x, *self.fit_args) def do_bestfit(self): """ Do bestfit """ self.check_important_variables() x = np.array(self.args["x"]) y = np.array(self.args["y"]) p = self.args.get("params", np.ones(self.args["num_vars"])) self.fit_args, self.cov = opt.curve_fit(self.args["func"], x, y, p) return self.fit_args class ODRFit(BestFit): """ BestFit with ODR """ important_variables = set(["x", "y"]) def __init__(self, **kwargs): """ Constructor """ BestFit.__init__(self, **kwargs) self.output = None def bestfit_func(self, bestfit_x): """ Returns y value """ if not self.bestfit_func: raise KeyError("Do do_bestfit first") return self.args["func"](self.fit_args, bestfit_x) def do_bestfit(self): """ do bestfit using scipy.odr """ self.check_important_variables() x = np.array(self.args["x"]) y = np.array(self.args["y"]) if self.args.get("use_RealData", True): realdata_kwargs = self.args.get("RealData_kwargs", {}) data = RealData(x, y, **realdata_kwargs) else: data_kwargs = self.args.get("Data_kwargs", {}) data = Data(x, y, **data_kwargs) model = self.args.get("Model", None) if model is None: if "func" not in self.args.keys(): raise KeyError("Need fitting function") model_kwargs = self.args.get("Model_kwargs", {}) model = Model(self.args["func"], **model_kwargs) odr_kwargs = self.args.get("ODR_kwargs", {}) odr = ODR(data, model, **odr_kwargs) self.output = odr.run() if self.args.get("pprint", False): self.output.pprint() self.fit_args = self.output.beta return self.fit_args PKfH13"labreporthelper/bestfit/bestfit.py""" Module containing BestFit abstract class """ import numpy as np from abc import ABCMeta, abstractmethod class BestFit(object): """ Base class for bestfit """ __metaclass__ = ABCMeta # variables needed to do bestfit important_variables = set(['x', 'y']) def __init__(self, **kwargs): """ Constructor """ self.args = kwargs self.done_bestfit = False self.fit_args = None def set_defaults(self, **defaults): """ Add all keyword arguments to self.args args: **defaults: key and value represents dictionary key and value """ try: defaults_items = defaults.iteritems() except AttributeError: defaults_items = defaults.items() for key, val in defaults_items: if key not in self.args.keys(): self.args[key] = val def set_args(self, **kwargs): """ Set more arguments to self.args args: **kwargs: key and value represents dictionary key and value """ try: kwargs_items = kwargs.iteritems() except AttributeError: kwargs_items = kwargs.items() for key, val in kwargs_items: self.args[key] = val def check_important_variables(self): """ Check all the variables needed are defined """ if len(self.important_variables - set(self.args.keys())): raise TypeError("Some important variables are not set") @abstractmethod def do_bestfit(self): """ Method to get fit_args after getting necessary variables """ pass @abstractmethod def bestfit_func(self, bestfit_x): """ Returns bestfit_function args: bestfit_x: scalar, array_like x value return: scalar, array_like bestfit y value """ pass def get_bestfit_line(self, x_min=None, x_max=None, resolution=None): """ Method to get bestfit line using the defined self.bestfit_func method args: x_min: scalar, default=min(x) minimum x value of the line x_max: scalar, default=max(x) maximum x value of the line resolution: int, default=1000 how many steps between x_min and x_max returns: [bestfit_x, bestfit_y] """ x = self.args["x"] if x_min is None: x_min = min(x) if x_max is None: x_max = max(x) if resolution is None: resolution = self.args.get("resolution", 1000) bestfit_x = np.linspace(x_min, x_max, resolution) return [bestfit_x, self.bestfit_func(bestfit_x)] def get_rmse(self, data_x=None, data_y=None): """ Get Root Mean Square Error using self.bestfit_func args: x_min: scalar, default=min(x) minimum x value of the line x_max: scalar, default=max(x) maximum x value of the line resolution: int, default=1000 how many steps between x_min and x_max """ if data_x is None: data_x = np.array(self.args["x"]) if data_y is None: data_y = np.array(self.args["y"]) if len(data_x) != len(data_y): raise ValueError("Lengths of data_x and data_y are different") rmse_y = self.bestfit_func(data_x) return np.sqrt(np.mean((rmse_y - data_y) ** 2)) def get_mae(self, data_x=None, data_y=None): """ Get Mean Absolute Error using self.bestfit_func args: data_x: array_like, default=x x value used to determine rmse, used if only a section of x is to be calculated data_y: array_like, default=y y value used to determine rmse, used if only a section of y is to be calculated """ if data_x is None: data_x = np.array(self.args["x"]) if data_y is None: data_y = np.array(self.args["y"]) if len(data_x) != len(data_y): raise ValueError("Lengths of data_x and data_y are different") mae_y = self.bestfit_func(data_x) return np.mean(abs(mae_y - data_y)) def get_fit_args(self): """ return fit_args """ return self.fit_args PK3(iHu&!labreporthelper/bestfit/expfit.py""" Exponential Fit """ import numpy as np from .bestfit import BestFit class ExpFit(BestFit): """ Exponential Fitting class """ def __init__(self, **kwargs): """ Constructor """ BestFit.__init__(self, **kwargs) # set defaults self.set_defaults(resolution=1000) self.fit_args_log = None def do_bestfit(self): """ Method to get fit_args after getting necessary variables """ self.check_important_variables() logx = np.log(self.args["x"]) logy = np.log(self.args["y"]) self.fit_args_log = np.polyfit(logx, logy, 1) self.fit_args = self.fit_args_log[:] self.fit_args[1] = np.exp(self.fit_args[1]) self.done_bestfit = True return self.fit_args def bestfit_func(self, bestfit_x): """ Returns bestfit_function args: bestfit_x: scalar, array_like x value return: scalar, array_like bestfit y value """ if not self.done_bestfit: raise KeyError("Do do_bestfit first") bestfit_y = self.fit_args[1] * (bestfit_x ** self.fit_args[0]) return bestfit_y PKHaH#labreporthelper/bestfit/__init__.pyPKH$lDQ2labreporthelper-0.2.1.data/scripts/make-lab-report#!python import os import json import stat def create_dir(): cwd = os.getcwd() # settings file settings = { "DATASETS_LIST": {"process":["Experiment"]}, "PURPOSE":{ "data":{"folder":"data", "extension":"dat"}, "pickle":{"folder":"pickle", "extension":"pickle"}, "plots":{"folder":"plots", "extension":"pdf"}, "tables":{"folder": "tables", "extension":"tex"}, }, "COMPUTE":{"name":"compute", "extension":"tex"}, "PYTHON_MODULE":"python", } folders = [a["folder"] for a in settings["PURPOSE"].itervalues()] folders.append(settings["PYTHON_MODULE"]) settings_path = cwd + os.sep + "settings" + os.extsep + "json" if os.path.isfile(settings_path): with open(settings_path, 'rb') as f: settings = json.load(f) else: json.dump(settings, open(settings_path, 'wb'), indent=4) for foldername in folders: fullpath = cwd + os.sep + foldername + os.sep if not os.path.exists(fullpath): os.makedirs(fullpath) fullpath += os.extsep + "keep" with open(fullpath, 'a'): os.utime(fullpath, None) init_path = cwd + os.sep + settings["PYTHON_MODULE"] + os.sep init_path += "__init__" + os.extsep + "py" with open(init_path, 'a'): os.utime(init_path, None) # manage.py manage_string = """#!/usr/bin/env python2.7 \"\"\" Pass sys.argv to labreporthelper.manage.manage \"\"\" import sys import os import json import importlib def main(): \"\"\" Method that interfaces with user \"\"\" from labreporthelper.manage import manage sys.path.append(os.path.realpath(__file__)) settings_path = os.sep.join(os.path.realpath(__file__).split(os.sep)[:-1]) settings_path += os.sep + "settings" + os.extsep + "json" with open(settings_path, 'rb') as settings_file: settings = json.load(settings_file) python_folder = settings.get("PYTHON_MODULE", "python") var = importlib.import_module(python_folder + ".variables") manage(settings_path, var.ROOT_DIR, sys.argv) if __name__ == "__main__": main() """ manage_path = cwd + os.sep + "manage" + os.extsep + "py" with open(manage_path, "wb") as f: f.write(manage_string) os.chmod(manage_path, stat.S_IXUSR) os.chmod(manage_path, stat.S_IXGRP) os.chmod(manage_path, stat.S_IXOTH) st = os.stat(manage_path) os.chmod( manage_path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | stat.S_IRUSR | stat.S_IWUSR ) # constants.py constant_string = """\"\"\" Module to store user defined constants \"\"\" """ with open(cwd + os.sep + settings["PYTHON_MODULE"] + os.sep + "constant" + os.extsep + "py", 'wb') as f: f.write(constant_string) # process.py process_string_pre = """\"\"\" Main Module. All operations are put here \"\"\" from labreporthelper.dataset import DataSets """ process_string_repeat = """ class {}(DataSets): \"\"\" Data Processing, Plots, LaTeX Macros, and Tables \"\"\" data = {{}} def operations(self): \"\"\" Main operations \"\"\" """ for key, val in settings["DATASETS_LIST"].iteritems(): with open(cwd + os.sep + settings["PYTHON_MODULE"] + os.sep + key + os.extsep + "py", 'wb') as f: f.write(process_string_pre) for classname in val: f.write(process_string_repeat.format(classname)) # variables.py variables_string = """\"\"\" Variables \"\"\" import os # NEEDED BY labreporthelper DO NOT DELETE ROOT_DIR = os.sep.join(os.path.realpath(__file__).split(os.sep)[:-2]) # USER DEFINED VARIABLES import {}.constant as constant """ with open(cwd + os.sep + settings["PYTHON_MODULE"] + os.sep + "variables" + os.extsep + "py", 'wb') as f: f.write(variables_string.format(settings["PYTHON_MODULE"])) create_dir() PKH^- /labreporthelper-0.2.1.dist-info/DESCRIPTION.rstUNKNOWN PKHe -labreporthelper-0.2.1.dist-info/metadata.json{"extensions": {"python.details": {"contacts": [{"email": "calvinku96@gmail.com", "name": "Calvin", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}, "project_urls": {"Home": "https://github.com/calvinku96/labreporthelper"}}}, "extras": [], "generator": "bdist_wheel (0.26.0)", "license": "MIT", "metadata_version": "2.0", "name": "labreporthelper", "run_requires": [{"requires": ["importlib", "matplotlib", "numpy", "scipy"]}], "summary": "labreporthelper", "version": "0.2.1"}PKHϩ6//(labreporthelper-0.2.1.dist-info/pbr.json{"is_release": false, "git_version": "a980262"}PKHo6+-labreporthelper-0.2.1.dist-info/top_level.txtlabreporthelper PKH''\\%labreporthelper-0.2.1.dist-info/WHEELWheel-Version: 1.0 Generator: bdist_wheel (0.26.0) Root-Is-Purelib: true Tag: py2-none-any PKH^fFF(labreporthelper-0.2.1.dist-info/METADATAMetadata-Version: 2.0 Name: labreporthelper Version: 0.2.1 Summary: labreporthelper Home-page: https://github.com/calvinku96/labreporthelper Author: Calvin Author-email: calvinku96@gmail.com License: MIT Platform: UNKNOWN Requires-Dist: importlib Requires-Dist: matplotlib Requires-Dist: numpy Requires-Dist: scipy UNKNOWN PKH%؃EE&labreporthelper-0.2.1.dist-info/RECORDlabreporthelper/__init__.py,sha256=uPaYUusrz4wzHfc_fRCmdlZxSn3EKf1G3bO-NSyEdjs,103 labreporthelper/datafile.py,sha256=0wUyxXIOvOkuoD9-v8fKxMBEXJHNNfK7eJ0uqVwk1Xk,7534 labreporthelper/dataset.py,sha256=ANMyKm2fbticJwEvi0bP618EVBY5w0X7lKcZ6cCFUdc,6021 labreporthelper/manage.py,sha256=vKZbkNnHykLqi6-Oqnj5jkPGO8CEFXKObHDaHAiSkC8,3185 labreporthelper/misc.py,sha256=iirlxLT6D2yyoqYXUhb8cFWHZYL9MQa3OXwxW-hdT9A,343 labreporthelper/parse.py,sha256=aD3a01h7HkZXenw-3oFZoFbviynfNpkpYRqPC2k2JRg,2048 labreporthelper/plot.py,sha256=bXfa9Ri4ILs8sn9HB_LT3JrTf_B6UipHBSXrC_iO8YM,7119 labreporthelper/table.py,sha256=4dmDes0DuzRgxuKIXObTuWK_TJhAvAa11UIPceqA7B0,1399 labreporthelper/bestfit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 labreporthelper/bestfit/bestfit.py,sha256=qd2DeKCcExImrLMahSj-T9f7GcC-uxfHYSiG3vOGOSM,4566 labreporthelper/bestfit/curvefit.py,sha256=HuyZKof6rucWENXC-h9s6NJA9PPsI0EjHRj_AbNHv78,2617 labreporthelper/bestfit/expfit.py,sha256=yROWHCWhiJ4jyOoeYkSnkOG3j7qyve-VQpASaCI_K8c,1229 labreporthelper/bestfit/polyfit.py,sha256=iUtOrBLyqUU-LWl8O8aX4qqd_20iQc4HiJCT6TL3RCs,1391 labreporthelper-0.2.1.data/scripts/make-lab-report,sha256=ImF95U6o538l_iE7EvOj3lTJ72mitX9vLGcPDg2z8YE,4005 labreporthelper-0.2.1.dist-info/DESCRIPTION.rst,sha256=OCTuuN6LcWulhHS3d5rfjdsQtW22n7HENFRh6jC6ego,10 labreporthelper-0.2.1.dist-info/METADATA,sha256=oHUJYfptgwb4hWeTTK-n5yEWPSVuTSMm4x81dzELiCY,326 labreporthelper-0.2.1.dist-info/RECORD,, labreporthelper-0.2.1.dist-info/WHEEL,sha256=JTb7YztR8fkPg6aSjc571Q4eiVHCwmUDlX8PhuuqIIE,92 labreporthelper-0.2.1.dist-info/metadata.json,sha256=35Bf6E6II8eceTo-7nwAxng4JVYD1gL2GALqx1yDNWY,502 labreporthelper-0.2.1.dist-info/pbr.json,sha256=tbyiuQbPw0NJcnRuACyE9bYf2wYPIFV9G-MTPh1_rE0,47 labreporthelper-0.2.1.dist-info/top_level.txt,sha256=KX48VwxtEgdIHR7YdLD3pPuF5044sh0yHvSVRukpQYM,16 PKm=H՞labreporthelper/plot.pyPKHaH!igq q labreporthelper/manage.pyPK ^HQdN(labreporthelper/dataset.pyPKHaH;Kggi@labreporthelper/__init__.pyPK(oHJww Alabreporthelper/table.pyPKfN|H7YnnFlabreporthelper/datafile.pyPKHaHp]dlabreporthelper/parse.pyPK]HԋWWllabreporthelper/misc.pyPKxwHzqoo"nlabreporthelper/bestfit/polyfit.pyPK#Hּ%9 9 #slabreporthelper/bestfit/curvefit.pyPKfH13"H~labreporthelper/bestfit/bestfit.pyPK3(iHu&!^labreporthelper/bestfit/expfit.pyPKHaH#jlabreporthelper/bestfit/__init__.pyPKH$lDQ2labreporthelper-0.2.1.data/scripts/make-lab-reportPKH^- /labreporthelper-0.2.1.dist-info/DESCRIPTION.rstPKHe -labreporthelper-0.2.1.dist-info/metadata.jsonPKHϩ6//(8labreporthelper-0.2.1.dist-info/pbr.jsonPKHo6+-labreporthelper-0.2.1.dist-info/top_level.txtPKH''\\%labreporthelper-0.2.1.dist-info/WHEELPKH^fFF(labreporthelper-0.2.1.dist-info/METADATAPKH%؃EE&3labreporthelper-0.2.1.dist-info/RECORDPK