Passed
Pull Request — main (#25)
by
unknown
01:54
created

mutis.astro   F

Complexity

Total Complexity 74

Size/Duplication

Total Lines 651
Duplicated Lines 14.29 %

Importance

Changes 0
Metric Value
eloc 387
dl 93
loc 651
rs 2.48
c 0
b 0
f 0
wmc 74

4 Functions

Rating   Name   Duplication   Size   Complexity  
F KnotsId2dGUI() 47 253 26
F KnotsIdAuto() 0 122 20
A pol_angle_reshape() 0 20 4
F KnotsIdGUI() 46 222 24

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like mutis.astro often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# Licensed under a 3-clause BSD style license - see LICENSE
2
"""Utils specific to the field of astrophysics"""
3
4
import logging
5
6
import numpy as np
0 ignored issues
show
introduced by
Unable to import 'numpy'
Loading history...
7
import pandas as pd
0 ignored issues
show
introduced by
Unable to import 'pandas'
Loading history...
8
9
import matplotlib as mplt
0 ignored issues
show
introduced by
Unable to import 'matplotlib'
Loading history...
Unused Code introduced by
Unused matplotlib imported as mplt
Loading history...
10
import matplotlib.pyplot as plt
0 ignored issues
show
introduced by
Unable to import 'matplotlib.pyplot'
Loading history...
11
12
from astropy.time import Time
0 ignored issues
show
introduced by
Unable to import 'astropy.time'
Loading history...
13
14
15
__all__ = ["Astro"]
0 ignored issues
show
Bug introduced by
Undefined variable name 'Astro' in __all__
Loading history...
16
17
log = logging.getLogger(__name__)
18
19
20
def pol_angle_reshape(s):
0 ignored issues
show
Coding Style Naming introduced by
Argument name "s" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
21
    """
22
        Reshape a signal as a polarization angle: shifting by 180 degrees in a way it varies as smoothly as possible.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
23
    """
24
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
25
    s = np.array(s)
26
    s = np.mod(s,180) # s % 180
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
27
28
    sn = np.empty(s.shape)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "sn" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
29
    for i in range(1,len(s)):
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
30
        #if t[i]-t[i-1] < 35:
31
        d = 181
0 ignored issues
show
Coding Style Naming introduced by
Variable name "d" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
32
        n = 0
0 ignored issues
show
Coding Style Naming introduced by
Variable name "n" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
33
        for m in range(0,100):
0 ignored issues
show
Coding Style Naming introduced by
Variable name "m" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
Exactly one space required after comma
Loading history...
34
            m2 = (-1)**(m+1)*np.floor((m+1)/2)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "m2" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
35
            if np.abs(s[i]+180*m2-sn[i-1]) < d:
36
                d = np.abs(s[i]+180*m2-sn[i-1])
0 ignored issues
show
Coding Style Naming introduced by
Variable name "d" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
37
                n = m2
0 ignored issues
show
Coding Style Naming introduced by
Variable name "n" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
38
        sn[i] = s[i]+180*n
39
    return sn 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
40
41
42
43
44
45
def  KnotsIdAuto(mod_dates, mod_dates_str, mod_data):
0 ignored issues
show
Coding Style Naming introduced by
Function name "KnotsIdAuto" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (17/15).
Loading history...
46
    """
47
       Identify the knots appearing in several epochs giving them names, based on their position.
48
       
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
49
        Parameters:
50
        -----------
51
         mod_dates : a list containing the dates (datetime) of each of the epochs.
52
         mod_data : a list containing a pandas.DataFrame for each epoch, with at least columns
53
             'X', 'Y' and 'Flux (Jy)'.
54
       
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
55
        Returns: ((Bx, By, B), mod)
56
        --------
57
         (Bx, By, B) : :tuple: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
58
            three lists, the first two containing the positions of 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
59
            the identified knots, the third their assigned names (for each epoch).
60
         mod : :pd.DataFrame:
61
             pandas.DataFrame containing every knot with their altered labels, with 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
62
             at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'.
63
         
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
64
           
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
65
        Usage: (old)
66
        ------
67
         >> Bx, By, B = KnotsIdAuto(mod_dates, mod_dates_str, mod_data)
68
         
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
69
         >> for i, (date, data) in enumerate(zip(mod_dates, mod_data)):
70
               data['label'] = B[i]
71
         
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
72
         >> mod = pd.concat(mod_data, ignore_index=True)
73
         >> mod
74
    """
75
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
76
    Bx = [[]]*len(mod_dates)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "Bx" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
77
    By = [[]]*len(mod_dates)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "By" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
78
    B = [[]]*len(mod_dates)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "B" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
79
80
81
    thresh = 0.03 #0.062
82
83
    news = 0
84
85
    for i, (date, data) in enumerate(zip(mod_dates, mod_data)):
0 ignored issues
show
unused-code introduced by
Too many nested blocks (6/5)
Loading history...
Unused Code introduced by
The variable date seems to be unused.
Loading history...
86
        log.debug(f'Analysing epoch i {i:3d} ({mod_dates_str[i]})')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
87
88
        if not len(data.index) > 0:
89
            log.error(' -> Error, no components found')
90
            break
91
92
        log.debug(f' it has {len(data.index)} components')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
93
94
        Bx[i] = np.ravel(data['X'])
95
        By[i] = np.ravel(data['Y'])
96
        B[i] = np.full(len(data.index), fill_value=None)
97
98
        # if first epoch, just give them new names...:
99
        if i == 0:
100
            log.debug(' first epoch, giving names...')
101
102
            for n in range(0,len(mod_data[i].index)):
0 ignored issues
show
Coding Style Naming introduced by
Variable name "n" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
Exactly one space required after comma
Loading history...
103
                if data['Flux (Jy)'][n] < 0.001:
104
                    log.debug('  skipping, too weak')
105
                    break
106
                if n == 0:
107
                    B[i][n] = 'A0'
108
                else:
109
                    news = news + 1
110
                    B[i][n] = f'B{news}'
111
            log.debug(f' -> FOUND: {B[i]}\n')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
112
            continue
113
114
        # if not first epoch...:
115
116
        for n in range(0,len(mod_data[i].index)):
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
Coding Style Naming introduced by
Variable name "n" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
117
            if data['Flux (Jy)'][n] < 0.001:
118
                    log.debug('  skipping, too weak')
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 16 spaces were expected, but 20 were found.
Loading history...
119
                    break
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 16 spaces were expected, but 20 were found.
Loading history...
120
            if n == 0:
121
                B[i][n] = 'A0'
122
            else:
123
                log.debug(f' -> id component {n}...')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
124
125
                close = None
126
127
                a = 0
0 ignored issues
show
Coding Style Naming introduced by
Variable name "a" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
128
                while ( i - a >= 0 and a < 4 and close is None):
0 ignored issues
show
Coding Style introduced by
No space allowed after bracket
Loading history...
129
                    a = a + 1
0 ignored issues
show
Coding Style Naming introduced by
Variable name "a" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
130
131
                    if not len(Bx[i-a])>0:
0 ignored issues
show
Coding Style introduced by
Exactly one space required around comparison
Loading history...
132
                        break
133
134
                    dist = ( (Bx[i-a]-Bx[i][n]) ** 2 + (By[i-a]-By[i][n]) ** 2 ) ** 0.5
0 ignored issues
show
Coding Style introduced by
No space allowed before bracket
Loading history...
Coding Style introduced by
No space allowed after bracket
Loading history...
135
136
                    for m in range(len(dist)):
0 ignored issues
show
unused-code introduced by
Consider using enumerate instead of iterating with range and len
Loading history...
Coding Style Naming introduced by
Variable name "m" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
137
                        if B[i-a][m] in B[i]:
138
                            dist[m] = np.inf
139
140
                    if np.amin(dist) < thresh*a**1.5:
141
                        close = np.argmin(dist)
142
143
                    if B[i-a][close] in B[i]:
144
                            close = None
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 24 spaces were expected, but 28 were found.
Loading history...
145
146
147
                if close is None:
148
                    news = news + 1
149
                    B[i][n] = f'B{news}'
150
                    log.debug(f'   component {n} is new, naming it {B[i][n]}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
151
                else:
152
                    log.debug(f'   component {n} is close to {B[i-a][close]} of previous epoch ({a} epochs before)')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (116/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
introduced by
Use lazy % formatting in logging functions
Loading history...
153
                    B[i][n] = B[i-a][close]
154
155
156
        log.debug(f' -> FOUND: {B[i]}\n')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
157
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
158
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
159
    #Bx, By, B = KnotsIdAuto(mod_dates, mod_dates_str, mod_data)
160
         
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
161
    for i, (date, data) in enumerate(zip(mod_dates, mod_data)):
162
        data['label'] = B[i]
163
         
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
164
    mod = pd.concat(mod_data, ignore_index=True)
165
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
166
    return (Bx, By, B), mod
167
168
169
170
def KnotsId2dGUI(mod, use_arrows=False, arrow_pos=1.0):
0 ignored issues
show
Coding Style Naming introduced by
Function name "KnotsId2dGUI" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (33/15).
Loading history...
171
    """
172
        Prompt a GUI to select identified knots and alter their label, reprenting their 2D
173
        spatial distribution in different times.
174
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
175
        It can be used inside jupyter notebooks, using '%matplotlib widget' first.
176
    """
177
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
178
    mod = mod.copy()
179
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
180
    knots = dict(tuple(mod.groupby('label')))
181
    knots_names = list(knots.keys())
182
    knots_values = list(knots.values())
183
    knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots}
184
    knots_X = {k:knots[k]['X'].to_numpy() for k in knots}
0 ignored issues
show
Coding Style Naming introduced by
Variable name "knots_X" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
185
    knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots}
0 ignored issues
show
Coding Style Naming introduced by
Variable name "knots_Y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
186
187
188
    from matplotlib.widgets import Slider, Button, TextBox, RectangleSelector
0 ignored issues
show
Unused Code introduced by
Unused Button imported from matplotlib.widgets
Loading history...
introduced by
Import outside toplevel (matplotlib.widgets.Slider, matplotlib.widgets.Button, matplotlib.widgets.TextBox, matplotlib.widgets.RectangleSelector)
Loading history...
introduced by
Unable to import 'matplotlib.widgets'
Loading history...
189
190
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8))
0 ignored issues
show
Coding Style Naming introduced by
Variable name "ax" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
Exactly one space required after comma
Loading history...
191
192
    lineas = list()
193
    flechas = list()
194
    textos = list()
195
196
    def draw_all(val=2008):
197
        nonlocal lineas, flechas, textos
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable flechas does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable lineas does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable textos does not seem to be defined.
Loading history...
198
199
        # instead of clearing the whole axis, remove artists
200
        for linea in lineas:
201
            if linea is not None:
202
                linea.remove()
203
        for texto in textos:
204
            if texto is not None:
205
                texto.remove()
206
        for flecha in flechas:
207
            if flecha is not None:
208
                flecha.remove()
209
        ax.set_prop_cycle(None)
210
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
211
        lineas = list()
212
        flechas = list()
213
        textos = list()
214
215
        xlim, ylim = ax.get_xlim(), ax.get_ylim()
216
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
217
        #ax.clear() # either clear the whole axis or remove every artist separetly
218
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
219
        for i, label in enumerate(knots_names):
220
            years = knots_jyears[label]
221
            idx = (val-1.5 < years) & (years < val)
222
            x = knots_X[label][idx]
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
223
            y = knots_Y[label][idx]
0 ignored issues
show
Coding Style Naming introduced by
Variable name "y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
224
225
            lineas.append(ax.plot(x, y, '.-', linewidth=0.6, alpha=0.4, label=label)[0])
226
227
            if use_arrows:
228
                if len(x) > 1:
229
                    flechas.append(ax.quiver(x[:-1], 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
230
                               y[:-1], 
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 14 spaces).
Loading history...
Coding Style introduced by
Trailing whitespace
Loading history...
231
                               arrow_pos*(x[1:] - x[:-1]), 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Wrong continued indentation (add 14 spaces).
Loading history...
232
                               arrow_pos*(y[1:] - y[:-1]), 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Wrong continued indentation (add 14 spaces).
Loading history...
233
                               scale_units='xy', angles='xy', scale=1, 
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 14 spaces).
Loading history...
Coding Style introduced by
Trailing whitespace
Loading history...
234
                               width=0.0015, headwidth=10, headlength=10, headaxislength=6,
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 14 spaces).
Loading history...
235
                               alpha=0.5, color=lineas[i].get_color()))
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 14 spaces).
Loading history...
236
                else:
237
                    flechas.append(None)
238
239
            if len(x) > 0:
240
                #textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (145/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
241
                textos.append(ax.text(x[-1]+0.015, y[-1]+0.015, label, {'color':lineas[i].get_color(), 'fontsize':14}))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (119/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
242
            else:
243
                textos.append(None)
244
245
        ax.set_xlim(xlim)
246
        ax.set_ylim(ylim)
247
        ax.set_aspect('equal')
248
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
249
        fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear()
250
251
    draw_all()
252
253
    def update(val):
254
        nonlocal lineas, flechas, textos
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable textos does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable flechas does not seem to be defined.
Loading history...
255
256
        for i, label in enumerate(knots_names):
257
            years = knots_jyears[label]
258
            idx = (val-1.5 < years) & (years < val)
259
            x = knots_X[label][idx]
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
260
            y = knots_Y[label][idx]
0 ignored issues
show
Coding Style Naming introduced by
Variable name "y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
261
262
            lineas[i].set_xdata(x)
263
            lineas[i].set_ydata(y)
264
265
            if textos[i] is not None:
266
                if len(x) > 0:    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
267
                    textos[i].set_position((x[-1]+0.015, y[-1]+0.015))
268
                    textos[i].set_text(label)
269
                else:
270
                    textos[i].remove()
271
                    textos[i] = None
272
                    #textos[i].set_position((10, 10))
273
            else:
274
                if len(x) > 0:    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
275
                    textos[i] = ax.text(x[-1]+0.02, y[-1]+0.02, label, {'color':lineas[i].get_color(), 'fontsize':14})
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (118/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
276
277
            if use_arrows:
278
                if flechas[i] is not None:
279
                    flechas[i].remove()
280
                    flechas[i] = None
281
282
                flechas[i] = ax.quiver(x[:-1], 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
283
                                   y[:-1], 
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
Coding Style introduced by
Trailing whitespace
Loading history...
284
                                   arrow_pos*(x[1:] - x[:-1]), 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
285
                                   arrow_pos*(y[1:] - y[:-1]), 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
286
                                   scale_units='xy', angles='xy', scale=1, 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
287
                                   width=0.0015, headwidth=10, headlength=10, headaxislength=6, 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
288
                                   alpha=0.5, color=lineas[i].get_color())
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 4 spaces).
Loading history...
289
290
        fig.canvas.draw_idle()
291
292
293
294
    selected_knot = None
295
    selected_ind = None
296
    selected_x = None
297
    selected_y = None
298
299
300 View Code Duplication
    def submit_textbox(text):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
301
        nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_X, knots_Y
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable knots_values does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_Y does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_jyears does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_names does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_X does not seem to be defined.
Loading history...
302
303
        log.debug('Submited with:')
304
        log.debug(f'   selected_knot {selected_knot}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
305
        log.debug(f'   selected_ind {selected_ind}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
306
        log.debug(f'   selected_x {selected_x}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
307
        log.debug(f'   selected_y {selected_y}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
308
309
        if selected_knot is not None:        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
310
            mod.loc[selected_ind, 'label'] = text.upper()
311
312
            knots = dict(tuple(mod.groupby('label')))
313
            knots_names = list(knots.keys())
314
            knots_values = list(knots.values())
315
            knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots}
316
            knots_X = {k:knots[k]['X'].to_numpy() for k in knots}
0 ignored issues
show
Coding Style Naming introduced by
Variable name "knots_X" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
317
            knots_Y = {k:knots[k]['Y'].to_numpy() for k in knots}
0 ignored issues
show
Coding Style Naming introduced by
Variable name "knots_Y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
318
319
            print(f"Updated index {selected_ind} to {text.upper()}")
320
        else:
321
            pass
322
323
        draw_all(slider_date.val)
324
325
    def line_select_callback(eclick, erelease):
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (17/15).
Loading history...
326
        nonlocal selected_knot,selected_x, selected_y, selected_ind
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
Comprehensibility Best Practice introduced by
The variable selected_ind does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable selected_y does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable selected_x does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable selected_knot does not seem to be defined.
Loading history...
327
328
        # 1 eclick and erelease are the press and release events
329
        x1, y1 = eclick.xdata, eclick.ydata
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x1" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Variable name "y1" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
330
        x2, y2 = erelease.xdata, erelease.ydata
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x2" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Variable name "y2" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
331
        log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
0 ignored issues
show
Coding Style Best Practice introduced by
Use lazy % formatting in logging functions
Loading history...
332
        log.debug("GUI:  The button you used were: %s %s" % (eclick.button, erelease.button))
0 ignored issues
show
Coding Style Best Practice introduced by
Use lazy % formatting in logging functions
Loading history...
333
334
        selected_knot = None
335
        selected_x = None
336
        selected_y = None
337
        selected_ind = None
338
339
        for i, label in enumerate(knots_names):
0 ignored issues
show
Unused Code introduced by
The variable i seems to be unused.
Loading history...
340
            years = knots_jyears[label]
341
            idx = (slider_date.val-1.5 < years) & (years < slider_date.val)
342
343
            if np.sum(idx) == 0:
344
                continue # we did not select any component from this component, next one
345
346
            x = np.array(knots_X[label])
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
347
            y = np.array(knots_Y[label])
0 ignored issues
show
Coding Style Naming introduced by
Variable name "y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
348
349
            # get points iside current rectangle for current date
350
            rect_idx = (x1 < x) & ( x < x2) & (y1 < y) & ( y < y2) & idx 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
No space allowed after bracket
Loading history...
351
352 View Code Duplication
            if np.sum(rect_idx) > 0:
0 ignored issues
show
Unused Code introduced by
Unnecessary "else" after "break"
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
353
                textbox.set_val(label)
354
                selected_knot = label
355
                selected_x = x[rect_idx].ravel()
356
                selected_y = y[rect_idx].ravel()
357
                selected_ind = knots[label].index[rect_idx]
358
                log.debug(f'Selected {label} points  rect_idx {rect_idx} x {x[rect_idx]}, y {y[rect_idx]} with indices {selected_ind}')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (135/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
introduced by
Use lazy % formatting in logging functions
Loading history...
359
                textbox.begin_typing(None)
360
                break # if we find selected components in this epoch, continue with renaming
361
            else:
362
                pass
363
364
        update(slider_date.val)
365
366
367 View Code Duplication
    def toggle_selector(event):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
368
        log.debug('GUI: Key pressed.', end=' ')
369
        if event.key in ['Q', 'q'] and toggle_selector.RS.active:
370
            log.debug('Selector deactivated.')
371
            toggle_selector.RS.set_active(False)
372
        if event.key in ['S', 's'] and not toggle_selector.RS.active:
373
            log.debug('Selector activated.')
374
            toggle_selector.RS.set_active(True)
375
        if event.key in ['R', 'r']:
376
            log.debug('Selector deactivated.')
377
            toggle_selector.RS.set_active(False)
378
            textbox.begin_typing(None)
379
            #textbox.set_val('')
380
381
382
    toggle_selector.RS = RectangleSelector(ax, line_select_callback,
383
                                           drawtype='box', useblit=True,
384
                                           button=[1, 3],  # don't use middle button
385
                                           minspanx=0, minspany=0,
386
                                           spancoords='data',
387
                                           interactive=False)
388
389
390
    #plt.connect('key_press_event', toggle_selector)
391
    fig.canvas.mpl_connect('key_press_event', toggle_selector)
392
393
394
395
    from mpl_toolkits.axes_grid1 import make_axes_locatable
0 ignored issues
show
introduced by
Unable to import 'mpl_toolkits.axes_grid1'
Loading history...
introduced by
Import outside toplevel (mpl_toolkits.axes_grid1.make_axes_locatable)
Loading history...
396
397
    divider_slider = make_axes_locatable(ax)
398
    slider_ax = divider_slider.append_axes("top", size="3%", pad="4%")  
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
399
    slider_date = Slider(ax=slider_ax, label="Date", valmin=2007, valmax=2020, valinit=2008, valstep=0.2, orientation="horizontal")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (131/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
400
    slider_date.on_changed(update)
401
402
    #divider_textbox = make_axes_locatable(ax)
403
    #textbox_ax = divider_textbox.append_axes("bottom", size="3%", pad="4%") 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
404
    textbox_ax = fig.add_axes([0.3,0.015,0.5,0.05])
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
405
    textbox = TextBox(textbox_ax, 'Knot name:', initial='None')
406
    textbox.on_submit(submit_textbox)
407
408
409
410
    ax.set_xlim([-1.0, +1.0])
411
    ax.set_ylim([-1.0, +1.0])
412
    ax.set_aspect('equal')
413
414
    fig.suptitle('S to select, R to rename, Q to deactivate selector')
415
416
    print('S to select, R to rename, Q to deactivate selector')
417
    print('(you can select points from one component at a time)')
418
    print('(if you use the zoom or movement tools, remember to unselect them)')
419
420
    plt.show()
421
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
422
    return mod
423
424
425
426
427
428
429
def KnotsIdGUI(mod):
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (27/15).
Loading history...
Coding Style Naming introduced by
Function name "KnotsIdGUI" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
430
    """
431
        Prompt a GUI to select identified knots and alter their label, reprenting their
432
        time evolution.
433
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
434
        It can be used inside jupyter notebooks, using '%matplotlib widget' first.
435
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
436
             mod_data : a list containing a pandas.DataFrame for each epoch, with at least columns
437
             'X', 'Y' and 'Flux (Jy)'.
438
       
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
439
        Parameters:
440
        -----------
441
         mod : :pd.DataFrame:
442
             pandas.DataFrame containing every knot, with at least columns 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
443
             'label', 'date', 'X', 'Y', 'Flux (Jy)'.
444
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
445
        Returns:
446
        --------
447
         mod : :pd.DataFrame:
448
             pandas.DataFrame containing every knot with their altered labels, with 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
449
             at least columns 'label', 'date', 'X', 'Y', 'Flux (Jy)'.
450
    """
451
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
452
    mod = mod.copy()
453
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
454
    knots = dict(tuple(mod.groupby('label')))
455
    knots_names = list(knots.keys())
456
    knots_values = list(knots.values())
457
    knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots}
458
    knots_dates = {k:knots[k]['date'].to_numpy() for k in knots}
459
    knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots}
460
461
    from matplotlib.widgets import TextBox, RectangleSelector
0 ignored issues
show
introduced by
Unable to import 'matplotlib.widgets'
Loading history...
introduced by
Import outside toplevel (matplotlib.widgets.TextBox, matplotlib.widgets.RectangleSelector)
Loading history...
462
463
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10,6))
0 ignored issues
show
Coding Style Naming introduced by
Variable name "ax" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
Exactly one space required after comma
Loading history...
464
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
465
    lineas = list()
466
    textos = list()
467
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
468
    def draw_all():
469
        nonlocal lineas, textos
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable textos does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable lineas does not seem to be defined.
Loading history...
470
471
        for linea in lineas:
472
            if linea is not None:
473
                linea.remove()
474
        for texto in textos:
475
            if texto is not None:
476
                texto.remove()
477
478
        ax.set_prop_cycle(None)
479
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
480
        lineas = list()
481
        textos = list()
482
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
483
        #ax.clear() # either clear the whole axis or remove every artist separetly
484
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
485
        for i, label in enumerate(knots_names):
486
            x, y = knots_jyears[label], knots_fluxes[label]
0 ignored issues
show
Coding Style Naming introduced by
Variable name "y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Variable name "x" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
487
488
            if len(x) > 0:
489
                lineas.append(ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0])
490
            else:
491
                lineas.append(None)
492
                
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
493
            if len(x) > 0:
494
                #textos.append(ax.annotate(label, (x[0], y[0]), (-28,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=14))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (145/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
495
                textos.append(ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14}))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (105/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
496
            else:
497
                textos.append(None)
498
                
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
499
        fig.canvas.draw_idle() # if removed every artist separately instead of ax.clear()
500
501
502
    def update():
503
        nonlocal lineas, textos
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable textos does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable lineas does not seem to be defined.
Loading history...
504
505
        for i, label in enumerate(knots_names):   
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
506
            x, y = knots_jyears[label], knots_fluxes[label]
0 ignored issues
show
Coding Style Naming introduced by
Variable name "y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Variable name "x" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
507
508
            if lineas[i] is not None:
509
                if len(x) > 0:
510
                    lineas[i].set_xdata(x)
511
                    lineas[i].set_ydata(y)
512
                else:
513
                    lineas[i].remove()
514
                    lineas[i] = None
515
            else:
516
                if len(x) > 0:    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
517
                    lineas[i] = ax.plot(x, y, '.-', linewidth=0.8, alpha=0.5, label=label)[0]
518
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
519
            if textos[i] is not None:
520
                if len(x) > 0:    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
521
                    textos[i].set_position((x[0], y[0]))
522
                    textos[i].set_text(label)
523
                else:
524
                    textos[i].remove()
525
                    textos[i] = None
526
                    #textos[i].set_position((10, 10))
527
            else:
528
                if len(x) > 0:    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
529
                    #textos[i] = ax.annotate(label, (x[0], y[0]), (-24,-10), textcoords='offset points', color=lineas[i].get_color(), fontsize=15)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (146/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
530
                    textos[i] = ax.text(x[0], y[0], label, {'color':lineas[i].get_color(), 'fontsize':14})
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (106/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
531
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
532
        fig.canvas.draw_idle()    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
533
   
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
534
535
    selected_knot = None
536
    selected_ind = None
537
    selected_date = None
538
    selected_flux = None
539
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
540 View Code Duplication
    def submit_textbox(text):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
541
        nonlocal mod, knots, knots_names, knots_values, knots_jyears, knots_dates, knots_fluxes
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable knots_jyears does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_fluxes does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_dates does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_names does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots_values does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable knots does not seem to be defined.
Loading history...
542
543
        log.debug('Submited with:')
544
        log.debug(f'   selected_knot {selected_knot}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
545
        log.debug(f'   selected_ind {selected_ind}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
546
        log.debug(f'   selected_flux {selected_flux}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
547
        log.debug(f'   selected_date {selected_date}')
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
548
549
        if selected_knot is not None:        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
550
            mod.loc[selected_ind, 'label'] = text.upper()
551
552
            knots = dict(tuple(mod.groupby('label')))
553
            knots_names = list(knots.keys())
554
            knots_values = list(knots.values())
555
            knots_jyears = {k:Time(knots[k]['date'].to_numpy()).jyear for k in knots}
556
            knots_dates = {k:knots[k]['date'].to_numpy() for k in knots}
557
            knots_fluxes = {k:knots[k]['Flux (Jy)'].to_numpy() for k in knots}
558
559
            print(f"Updated index {selected_ind} to {text.upper()}")
560
        else:
561
            pass
562
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
563
        draw_all()
564
565
    def line_select_callback(eclick, erelease):
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (19/15).
Loading history...
566
        nonlocal selected_knot,selected_date, selected_flux, selected_ind
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable selected_flux does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable selected_knot does not seem to be defined.
Loading history...
Coding Style introduced by
Exactly one space required after comma
Loading history...
Comprehensibility Best Practice introduced by
The variable selected_date does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable selected_ind does not seem to be defined.
Loading history...
567
568
        # 1 eclick and erelease are the press and release events
569
        x1, y1 = eclick.xdata, eclick.ydata
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x1" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Variable name "y1" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
570
        x2, y2 = erelease.xdata, erelease.ydata
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x2" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
Variable name "y2" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
571
        log.debug("GUI: (%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
0 ignored issues
show
Coding Style Best Practice introduced by
Use lazy % formatting in logging functions
Loading history...
572
        log.debug("GUI:  The button you used were: %s %s" % (eclick.button, erelease.button))
0 ignored issues
show
Coding Style Best Practice introduced by
Use lazy % formatting in logging functions
Loading history...
573
574
        selected_knot = None
575
        selected_date = None
576
        selected_flux = None
577
        selected_ind = None
578
579
        for i, label in enumerate(knots_names):
0 ignored issues
show
Unused Code introduced by
The variable i seems to be unused.
Loading history...
580
            years = knots_jyears[label]
581
            fluxes = knots_fluxes[label]
582
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
583
            # get points iside current rectangle for current date
584
            rect_idx = (x1 < years) & ( years < x2) & (y1 < fluxes) & ( fluxes < y2) 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
Coding Style introduced by
No space allowed after bracket
Loading history...
585
            
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
586
            if np.sum(rect_idx) == 0:
587
                continue # we did not select any component from this component, next one
588
589
            x = np.array(years)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "x" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
590
            y = np.array(fluxes)
0 ignored issues
show
Coding Style Naming introduced by
Variable name "y" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
591
592 View Code Duplication
            if np.sum(rect_idx) > 0:
0 ignored issues
show
Unused Code introduced by
Unnecessary "else" after "break"
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
593
                textbox.set_val(label)
594
                selected_knot = label
595
                selected_x = x[rect_idx].ravel()
0 ignored issues
show
Unused Code introduced by
The variable selected_x seems to be unused.
Loading history...
596
                selected_y = y[rect_idx].ravel()
0 ignored issues
show
Unused Code introduced by
The variable selected_y seems to be unused.
Loading history...
597
                selected_ind = knots[label].index[rect_idx]
598
                log.debug(f'Selected {label} points  rect_idx {rect_idx} date {x[rect_idx]}, flux {y[rect_idx]} with indices {selected_ind}')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (141/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
introduced by
Use lazy % formatting in logging functions
Loading history...
599
                break # if we find selected components in this epoch, continue with renaming
600
            else:
601
                pass
602
603
        update()
604
605
606 View Code Duplication
    def toggle_selector(event):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
607
        log.debug('GUI: Key pressed.')
608
        if event.key in ['Q', 'q'] and toggle_selector.RS.active:
609
            log.debug('Selector deactivated.')
610
            toggle_selector.RS.set_active(False)
611
        if event.key in ['S', 's'] and not toggle_selector.RS.active:
612
            log.debug('Selector activated.')
613
            toggle_selector.RS.set_active(True)
614
        if event.key in ['R', 'r']:
615
            log.debug('Selector deactivated.')
616
            toggle_selector.RS.set_active(False)
617
            textbox.begin_typing(None)
618
            #textbox.set_val('')
619
620
621
    toggle_selector.RS = RectangleSelector(ax, line_select_callback,
622
                                           drawtype='box', useblit=True,
623
                                           button=[1, 3],  # don't use middle button
624
                                           minspanx=0, minspany=0,
625
                                           spancoords='data',
626
                                           interactive=False)
627
628
629
    fig.canvas.mpl_connect('key_press_event', toggle_selector)
630
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
631
    from mpl_toolkits.axes_grid1 import make_axes_locatable
0 ignored issues
show
introduced by
Unable to import 'mpl_toolkits.axes_grid1'
Loading history...
introduced by
Import outside toplevel (mpl_toolkits.axes_grid1.make_axes_locatable)
Loading history...
632
633
    divider_textbox = make_axes_locatable(ax)
634
    textbox_ax = divider_textbox.append_axes("bottom", size="10%", pad="15%") 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
635
    #textbox_ax = fig.add_axes([0.3,0,0.5,0.05])
636
    textbox = TextBox(textbox_ax, 'Knot name:', initial='None')
637
    textbox.on_submit(submit_textbox)
638
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
639
    draw_all()
640
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
641
    #ax.autoscale()
642
    ax.set_xlabel('date (year)')
643
    ax.set_ylabel('Flux (Jy)')
644
    ax.set_title('Flux from each component')
645
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
646
    xlims = Time(np.amin(mod['date'])).jyear, Time(np.amax(mod['date'])).jyear
647
    ax.set_xlim((xlims[0]-0.03*np.abs(xlims[1]-xlims[0]), xlims[1]+0.03*np.abs(xlims[1]-xlims[0])))
648
    plt.show()
649
650
    return mod
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...