utils.py
This module contains general pipeline utility functions.
StopWatch
¶
A simple stopwatch to simplify timing code.
Source code in vast_pipeline/utils/utils.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 |
|
__init__()
¶
Initialise the StopWatch
Returns:
Type | Description |
---|---|
None | None. |
Source code in vast_pipeline/utils/utils.py
27 28 29 30 31 32 33 34 35 |
|
reset()
¶
Reset the stopwatch and return the time since last reset (seconds).
Returns:
Type | Description |
---|---|
float | The time in seconds since the last reset. |
Source code in vast_pipeline/utils/utils.py
37 38 39 40 41 42 43 44 45 46 47 48 |
|
reset_init()
¶
Reset the stopwatch and return the total time since initialisation.
Returns:
Type | Description |
---|---|
float | The time in seconds since the initialisation. |
Source code in vast_pipeline/utils/utils.py
50 51 52 53 54 55 56 57 58 59 60 61 |
|
calculate_n_partitions(df, n_cpu, partition_size_mb=15)
¶
This function will calculate how many partitions a dataframe should be split into.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | The pandas dataframe to be partitionined. | required | |
n_cpu | The number of available CPUs. | required | |
partition_size | The optimal partition size in MB. NOTE: The default partition size of 15MB is chosen because many of the parallelised operations on partitioned DataFrames can consume a much larger amount of memory than the size of the partition. 15MB avoids consuming too much memory for significant amounts of parallelism (e.g. n_cpu > 10) without significant cost to processing speed. | required |
Returns:
Type | Description |
---|---|
The optimal number of partitions. |
Source code in vast_pipeline/utils/utils.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
calculate_workers_and_partitions(df, n_cpu=None, max_partition_mb=15)
¶
Return number of workers and the number of partitions for Dask
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | The pandas dataframe to be partitionined. Don't calculate partitions if df is None | required | |
num_cpu_max | The maximum number of workers to allocate. The default of None means use one less than all available cores | required | |
max_partition_mb | The maximum partition size in MB. | 15 |
Returns:
Type | Description |
---|---|
(num_workers, n_partitions) | Calculated workers and partitions. |
Source code in vast_pipeline/utils/utils.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
|
check_read_write_perm(path, perm='W')
¶
Assess the file permission on a path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path | str | The system path to assess. | required |
perm | str | The permission to check for. | 'W' |
Returns:
Type | Description |
---|---|
None | None |
Raises:
Type | Description |
---|---|
IOError | The permission is not valid on the checked directory. |
Source code in vast_pipeline/utils/utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
deg2dms(deg, dms_format=False, precision=2, truncate=False, latitude=True)
¶
Convert angle in degrees into a DMS formatted string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
deg | float | The angle to convert in degrees. | required |
dms_format | optional | If | False |
precision | optional | Floating point precision of the arcseconds component. Can be 0 or a positive integer. Negative values will be interpreted as 0. Defaults to 2. | 2 |
truncate | optional | Truncate values after the decimal point instead of rounding. Defaults to False (rounding). | False |
latitude | optional | The input | True |
Returns:
Type | Description |
---|---|
str |
|
Example
deg2dms(12.582438888888889) '+12:34:56.78' deg2dms(2.582438888888889, dms_format=True) '+02d34m56.78s' deg2dms(-12.582438888888889, precision=1) '-12:34:56.8' deg2dms(-12.582438888888889, precision=1, truncate=True) '-12:34:56.7'
Source code in vast_pipeline/utils/utils.py
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 |
|
deg2hms(deg, hms_format=False, precision=2, truncate=False, longitude=True)
¶
Convert angle in degrees into a HMS formatted string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
deg | float | The angle to convert in degrees. | required |
hms_format | optional | If | False |
precision | optional | Floating point precision of the seconds component. Can be 0 or a positive integer. Negative values will be interpreted as 0. Defaults to 2. | 2 |
truncate | optional | Truncate values after the decimal point instead of rounding. Defaults to False (rounding). | False |
longitude | optional | The input | True |
Returns:
Type | Description |
---|---|
str |
|
Example
deg2hms(188.73658333333333) '12:34:56.78' deg2hms(-188.73658333333333, hms_format=True) '12h34m56.78s' deg2hms(188.73658333333333, precision=1) '12:34:56.8' deg2hms(188.73658333333333, precision=1, truncate=True) '12:34:56.7'
Source code in vast_pipeline/utils/utils.py
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 |
|
dict_merge(dct, merge_dct, add_keys=True)
¶
Recursive dict merge. Inspired by dict.update(), instead of updating only top-level keys, dict_merge recurses down into dicts nested to an arbitrary depth, updating keys. The merge_dct
is merged into dct
.
This version will return a copy of the dictionary and leave the original arguments untouched.
The optional argument add_keys
, determines whether keys which are present in merge_dict
but not dct
should be included in the new dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dct | dict | onto which the merge is executed | required |
merge_dct | dict | dct merged into dct | required |
add_keys | bool | whether to add new keys | True |
Returns:
Type | Description |
---|---|
Dict[Any, Any] | Updated dict. |
Source code in vast_pipeline/utils/utils.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
eq_to_cart(ra, dec)
¶
Find the cartesian co-ordinates on the unit sphere given the eq. co-ords. ra, dec should be in degrees.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ra | float | The right ascension coordinate, in degrees, to convert. | required |
dec | float | The declination coordinate, in degrees, to convert. | required |
Returns:
Type | Description |
---|---|
float | The cartesian x coordinate. |
float | The cartesian y coordinate. |
float | The cartesian z coordinate. |
Source code in vast_pipeline/utils/utils.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
equ2gal(ra, dec)
¶
Convert equatorial coordinates to galactic
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ra | float | Right ascension in units of degrees. | required |
dec | float | Declination in units of degrees. | required |
Returns:
Type | Description |
---|---|
float | Galactic longitude in degrees. |
float | Galactic latitude in degrees. |
Source code in vast_pipeline/utils/utils.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
gal2equ(l, b)
¶
Convert galactic coordinates to equatorial.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
l | float | Galactic longitude in degrees. | required |
b | float | Galactic latitude in degrees. | required |
Returns:
Type | Description |
---|---|
float | Right ascension in degrees. |
float | Declination in degrees. |
Source code in vast_pipeline/utils/utils.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
optimize_floats(df)
¶
Downcast float columns in a pd.DataFrame to the smallest data type without losing any information.
Credit to Robbert van der Gugten.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | input dataframe, no specific columns. | required |
Returns:
Type | Description |
---|---|
DataFrame | The input dataframe with the |
Source code in vast_pipeline/utils/utils.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
optimize_ints(df)
¶
Downcast integer columns in a pd.DataFrame to the smallest data type without losing any information.
Credit to Robbert van der Gugten.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Input dataframe, no specific columns. | required |
Returns:
Type | Description |
---|---|
DataFrame | The input dataframe with the |
Source code in vast_pipeline/utils/utils.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
parse_coord(coord_string, coord_frame='icrs')
¶
Parse a coordinate string and return a SkyCoord. The units may be expressed within coord_string
e.g. "21h52m03.1s -62d08m19.7s", "18.4d +43.1d". If no units are given, the following assumptions are made: - if both coordinate components are decimals, they are assumed to be in degrees. - if a sexagesimal coordinate is given and the frame is galactic, both components are assumed to be in degrees. For any other frame, the first component is assumed to be in hourangles and the second in degrees. Will raise a ValueError if SkyCoord is unable to parse coord_string
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coord_string | str | The coordinate string to parse. | required |
coord_frame | str | The frame of | 'icrs' |
Returns:
Type | Description |
---|---|
SkyCoord | The SkyCoord object. |
Source code in vast_pipeline/utils/utils.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|