• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timcera / tstoolbox / 3270623368

pending completion
3270623368

push

github

Tim Cera
test: commented out two tests that only fail on github

3016 of 5045 relevant lines covered (59.78%)

2.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.14
/src/tstoolbox/skill_metrics/write_taylor_stats.py
1
import os
4✔
2

3
import xlsxwriter
4✔
4

5

6
def write_taylor_stats(filename, data, **kwargs):
4✔
7
    """
8
    Write statistics used in a target diagram to an Excel file.
9

10
    This function writes to an Excel file FILENAME the statistics
11
    used to create a Taylor diagram for each of the dictionaries
12
    contained in DATA. The first 2 arguments must be the inputs as
13
    described below followed by keyword arguments in the format of
14
    OPTION = VALUE.
15

16
    INPUTS:
17
    filename      : name for statistics Excel file
18
    data          : a dictionary containing the statistics
19
    data['sdev']  : Standard deviations (sigma)
20
    data['crmsd'] : Centered Root Mean Square Difference (CRMSD)
21
    data['ccoef'] : Correlation Coefficient (r)
22

23
    OUTPUTS:
24
        None.
25

26
    LIST OF OPTIONS:
27
      A title description for each dictionary (TITLE) can be
28
    optionally provided as well as a LABEL for each data point in
29
    the diagram.
30

31
    label = label : label for each data point in target diagram, e.g.
32
                    'OC445 (CB)'
33
    overwrite = boolean : true/false flag to overwrite Excel file
34
    title = title : title descriptor data set, e.g. 'Expt. 01.0'
35

36
    Author: Peter A. Rochford
37
        Symplectic, LLC
38
        www.thesymplectic.com
39
        prochford@thesymplectic.com
40

41
    Created on Dec 12, 2016
42
    """
43
    option = get_write_taylor_stats_options(**kwargs)
×
44

45
    # Check for existence of file
46
    if os.path.isfile(filename):
×
47
        if option["overwrite"]:
×
48
            os.remove(filename)
×
49
        else:
50
            ValueError(f"File already exists: {filename}")
×
51

52
    # Convert data to list if necessary
53
    if not isinstance(data, list):
×
54
        data = [data]
×
55

56
    # Write title information to file
57
    workbook = xlsxwriter.Workbook(filename)
×
58
    worksheet = workbook.add_worksheet()
×
59

60
    # Write title information to file
61
    worksheet.write(1, 0, "Taylor Statistics")
×
62

63
    # Determine number of dictionaries in data variable
64
    ncell = len(data)
×
65

66
    # Write data for each dictionary
67
    row = 2
×
68
    headers = ["Description", "Standard Deviation", "CRMSD", "Correlation Coeff."]
×
69
    for i in range(ncell):
×
70
        row += 1
×
71
        if len(option["title"]) > 0:
×
72
            worksheet.write(row, 0, option["title"][i])
×
73

74
        # Write column headers
75
        row += 1
×
76
        for j, h in enumerate(headers):
×
77
            worksheet.write(row, j, h)
×
78

79
        # Retrieve input values as list
80
        try:
×
81
            iter(data[i]["sdev"])
×
82
        except TypeError:
×
83
            sdev = [data[i]["sdev"]]
×
84
            crmsd = [data[i]["crmsd"]]
×
85
            ccoef = [data[i]["ccoef"]]
×
86
        else:
87
            sdev = data[i]["sdev"]
×
88
            crmsd = data[i]["crmsd"]
×
89
            ccoef = data[i]["ccoef"]
×
90
        ndata = len(sdev)
×
91

92
        # Write each row of data
93
        row += 1
×
94
        for j in range(ndata):
×
95
            if len(option["label"]) > 0:
×
96
                worksheet.write(row, 0, option["label"][j])
×
97

98
            worksheet.write(row, 1, sdev[j])
×
99
            worksheet.write(row, 2, crmsd[j])
×
100
            worksheet.write(row, 3, ccoef[j])
×
101
            row += 1
×
102

103
    workbook.close()
×
104

105

106
def get_write_taylor_stats_options(**kwargs):
4✔
107
    """
108
    Get optional arguments for write_taylor_stats function.
109

110
    Retrieves the keywords supplied to the WRITE_TARGET_STATS
111
    function (**KWARGS), and returns the values in a OPTION dictionary.
112
    Default values are assigned to selected optional arguments. The
113
    function will terminate with an error if an unrecognized optional
114
    argument is supplied.
115

116
    INPUTS:
117
    **kwargs : keyword argument list
118

119
    OUTPUTS:
120
    option : data structure containing option values.
121
    option['title']     : title descriptor for data set.
122
    option['overwrite'] : boolean to overwrite Excel file.
123

124
    LIST OF OPTIONS:
125
      A title description for each dataset TITLE can be optionally
126
    provided as well as an overwrite option if the file name currently
127
    exists.
128

129
    label = label : label for each data point in target diagram, e.g.
130
                    'OC445 (CB)'
131
    overwrite = boolean : true/false flag to overwrite Excel file
132
    title = title : title descriptor for each data set in data, e.g.
133
                   'Expt. 01.0'
134

135
    Author: Peter A. Rochford
136
    Acorn Science & Innovation
137
    prochford@acornsi.com
138

139
    Created on Dec 10, 2016
140

141
    @author: rochfordp
142
    """
143
    from . import check_on_off
×
144

145
    nargin = len(kwargs)
×
146

147
    #  Set default parameters
148
    option = {"overwrite": False, "label": [], "title": ""}
×
149
    if nargin == 0:
×
150
        # No options requested, so return with only defaults
151
        return option
×
152

153
    # Load custom options, storing values in option data structure
154

155
    # Check for valid keys and values in dictionary
156
    for optname, optvalue in kwargs.items():
×
157
        optname = optname.lower()
×
158
        if optname not in option:
×
159
            raise ValueError(f"Unrecognized option: {optname}")
×
160
        # Replace option value with that from arguments
161
        option[optname] = optvalue
×
162

163
        # Check values for specific options
164
        if optname == "overwrite":
×
165
            option["overwrite"] = check_on_off(option["overwrite"])
×
166

167
    return option
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc