{ "cells": [ { "cell_type": "markdown", "id": "3d59c632", "metadata": {}, "source": [ "# running automatic fitting in ROD\n", "\n", "before running the cells below make sure you have done the following:\n", "- have a version of RODautofitting.py in the same directory as where you have saved this notebook\n", "- made a copy of rod_init.mac in the ROD directory saved as rod_init_template.mac\n", "- saved a copy of fitlogloop5.mac and fitlogloop5run.mac in the ROD directory. Use the text below, replacing 'path\\to\\fitting\\folder' with you own chosen folder. This will save the output of 5 fitting runs to fitting_#.par files\n", "\n", "fitlogloop5.mac - carries out 5 ASA fitting loops and saves output\n", "```\n", "li par \"path\\to\\fitting\\folder\\fitting_0.par\" start_parameters\n", "fit control open \"path\\to\\fitting\\folder\\\\fitrun.log\" return return\n", "fit asa pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_1.par\" results_from_loop_1\n", "fit asa pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_2.par\" results_from_loop_2\n", "fit asa pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_3.par\" results_from_loop_3\n", "fit asa pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_4.par\" results_from_loop_4\n", "fit asa pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_5.par\" results_from_loop_5\n", "fit control close return return\n", "mac modelout\n", "```\n", "\n", "fitlogloop5run.mac - carries out 5 run fitting loops and saves output\n", "```\n", "li par \"path\\to\\fitting\\folder\\fitting_0.par\" start_parameters\n", "fit control open \"path\\to\\fitting\\folder\\\\fitrun.log\" return return\n", "fit run pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_1.par\" results_from_loop_1\n", "fit run pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_2.par\" results_from_loop_2\n", "fit run pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_3.par\" results_from_loop_3\n", "fit run pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_4.par\" results_from_loop_4\n", "fit run pl allbo\n", "li par \"path\\to\\fitting\\folder\\\\fitting_5.par\" results_from_loop_5\n", "fit control close return return\n", "mac modelout\n", "```" ] }, { "cell_type": "markdown", "id": "0a479a68", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "464603d5", "metadata": { "vscode": { "languageId": "plaintext" } }, "source": [ "## Define all file paths\n", "run cell below to load in the path information for where to find and/or save all the important files" ] }, { "cell_type": "code", "execution_count": null, "id": "5e1b4dc0", "metadata": {}, "outputs": [], "source": [ "from RODautofitting import * \n", "import os\n", "\n", "# give the path to where the final 5th fitting run is saved for each loop of 5\n", "fit5path=fr\"\\path\\to\\fitting\\folder\\fitting_5.par\"\n", "folderpath='define path here'\n", "# define the files to load into ROD using a dictionary\n", "files={}\n", "files['fit']= fr\"{folderpath}\\path\\to\\fit\\file\"\n", "files['dat']= fr\"{folderpath}path\\to\\dat\\file\"\n", "files['bul']= fr\"{folderpath}\\path\\to\\bulk\\file\"\n", "files['loadmac']= fr\"{folderpath}\\path\\to\\macro\\file\" # e.g. for loading matrices\n", "files['par']= fr\"{folderpath}\\path\\to\\par\\file\" #used as starting values\n", "files['fitmac']=fr\"{folderpath}\\path\\to\\fittingmacro\\file\"\n", "\n", "# create model name based on fitfile\n", "modname = files['fit'].split(r\"\\\\\")[-1]\n", "\n", "# set computer specific location for rod program and its init.mac file\n", "rodexe=fr\"\\path\\to\\anarod_standard_1-7_mingw\\rod.exe\"\n", "initfile=fr\"\\path\\to\\\\anarod_standard_1-7_mingw\\rod_init.mac\"\n", "\n", "#make a copy of initial rod_init.mac before any edits saved as rod_init_template.mac\n", "templateinitfile=fr\"\\path\\to\\anarod_standard_1-7_mingw\\rod_init_template.mac\"\n", "\n", "# set where to save dummy file used to flag when fitting loop is done\n", "donefile=fr\"\\path\\to\\to\\done\\file\"\n", "\n", "# set path where fitting results are saved as defined in macro fitlogloop5.mac\n", "fit5path=fr\"\\path\\to\\path\\to\\fitting\\folder\\fitting_5.par\"\n" ] }, { "cell_type": "markdown", "id": "b8d02686", "metadata": {}, "source": [ "## start the fitting routine\n", "Running the cell below starts the fitting cycles which does a series of ASA fits and then run fits until the chi squared values have stabilised" ] }, { "cell_type": "code", "execution_count": null, "id": "a4b842cd", "metadata": {}, "outputs": [], "source": [ "#create a ASA loop macro based on files dictionary\n", "createmacro(modname,files,files['par'],donefile,'ASA')\n", "\n", "#do an initial ASA fitting round\n", "domacrofits(donefile,rodexe)\n", "\n", "#define where the outputs are being save for each loop macro\n", "run = os.path.dirname(fit5path)+rf\"\\fitting\"\n", "\n", "#calculate chi-squared information for first fit\n", "fitlist, fitted, ranges, fitdf=calcfitlist(run, 5)\n", "asacount = 1\n", "print(f\"asa loop {asacount}\")\n", "\n", "#enter a loop that continues running rounds of 5 ASA fits until the final three chi squared values are within 0.01 of each other\n", "endasachi,asacount=fitloop(\n", " modname, fitlist, files, fit5path, donefile, rodexe, asacount, run,'ASA'\n", " )\n", "\n", "# once the ASA fitting gave a stable answer copy the parameters to a set location\n", "shutil.copy(\n", " f\"{fit5path}\",fr\"{folderpath}\\path\\to\\saving\\location\\asafit_{modname.replace('.fit','')}.par\"\n", " )\n", "\n", "# set starting parameters to the last of the ASA fits\n", "startvals = fit5path\n", "\n", "# create a run loop macro\n", "createmacro(modname, files, startvals, donefile,'RUN')\n", "\n", "# do an initial run fitting round\n", "domacrofits(donefile, rodexe)\n", "\n", "#calculate chi-squared information for first fit\n", "fitlist, fitted, ranges, fitdf = calcfitlist(run,5)\n", "runcount = 1\n", "print(f'run loop {runcount}')\n", "\n", "#enter a loop that continues running rounds of 5 run fits until the final three chi squared values are within 0.005 of each other\n", "endrunchi,runcount=fitloop(\n", " modname,fitlist,files,fit5path,donefile,rodexe,runcount,run,'RUN'\n", " )\n", "\n", "#once the run fitting gave a stable answer copy the parameters to a set location\n", "shutil.copy(f\"{fit5path}\",fr\"{folderpath}\\path\\to\\saving\\location\\runfit_{modname.replace('.fit','')}.par\")" ] }, { "cell_type": "markdown", "id": "de8e5cd2", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "babcba9b", "metadata": { "vscode": { "languageId": "plaintext" } }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python (nbs)", "language": "python", "name": "python" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }