Skip to content

Commit

Permalink
Merge pull request #379 from wearepal/complement-comp-bugfix
Browse files Browse the repository at this point in the history
Bugfix: apply mask to outputs from Compliment Component.
  • Loading branch information
paulthatjazz authored Jun 5, 2024
2 parents a6219c0 + c53eb2e commit fe7110d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
4 changes: 3 additions & 1 deletion app/javascript/modelling/worker/performOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function performOperation(operationName, ...inputs) {

for (let x = out.x; x < out.x + out.width; ++x) {
for (let y = out.y; y < out.y + out.height; ++y) {
out.set(x, y, operation.fn(...inputs.map(i => i.get(x, y, zoom))))
out.set(x, y,
operation.fn(...inputs.map(i => i.get(x, y, zoom)))
)
}
}
return out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ import { PreviewControl } from '../controls/preview'
import { isEqual } from 'lodash'
import { NumericConstant } from '../numeric_constant'
import { numericNumberDataSocket } from '../socket_types'
import { ProjectProperties } from '.'
import { maskFromExtentAndShape } from '../bounding_box'

export class BinaryOpComponent extends BaseComponent {
operator: string
operation: string
inputSocket: Socket
outputSocket: Socket
projectProperties: ProjectProperties

constructor(operation: string, operator: string, inputSocket: Socket, outputSocket: Socket, category: string) {
constructor(operation: string, operator: string, inputSocket: Socket, outputSocket: Socket, category: string, projectProperties: ProjectProperties) {
super(operation)
this.operator = operator
this.operation = operation
this.inputSocket = inputSocket
this.outputSocket = outputSocket
this.category = category
this.contextMenuName = operator ? `${operation} (${operator})` : operation
this.projectProperties = projectProperties
}

async builder(node: Node) {
Expand Down Expand Up @@ -66,6 +70,7 @@ export class BinaryOpComponent extends BaseComponent {

if (outputs['out'] instanceof BooleanTileGrid || outputs['out'] instanceof NumericConstant) outputs['out'].name = (editorNode.data.name as string !== undefined && editorNode.data.name as string !== "") ? editorNode.data.name as string : `${this.operation} data`



const previewControl: any = editorNode.controls.get('Preview')
previewControl.update()
Expand Down
39 changes: 26 additions & 13 deletions app/javascript/projects/modelling/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ import { DesignationsComponent } from "./designations_component"
import { ORValComponent } from "./orval_component"
import { IMDComponent } from "./imd_component"

export interface ProjectProperties {
extent: Extent
zoom: number
mask: boolean
maskLayer: string
maskCQL: string
}

export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: SaveModel, getDatasets: getDatasets, extent: Extent, zoom: number, mask: boolean, maskLayer: string, maskCQL: string): BaseComponent[] {

const projectProps = { extent, zoom, mask, maskLayer, maskCQL }

return [
// TODO: Replace extent, mask, zoom, maskLayer, maskCQL with projectProps in all components

// Inputs
new UkcehLandCoverComponent(extent, zoom, mask, maskLayer, maskCQL),
new LehLandCoverComponent(extent, zoom, mask, maskLayer, maskCQL),
Expand Down Expand Up @@ -75,27 +88,27 @@ export function createDefaultComponents(saveMapLayer: SaveMapLayer, saveModel: S
// Set operations
new VariadicOpComponent('Union', '⋃', booleanDataSocket, booleanDataSocket, 'Set operations'),
new VariadicOpComponent('Intersection', '⋂', booleanDataSocket, booleanDataSocket, 'Set operations'),
new BinaryOpComponent('Set difference', '−', booleanDataSocket, booleanDataSocket, 'Set operations'),
new BinaryOpComponent('Set difference', '−', booleanDataSocket, booleanDataSocket, 'Set operations', projectProps),
new VariadicOpComponent('Symmetric difference', 'Δ', booleanDataSocket, booleanDataSocket, 'Set operations'),
new UnaryOpComponent('Complement', '′', 'postfix', booleanDataSocket, booleanDataSocket, 'Set operations'),
new UnaryOpComponent('Complement', '′', 'postfix', booleanDataSocket, booleanDataSocket, 'Set operations', projectProps),

// Arithmetic
new MaskNumericDataComponent(),
new ExpressionComponent(),
new BinaryOpComponent('Min', '', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Max', '', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Min', '', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic', projectProps),
new BinaryOpComponent('Max', '', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic', projectProps),
new VariadicOpComponent('Sum', '∑', numericDataSocket, numericDataSocket, 'Arithmetic', 'Sum all inputs'),
new VariadicOpComponent('Merge', '', numericDataSocket, numericDataSocket, 'Arithmetic', 'Merge all inputs into a single dataset, NaN logic is overridden'),
new VariadicOpComponent('Product', '∏', numericDataSocket, numericDataSocket, 'Arithmetic'),
new BinaryOpComponent('Add', '+', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Subtract', '−', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Multiply', '×', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Divide', '÷', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new BinaryOpComponent('Power', '^', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic'),
new UnaryOpComponent('Negate', '−', 'prefix', numericDataSocket, numericDataSocket, 'Arithmetic'),
new UnaryOpComponent('Reciprocal', '⁻¹', 'postfix', numericDataSocket, numericDataSocket, 'Arithmetic'),
new BinaryOpComponent('Less', '<', numericNumberDataSocket, booleanDataSocket, 'Arithmetic'),
new BinaryOpComponent('Greater', '>', numericNumberDataSocket, booleanDataSocket, 'Arithmetic'),
new BinaryOpComponent('Add', '+', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic', projectProps),
new BinaryOpComponent('Subtract', '−', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic', projectProps),
new BinaryOpComponent('Multiply', '×', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic', projectProps),
new BinaryOpComponent('Divide', '÷', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic', projectProps),
new BinaryOpComponent('Power', '^', numericNumberDataSocket, numericNumberDataSocket, 'Arithmetic', projectProps),
new UnaryOpComponent('Negate', '−', 'prefix', numericDataSocket, numericDataSocket, 'Arithmetic', projectProps),
new UnaryOpComponent('Reciprocal', '⁻¹', 'postfix', numericDataSocket, numericDataSocket, 'Arithmetic', projectProps),
new BinaryOpComponent('Less', '<', numericNumberDataSocket, booleanDataSocket, 'Arithmetic', projectProps),
new BinaryOpComponent('Greater', '>', numericNumberDataSocket, booleanDataSocket, 'Arithmetic', projectProps),
new ReplaceNaNComponent(),

// DEBUG TOOLS
Expand Down
28 changes: 25 additions & 3 deletions app/javascript/projects/modelling/components/unary_op_component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Input, Node, Output, Socket } from 'rete'
import { NodeData, WorkerInputs, WorkerOutputs } from 'rete/types/core/data'
import { PreviewControl } from '../controls/preview'
import { BooleanTileGrid } from '../tile_grid'
import { BooleanTileGrid, NumericTileGrid } from '../tile_grid'
import { workerPool } from '../../../modelling/workerPool'
import { BaseComponent } from './base_component'
import { ProjectProperties } from '.'
import { maskFromExtentAndShape } from '../bounding_box'

type Affix = 'prefix' | 'postfix'

Expand All @@ -13,8 +15,9 @@ export class UnaryOpComponent extends BaseComponent {
affix: Affix
inputSocket: Socket
outputSocket: Socket
projectProperties: ProjectProperties

constructor(operation: string, operator: string, affix: Affix, inputSocket: Socket, outputSocket: Socket, category: string) {
constructor(operation: string, operator: string, affix: Affix, inputSocket: Socket, outputSocket: Socket, category: string, projectProperties: ProjectProperties) {
super(operation)
this.operator = operator
this.operation = operation
Expand All @@ -23,6 +26,7 @@ export class UnaryOpComponent extends BaseComponent {
this.outputSocket = outputSocket
this.category = category
this.contextMenuName = `${operation} (${operator})`
this.projectProperties = projectProperties
}

async builder(node: Node) {
Expand All @@ -48,6 +52,8 @@ export class UnaryOpComponent extends BaseComponent {
const editorNode = this.editor?.nodes.find(n => n.id === node.id)
if (editorNode === undefined) { return }

const mask = await maskFromExtentAndShape(this.projectProperties.extent, this.projectProperties.zoom, this.projectProperties.maskLayer, this.projectProperties.maskCQL)

if (inputs['a'].length === 0) {
editorNode.meta.errorMessage = 'No input'
}
Expand All @@ -59,10 +65,26 @@ export class UnaryOpComponent extends BaseComponent {
else {
delete editorNode.meta.errorMessage
editorNode.meta.previousInput = inputs['a'][0]
editorNode.meta.output = outputs['out'] = await workerPool.queue(async worker =>
const result = await workerPool.queue(async worker =>
worker.performOperation(this.name, inputs['a'][0])
)

if(mask) {
// TODO: a little dirty and slightly inefficient. Move into performOperation with mask as an optional argument
if(result instanceof BooleanTileGrid) {
result.iterate((x, y, value) => {
if(!mask.get(x, y)) result.set(x, y, false)
})
}
if(result instanceof NumericTileGrid) {
result.iterate((x, y, value) => {
if(!mask.get(x, y)) result.set(x, y, NaN)
})
}
}

editorNode.meta.output = outputs['out'] = result

}

if (outputs['out'] instanceof BooleanTileGrid) outputs['out'].name = (editorNode.data.name as string !== undefined && editorNode.data.name as string !== "") ? editorNode.data.name as string : `${this.operation} data`
Expand Down

0 comments on commit fe7110d

Please sign in to comment.