(array, name, format)
| 712 | |
| 713 | |
| 714 | def array_to_meta_xml(array, name, format): |
| 715 | type = array.dtype.kind |
| 716 | slice = name |
| 717 | l = len(array.shape) |
| 718 | |
| 719 | # initial load, compute slice |
| 720 | if format == "%": |
| 721 | if l > 2: |
| 722 | slice += "[0]" * (l - 2) |
| 723 | for r in range(l - 2): |
| 724 | array = array[0] |
| 725 | if type == "f": |
| 726 | format = ".5f" |
| 727 | elif type == "i" or type == "u": |
| 728 | format = "d" |
| 729 | else: |
| 730 | format = "s" |
| 731 | else: |
| 732 | format = format.replace("%", "") |
| 733 | |
| 734 | l = len(array.shape) |
| 735 | reslice = "" |
| 736 | if l > 2: |
| 737 | raise Exception("%s has more than 2 dimensions." % slice) |
| 738 | elif l == 1: |
| 739 | # special case with 1D arrays arr[i, :] - row, but arr[:, i] - column with equal shape and ndim |
| 740 | # http://stackoverflow.com/questions/16837946/numpy-a-2-rows-1-column-file-loadtxt-returns-1row-2-columns |
| 741 | # explanation: http://stackoverflow.com/questions/15165170/how-do-i-maintain-row-column-orientation-of-vectors-in-numpy?rq=1 |
| 742 | # we use kind of a hack - get information about memory from C_CONTIGUOUS |
| 743 | is_row = array.flags["C_CONTIGUOUS"] |
| 744 | |
| 745 | if is_row: |
| 746 | rows = 1 |
| 747 | cols = min(len(array), MAX_SLICE_SIZE) |
| 748 | if cols < len(array): |
| 749 | reslice = "[0:%s]" % (cols) |
| 750 | array = array[0:cols] |
| 751 | else: |
| 752 | cols = 1 |
| 753 | rows = min(len(array), MAX_SLICE_SIZE) |
| 754 | if rows < len(array): |
| 755 | reslice = "[0:%s]" % (rows) |
| 756 | array = array[0:rows] |
| 757 | elif l == 2: |
| 758 | rows = min(array.shape[-2], MAX_SLICE_SIZE) |
| 759 | cols = min(array.shape[-1], MAX_SLICE_SIZE) |
| 760 | if cols < array.shape[-1] or rows < array.shape[-2]: |
| 761 | reslice = "[0:%s, 0:%s]" % (rows, cols) |
| 762 | array = array[0:rows, 0:cols] |
| 763 | |
| 764 | # avoid slice duplication |
| 765 | if not slice.endswith(reslice): |
| 766 | slice += reslice |
| 767 | |
| 768 | bounds = (0, 0) |
| 769 | if type in "biufc": |
| 770 | bounds = (array.min(), array.max()) |
| 771 | xml = '<array slice="%s" rows="%s" cols="%s" format="%s" type="%s" max="%s" min="%s"/>' % ( |
no outgoing calls
no test coverage detected