forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpzDictionary.cs
More file actions
206 lines (167 loc) · 4.93 KB
/
NpzDictionary.cs
File metadata and controls
206 lines (167 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using Tensorflow.Util;
namespace Tensorflow.NumPy
{
public class NpzDictionary<T> : IDisposable, IReadOnlyDictionary<string, T>, ICollection<T>
where T : class,
ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
{
Stream stream;
ZipArchive archive;
bool disposedValue = false;
Dictionary<string, ZipArchiveEntry> entries;
Dictionary<string, T> arrays;
public NpzDictionary(Stream stream)
{
this.stream = stream;
this.archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true);
this.entries = new Dictionary<string, ZipArchiveEntry>();
foreach (var entry in archive.Entries)
this.entries[entry.FullName] = entry;
this.arrays = new Dictionary<string, T>();
}
public IEnumerable<string> Keys
{
get { return entries.Keys; }
}
public IEnumerable<T> Values
{
get { return entries.Values.Select(OpenEntry); }
}
public int Count
{
get { return entries.Count; }
}
public object SyncRoot
{
get { return ((ICollection)entries).SyncRoot; }
}
public bool IsSynchronized
{
get { return ((ICollection)entries).IsSynchronized; }
}
public bool IsReadOnly
{
get { return true; }
}
public T this[string key]
{
get { return OpenEntry(entries[key]); }
}
private T OpenEntry(ZipArchiveEntry entry)
{
T array;
if (arrays.TryGetValue(entry.FullName, out array))
return array;
using (Stream s = entry.Open())
{
array = Load_Npz(s);
arrays[entry.FullName] = array;
return array;
}
}
protected virtual T Load_Npz(Stream s)
{
return np.Load<T>(s);
}
public bool ContainsKey(string key)
{
return entries.ContainsKey(key);
}
public bool TryGetValue(string key, out T value)
{
value = default(T);
ZipArchiveEntry entry;
if (!entries.TryGetValue(key, out entry))
return false;
value = OpenEntry(entry);
return true;
}
public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
{
foreach (var entry in archive.Entries)
yield return new KeyValuePair<string, T>(entry.FullName, OpenEntry(entry));
}
IEnumerator IEnumerable.GetEnumerator()
{
foreach (var entry in archive.Entries)
yield return new KeyValuePair<string, T>(entry.FullName, OpenEntry(entry));
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
foreach (var entry in archive.Entries)
yield return OpenEntry(entry);
}
public void CopyTo(Array array, int arrayIndex)
{
foreach (var v in this)
array.SetValue(v, arrayIndex++);
}
public void CopyTo(T[] array, int arrayIndex)
{
foreach (var v in this)
array.SetValue(v, arrayIndex++);
}
public void Add(T item)
{
throw new ReadOnlyException();
}
public void Clear()
{
throw new ReadOnlyException();
}
public bool Contains(T item)
{
foreach (var v in this)
if (Object.Equals(v.Value, item))
return true;
return false;
}
public bool Remove(T item)
{
throw new ReadOnlyException();
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
archive.Dispose();
stream.Dispose();
}
archive = null;
stream = null;
entries = null;
arrays = null;
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
public class NpzDictionary : NpzDictionary<Array>
{
bool jagged;
public NpzDictionary(Stream stream, bool jagged)
: base(stream)
{
this.jagged = jagged;
}
protected override Array Load_Npz(Stream s)
{
//if (jagged)
//return np.LoadJagged(s);
return np.LoadMatrix(s);
}
}
}