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
21 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 |
|
__init__(self)
¶
Initialise the StopWatch
Returns:
Type | Description |
---|---|
None | None. |
Source code in vast_pipeline/utils/utils.py
26 27 28 29 30 31 32 33 34 |
|
reset(self)
¶
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
36 37 38 39 40 41 42 43 44 45 46 47 |
|
reset_init(self)
¶
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
49 50 51 52 53 54 55 56 57 58 59 60 |
|
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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
deg2dms(deg, dms_format=False, precision=2)
¶
Convert angle in degrees into a DMS formatted string. e.g.
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 |
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'
Source code in vast_pipeline/utils/utils.py
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 |
|
deg2hms(deg, hms_format=False, precision=2)
¶
Convert angle in degrees into a HMS formatted string. e.g.
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 |
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'
Source code in vast_pipeline/utils/utils.py
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 |
|
deg2sex(deg)
¶
Converts an angle in degrees to a tuple containing its sexagesimal components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
deg | float | The angle to convert in degrees. | required |
Returns:
Type | Description |
---|---|
int | A nested tuple in the form (sign, (degrees, minutes, seconds)), where sign is |
Tuple[float, float, float] | either -1 or 1. |
Example
deg2sex(12.582438888888889) (12, 34, 56.78000000000182) deg2sex(-12.582438888888889) (-12, 34, 56.78000000000182)
Source code in vast_pipeline/utils/utils.py
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 |
|
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:
Name | Type | Description |
---|---|---|
dict | Dict[Any, Any] | updated dict |
Source code in vast_pipeline/utils/utils.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
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 |
---|---|
Tuple[float, float, float] | The cartesian coordinates. |
Source code in vast_pipeline/utils/utils.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
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:
Name | Type | Description |
---|---|---|
Tuple | float, float | Galactic longitude and latitude in degrees. |
Source code in vast_pipeline/utils/utils.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
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:
Name | Type | Description |
---|---|---|
Tuple | float, float | Right ascension and declination in units of degrees. |
Source code in vast_pipeline/utils/utils.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
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 | pd.DataFrame | input dataframe, no specific columns. | required |
Returns:
Type | Description |
---|---|
pd.DataFrame | The input dataframe with the |
pd.DataFrame | columns downcasted. |
Source code in vast_pipeline/utils/utils.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
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 | pd.DataFrame | Input dataframe, no specific columns. | required |
Returns:
Type | Description |
---|---|
pd.DataFrame | The input dataframe with the |
pd.DataFrame | columns downcasted. |
Source code in vast_pipeline/utils/utils.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
parse_coord(coord_string, coord_frame='icrs')
¶
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.
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, 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 | SkyCoord |
Source code in vast_pipeline/utils/utils.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
Created: March 2, 2022