@@ -2302,147 +2302,112 @@ def get_instancemethod(self):
2302
2302
return getattr (self .parent_obj , self .instancemethod_name )
2303
2303
2304
2304
2305
- def _step_validation (x , * args ):
2306
- """
2307
- Helper function of `pts_to_*step` functions
2308
-
2309
- This function does all of the normalization required to the
2310
- input and generate the template for output
2311
-
2312
-
2313
- """
2314
- args = tuple (np .asanyarray (y ) for y in args )
2315
- x = np .asanyarray (x )
2316
- if x .ndim != 1 :
2317
- raise ValueError ("x must be 1 dimensional" )
2318
- if len (args ) == 0 :
2319
- raise ValueError ("At least one Y value must be passed" )
2320
-
2321
- return np .vstack ((x , ) + args )
2322
-
2323
-
2324
2305
def pts_to_prestep (x , * args ):
2325
2306
"""
2326
- Covert continuous line to pre-steps
2307
+ Convert continuous line to pre-steps.
2327
2308
2328
- Given a set of N points convert to 2 N -1 points
2329
- which when connected linearly give a step function
2330
- which changes values at the beginning of the intervals.
2309
+ Given a set of ``N`` points, convert to ``2N - 1`` points, which when
2310
+ connected linearly give a step function which changes values at the
2311
+ beginning of the intervals.
2331
2312
2332
2313
Parameters
2333
2314
----------
2334
2315
x : array
2335
- The x location of the steps
2316
+ The x location of the steps.
2336
2317
2337
- y1, y2, ... : array
2338
- Any number of y arrays to be turned into steps.
2339
- All must be the same length as ``x``
2318
+ y1, ..., yp : array
2319
+ y arrays to be turned into steps; all must be the same length as ``x``.
2340
2320
2341
2321
Returns
2342
2322
-------
2343
- x, y1, y2, .. : array
2344
- The x and y values converted to steps in the same order
2345
- as the input. If the input is length ``N``, each of these arrays
2346
- will be length ``2N + 1``
2347
-
2323
+ out : array
2324
+ The x and y values converted to steps in the same order as the input;
2325
+ can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
2326
+ length ``N``, each of these arrays will be length ``2N + 1``.
2348
2327
2349
2328
Examples
2350
2329
--------
2351
2330
>> x_s, y1_s, y2_s = pts_to_prestep(x, y1, y2)
2352
2331
"""
2353
- # do normalization
2354
- vertices = _step_validation (x , * args )
2355
- # create the output array
2356
- steps = np .zeros ((vertices .shape [0 ], 2 * len (x ) - 1 ), float )
2357
- # do the to step conversion logic
2358
- steps [0 , 0 ::2 ], steps [0 , 1 ::2 ] = vertices [0 , :], vertices [0 , :- 1 ]
2359
- steps [1 :, 0 ::2 ], steps [1 :, 1 :- 1 :2 ] = vertices [1 :, :], vertices [1 :, 1 :]
2360
- # convert 2D array back to tuple
2361
- return tuple (steps )
2332
+ steps = np .zeros ((1 + len (args ), 2 * len (x ) - 1 ))
2333
+ # In all `pts_to_*step` functions, only assign *once* using `x` and `args`,
2334
+ # as converting to an array may be expensive.
2335
+ steps [0 , 0 ::2 ] = x
2336
+ steps [0 , 1 ::2 ] = steps [0 , 0 :- 2 :2 ]
2337
+ steps [1 :, 0 ::2 ] = args
2338
+ steps [1 :, 1 ::2 ] = steps [1 :, 2 ::2 ]
2339
+ return steps
2362
2340
2363
2341
2364
2342
def pts_to_poststep (x , * args ):
2365
2343
"""
2366
- Covert continuous line to pre -steps
2344
+ Convert continuous line to post -steps.
2367
2345
2368
- Given a set of N points convert to 2 N -1 points
2369
- which when connected linearly give a step function
2370
- which changes values at the end of the intervals.
2346
+ Given a set of ``N`` points convert to ``2N + 1`` points, which when
2347
+ connected linearly give a step function which changes values at the end of
2348
+ the intervals.
2371
2349
2372
2350
Parameters
2373
2351
----------
2374
2352
x : array
2375
- The x location of the steps
2353
+ The x location of the steps.
2376
2354
2377
- y1, y2, ... : array
2378
- Any number of y arrays to be turned into steps.
2379
- All must be the same length as ``x``
2355
+ y1, ..., yp : array
2356
+ y arrays to be turned into steps; all must be the same length as ``x``.
2380
2357
2381
2358
Returns
2382
2359
-------
2383
- x, y1, y2, .. : array
2384
- The x and y values converted to steps in the same order
2385
- as the input. If the input is length ``N``, each of these arrays
2386
- will be length ``2N + 1``
2387
-
2360
+ out : array
2361
+ The x and y values converted to steps in the same order as the input;
2362
+ can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
2363
+ length ``N``, each of these arrays will be length ``2N + 1``.
2388
2364
2389
2365
Examples
2390
2366
--------
2391
2367
>> x_s, y1_s, y2_s = pts_to_poststep(x, y1, y2)
2392
2368
"""
2393
- # do normalization
2394
- vertices = _step_validation (x , * args )
2395
- # create the output array
2396
- steps = np .zeros ((vertices .shape [0 ], 2 * len (x ) - 1 ), float )
2397
- # do the to step conversion logic
2398
- steps [0 , ::2 ], steps [0 , 1 :- 1 :2 ] = vertices [0 , :], vertices [0 , 1 :]
2399
- steps [1 :, 0 ::2 ], steps [1 :, 1 ::2 ] = vertices [1 :, :], vertices [1 :, :- 1 ]
2400
-
2401
- # convert 2D array back to tuple
2402
- return tuple (steps )
2369
+ steps = np .zeros ((1 + len (args ), 2 * len (x ) - 1 ))
2370
+ steps [0 , 0 ::2 ] = x
2371
+ steps [0 , 1 ::2 ] = steps [0 , 2 ::2 ]
2372
+ steps [1 :, 0 ::2 ] = args
2373
+ steps [1 :, 1 ::2 ] = steps [1 :, 0 :- 2 :2 ]
2374
+ return steps
2403
2375
2404
2376
2405
2377
def pts_to_midstep (x , * args ):
2406
2378
"""
2407
- Covert continuous line to pre -steps
2379
+ Convert continuous line to mid -steps.
2408
2380
2409
- Given a set of N points convert to 2 N -1 points
2410
- which when connected linearly give a step function
2411
- which changes values at the middle of the intervals.
2381
+ Given a set of ``N`` points convert to ``2N`` points which when connected
2382
+ linearly give a step function which changes values at the middle of the
2383
+ intervals.
2412
2384
2413
2385
Parameters
2414
2386
----------
2415
2387
x : array
2416
- The x location of the steps
2388
+ The x location of the steps.
2417
2389
2418
- y1, y2, ... : array
2419
- Any number of y arrays to be turned into steps.
2420
- All must be the same length as ``x``
2390
+ y1, ..., yp : array
2391
+ y arrays to be turned into steps; all must be the same length as ``x``.
2421
2392
2422
2393
Returns
2423
2394
-------
2424
- x, y1, y2, .. : array
2425
- The x and y values converted to steps in the same order
2426
- as the input. If the input is length ``N``, each of these arrays
2427
- will be length ``2N + 1``
2428
-
2395
+ out : array
2396
+ The x and y values converted to steps in the same order as the input;
2397
+ can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
2398
+ length ``N``, each of these arrays will be length ``2N``.
2429
2399
2430
2400
Examples
2431
2401
--------
2432
2402
>> x_s, y1_s, y2_s = pts_to_midstep(x, y1, y2)
2433
2403
"""
2434
- # do normalization
2435
- vertices = _step_validation (x , * args )
2436
- # create the output array
2437
- steps = np .zeros ((vertices .shape [0 ], 2 * len (x )), float )
2438
- steps [0 , 1 :- 1 :2 ] = 0.5 * (vertices [0 , :- 1 ] + vertices [0 , 1 :])
2439
- steps [0 , 2 ::2 ] = 0.5 * (vertices [0 , :- 1 ] + vertices [0 , 1 :])
2440
- steps [0 , 0 ] = vertices [0 , 0 ]
2441
- steps [0 , - 1 ] = vertices [0 , - 1 ]
2442
- steps [1 :, 0 ::2 ], steps [1 :, 1 ::2 ] = vertices [1 :, :], vertices [1 :, :]
2443
-
2444
- # convert 2D array back to tuple
2445
- return tuple (steps )
2404
+ steps = np .zeros ((1 + len (args ), 2 * len (x )))
2405
+ x = np .asanyarray (x )
2406
+ steps [0 , 1 :- 1 :2 ] = steps [0 , 2 ::2 ] = (x [:- 1 ] + x [1 :]) / 2
2407
+ steps [0 , 0 ], steps [0 , - 1 ] = x [0 ], x [- 1 ]
2408
+ steps [1 :, 0 ::2 ] = args
2409
+ steps [1 :, 1 ::2 ] = steps [1 :, 0 ::2 ]
2410
+ return steps
2446
2411
2447
2412
2448
2413
STEP_LOOKUP_MAP = {'default' : lambda x , y : (x , y ),
0 commit comments