You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

nest.py.cs 44 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using NumSharp;
  18. namespace Tensorflow.Util
  19. {
  20. //Functions for working with arbitrarily nested sequences of elements.
  21. //This module can perform operations on nested structures. A nested structure is a
  22. //Python sequence, tuple (including `namedtuple`), or dict that can contain
  23. //further sequences, tuples, and dicts.
  24. //The utilities here assume (and do not check) that the nested structures form a
  25. //'tree', i.e., no references in the structure of the input of these functions
  26. //should be recursive.
  27. //Example structures: `((3, 4), 5, (6, 7, (9, 10), 8))`, `(np.array(0),
  28. // (np.array([3, 4]), tf.constant([3, 4])))`
  29. //
  30. public static class nest
  31. {
  32. /// <summary>
  33. /// Untyped implementation of zip for arbitrary data
  34. ///
  35. /// Converts an list of lists or arrays [[1,2,3], [4,5,6], [7,8,9]] into a list of arrays
  36. /// representing tuples of the same index of all source arrays [[1,4,7], [2,5,9], [3,6,9]]
  37. /// </summary>
  38. /// <param name="lists">one or multiple sequences to be zipped</param>
  39. /// <returns></returns>
  40. public static IEnumerable<object[]> zip_many(params IEnumerable<object>[] lists)
  41. {
  42. if (lists.Length == 0)
  43. yield break;
  44. var first = lists[0];
  45. if (first == null)
  46. yield break;
  47. var arity = first.Count();
  48. for (int i = 0; i < arity; i++)
  49. {
  50. var array = new object[lists.Length];
  51. for (int j = 0; j < lists.Length; j++)
  52. array[j] = GetSequenceElementAt(lists[j], i);
  53. yield return array;
  54. }
  55. }
  56. private static object GetSequenceElementAt(object sequence, int i)
  57. {
  58. switch (sequence)
  59. {
  60. case Array array:
  61. return array.GetValue(i);
  62. case IList list:
  63. return list[i];
  64. default:
  65. return _yield_value(sequence).Skip(Math.Max(0, i)).FirstOrDefault();
  66. }
  67. }
  68. public static IEnumerable<(T1, T2)> zip<T1, T2>(IEnumerable<T1> e1, IEnumerable<T2> e2)
  69. => Python.zip(e1, e2);
  70. public static Dictionary<string, object> ConvertToDict(object dyn)
  71. => Python.ConvertToDict(dyn);
  72. //def _get_attrs_values(obj):
  73. // """Returns the list of values from an attrs instance."""
  74. // attrs = getattr(obj.__class__, "__attrs_attrs__")
  75. // return [getattr(obj, a.name) for a in attrs]
  76. /// <summary>
  77. /// Returns a sorted list of the dict keys, with error if keys not sortable.
  78. /// </summary>
  79. private static IEnumerable<object> _sorted(IDictionary dict_)
  80. {
  81. return dict_.Keys.OfType<object>().OrderBy(x => x);
  82. }
  83. //def _is_namedtuple(instance, strict=False):
  84. // """Returns True iff `instance` is a `namedtuple`.
  85. // Args:
  86. // instance: An instance of a Python object.
  87. // strict: If True, `instance` is considered to be a `namedtuple` only if
  88. // it is a "plain" namedtuple. For instance, a class inheriting
  89. // from a `namedtuple` will be considered to be a `namedtuple`
  90. // iff `strict=False`.
  91. // Returns:
  92. // True if `instance` is a `namedtuple`.
  93. // """
  94. // return _pywrap_tensorflow.IsNamedtuple(instance, strict)
  95. //# See the swig file (util.i) for documentation.
  96. //_is_mapping = _pywrap_tensorflow.IsMapping
  97. //_is_attrs = _pywrap_tensorflow.IsAttrs
  98. /// <summary>
  99. /// Converts the sequence `args` to the same type as `instance`.
  100. /// </summary>
  101. /// <param name="instance">an instance of `tuple`, `list`, `namedtuple`, `dict`, or
  102. /// `collections.OrderedDict`.</param>
  103. /// <param name="args">elements to be converted to the `instance` type.</param>
  104. /// <returns>`args` with the type of `instance`.</returns>
  105. private static object _sequence_like(object instance, IEnumerable<object> args)
  106. {
  107. if (is_mapping(instance))
  108. {
  109. //# Pack dictionaries in a deterministic order by sorting the keys.
  110. //# Notice this means that we ignore the original order of `OrderedDict`
  111. //# instances. This is intentional, to avoid potential bugs caused by mixing
  112. //# ordered and plain dicts (e.g., flattening a dict but using a
  113. //# corresponding `OrderedDict` to pack it back).
  114. switch (instance)
  115. {
  116. case Hashtable hash:
  117. var result = new Hashtable();
  118. foreach ((object key, object value) in zip<object, object>(_sorted(hash), args))
  119. result[key] = value;
  120. return result;
  121. }
  122. }
  123. //else if( _is_namedtuple(instance) || _is_attrs(instance))
  124. // return type(instance)(*args)
  125. else
  126. {
  127. // Not a namedtuple
  128. switch (instance)
  129. {
  130. case object[] array:
  131. var result_array = new object[args.Count()];
  132. int i = 0;
  133. foreach (var x in args)
  134. {
  135. result_array[i] = x;
  136. i++;
  137. }
  138. return result_array;
  139. case List<object> list:
  140. return new List<object>(args);
  141. default:
  142. throw new TypeError("Type of sequence not supported (yet): " + instance.GetType());
  143. }
  144. }
  145. throw new TypeError("Type of sequence not supported (yet): " + instance.GetType());
  146. }
  147. /// <summary>
  148. /// Yields the next value from the given iterable.
  149. /// </summary>
  150. private static IEnumerable<object> _yield_value(object iterable)
  151. {
  152. if (is_mapping(iterable))
  153. {
  154. var dict = iterable as IDictionary;
  155. //# Iterate through dictionaries in a deterministic order by sorting the
  156. //# keys. Notice this means that we ignore the original order of `OrderedDict`
  157. //# instances. This is intentional, to avoid potential bugs caused by mixing
  158. //# ordered and plain dicts (e.g., flattening a dict but using a
  159. //# corresponding `OrderedDict` to pack it back).
  160. foreach (var key in _sorted(dict))
  161. yield return dict[key];
  162. }
  163. //else if (_is_attrs(iterable))
  164. //{
  165. // // for value in _get_attrs_values(iterable):
  166. // // yield value
  167. //}
  168. else if (iterable is IEnumerable)
  169. {
  170. var enumerable = iterable as IEnumerable;
  171. foreach (var value in enumerable)
  172. yield return value;
  173. }
  174. else
  175. {
  176. throw new TypeError("Unexpected iterable type: " + iterable.GetType());
  177. //var jobj = JObject.FromObject(iterable);
  178. //foreach (var key in _sorted())
  179. // yield return jobj[key];
  180. }
  181. }
  182. //# See the swig file (util.i) for documentation.
  183. public static bool is_sequence(object arg)
  184. => arg is IEnumerable && !(arg is string) && !(arg is NDArray) &&
  185. !(arg.GetType().IsGenericType && arg.GetType().GetGenericTypeDefinition() == typeof(HashSet<>));
  186. public static bool is_mapping(object arg) => arg is IDictionary;
  187. //# See the swig file (util.i) for documentation.
  188. //flatten = _pywrap_tensorflow.Flatten
  189. public static List<T> flatten<T>(T structure)
  190. {
  191. var list = new List<T>();
  192. _flatten_recursive(structure, list);
  193. return list;
  194. }
  195. private static void _flatten_recursive<T>(T obj, List<T> list)
  196. {
  197. if (obj is string)
  198. {
  199. list.Add(obj);
  200. return;
  201. }
  202. if (obj is IDictionary)
  203. {
  204. var dict = obj as IDictionary;
  205. foreach (var key in _sorted(dict))
  206. _flatten_recursive((T)dict[key], list);
  207. return;
  208. }
  209. if (obj is NDArray)
  210. {
  211. list.Add(obj);
  212. return;
  213. }
  214. if (obj is IEnumerable)
  215. {
  216. var structure = obj as IEnumerable;
  217. foreach (var child in structure)
  218. _flatten_recursive((T)child, list);
  219. return;
  220. }
  221. list.Add(obj);
  222. }
  223. //# See the swig file (util.i) for documentation.
  224. //_same_namedtuples = _pywrap_tensorflow.SameNamedtuples
  225. //class _DotString(object):
  226. // def __str__(self):
  227. // return "."
  228. // def __repr__(self):
  229. // return "."
  230. //_DOT = _DotString()
  231. //def assert_same_structure(nest1, nest2, check_types=True):
  232. // """Asserts that two structures are nested in the same way.
  233. // Note that namedtuples with identical name and fields are always considered
  234. // to have the same shallow structure (even with `check_types=True`).
  235. // For intance, this code will print `True`:
  236. // ```python
  237. // def nt(a, b):
  238. // return collections.namedtuple('foo', 'a b')(a, b)
  239. // print(assert_same_structure(nt(0, 1), nt(2, 3)))
  240. // ```
  241. // Args:
  242. // nest1: an arbitrarily nested structure.
  243. // nest2: an arbitrarily nested structure.
  244. // check_types: if `True` (default) types of sequences are checked as well,
  245. // including the keys of dictionaries. If set to `False`, for example a
  246. // list and a tuple of objects will look the same if they have the same
  247. // size. Note that namedtuples with identical name and fields are always
  248. // considered to have the same shallow structure. Two types will also be
  249. // considered the same if they are both list subtypes (which allows "list"
  250. // and "_ListWrapper" from checkpointable dependency tracking to compare
  251. // equal).
  252. // Raises:
  253. // ValueError: If the two structures do not have the same number of elements or
  254. // if the two structures are not nested in the same way.
  255. // TypeError: If the two structures differ in the type of sequence in any of
  256. // their substructures. Only possible if `check_types` is `True`.
  257. // """
  258. // try:
  259. // _pywrap_tensorflow.AssertSameStructure(nest1, nest2, check_types)
  260. // except (ValueError, TypeError) as e:
  261. // str1 = str(map_structure(lambda _: _DOT, nest1))
  262. // str2 = str(map_structure(lambda _: _DOT, nest2))
  263. // raise type(e)("%s\n"
  264. // "Entire first structure:\n%s\n"
  265. // "Entire second structure:\n%s"
  266. // % (str(e), str1, str2))
  267. //def flatten_dict_items(dictionary):
  268. // """Returns a dictionary with flattened keys and values.
  269. // This function flattens the keys and values of a dictionary, which can be
  270. // arbitrarily nested structures, and returns the flattened version of such
  271. // structures:
  272. // ```python
  273. // example_dictionary = {(4, 5, (6, 8)): ("a", "b", ("c", "d"))}
  274. // result = {4: "a", 5: "b", 6: "c", 8: "d"}
  275. // flatten_dict_items(example_dictionary) == result
  276. // ```
  277. // The input dictionary must satisfy two properties:
  278. // 1. Its keys and values should have the same exact nested structure.
  279. // 2. The set of all flattened keys of the dictionary must not contain repeated
  280. // keys.
  281. // Args:
  282. // dictionary: the dictionary to zip
  283. // Returns:
  284. // The zipped dictionary.
  285. // Raises:
  286. // TypeError: If the input is not a dictionary.
  287. // ValueError: If any key and value have not the same structure, or if keys are
  288. // not unique.
  289. // """
  290. // if not isinstance(dictionary, (dict, _collections.Mapping)):
  291. // raise TypeError("input must be a dictionary")
  292. // flat_dictionary = {}
  293. // for i, v in _six.iteritems(dictionary):
  294. // if not is_sequence(i):
  295. // if i in flat_dictionary:
  296. // raise ValueError(
  297. // "Could not flatten dictionary: key %s is not unique." % i)
  298. // flat_dictionary[i] = v
  299. // else:
  300. // flat_i = flatten(i)
  301. // flat_v = flatten(v)
  302. // if len(flat_i) != len(flat_v):
  303. // raise ValueError(
  304. // "Could not flatten dictionary. Key had %d elements, but value had "
  305. // "%d elements. Key: %s, value: %s."
  306. // % (len(flat_i), len(flat_v), flat_i, flat_v))
  307. // for new_i, new_v in zip(flat_i, flat_v):
  308. // if new_i in flat_dictionary:
  309. // raise ValueError(
  310. // "Could not flatten dictionary: key %s is not unique."
  311. // % (new_i))
  312. // flat_dictionary[new_i] = new_v
  313. // return flat_dictionary
  314. /// <summary>
  315. /// Helper function for pack_sequence_as.
  316. /// </summary>
  317. /// <param name="structure">Substructure (list / tuple / dict) to mimic.</param>
  318. /// <param name="flat">Flattened values to output substructure for.</param>
  319. /// <param name="index">Index at which to start reading from flat.</param>
  320. /// <returns>
  321. /// The tuple(new_index, child), where:
  322. /// * new_index - the updated index into `flat` having processed `structure`.
  323. /// * packed - the subset of `flat` corresponding to `structure`,
  324. /// having started at `index`, and packed into the same nested
  325. /// format.</returns>
  326. private static (int new_index, List<object> child) _packed_nest_with_indices(object structure, List<object> flat,
  327. int index)
  328. {
  329. var packed = new List<object>();
  330. foreach (var s in _yield_value(structure))
  331. {
  332. if (is_sequence(s))
  333. {
  334. var (new_index, child) = _packed_nest_with_indices(s, flat, index);
  335. packed.Add(_sequence_like(s, child));
  336. index = new_index;
  337. }
  338. else
  339. {
  340. packed.Add(flat[index]);
  341. index += 1;
  342. }
  343. }
  344. return (index, packed);
  345. }
  346. private static int len(IEnumerable<object> x) => x.Count();
  347. /// <summary>
  348. /// Returns a given flattened sequence packed into a given structure.
  349. /// If `structure` is a scalar, `flat_sequence` must be a single-element list;
  350. /// in this case the return value is `flat_sequence[0]`.
  351. ///
  352. /// If `structure` is or contains a dict instance, the keys will be sorted to
  353. /// pack the flat sequence in deterministic order. This is true also for
  354. /// `OrderedDict` instances: their sequence order is ignored, the sorting order of
  355. /// keys is used instead. The same convention is followed in `flatten`.
  356. /// This correctly repacks dicts and `OrderedDict`s after they have been
  357. /// flattened, and also allows flattening an `OrderedDict` and then repacking it
  358. /// back using a corresponding plain dict, or vice-versa.
  359. /// Dictionaries with non-sortable keys cannot be flattened.
  360. /// </summary>
  361. /// <param name="structure">
  362. /// Nested structure, whose structure is given by nested lists,
  363. /// tuples, and dicts. Note: numpy arrays and strings are considered
  364. /// scalars.
  365. /// </param>
  366. /// <param name="flat_sequence"> flat sequence to pack.</param>
  367. /// <returns> `flat_sequence` converted to have the same recursive structure as
  368. /// `structure`.
  369. /// </returns>
  370. public static object pack_sequence_as(object structure, IEnumerable<object> flat_sequence)
  371. {
  372. List<object> flat = null;
  373. if (flat_sequence is List<object>)
  374. flat = flat_sequence as List<object>;
  375. else
  376. flat=new List<object>(flat_sequence);
  377. if (flat_sequence==null)
  378. throw new ArgumentException("flat_sequence must not be null");
  379. // if not is_sequence(flat_sequence):
  380. // raise TypeError("flat_sequence must be a sequence")
  381. if (!is_sequence(structure))
  382. {
  383. if (len(flat) != 1)
  384. throw new ValueError($"Structure is a scalar but len(flat_sequence) == {len(flat)} > 1");
  385. return flat.FirstOrDefault();
  386. }
  387. int final_index = 0;
  388. List<object> packed = null;
  389. try
  390. {
  391. (final_index, packed) = _packed_nest_with_indices(structure, flat, 0);
  392. if (final_index < len(flat))
  393. throw new IndexOutOfRangeException(
  394. $"Final index: {final_index} was smaller than len(flat_sequence): {len(flat)}");
  395. return _sequence_like(structure, packed);
  396. }
  397. catch (IndexOutOfRangeException)
  398. {
  399. var flat_structure = flatten(structure);
  400. if (len(flat_structure) != len(flat))
  401. {
  402. throw new ValueError("Could not pack sequence. Structure had {len(structure)} elements, but " +
  403. $"flat_sequence had {len(flat_structure)} elements. flat_sequence had: {len(flat)}");
  404. }
  405. return _sequence_like(structure, packed);
  406. }
  407. catch (ArgumentOutOfRangeException)
  408. {
  409. var flat_structure = flatten(structure);
  410. if (len(flat_structure) != len(flat))
  411. {
  412. throw new ValueError("Could not pack sequence. Structure had {len(structure)} elements, but " +
  413. $"flat_sequence had {len(flat_structure)} elements. flat_sequence had: {len(flat)}");
  414. }
  415. return _sequence_like(structure, packed);
  416. }
  417. }
  418. /// <summary>
  419. /// Applies `func` to each entry in `structure` and returns a new structure.
  420. ///
  421. /// Applies `func(x[0], x[1], ...)` where x[i] is an entry in
  422. /// `structure[i]`. All structures in `structure` must have the same arity,
  423. /// and the return value will contain the results in the same structure.
  424. /// </summary>
  425. /// <param name="func"> A callable that accepts as many arguments as there are structures.</param>
  426. /// <param name="structures">one or many IEnumerable of object</param>
  427. /// <param name="check_types">If set to
  428. /// `True` (default) the types of iterables within the structures have to be
  429. /// same (e.g. `map_structure(func, [1], (1,))` raises a `TypeError`
  430. /// exception). To allow this set this argument to `False`.
  431. /// Note that namedtuples with identical name and fields are always
  432. /// considered to have the same shallow structure.</param>
  433. /// <returns>
  434. /// A new structure with the same arity as `structure`, whose values correspond
  435. /// to `func(x[0], x[1], ...)` where `x[i]` is a value in the corresponding
  436. /// location in `structure[i]`. If there are different sequence types and
  437. /// `check_types` is `False` the sequence types of the first structure will be
  438. /// used.
  439. /// </returns>
  440. public static IEnumerable<object> map_structure(Func<object[], object> func, params IEnumerable<object>[] structure)
  441. {
  442. // TODO: check structure and types
  443. // for other in structure[1:]:
  444. // assert_same_structure(structure[0], other, check_types=check_types)
  445. if (structure.Length==1)
  446. {
  447. // we don't need to zip if we have only one structure
  448. return map_structure(a => func(new object[]{a}), structure[0]);
  449. }
  450. var flat_structures = structure.Select(flatten).ToArray(); // ToArray is important here!
  451. var entries = zip_many(flat_structures);
  452. var mapped_flat_structure = entries.Select(func);
  453. return _yield_value(pack_sequence_as(structure[0], mapped_flat_structure)).ToList();
  454. }
  455. /// <summary>
  456. /// Same as map_structure, but with only one structure (no combining of multiple structures)
  457. /// </summary>
  458. /// <param name="func"></param>
  459. /// <param name="structure"></param>
  460. /// <returns></returns>
  461. public static IEnumerable<object> map_structure(Func<object, object> func, IEnumerable<object> structure)
  462. {
  463. // TODO: check structure and types
  464. // for other in structure[1:]:
  465. // assert_same_structure(structure[0], other, check_types=check_types)
  466. var flat_structure = flatten(structure);
  467. var mapped_flat_structure = flat_structure.Select(func).ToList();
  468. return _yield_value(pack_sequence_as(structure, mapped_flat_structure)).ToList();
  469. }
  470. //def map_structure_with_paths(func, *structure, **kwargs):
  471. // """Applies `func` to each entry in `structure` and returns a new structure.
  472. // Applies `func(path, x[0], x[1], ..., **kwargs)` where x[i] is an entry in
  473. // `structure[i]` and `path` is the common path to x[i] in the structures. All
  474. // structures in `structure` must have the same arity, and the return value will
  475. // contain the results in the same structure. Special kwarg `check_types`
  476. // determines whether the types of iterables within the structure must be the
  477. // same-- see **kwargs definition below.
  478. // Args:
  479. // func: A callable with the signature func(path, *values, **kwargs) that is
  480. // evaluated on the leaves of the structure.
  481. // *structure: A variable number of compatible structures to process.
  482. // **kwargs: Optional kwargs to be passed through to func. Special kwarg
  483. // `check_types` is not passed to func, but instead determines whether the
  484. // types of iterables within the structures have to be same (e.g.,
  485. // `map_structure(func, [1], (1,))` raises a `TypeError` exception). By
  486. // default, the types must match. To allow iteration over structures of
  487. // different types (but common arity), set this kwarg to `False`.
  488. // Returns:
  489. // A structure of the same form as the input structures whose leaves are the
  490. // result of evaluating func on corresponding leaves of the input structures.
  491. // Raises:
  492. // TypeError: If `func` is not callable or if the structures do not match
  493. // each other by depth tree.
  494. // TypeError: If `check_types` is not `False` and the two structures differ in
  495. // the type of sequence in any of their substructures.
  496. // ValueError: If no structures are provided.
  497. // """
  498. // if not callable(func):
  499. // raise TypeError("func must be callable, got: %s" % func)
  500. // if not structure:
  501. // raise ValueError("Must provide at least one structure")
  502. // check_types = kwargs.pop("check_types", True)
  503. // for other in structure[1:]:
  504. // assert_same_structure(structure[0], other, check_types=check_types)
  505. //# First set paths_and_values to:
  506. //# [[(p11, v11), ... (p1n, v1n)], ... [(pm1, vm1), ... (pmn, vmn)]]
  507. // paths_and_values = [flatten_with_joined_string_paths(s) for s in structure]
  508. //# Now zip(*paths_and_values) would be:
  509. //# [((p11, v11), ... (pm1, vm1)), ... ((p1n, v1n), ... (pmn, vmn))]
  510. //# so grouped_by_path is set to:
  511. //# [[(p11, ... pm1), (v11, ... vm1)], ... [(p1n, ... pmn), (v1n, ... vmn)]]
  512. //# Note that p1i, ... pmi must all be equal since the structures are the same.
  513. // grouped_by_path = [zip(*p_v) for p_v in zip(*paths_and_values)]
  514. // return pack_sequence_as(structure[0], [
  515. // func(paths[0], *values, **kwargs) for paths, values in grouped_by_path])
  516. //def _yield_flat_up_to(shallow_tree, input_tree):
  517. // """Yields elements `input_tree` partially flattened up to `shallow_tree`."""
  518. // if is_sequence(shallow_tree):
  519. // for shallow_branch, input_branch in zip(_yield_value(shallow_tree),
  520. // _yield_value(input_tree)):
  521. // for input_leaf in _yield_flat_up_to(shallow_branch, input_branch):
  522. // yield input_leaf
  523. // else:
  524. // yield input_tree
  525. //def assert_shallow_structure(shallow_tree, input_tree, check_types=True):
  526. // """Asserts that `shallow_tree` is a shallow structure of `input_tree`.
  527. // That is, this function tests if the `input_tree` structure can be created from
  528. // the `shallow_tree` structure by replacing its leaf nodes with deeper
  529. // tree structures.
  530. // Examples:
  531. // The following code will raise an exception:
  532. // ```python
  533. // shallow_tree = ["a", "b"]
  534. // input_tree = ["c", ["d", "e"], "f"]
  535. // assert_shallow_structure(shallow_tree, input_tree)
  536. // ```
  537. // The following code will not raise an exception:
  538. // ```python
  539. // shallow_tree = ["a", "b"]
  540. // input_tree = ["c", ["d", "e"]]
  541. // assert_shallow_structure(shallow_tree, input_tree)
  542. // ```
  543. // Args:
  544. // shallow_tree: an arbitrarily nested structure.
  545. // input_tree: an arbitrarily nested structure.
  546. // check_types: if `True` (default) the sequence types of `shallow_tree` and
  547. // `input_tree` have to be the same. Note that even with check_types==True,
  548. // this function will consider two different namedtuple classes with the same
  549. // name and _fields attribute to be the same class.
  550. // Raises:
  551. // TypeError: If `shallow_tree` is a sequence but `input_tree` is not.
  552. // TypeError: If the sequence types of `shallow_tree` are different from
  553. // `input_tree`. Only raised if `check_types` is `True`.
  554. // ValueError: If the sequence lengths of `shallow_tree` are different from
  555. // `input_tree`.
  556. // """
  557. // if is_sequence(shallow_tree):
  558. // if not is_sequence(input_tree):
  559. // raise TypeError(
  560. // "If shallow structure is a sequence, input must also be a sequence. "
  561. // "Input has type: %s." % type(input_tree))
  562. // if check_types and not isinstance(input_tree, type(shallow_tree)):
  563. //# Duck-typing means that nest should be fine with two different
  564. //# namedtuples with identical name and fields.
  565. // shallow_is_namedtuple = _is_namedtuple(shallow_tree, False)
  566. // input_is_namedtuple = _is_namedtuple(input_tree, False)
  567. // if shallow_is_namedtuple and input_is_namedtuple:
  568. // if not _same_namedtuples(shallow_tree, input_tree):
  569. // raise TypeError(
  570. // "The two namedtuples don't have the same sequence type. Input "
  571. // "structure has type %s, while shallow structure has type %s."
  572. // % (type(input_tree), type(shallow_tree)))
  573. // elif not (isinstance(shallow_tree, _collections.Mapping)
  574. // and isinstance(input_tree, _collections.Mapping)):
  575. // raise TypeError(
  576. // "The two structures don't have the same sequence type. Input "
  577. // "structure has type %s, while shallow structure has type %s."
  578. // % (type(input_tree), type(shallow_tree)))
  579. // if len(input_tree) != len(shallow_tree):
  580. // raise ValueError(
  581. // "The two structures don't have the same sequence length. Input "
  582. // "structure has length %s, while shallow structure has length %s."
  583. // % (len(input_tree), len(shallow_tree)))
  584. // if check_types and isinstance(shallow_tree, (dict, _collections.Mapping)):
  585. // if set(input_tree) != set(shallow_tree):
  586. // raise ValueError(
  587. // "The two structures don't have the same keys. Input "
  588. // "structure has keys %s, while shallow structure has keys %s." %
  589. // (list(_six.iterkeys(input_tree)),
  590. // list(_six.iterkeys(shallow_tree))))
  591. // input_tree = list(sorted(_six.iteritems(input_tree)))
  592. // shallow_tree = list(sorted(_six.iteritems(shallow_tree)))
  593. // for shallow_branch, input_branch in zip(shallow_tree, input_tree):
  594. // assert_shallow_structure(shallow_branch, input_branch,
  595. // check_types=check_types)
  596. //def flatten_up_to(shallow_tree, input_tree):
  597. // """Flattens `input_tree` up to `shallow_tree`.
  598. // Any further depth in structure in `input_tree` is retained as elements in the
  599. // partially flatten output.
  600. // If `shallow_tree` and `input_tree` are not sequences, this returns a
  601. // single-element list: `[input_tree]`.
  602. // Use Case:
  603. // Sometimes we may wish to partially flatten a nested sequence, retaining some
  604. // of the nested structure. We achieve this by specifying a shallow structure,
  605. // `shallow_tree`, we wish to flatten up to.
  606. // The input, `input_tree`, can be thought of as having the same structure as
  607. // `shallow_tree`, but with leaf nodes that are themselves tree structures.
  608. // Examples:
  609. // ```python
  610. // input_tree = [[[2, 2], [3, 3]], [[4, 9], [5, 5]]]
  611. // shallow_tree = [[True, True], [False, True]]
  612. // flattened_input_tree = flatten_up_to(shallow_tree, input_tree)
  613. // flattened_shallow_tree = flatten_up_to(shallow_tree, shallow_tree)
  614. //# Output is:
  615. //# [[2, 2], [3, 3], [4, 9], [5, 5]]
  616. //# [True, True, False, True]
  617. // ```
  618. // ```python
  619. // input_tree = [[('a', 1), [('b', 2), [('c', 3), [('d', 4)]]]]]
  620. // shallow_tree = [['level_1', ['level_2', ['level_3', ['level_4']]]]]
  621. // input_tree_flattened_as_shallow_tree = flatten_up_to(shallow_tree, input_tree)
  622. // input_tree_flattened = flatten(input_tree)
  623. //# Output is:
  624. //# [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
  625. //# ['a', 1, 'b', 2, 'c', 3, 'd', 4]
  626. // ```
  627. // Non-Sequence Edge Cases:
  628. // ```python
  629. // flatten_up_to(0, 0) # Output: [0]
  630. // flatten_up_to(0, [0, 1, 2]) # Output: [[0, 1, 2]]
  631. // flatten_up_to([0, 1, 2], 0) # Output: TypeError
  632. // flatten_up_to([0, 1, 2], [0, 1, 2]) # Output: [0, 1, 2]
  633. // ```
  634. // Args:
  635. // shallow_tree: a possibly pruned structure of input_tree.
  636. // input_tree: an arbitrarily nested structure or a scalar object.
  637. // Note, numpy arrays are considered scalars.
  638. // Returns:
  639. // A Python list, the partially flattened version of `input_tree` according to
  640. // the structure of `shallow_tree`.
  641. // Raises:
  642. // TypeError: If `shallow_tree` is a sequence but `input_tree` is not.
  643. // TypeError: If the sequence types of `shallow_tree` are different from
  644. // `input_tree`.
  645. // ValueError: If the sequence lengths of `shallow_tree` are different from
  646. // `input_tree`.
  647. // """
  648. // assert_shallow_structure(shallow_tree, input_tree)
  649. // return list(_yield_flat_up_to(shallow_tree, input_tree))
  650. //def map_structure_up_to(shallow_tree, func, *inputs):
  651. // """Applies a function or op to a number of partially flattened inputs.
  652. // The `inputs` are flattened up to `shallow_tree` before being mapped.
  653. // Use Case:
  654. // Sometimes we wish to apply a function to a partially flattened
  655. // sequence (for example when the function itself takes sequence inputs). We
  656. // achieve this by specifying a shallow structure, `shallow_tree` we wish to
  657. // flatten up to.
  658. // The `inputs`, can be thought of as having the same structure as
  659. // `shallow_tree`, but with leaf nodes that are themselves tree structures.
  660. // This function therefore will return something with the same base structure as
  661. // `shallow_tree`.
  662. // Examples:
  663. // ```python
  664. // ab_tuple = collections.namedtuple("ab_tuple", "a, b")
  665. // op_tuple = collections.namedtuple("op_tuple", "add, mul")
  666. // inp_val = ab_tuple(a=2, b=3)
  667. // inp_ops = ab_tuple(a=op_tuple(add=1, mul=2), b=op_tuple(add=2, mul=3))
  668. // out = map_structure_up_to(inp_val, lambda val, ops: (val + ops.add) * ops.mul,
  669. // inp_val, inp_ops)
  670. //# Output is: ab_tuple(a=6, b=15)
  671. // ```
  672. // ```python
  673. // data_list = [[2, 4, 6, 8], [[1, 3, 5, 7, 9], [3, 5, 7]]]
  674. // name_list = ['evens', ['odds', 'primes']]
  675. // out = map_structure_up_to(
  676. // name_list,
  677. // lambda name, sec: "first_{}_{}".format(len(sec), name),
  678. // name_list, data_list)
  679. //# Output is: ['first_4_evens', ['first_5_odds', 'first_3_primes']]
  680. // ```
  681. // Args:
  682. // shallow_tree: a shallow tree, common to all the inputs.
  683. // func: callable which will be applied to each input individually.
  684. // *inputs: arbitrarily nested combination of objects that are compatible with
  685. // shallow_tree. The function `func` is applied to corresponding
  686. // partially flattened elements of each input, so the function must support
  687. // arity of `len(inputs)`.
  688. // Raises:
  689. // TypeError: If `shallow_tree` is a sequence but `input_tree` is not.
  690. // TypeError: If the sequence types of `shallow_tree` are different from
  691. // `input_tree`.
  692. // ValueError: If the sequence lengths of `shallow_tree` are different from
  693. // `input_tree`.
  694. // Returns:
  695. // result of repeatedly applying `func`, with same structure as
  696. // `shallow_tree`.
  697. // """
  698. // if not inputs:
  699. // raise ValueError("Cannot map over no sequences")
  700. // for input_tree in inputs:
  701. // assert_shallow_structure(shallow_tree, input_tree)
  702. //# Flatten each input separately, apply the function to corresponding elements,
  703. //# then repack based on the structure of the first input.
  704. // all_flattened_up_to = [flatten_up_to(shallow_tree, input_tree)
  705. // for input_tree in inputs]
  706. // results = [func(*tensors) for tensors in zip(*all_flattened_up_to)]
  707. // return pack_sequence_as(structure=shallow_tree, flat_sequence=results)
  708. //def get_traverse_shallow_structure(traverse_fn, structure):
  709. // """Generates a shallow structure from a `traverse_fn` and `structure`.
  710. // `traverse_fn` must accept any possible subtree of `structure` and return
  711. // a depth=1 structure containing `True` or `False` values, describing which
  712. // of the top-level subtrees may be traversed. It may also
  713. // return scalar `True` or `False` "traversal is OK / not OK for all subtrees."
  714. // Examples are available in the unit tests (nest_test.py).
  715. // Args:
  716. // traverse_fn: Function taking a substructure and returning either a scalar
  717. // `bool` (whether to traverse that substructure or not) or a depth=1
  718. // shallow structure of the same type, describing which parts of the
  719. // substructure to traverse.
  720. // structure: The structure to traverse.
  721. // Returns:
  722. // A shallow structure containing python bools, which can be passed to
  723. // `map_structure_up_to` and `flatten_up_to`.
  724. // Raises:
  725. // TypeError: if `traverse_fn` returns a sequence for a non-sequence input,
  726. // or a structure with depth higher than 1 for a sequence input,
  727. // or if any leaf values in the returned structure or scalar are not type
  728. // `bool`.
  729. // """
  730. // to_traverse = traverse_fn(structure)
  731. // if not is_sequence(structure):
  732. // if not isinstance(to_traverse, bool):
  733. // raise TypeError("traverse_fn returned structure: %s for non-structure: %s"
  734. // % (to_traverse, structure))
  735. // return to_traverse
  736. // level_traverse = []
  737. // if isinstance(to_traverse, bool):
  738. // if not to_traverse:
  739. //# Do not traverse this substructure at all. Exit early.
  740. // return False
  741. // else:
  742. //# Traverse the entire substructure.
  743. // for branch in _yield_value(structure):
  744. // level_traverse.append(
  745. // get_traverse_shallow_structure(traverse_fn, branch))
  746. // elif not is_sequence(to_traverse):
  747. // raise TypeError("traverse_fn returned a non-bool scalar: %s for input: %s"
  748. // % (to_traverse, structure))
  749. // else:
  750. //# Traverse some subset of this substructure.
  751. // assert_shallow_structure(to_traverse, structure)
  752. // for t, branch in zip(_yield_value(to_traverse), _yield_value(structure)):
  753. // if not isinstance(t, bool):
  754. // raise TypeError(
  755. // "traverse_fn didn't return a depth=1 structure of bools. saw: %s "
  756. // " for structure: %s" % (to_traverse, structure))
  757. // if t:
  758. // level_traverse.append(
  759. // get_traverse_shallow_structure(traverse_fn, branch))
  760. // else:
  761. // level_traverse.append(False)
  762. // return _sequence_like(structure, level_traverse)
  763. //def yield_flat_paths(nest):
  764. // """Yields paths for some nested structure.
  765. // Paths are lists of objects which can be str-converted, which may include
  766. // integers or other types which are used as indices in a dict.
  767. // The flat list will be in the corresponding order as if you called
  768. // `snt.nest.flatten` on the structure. This is handy for naming Tensors such
  769. // the TF scope structure matches the tuple structure.
  770. // E.g. if we have a tuple `value = Foo(a=3, b=Bar(c=23, d=42))`
  771. // ```shell
  772. // >>> nest.flatten(value)
  773. // [3, 23, 42]
  774. // >>> list(nest.yield_flat_paths(value))
  775. // [('a',), ('b', 'c'), ('b', 'd')]
  776. // ```
  777. // ```shell
  778. // >>> list(nest.yield_flat_paths({'a': [3]}))
  779. // [('a', 0)]
  780. // >>> list(nest.yield_flat_paths({'a': 3}))
  781. // [('a',)]
  782. // ```
  783. // Args:
  784. // nest: the value to produce a flattened paths list for.
  785. // Yields:
  786. // Tuples containing index or key values which form the path to a specific
  787. // leaf value in the nested structure.
  788. // """
  789. //# The _maybe_add_final_path_element function is used below in order to avoid
  790. //# adding trailing slashes when the sub-element recursed into is a leaf.
  791. // if isinstance(nest, (dict, _collections.Mapping)):
  792. // for key in _sorted(nest):
  793. // value = nest[key]
  794. // for sub_path in yield_flat_paths(value):
  795. // yield (key,) + sub_path
  796. // elif _is_namedtuple(nest):
  797. // for key in nest._fields:
  798. // value = getattr(nest, key)
  799. // for sub_path in yield_flat_paths(value):
  800. // yield (key,) + sub_path
  801. // elif isinstance(nest, _six.string_types):
  802. // yield ()
  803. // elif isinstance(nest, _collections.Sequence):
  804. // for idx, value in enumerate(nest):
  805. // for sub_path in yield_flat_paths(value):
  806. // yield (idx,) + sub_path
  807. // else:
  808. // yield ()
  809. //def flatten_with_joined_string_paths(structure, separator="/"):
  810. // """Returns a list of (string path, data element) tuples.
  811. // The order of tuples produced matches that of `nest.flatten`. This allows you
  812. // to flatten a nested structure while keeping information about where in the
  813. // structure each data element was located. See `nest.yield_flat_paths`
  814. // for more information.
  815. // Args:
  816. // structure: the nested structure to flatten.
  817. // separator: string to separate levels of hierarchy in the results, defaults
  818. // to '/'.
  819. // Returns:
  820. // A list of (string, data element) tuples.
  821. // """
  822. // flat_paths = yield_flat_paths(structure)
  823. // def stringify_and_join(path_elements):
  824. // return separator.join(str(path_element) for path_element in path_elements)
  825. // flat_string_paths = [stringify_and_join(path) for path in flat_paths]
  826. // return list(zip(flat_string_paths, flatten(structure)))
  827. }
  828. }