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.
Passed
Push — master ( de7c60...c9fd59 )
by Keertana
02:19
created

functions_and_tests.file_read.read_file()   C

Complexity

Conditions 10

Size

Total Lines 46
Code Lines 27

Duplication

Lines 46
Ratio 100 %

Importance

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

How to fix   Complexity   

Complexity

Complex classes like 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
"""This module test the file reading functions."""
2
3
import copy
4
import pandas as pd
5
import matplotlib.pyplot as plt
6
7
8 View Code Duplication
def read_cycle(data):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
    """This function reads a segment of datafile (corresponding a cycle)
10
    and generates a dataframe with columns 'Potential' and 'Current'
11
12
    Parameters
13
    __________
14
    data: segment of data file
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
    zipped_list = list(zip(potential, current))
26
    dataframe = pd.DataFrame(zipped_list, columns=['Potential', 'Current'])
27
    return dataframe
28
29
30 View Code Duplication
def read_file(file):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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_val = 0
50
    l_val = 0
51
    n_cycle = 0
52
    #a = []
53
    with open(file, 'rt') as f_val:
54
        print(file + ' Opened')
55
        for line in f_val:
56
            record = 0
57
            if not (h_val and l_val):
58
                if line.startswith('SCANRATE'):
59
                    scan_rate = float(line.split()[2])
60
                    h_val = 1
61
                if line.startswith('STEPSIZE'):
62
                    step_size = float(line.split()[2])
63
                    l_val = 1
64
            if line.startswith('CURVE'):
65
                n_cycle += 1
66
                if n_cycle > 1:
67
                    number = n_cycle - 1
68
                    data = read_cycle(a_val)
0 ignored issues
show
introduced by
The variable a_val 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(data)
72
                a_val = []
73
            if n_cycle:
74
                a_val.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 View Code Duplication
def data_frame(dict_cycle, number):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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(number)).items()))
95
    zipped_list = list(zip(list1[1], list2[1]))
96
    data = pd.DataFrame(zipped_list, columns=['Potential', 'Current'])
97
    return data
98
99 View Code Duplication
def plot_fig(dict_cycle, number):
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(number):
111
        print(i+1)
112
        data = data_frame(dict_cycle, i+1)
113
        plt.plot(data.Potential, data.Current, label="Cycle{}".format(i+1))
114
115
    print(data.head())
0 ignored issues
show
introduced by
The variable data 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