Project: s41467-025-61628-x | Function: create_tree (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Creates a linear regression tree by recursively finding optimal split points that minimize prediction error. This implements the tree-based segmentation algorithm used to divide developmental processes into short pseudotime sections for local pattern analysis.

**KEY TERMS:** {
  "feat": "feature index for splitting",
  "val": "split value",
  "S": "total squared error",
  "bestS": "best split error",
  "rate": "minimum improvement threshold",
  "dur": "minimum section duration"
}

**CONTEXT CODE:**             newS = model_error(mat0) + model_error(mat1)
            if newS < bestS:
                bestIndex = featIndex
                bestValue = splitVal
                bestS = newS

    return bestIndex, bestValue, model_leaf(dataSet), S, bestS


def is_tree(obj):
    return isinstance(obj, dict)


def model_tree_eval(model, inDat):
    n = np.shape(inDat)[1]
    X = np.ones((1, n+1))
    X[:, 1:n+1] = inDat
    return float(np.dot(X, model))


def tree_forecast(tree, inData):
    if not is_tree(tree):
        return model_tree_eval(tree, inData)
    if inData[tree['spInd']] > tree['spVal']:
        if is_tree(tree['left']):
            return tree_forecast(tree['left'], inData)
        else:
            return model_tree_eval(tree['left'], inData)
    else:
        if is_tree(tree['right']):
            return tree_forecast(tree['right'], inData)
        else:
            return model_tree_eval(tree['right'], inData)


def create_forecast(tree, testData):
    m = len(testData)
    yHat = np.zeros(m)
    for i in range(m):
        yHat[i] = tree_forecast(tree, np.mat(testData[i]))
    return yHat


def draw(dataSet, tree):
    plt.scatter(dataSet[:, 0], dataSet[:, 1], s=5)
    yHat = create_forecast(tree, dataSet[:, 0])
    plt.plot(dataSet[:, 0], yHat, linewidth=2.0, color='red')
    plt.show()


def create_tree(dataSet, rate, dur, step):
    import os
    if os.getenv('TEST_GOLDEN', 'true') == 'false':
        raise NotImplementedError('This function is masked for testing purposes.')
    if len(set(dataSet[:, -1])) == 1:
        return model_leaf(dataSet)
    feat, val, ws, S, bestS = choose_best_split(dataSet, dur, step)
    if (S - bestS) < rate:
        return ws
    retTree = {}
    retTree['spInd'] = feat
    retTree['spVal'] = val
    lSet, rSet = bin_split_dataset(dataSet, feat, val)
    retTree['left'] = create_tree(lSet, rate, dur, step)
    retTree['right'] = create_tree(rSet, rate, dur, step)
    return retTree


if __name__ == '__main__':
    import tushare as ts

    df = ts.get_k_data(code='002230', start='2000-01-01')
    e = pd.DataFrame()
    e['idx'] = df.index
    e['close'] = df['close']
    arr = np.array(e)

    tree = create_tree(arr, 100, 10, 1)
    draw(arr, tree)




Complete the function implementation replacing "raise NotImplementedError".