| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,44 +131,6 @@ public static float time() | |||
| 131 | 131 | } | |
| 132 | 132 | } | |
| 133 | 133 | ||
| 134 | - /// <summary> | ||
| 135 | - /// Untyped implementation of zip for arbitrary data | ||
| 136 | - /// | ||
| 137 | - /// Converts an list of lists or arrays [[1,2,3], [4,5,6], [7,8,9]] into a list of arrays | ||
| 138 | - /// representing tuples of the same index of all source arrays [[1,4,7], [2,5,9], [3,6,9]] | ||
| 139 | - /// </summary> | ||
| 140 | - /// <param name="lists">one or multiple sequences to be zipped</param> | ||
| 141 | - /// <returns></returns> | ||
| 142 | - public static IEnumerable<object[]> zip(params object[] lists) | ||
| 143 | - { | ||
| 144 | - if (lists.Length == 0) | ||
| 145 | - yield break; | ||
| 146 | - var first = lists[0]; | ||
| 147 | - if (first == null) | ||
| 148 | - yield break; | ||
| 149 | - var arity = (first as IEnumerable).OfType<object>().Count(); | ||
| 150 | - for (int i = 0; i < arity; i++) | ||
| 151 | - { | ||
| 152 | - var array= new object[lists.Length]; | ||
| 153 | - for (int j = 0; j < lists.Length; j++) | ||
| 154 | - array[j] = GetSequenceElementAt(lists[j], i); | ||
| 155 | - yield return array; | ||
| 156 | - } | ||
| 157 | - } | ||
| 158 | - | ||
| 159 | - private static object GetSequenceElementAt(object sequence, int i) | ||
| 160 | - { | ||
| 161 | - switch (sequence) | ||
| 162 | - { | ||
| 163 | - case Array array: | ||
| 164 | - return array.GetValue(i); | ||
| 165 | - case IList list: | ||
| 166 | - return list[i]; | ||
| 167 | - default: | ||
| 168 | - return (sequence as IEnumerable).OfType<object>().Skip(Math.Max(0, i)).FirstOrDefault(); | ||
| 169 | - } | ||
| 170 | - } | ||
| 171 | - | ||
| 172 | 134 | public static IEnumerable<(int, T)> enumerate<T>(IList<T> values) | |
| 173 | 135 | { | |
| 174 | 136 | for (int i = 0; i < values.Count; i++) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,8 +23,44 @@ namespace Tensorflow.Util | |||
| 23 | 23 | public static class nest | |
| 24 | 24 | { | |
| 25 | 25 | ||
| 26 | - public static IEnumerable<object[]> zip(params object[] structures) | ||
| 27 | - => Python.zip(structures); | ||
| 26 | + | ||
| 27 | + /// <summary> | ||
| 28 | + /// Untyped implementation of zip for arbitrary data | ||
| 29 | + /// | ||
| 30 | + /// Converts an list of lists or arrays [[1,2,3], [4,5,6], [7,8,9]] into a list of arrays | ||
| 31 | + /// representing tuples of the same index of all source arrays [[1,4,7], [2,5,9], [3,6,9]] | ||
| 32 | + /// </summary> | ||
| 33 | + /// <param name="lists">one or multiple sequences to be zipped</param> | ||
| 34 | + /// <returns></returns> | ||
| 35 | + public static IEnumerable<object[]> zip_many(params IEnumerable<object>[] lists) | ||
| 36 | + { | ||
| 37 | + if (lists.Length == 0) | ||
| 38 | + yield break; | ||
| 39 | + var first = lists[0]; | ||
| 40 | + if (first == null) | ||
| 41 | + yield break; | ||
| 42 | + var arity = first.Count(); | ||
| 43 | + for (int i = 0; i < arity; i++) | ||
| 44 | + { | ||
| 45 | + var array = new object[lists.Length]; | ||
| 46 | + for (int j = 0; j < lists.Length; j++) | ||
| 47 | + array[j] = GetSequenceElementAt(lists[j], i); | ||
| 48 | + yield return array; | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + private static object GetSequenceElementAt(object sequence, int i) | ||
| 53 | + { | ||
| 54 | + switch (sequence) | ||
| 55 | + { | ||
| 56 | + case Array array: | ||
| 57 | + return array.GetValue(i); | ||
| 58 | + case IList list: | ||
| 59 | + return list[i]; | ||
| 60 | + default: | ||
| 61 | + return _yield_value(sequence).Skip(Math.Max(0, i)).FirstOrDefault(); | ||
| 62 | + } | ||
| 63 | + } | ||
| 28 | 64 | ||
| 29 | 65 | public static IEnumerable<(T1, T2)> zip<T1, T2>(IEnumerable<T1> e1, IEnumerable<T2> e2) | |
| 30 | 66 | => Python.zip(e1, e2); | |
@@ -40,9 +76,9 @@ public static Dictionary<string, object> ConvertToDict(object dyn) | |||
| 40 | 76 | /// <summary> | |
| 41 | 77 | /// Returns a sorted list of the dict keys, with error if keys not sortable. | |
| 42 | 78 | /// </summary> | |
| 43 | - private static IEnumerable<string> _sorted(IDictionary dict_) | ||
| 79 | + private static IEnumerable<object> _sorted(IDictionary dict_) | ||
| 44 | 80 | { | |
| 45 | - return dict_.Keys.OfType<string>().OrderBy(x => x); | ||
| 81 | + return dict_.Keys.OfType<object>().OrderBy(x => x); | ||
| 46 | 82 | } | |
| 47 | 83 | ||
| 48 | 84 | ||
@@ -86,7 +122,7 @@ private static object _sequence_like(object instance, IEnumerable<object> args) | |||
| 86 | 122 | { | |
| 87 | 123 | case Hashtable hash: | |
| 88 | 124 | var result = new Hashtable(); | |
| 89 | - foreach ((object key, object value) in zip(_sorted(hash).OfType<object>(), args)) | ||
| 125 | + foreach ((object key, object value) in zip<object, object>(_sorted(hash), args)) | ||
| 90 | 126 | result[key] = value; | |
| 91 | 127 | return result; | |
| 92 | 128 | } | |
@@ -370,13 +406,13 @@ private static (int new_index, List<object> child) _packed_nest_with_indices(obj | |||
| 370 | 406 | /// <returns> `flat_sequence` converted to have the same recursive structure as | |
| 371 | 407 | /// `structure`. | |
| 372 | 408 | /// </returns> | |
| 373 | - public static object pack_sequence_as<T>(object structure, IEnumerable<T> flat_sequence) | ||
| 409 | + public static object pack_sequence_as(object structure, IEnumerable<object> flat_sequence) | ||
| 374 | 410 | { | |
| 375 | 411 | List<object> flat = null; | |
| 376 | 412 | if (flat_sequence is List<object>) | |
| 377 | 413 | flat = flat_sequence as List<object>; | |
| 378 | 414 | else | |
| 379 | - flat=new List<object>(flat_sequence.OfType<object>()); | ||
| 415 | + flat=new List<object>(flat_sequence); | ||
| 380 | 416 | if (flat_sequence==null) | |
| 381 | 417 | throw new ArgumentException("flat_sequence must not be null"); | |
| 382 | 418 | // if not is_sequence(flat_sequence): | |
@@ -403,7 +439,7 @@ public static object pack_sequence_as<T>(object structure, IEnumerable<T> flat_s | |||
| 403 | 439 | var flat_structure = flatten(structure); | |
| 404 | 440 | if (len(flat_structure) != len(flat)) | |
| 405 | 441 | { | |
| 406 | - throw new ValueError("Could not pack sequence. Structure had %d elements, but " + | ||
| 442 | + throw new ValueError("Could not pack sequence. Structure had {len(structure)} elements, but " + | ||
| 407 | 443 | $"flat_sequence had {len(flat_structure)} elements. flat_sequence had: {len(flat)}"); | |
| 408 | 444 | } | |
| 409 | 445 | return _sequence_like(structure, packed); | |
@@ -413,7 +449,7 @@ public static object pack_sequence_as<T>(object structure, IEnumerable<T> flat_s | |||
| 413 | 449 | var flat_structure = flatten(structure); | |
| 414 | 450 | if (len(flat_structure) != len(flat)) | |
| 415 | 451 | { | |
| 416 | - throw new ValueError("Could not pack sequence. Structure had %d elements, but " + | ||
| 452 | + throw new ValueError("Could not pack sequence. Structure had {len(structure)} elements, but " + | ||
| 417 | 453 | $"flat_sequence had {len(flat_structure)} elements. flat_sequence had: {len(flat)}"); | |
| 418 | 454 | } | |
| 419 | 455 | return _sequence_like(structure, packed); | |
@@ -427,10 +463,8 @@ public static object pack_sequence_as<T>(object structure, IEnumerable<T> flat_s | |||
| 427 | 463 | /// `structure[i]`. All structures in `structure` must have the same arity, | |
| 428 | 464 | /// and the return value will contain the results in the same structure. | |
| 429 | 465 | /// </summary> | |
| 430 | - /// <typeparam name="T">the type of the elements of the output structure (object if diverse)</typeparam> | ||
| 431 | 466 | /// <param name="func"> A callable that accepts as many arguments as there are structures.</param> | |
| 432 | - /// <param name="structures">scalar, or tuple or list of constructed scalars and/or other | ||
| 433 | - /// tuples/lists, or scalars. Note: numpy arrays are considered as scalars.</param> | ||
| 467 | + /// <param name="structures">one or many IEnumerable of object</param> | ||
| 434 | 468 | /// <param name="check_types">If set to | |
| 435 | 469 | /// `True` (default) the types of iterables within the structures have to be | |
| 436 | 470 | /// same (e.g. `map_structure(func, [1], (1,))` raises a `TypeError` | |
@@ -444,23 +478,22 @@ public static object pack_sequence_as<T>(object structure, IEnumerable<T> flat_s | |||
| 444 | 478 | /// `check_types` is `False` the sequence types of the first structure will be | |
| 445 | 479 | /// used. | |
| 446 | 480 | /// </returns> | |
| 447 | - public static IEnumerable<object> map_structure(Func<object[], object> func, object structure, params object[] more_structures) | ||
| 481 | + public static IEnumerable<object> map_structure(Func<object[], object> func, params IEnumerable<object>[] structure) | ||
| 448 | 482 | { | |
| 449 | 483 | // TODO: check structure and types | |
| 450 | 484 | // for other in structure[1:]: | |
| 451 | 485 | // assert_same_structure(structure[0], other, check_types=check_types) | |
| 452 | 486 | ||
| 453 | - if (more_structures.Length==0) | ||
| 487 | + if (structure.Length==1) | ||
| 454 | 488 | { | |
| 455 | 489 | // we don't need to zip if we have only one structure | |
| 456 | - return map_structure(a => func(new object[]{a}), structure); | ||
| 490 | + return map_structure(a => func(new object[]{a}), structure[0]); | ||
| 457 | 491 | } | |
| 458 | - var flat_structures = new List<object>() { flatten(structure) }; | ||
| 459 | - flat_structures.AddRange(more_structures.Select(flatten)); | ||
| 460 | - var entries = zip(flat_structures); | ||
| 492 | + var flat_structures = structure.Select(flatten).ToArray(); // ToArray is important here! | ||
| 493 | + var entries = zip_many(flat_structures); | ||
| 461 | 494 | var mapped_flat_structure = entries.Select(func); | |
| 462 | 495 | ||
| 463 | - return (pack_sequence_as(structure, mapped_flat_structure) as IEnumerable).OfType<object>(); | ||
| 496 | + return _yield_value(pack_sequence_as(structure[0], mapped_flat_structure)).ToList(); | ||
| 464 | 497 | } | |
| 465 | 498 | ||
| 466 | 499 | /// <summary> | |
@@ -469,7 +502,7 @@ public static IEnumerable<object> map_structure(Func<object[], object> func, obj | |||
| 469 | 502 | /// <param name="func"></param> | |
| 470 | 503 | /// <param name="structure"></param> | |
| 471 | 504 | /// <returns></returns> | |
| 472 | - public static IEnumerable<object> map_structure(Func<object, object> func, object structure) | ||
| 505 | + public static IEnumerable<object> map_structure(Func<object, object> func, IEnumerable<object> structure) | ||
| 473 | 506 | { | |
| 474 | 507 | // TODO: check structure and types | |
| 475 | 508 | // for other in structure[1:]: | |
@@ -478,7 +511,7 @@ public static IEnumerable<object> map_structure(Func<object, object> func, objec | |||
| 478 | 511 | var flat_structure = flatten(structure); | |
| 479 | 512 | var mapped_flat_structure = flat_structure.Select(func).ToList(); | |
| 480 | 513 | ||
| 481 | - return (pack_sequence_as(structure, mapped_flat_structure) as IEnumerable).OfType<object>(); | ||
| 514 | + return _yield_value(pack_sequence_as(structure, mapped_flat_structure)).ToList(); | ||
| 482 | 515 | } | |
| 483 | 516 | ||
| 484 | 517 | //def map_structure_with_paths(func, *structure, **kwargs): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -387,11 +387,10 @@ public void testMapStructure() | |||
| 387 | 387 | // nest.assert_same_structure(structure1, structure1_plus1) | |
| 388 | 388 | self.assertAllEqual( nest.flatten(structure1_plus1), new object[] { 2, 3, 4, 5, 6, 7 }); | |
| 389 | 389 | self.assertAllEqual(nest.flatten(structure1_strings), new object[] { "1", "2", "3", "4", "5", "6" }); | |
| 390 | - // structure1_plus_structure2 = nest.map_structure( | ||
| 391 | - // lambda x, y: x + y, structure1, structure2) | ||
| 392 | - // self.assertEqual( | ||
| 393 | - // (((1 + 7, 2 + 8), 3 + 9), 4 + 10, (5 + 11, 6 + 12)), | ||
| 394 | - // structure1_plus_structure2) | ||
| 390 | + var structure1_plus_structure2 = nest.map_structure(x => (int)(x[0]) + (int)(x[1]), structure1, structure2); | ||
| 391 | + self.assertEqual( | ||
| 392 | + new object[] { new object[] { new object[] { 1 + 7, 2 + 8}, 3 + 9}, 4 + 10, new object[] { 5 + 11, 6 + 12}}, | ||
| 393 | + structure1_plus_structure2); | ||
| 395 | 394 | ||
| 396 | 395 | // self.assertEqual(3, nest.map_structure(lambda x: x - 1, 4)) | |
| 397 | 396 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments