GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 69efa7...3bf7f5 )
by Sabiha
02:48 queued 11s
created

read_file()   C

Complexity

Conditions 10

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
nop 1
dl 0
loc 46
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like voltcycle.functions_and_tests.file_read.read_file() 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
import pandas as pd
2
import numpy as np
3
import matplotlib.pyplot as plt
4
import copy
5
from matplotlib import rcParams
6
7
def read_cycle(data):
8
    """This function reads a segment of datafile (corresponding a cycle)
9
    and generates a dataframe with columns 'Potential' and 'Current'
10
11
    Parameters
12
    __________
13
    data: segment of data file
14
15
    Returns
16
    _______
17
    A dataframe with potential and current columns  
18
    """     
19
20
    current = []
21
    potential = []
22
    for i in data[3:]:
23
        current.append(float(i.split("\t")[4]))
24
        potential.append(float(i.split("\t")[3]))
25
    zippedList = list(zip(potential, current))
26
    df = pd.DataFrame(zippedList, columns = ['Potential' , 'Current'])
27
    return df
28
29
30
def read_file(file):
31
    """This function reads the raw data file, gets the scanrate and stepsize
32
    and then reads the lines according to cycle number. Once it reads the data
33
    for one cycle, it calls read_cycle function to denerate a dataframe. It 
34
    does the same thing for all the cycles and finally returns a dictionary,
35
    the keys of which are the cycle numbers and the values are the 
36
    corresponding dataframes.
37
38
    Parameters
39
    __________
40
    file: raw data file
41
42
    Returns:
43
    ________
44
    dict_of_df: dictionary of dataframes with keys = cycle numbers and
45
    values = dataframes for each cycle
46
    n_cycle: number of cycles in the raw file  
47
    """   
48
    dict_of_df = {} 
49
    h = 0
50
    l = 0
51
    n_cycle = 0
52
    #a = []
53
    with open(file, 'rt') as f:
54
        print(file + ' Opened')
55
        for line in f:
56
            record = 0
57
            if not (h and l):
58
                if line.startswith('SCANRATE'):
59
                    scan_rate = float(line.split()[2])
60
                    h = 1
61
                if line.startswith('STEPSIZE'):
62
                    step_size = float(line.split()[2])
63
                    l = 1
64
            if line.startswith('CURVE'):
65
                n_cycle += 1
66
                if n_cycle > 1:
67
                    number = n_cycle - 1
68
                    df = read_cycle(a)
0 ignored issues
show
introduced by
The variable a does not seem to be defined for all execution paths.
Loading history...
69
                    key_name = 'cycle_' + str(number)
70
                    #key_name = number
71
                    dict_of_df[key_name] = copy.deepcopy(df)
72
                a = []
73
            if n_cycle:
74
                a.append(line)
75
    return dict_of_df, number
0 ignored issues
show
introduced by
The variable number does not seem to be defined for all execution paths.
Loading history...
76
77
78
#df = pd.DataFrame(list(dict1['df_1'].items()))
79
#list1, list2 = list(dict1['df_1'].items())
80
#list1, list2 = list(dict1.get('df_'+str(1)))
81
82
def data_frame(dict_cycle, n):
83
    """Reads the dictionary of dataframes and returns dataframes for each cycle
84
85
    Parameters
86
    __________
87
    dict_cycle: Dictionary of dataframes
88
    n: cycle number
89
90
    Returns:
91
    _______
92
    Dataframe correcponding to the cycle number 
93
    """
94
    list1, list2 = (list(dict_cycle.get('cycle_'+str(n)).items()))
95
    zippedList = list(zip(list1[1], list2[1]))
96
    data  = pd.DataFrame(zippedList, columns = ['Potential' , 'Current'])
97
    return data
98
    
99 View Code Duplication
def plot_fig(dict_cycle, n):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
100
    """For basic plotting of the cycle data
101
  
102
    Parameters
103
    __________
104
    dict: dictionary of dataframes for all the cycles
105
    n: number of cycles
106
107
    Saves the plot in a file called cycle.png 
108
    """
109
110
    for i in range(n):
111
        print(i+1)
112
        df = data_frame(dict_cycle, i+1)
113
        plt.plot(df.Potential, df.Current, label = "Cycle{}".format(i+1))
114
        
115
    print(df.head())
0 ignored issues
show
introduced by
The variable df does not seem to be defined in case the for loop on line 110 is not entered. Are you sure this can never be the case?
Loading history...
116
    plt.xlabel('Voltage')
117
    plt.ylabel('Current')
118
    plt.legend()
119
    plt.savefig('cycle.png')
120
    print('executed')
121