Creates a MaskedRecords from a list of records. Parameters ---------- reclist : sequence A list of records. Each element of the sequence is first converted to a masked array if needed. If a 2D array is passed as argument, it is processed line by line dty
(reclist, dtype=None, shape=None, formats=None, names=None,
titles=None, aligned=False, byteorder=None,
fill_value=None, mask=ma.nomask)
| 512 | |
| 513 | |
| 514 | def fromrecords(reclist, dtype=None, shape=None, formats=None, names=None, |
| 515 | titles=None, aligned=False, byteorder=None, |
| 516 | fill_value=None, mask=ma.nomask): |
| 517 | """ |
| 518 | Creates a MaskedRecords from a list of records. |
| 519 | |
| 520 | Parameters |
| 521 | ---------- |
| 522 | reclist : sequence |
| 523 | A list of records. Each element of the sequence is first converted |
| 524 | to a masked array if needed. If a 2D array is passed as argument, it is |
| 525 | processed line by line |
| 526 | dtype : {None, dtype}, optional |
| 527 | Data type descriptor. |
| 528 | shape : {None,int}, optional |
| 529 | Number of records. If None, ``shape`` is defined from the shape of the |
| 530 | first array in the list. |
| 531 | formats : {None, sequence}, optional |
| 532 | Sequence of formats for each individual field. If None, the formats will |
| 533 | be autodetected by inspecting the fields and selecting the highest dtype |
| 534 | possible. |
| 535 | names : {None, sequence}, optional |
| 536 | Sequence of the names of each field. |
| 537 | fill_value : {None, sequence}, optional |
| 538 | Sequence of data to be used as filling values. |
| 539 | mask : {nomask, sequence}, optional. |
| 540 | External mask to apply on the data. |
| 541 | |
| 542 | Notes |
| 543 | ----- |
| 544 | Lists of tuples should be preferred over lists of lists for faster processing. |
| 545 | |
| 546 | """ |
| 547 | # Grab the initial _fieldmask, if needed: |
| 548 | _mask = getattr(reclist, '_mask', None) |
| 549 | # Get the list of records. |
| 550 | if isinstance(reclist, np.ndarray): |
| 551 | # Make sure we don't have some hidden mask |
| 552 | if isinstance(reclist, ma.MaskedArray): |
| 553 | reclist = reclist.filled().view(np.ndarray) |
| 554 | # Grab the initial dtype, just in case |
| 555 | if dtype is None: |
| 556 | dtype = reclist.dtype |
| 557 | reclist = reclist.tolist() |
| 558 | mrec = np.rec.fromrecords(reclist, dtype=dtype, shape=shape, formats=formats, |
| 559 | names=names, titles=titles, |
| 560 | aligned=aligned, byteorder=byteorder).view(mrecarray) |
| 561 | # Set the fill_value if needed |
| 562 | if fill_value is not None: |
| 563 | mrec.fill_value = fill_value |
| 564 | # Now, let's deal w/ the mask |
| 565 | if mask is not ma.nomask: |
| 566 | mask = np.asarray(mask) |
| 567 | maskrecordlength = len(mask.dtype) |
| 568 | if maskrecordlength: |
| 569 | mrec._mask.flat = mask |
| 570 | elif mask.ndim == 2: |
| 571 | mrec._mask.flat = [tuple(m) for m in mask] |
searching dependent graphs…