Skip to content

Commit

Permalink
Update numpy.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Aug 6, 2023
1 parent 9d73621 commit f044871
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 102 deletions.
110 changes: 19 additions & 91 deletions source/numpy.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,56 +158,26 @@ numpy.ModelFactory = class {
numpy.Model = class {

constructor(format, graphs) {
this._format = format;
this._graphs = graphs.map((graph) => new numpy.Graph(graph));
}

get format() {
return this._format;
}

get graphs() {
return this._graphs;
this.format = format;
this.graphs = graphs.map((graph) => new numpy.Graph(graph));
}
};

numpy.Graph = class {

constructor(graph) {
this._name = graph.name || '';
this._nodes = graph.layers.map((layer) => new numpy.Node(layer));
}

get name() {
return this._name;
}

get inputs() {
return [];
}

get outputs() {
return [];
}

get nodes() {
return this._nodes;
this.name = graph.name || '';
this.nodes = graph.layers.map((layer) => new numpy.Node(layer));
this.inputs = [];
this.outputs = [];
}
};

numpy.Argument = class {

constructor(name, value) {
this._name = name;
this._value = value;
}

get name() {
return this._name;
}

get value() {
return this._value;
this.name = name;
this.value = value;
}
};

Expand All @@ -217,20 +187,9 @@ numpy.Value = class {
if (typeof name !== 'string') {
throw new numpy.Error("Invalid value identifier '" + JSON.stringify(name) + "'.");
}
this._name = name;
this._initializer = initializer || null;
}

get name() {
return this._name;
}

get type() {
return this._initializer.type;
}

get initializer() {
return this._initializer;
this.name = name;
this.type = initializer.type;
this.initializer = initializer || null;
}
};

Expand Down Expand Up @@ -272,63 +231,32 @@ numpy.Node = class {
numpy.Tensor = class {

constructor(array) {
this._type = new numpy.TensorType(array.dtype.__name__, new numpy.TensorShape(array.shape));
this._byteorder = array.dtype.byteorder;
this._data = this._type.dataType == 'string' || this._type.dataType == 'object' ? array.flatten().tolist() : array.tobytes();
}

get type() {
return this._type;
}

get category() {
return 'NumPy Array';
}

get layout() {
return this._type.dataType == 'string' || this._type.dataType == 'object' ? '|' : this._byteorder;
}

get values() {
return this._data;
this.type = new numpy.TensorType(array.dtype.__name__, new numpy.TensorShape(array.shape));
this.values = this.type.dataType == 'string' || this.type.dataType == 'object' ? array.flatten().tolist() : array.tobytes();
this.layout = this.type.dataType == 'string' || this.type.dataType == 'object' ? '|' : array.dtype.byteorder;
}
};

numpy.TensorType = class {

constructor(dataType, shape) {
this._dataType = dataType;
this._shape = shape;
}

get dataType() {
return this._dataType || '?';
}

get shape() {
return this._shape;
this.dataType = dataType || '?';
this.shape = shape;
}

toString() {
return this.dataType + this._shape.toString();
return this.dataType + this.shape.toString();
}
};

numpy.TensorShape = class {

constructor(dimensions) {
this._dimensions = dimensions;
}

get dimensions() {
return this._dimensions;
this.dimensions = dimensions;
}

toString() {
if (!this._dimensions || this._dimensions.length == 0) {
return '';
}
return '[' + this._dimensions.join(',') + ']';
return this.dimensions && this.dimensions.length > 0 ? '[' + this.dimensions.join(',') + ']' : '';
}
};

Expand Down
3 changes: 1 addition & 2 deletions source/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -3619,7 +3619,7 @@ python.Execution = class {
let header = file.read(header_length);
const decoder = new TextDecoder(major >= 3 ? 'utf-8' : 'ascii');
header = decoder.decode(header);
header = JSON.parse(header.replace(/\(/,'[').replace(/\)/,']').replace('[,','[1,]').replace(',]',',1]').replace(/'/g, '"').replace(/:\s*False\s*,/,':false,').replace(/:\s*True\s*,/,':true,').replace(/,\s*\}/, ' }'));
header = JSON.parse(header.replace(/\(/,'[').replace(/\)/,']').replace('[,','[1,]').replace(',]',']').replace(/'/g, '"').replace(/:\s*False\s*,/,':false,').replace(/:\s*True\s*,/,':true,').replace(/,\s*\}/, ' }'));
if (!header.descr || header.descr.length < 2) {
throw new python.Error("Missing property 'descr'.");
}
Expand Down Expand Up @@ -3769,7 +3769,6 @@ python.Execution = class {
context.view = new DataView(context.data.buffer, context.data.byteOffset, size);
encode(context, a, 0);
return self.invoke('numpy.ndarray', [ shape, dtype, context.data ]);

});
this.registerFunction('numpy.ma.core._mareconstruct', function(subtype, baseclass, baseshape, basetype) {
const data = self.invoke(baseclass, [ baseshape, basetype ]);
Expand Down
12 changes: 3 additions & 9 deletions source/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,8 @@ view.NodeSidebar = class extends view.ObjectSidebar {
switch (attribute.type) {
case 'tensor': {
value = new view.ValueView(this._host, { type: attribute.value.type, initializer: attribute.value }, '');
value.on('export-tensor', (sender, value) => this.emit('export-tensor', value));
value.on('error', (sender, value) => this.emit('error', value));
break;
}
case 'tensor[]': {
Expand Down Expand Up @@ -2511,15 +2513,7 @@ view.AttributeView = class extends view.Control {
break;
}
case 'tensor': {
const value = {
name: '',
initializer: attribute.value
};
const item = new view.ValueView(host, value);
for (const element of item.render()) {
this._element.appendChild(element);
}
break;
throw new view.Error('Attribute view tensor not implemented.');
}
default: {
let content = new view.Formatter(value, type).toString();
Expand Down

0 comments on commit f044871

Please sign in to comment.