SQL Database
DisruptionPy uses logbook sql databases for convenience when retrieving data from MDSPlus. Users may also use DisruptionPy to directly retrieve data from the logbook database's disruption_warning
tables.
The disruption_warning
table¤
The disruption_warning
sql tables for CMod and DIII-D contain important disruption parameters for a large number of shots.
CMod Dataset¤
The dataset contains unique plasma discharges from MIT's Alcator C-Mod tokamak, from the 2012 to 2016 experimental campaigns, plus additional discharges from 2005.
Available columns on CMod
'dbkey', 'shot', 'time', 'time_until_disrupt', 'ip_error', 'dip_dt',
'beta_p', 'beta_n', 'li', 'n_equal_1_normalized', 'z_error', 'v_z',
'z_times_v_z', 'kappa', 'pressure_peaking', 'H98', 'q0', 'qstar', 'q95',
'v_0', 'v_mid', 'v_edge', 'dn_dt', 'p_rad_slow', 'p_oh_slow', 'p_icrf',
'p_lh', 'radiated_fraction', 'power_supply_railed', 'v_loop_efit',
'r_dd', 'lower_gap', 'upper_gap', 'dbetap_dt', 'dli_dt', 'ip', 'zcur',
'n_e', 'dipprog_dt', 'v_loop', 'p_rad', 'p_oh', 'ssep', 'dWmhd_dt',
'dprad_dt', 'v_0_uncalibrated', 'Te_width', 'Greenwald_fraction',
'intentional_disruption', 'Te_width_ECE', 'Wmhd', 'n_over_ncrit',
'n_equal_1_mode', 'Mirnov', 'Mirnov_norm_btor', 'Mirnov_norm_bpol',
'Te_peaking', 'ne_peaking', 'Te_peaking_ECE', 'SXR_peaking',
'kappa_area', 'I_efc', 'SXR', 'H_alpha', 'Prad_peaking_CVA'
For more details on computed values please see parameter reference.
Retrieving data from the SQL database¤
Here is an example retrieving data from disruption_warning
or disruptions
table
#!/usr/bin/env python3
"""
example module for SQL.
"""
from disruption_py.machine.tokamak import Tokamak, resolve_tokamak_from_environment
from disruption_py.workflow import get_database
def main():
"""
execute a few meaningful queries to test DB connection.
"""
queries = [
"select count(distinct shot) from disruption_warning",
"select count(distinct shot) from disruption_warning"
+ " where shot not in (select shot from disruptions)",
"select count(distinct shot) from disruption_warning"
+ " where shot in (select shot from disruptions)",
"select count(distinct shot) from disruptions",
]
tokamak = resolve_tokamak_from_environment()
db = get_database(tokamak=tokamak)
if tokamak is Tokamak.D3D:
vals = [13245, 8055, 5190, 24219]
elif tokamak is Tokamak.CMOD:
vals = [10435, 6640, 3795, 13785]
elif tokamak is Tokamak.EAST:
vals = [18568, 9875, 8693, 30482]
else:
raise ValueError(f"Unspecified or unsupported tokamak: {tokamak}.")
print(f"Initialized DB: {db.user}@{db.host}/{db.db_name}")
print("Version:", db.get_version())
while queries:
query = queries.pop(0)
print(">", query.strip(" "))
out = db.query(query)
print("=", out.shape)
print(out.iloc[0] if out.shape[0] == 1 else out, "\n")
if vals:
assert out.iloc[0, 0] == vals.pop(0)
if __name__ == "__main__":
main()
Database Class Reference¤
Module for managing SQL database connections.
DummyDatabase ¤
Bases: ShotDatabase
A database class that does not require connecting to an SQL server and returns no data.
Source code in disruption_py/inout/sql.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
DummyObject ¤
A dummy connection object.
Source code in disruption_py/inout/sql.py
236 237 238 239 240 241 242 243 244 245 246 247 |
|
ShotDatabase ¤
Handles grabbing data from MySQL server.
Source code in disruption_py/inout/sql.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
conn
property
¤
conn
Property returning a connection to sql database.
If a connection exists for the given thread returns that connection, otherwise creates a new connection
RETURNS | DESCRIPTION |
---|---|
_type_
|
Database connection |
from_config
classmethod
¤
from_config(tokamak: Tokamak)
Initialize database from config.
Source code in disruption_py/inout/sql.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
get_disruption_time ¤
get_disruption_time(shot_id)
Get disruption time for shot_id or None if there was no disruption.
Source code in disruption_py/inout/sql.py
223 224 225 226 227 228 229 230 231 232 233 |
|
get_shots_data ¤
get_shots_data(
shotlist: List[int],
cols: List[str] = None,
sql_table="disruption_warning",
)
get_shots_data retrieves columns from sql data for given shotlist
PARAMETER | DESCRIPTION |
---|---|
shotlist
|
List of shot ids to get data for.
TYPE:
|
cols
|
List of columns to retrieve. Default value is ["*"], meaning all columns.
TYPE:
|
sql_table
|
The sql_table to retrieve data from. Default value is "disruption_warning".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dataframe
|
Dataframe containing queried data |
Source code in disruption_py/inout/sql.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
get_version ¤
get_version()
Query the version of the SQL database.
Source code in disruption_py/inout/sql.py
178 179 180 181 182 183 184 185 |
|
query ¤
query(query: str, use_pandas=True)
query sql database
PARAMETER | DESCRIPTION |
---|---|
query
|
The query string
TYPE:
|
use_pandas
|
Whether pd.read_sql_query should be used to run the query. Default value is true.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Any
|
Result of query |
Source code in disruption_py/inout/sql.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|