Code Monkey home page Code Monkey logo

Comments (5)

root-11 avatar root-11 commented on May 26, 2024

This is a great addition!

from tablite.

root-11 avatar root-11 commented on May 26, 2024

Would this achieve the desired result?

import numpy as np
from tablite.base import Table, Column
from tablite.reindex import reindex 
from tablite.utils import unique_name

def match(T, other, *criteria):
    """
    matches value from T with other
    """
    # analyze
    for a,_,b in criteria:
        assert _ == "=="
        
        indices = other[b].index()  # {'a':[0,2], 'b': [1,4], 'c': [3]}
        index = np.full((len(T), ), -1, dtype=int)
        for ix,v in enumerate(T[a][:]):
            if index[ix] == -1:
                index[ix] = indices[v][0]
    # write
    result = T.copy()
    second = reindex(other, index)
    for name in other.columns:
        revised_name = unique_name(name, result.columns)
        result[revised_name] = second[name]
    return result


def test():
    from tablite import Table
    from tablite.config import Config

    Config.PAGE_SIZE = 1

    a = Table({
        "bom_id": [1, 2, 3, 4, 5], 
        "partial_of": [1, 2, 3, 4, 5], 
        "sku": ["A", "empty tote", "empty carton", "pkd tote", "pkd carton"], 
        "material_id": [None, None, None, 2, 3], 
        "quantity": [1, 1, 1, 1, 1], 
        "L": [1, 5.1, 25.5, None, None], 
        "W": [1, 5.1, 25.5, None, None], 
        "H": [1, 5.1, 0.1, None, None], 
        "M": [1, 0.1, 0.5, None, None], 
        "L2": [None, None, None, None, None], 
        "W2": [None, None, None, None, None], 
        "H2": [None, None, None, None, None], 
        "M2": [None, None, None, None, None], 
        "packtype": ["item", "material", "material", "tote", "carton"], 
        "properties": [None, None, None, None, None]
        })
    b = Table({
        "bom_id": [1, 5], 
        "sku": ["item (A)", "carton (A)"], 
        "supply_time": ["P0DT1200.0S", "P0DT1200.0S"], 
        "moq": [1, 1]
        })

    lup = (("bom_id", "==", "bom_id"), ("partial_of", "==", "bom_id"))
    r = a.lookup(b, *lup, all=False)

    r = match(a,b, *lup)
     

or shall we write it in nim?

from tablite.

realratchet avatar realratchet commented on May 26, 2024

Writing in nim is a major task as it requires writing numpy loader from scrach, while we only need a small subsect of types supported it's still non-trivial to support pickled MetaArray pages and the like.

As for implementation, seems to be crashing

import numpy as np
from tablite import Table
from tablite.reindex import reindex 
from tablite.utils import unique_name

def match(T, other, *criteria):
    """
    matches value from T with other
    """
    # analyze
    for a,_,b in criteria:
        assert _ == "=="
        
        indices = other[b].index()  # {'a':[0,2], 'b': [1,4], 'c': [3]}
        index = np.full((len(T), ), -1, dtype=int)
        for ix,v in enumerate(T[a][:]):
            if index[ix] == -1:
                index[ix] = indices[v][0]
    # write
    result = T.copy()
    second = reindex(other, index)
    for name in other.columns:
        revised_name = unique_name(name, result.columns)
        result[revised_name] = second[name]
    
    return result

normalized_bom = Table({'bom_id': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'partial_of': [1, 2, 3, 4, 5, 6, 7, 4, 6], 'sku': ['A', 'irrelevant', 'empty carton', 'pkd carton', 'empty pallet', 'pkd pallet', 'pkd irrelevant', 'ppkd carton', 'ppkd pallet'], 'material_id': [None, None, None, 3, None, 5, 3, 3, 5], 'quantity': [1, 1, 1, 1, 1, 1, 1, 1, 1], 'L': [0.25, 0.1, 0.6, 0.6, 1.2, 1.2, 0.6, 0.6, 1.2], 'W': [0.15, 0.1, 0.4, 0.4, 1.0, 1.0, 0.4, 0.4, 1.0], 'H': [0.15, 0.1, 0.3, 0.3, 0.15, 0.15, 0.3, 0.3, 0.15], 'M': [0.13, 1.0, 0.3, 0.3, 12.5, 12.5, 0.3, 0.3, 12.5], 'L2': [None, None, 0.6, 0.6, 1.2, 1.2, 0.6, 0.6, 1.2], 'W2': [None, None, 0.4, 0.4, 1.0, 1.0, 0.4, 0.4, 1.0], 'H2': [None, None, 0.3, 0.3, 1.8, 1.8, 0.3, 0.3, 1.8], 'M2': [None, None, 40.0, 40.0, 1200.0, 1200.0, 40.0, 40.0, 1200.0], 'packtype': ['item', 'item', 'material', 'carton', 'material', 'pallet', 'carton', 'carton', 'pallet'], 'properties': [None, None, None, None, None, None, None, None, None]})
accepts = Table({'bom_id': [6], 'sku': ['pkd pallet'], 'supply_time': [np.timedelta64(3600000000,'us')], 'moq': [1]})
products_lookup = normalized_bom.lookup(accepts, ("bom_id", "==", "bom_id"), ("partial_of", "==", "bom_id"), all=False)
products = products_lookup.all(bom_id_1=lambda x: x is not None)

products_matched = match(normalized_bom, accepts, ("bom_id", "==", "bom_id"), ("partial_of", "==", "bom_id"))

assert products.to_dict() == products_matched.to_dict()

from tablite.

root-11 avatar root-11 commented on May 26, 2024

Match operator is now in tablite/match.py

Please verify and I will add it to core.py.

from tablite.

root-11 avatar root-11 commented on May 26, 2024

Added to core for release in 2023.9

from tablite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.