xp1632 / VPE_IP

0 stars 0 forks source link

VP code summary- How does code generator work? #56

Open xp1632 opened 5 months ago

xp1632 commented 5 months ago

Now I'll do a final summary of generator_related_code in the VP project:


visual_program:

generation result


code_generator and python generator


xp1632 commented 4 months ago

generators/visual_program.ts

xp1632 commented 4 months ago
xp1632 commented 4 months ago

generators/generation_result.ts

default (4)

xp1632 commented 4 months ago

code_generator.ts

_cgi-bin_mmwebwx-bin_webwxgetmsgimg__ MsgID=8788316186860248504 skey=@crypt_e8cb0f1c_3023c93a6f29d50b061ee72100621d30 mmweb_appid=wx_webfilehelper

xp1632 commented 4 months ago

python_generator.ts:

xp1632 commented 4 months ago
xp1632 commented 4 months ago
xp1632 commented 4 months ago

CaptureImageCode function for intermediate result:

image

image


image

image

-Then it gets a conversion from the current data type of the image to a numpy array using the getConversion method of this.imageTypeConvert.


image


In summary, the captureImageCode function generates Python code that captures an image from a given variable, converts it to a base64 string, and sends it to a specific DOM element in a Jupyter notebook.

xp1632 commented 3 months ago

More information about getInputValueOfNode and getOutputValueOfNode


/**
 * Generates the Python code for an exec node given its inputs and outputs.
 * The function can be adapted to handle more complex scenarios and various metadata types.
 *
 * @param inputs An array of strings representing literals or variable names e.g., '1', '"str"'. 
 * @param outputs An array of strings for variable names or subsequent Python code.
 * @param node The node for which code is being generated.
 * @param generator An object providing context and utility functions for code generation.
 *
 * @returns  A string containing the generated Python code snippet.
 */
function code(inputs, outputs, node, generator) {  
 // 1. Read an image and assign it to an output variable.
 // 2. Create a metadata dictionary and attach when dealing with image data.
 // 3. Prepare the execution sequence for the subsequent code.
  const code = `${outputs[1]} = io.read_image(${inputs[1]}, ${inputs[2]})
${outputs[1]} = {
  'value': ${outputs[1]},
  'dataType': 'torch.tensor',
  'metadata': {
    'colorChannel': 'rgb',
    'channelOrder': 'channelFirst',
    'isMiniBatched': false,
    'intensityRange': '0-255',
    'device': 'cpu'
  }
}
${outputs[0]}`;
  return code;
}

// Convert the code generation function to a string format for JSON configuration.
const codeGeneratorFunction = code; // Assign the function for conversion
const jsonFormattedString = JSON.stringify(codeGeneratorFunction.toString());
console.log(jsonFormattedString);
  • @param inputs An array of strings representing literals or variable names e.g., '1', '"str"'.
  • @param outputs An array of strings for variable names or subsequent Python code.
nodeSourceGeneration(
    node: Node,
    inputs: string[],
    outputs: string[]
  ): string {
    const nodeGenerator: string = node.data.codeGenerator;
    if (!nodeGenerator) {
      console.warn(
        `Node type:${node.data.configType as string} has no source code`
      );
      return '';
    }
    // eslint-disable-next-line no-eval
    const func = eval(`(${nodeGenerator})`);
    // remove the trailing new line
    return func(inputs, outputs, node, this).replace(/\n+$/, '');
  }



python_generator.ts:

  • This file is more complex than I thought so I'll first write down the basic understanding to help sort it out

  • NodeToCode():

    • This function processes the input and output of a node to python code we need
    • we create result with NodeGenRes type to store generation result
    • we create prerequisiteCodeOfInputs to store the code should be executed before our current code
  • For every Input, we have getInputValueOfNode() method to

    - **For Node that input connects with no other handles**
    - indicates this node itself is a `valueNode`
    - we get its value and convert it to python format
    - details are in #57 
    - 
    - **For ImageInput**
    - set `converter` between `imageInput` and `imageOutput`
    - add imports that `convertFunction` needs
    - 
    - **For ValueInput**
    - We calls `nodeToCode()` for its `incomingNode`
    - And put the code we get to `currentNode`'s `prerequisiteCode`
    - add imports for `incomingNode`'s code's import 
  • Then we check the prerequisiteCode of this input

    • To avoid duplication, we only add the prerequisiteCode of current input when it's not already in the main prerequisiteCodeOfInputs