File: python_interface.rst

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (112 lines) | stat: -rw-r--r-- 3,660 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
Python interface
================

The external Python API mirrors the external C++ API, and only differs in that it follows
the common Python naming conventions. For a reference of the functions and a general
overview of what can be done with the Python interface, see the
:doc:`C++ interface <cpp_interface>`. A reference of the Python API can also be obtained
by running ``help(aoflagger)`` in (i)python:

.. code-block:: python

   In [1]: import aoflagger

   In [2]: help(aoflagger)
   Help on module aoflagger:

   NAME
       aoflagger - AOFlagger module for detection of radio-frequency interference

   CLASSES
       pybind11_builtins.pybind11_object(builtins.object)
           AOFlagger
           FlagMask
           ImageSet
           QualityStatistics
           Strategy
           TelescopeId
    
       class AOFlagger(pybind11_builtins.pybind11_object)
        |  Main class that gives access to the aoflagger functions.
   [..]
   
The rest of this chapter discusses a few practical topics related to the Python interface.

Installation
^^^^^^^^^^^^

The AOFlagger Python module is compiled into an object library. It is compiled
along when you run ``make`` as described on the :doc:`installation` chapter.
Currently, make compiles the following library on
my computer:

.. code-block:: text

   build/python/aoflagger.cpython-39-x86_64-linux-gnu.so

The normal/proper way of installing this library into its correct location, is by
running ``make install``. On my computer, this copies that file to
``/install-prefix/lib``. For Python to find this library, the path needs to
be in your Python search path, which is normally set with the 
``PYTHONPATH`` environment variable, e.g.:

.. code-block:: bash

   export PYTHONPATH=/install-prefix/lib:${PYTHONPATH}
   
There's no need to run a ``setup.py`` for AOFlagger.
Also, AOFlagger can't be installed via pip. When installing AOFlagger
via a Debian/Ubuntu package, the library should be installed and found
without any manual user tweaking. Be aware that the Python interface
and binding tool was improved in version 3.0, and it is therefore recommended
that the latest release of aoflagger (>= 3.0) is used.
   
Using the Python interface
^^^^^^^^^^^^^^^^^^^^^^^^^^

The aoflagger module can be included in
Python using a standard ``import`` command:

.. code-block:: python

    import aoflagger
   
A few examples are given in the ``data`` directory. The following is an example to calculate
the false-positives ratio of the default strategy:

.. code-block:: python

    import aoflagger
    import numpy

    nch = 256
    ntimes = 1000
    count = 50       # number of trials in the false-positives test

    flagger = aoflagger.AOFlagger()
    path = flagger.find_strategy_file(aoflagger.TelescopeId.Generic)
    strategy = flagger.load_strategy_file(path)
    data = flagger.make_image_set(ntimes, nch, 8)

    ratiosum = 0.0
    ratiosumsq = 0.0
    for repeat in range(count):
        for imgindex in range(8):
            # Initialize data with random numbers
            values = numpy.random.normal(0, 1, [nch, ntimes])
            data.set_image_buffer(imgindex, values)
            
        flags = strategy.run(data)
        flagvalues = flags.get_buffer()
        ratio = float(sum(sum(flagvalues))) / (nch*ntimes)
        ratiosum += ratio
        ratiosumsq += ratio*ratio

    print("Percentage flags (false-positive rate) on Gaussian data: " +
        str(ratiosum * 100.0 / count) + "% +/- " +
        str(numpy.sqrt(
            (ratiosumsq/count - ratiosum*ratiosum / (count*count) )
            ) * 100.0) )

This takes about 10 seconds to run on my computer.