.. _configure_editor:

Configure Editor
################

|popy| is script driven, so it is very important to have a suitable editor installed on your system. 

We recommended that you use :ref:`notepadpp` to edit |popy| text files. 

.. note:: 

    It is especially important that you follow the advice in :ref:`configure_notepadpp_tabs`. 

.. _notepadpp:

|npp|
===========

|npp| is a text editor that uses tabs and context highlighting, both of which are helpful when editing |popy| script files.

.. _install_notepadpp:

Install |npp|
--------------

Download it from:-

https://notepad-plus-plus.org/download/ 

If you install the 64bit binary installer the default install directory is:-

.. code-block:: console

 C:\Program Files\Notepad++
  
But you may install |npp| at another location.

.. _configure_notepadpp_tabs:

Configure |npp| Tabs
----------------------

|popy| is written in |python|, and |python| does |not| like tabs. It is essential that you set this from within Notepad++:-

.. code-block:: console

    Settings->Preferences->Language->Replace by space
 
The default tab size is 4, which is a sensible choice.

Note this means that when you hit the tab key you will get 4 space characters instead of a tab character. To check that this is the case do:-

.. code-block:: console

    View->Show Symbol->Show white space and Tab
 
Then |npp| will give you a faint orange dot for a space character and an arrow for a tab.  You are advised to delete any tabs you have in your \*.pyml files. 

Note that it is still possible to introduce tabs into your text file accidentally using cut and paste.

|python| using spaces instead of the more conventional curly brackets is an endearing (but perhaps controversial) language feature. See this blog post, for a fairly balanced discussion of the pros and cons of white spacing:-

https://jayconrod.com/posts/101/how-python-parses-white-space

However |popy| uses |python|, so it is white space for us.

.. _configure_notepadpp_path:

Configure |npp| Path
----------------------

Note that to use |npp| with :ref:`popy_edit` you need to make sure your :ref:`popy_config_pyml` contains this entry:-

.. code-block:: pyml

    text_editor_path: "C:/Program Files/Notepad++/notepad++.exe"

Which points to where you have installed |npp|. Note with the path above set correctly you can now :ref:`open_a_popy_command_prompt` and do:-

.. code-block:: console
 
    $ popy_edit my_script.pyml
    
The file 'my_script.pyml' should then open within |npp|. See :ref:`popy_edit`.

.. _configure_notepadpp_colouring:

Configure |npp| Colouring
----------------------------

It is highly advisable to load in the PoPy xml colouring file. You do this by opening |npp| and doing:-

.. code-block:: console  

    Language->Define your language->Import..
 
Then selecting:-

.. code-block:: console  

    c:\PoPy\conf\notepadplusplus_popy.xml
 
You should then be able to open any \*.pyml file and see the variables coloured like this:-

.. code-block:: pyml                     

    DERIVATIVES: |
        # s[DEPOT,CENTRAL,PERI] = @dep_two_cmp_cl{dose:@bolus{amt:c[AMT]}}
        d[DEPOT] = @bolus{amt:c[AMT]} - m[KA]*s[DEPOT]
        d[CENTRAL] = (
            m[KA]*s[DEPOT] - s[CENTRAL]*m[CL]/m[V1] 
            - s[CENTRAL]*m[Q]/m[V1]  + s[PERI]*m[Q]/m[V2]
        )
        d[PERI] = s[CENTRAL]*m[Q]/m[V1] - s[PERI]*m[Q]/m[V2]
    PREDICTIONS: |
        p[DV_CENTRAL] = s[CENTRAL]/m[V1]
        var = m[ANOISE]**2 + m[PNOISE]**2 * p[DV_CENTRAL]**2
        c[DV_CENTRAL] ~ norm(p[DV_CENTRAL], var)
        
As opposed to the default (plain) |npp| text display, like this:-

.. code-block:: none

    DERIVATIVES: |
        # s[DEPOT,CENTRAL,PERI] = @dep_two_cmp_cl{dose:@bolus{amt:c[AMT]}}
        d[DEPOT] = @bolus{amt:c[AMT]} - m[KA]*s[DEPOT]
        d[CENTRAL] = (
            m[KA]*s[DEPOT] - s[CENTRAL]*m[CL]/m[V1] 
            - s[CENTRAL]*m[Q]/m[V1]  + s[PERI]*m[Q]/m[V2]
        )
        d[PERI] = s[CENTRAL]*m[Q]/m[V1] - s[PERI]*m[Q]/m[V2]
    PREDICTIONS: |
        p[DV_CENTRAL] = s[CENTRAL]/m[V1]
        var = m[ANOISE]**2 + m[PNOISE]**2 * p[DV_CENTRAL]**2
        c[DV_CENTRAL] ~ norm(p[DV_CENTRAL], var)

Note that it might be necessary to restart |npp| to get the colouring file to work.

We find that the variable colouring, makes model editing easier and less error prone. For example, if you misspell a section header e.g "DERVIATIVES", then you will notice because the section header will not appear in bold.

The colouring file also just makes |pkpd| models look nicer. 

.. only:: browser

    .. editor_next_steps:

    Next Steps
    ============

    Now you have your editor setup correctly take a look at :ref:`simple_fit_example`.



