Pre-processing data within a Julia structure

I would like to have a Julia structure with data and parameters contained with it, to be used with various functions. E.g., I start with data frame of numbers, but many functions use it as a matrix and also need to know its dimensions. In python I would use something like:

class my_class(object):
    def __init__(self, df):
        X = df.to_numpy()
        self.n, self.m = X.shape
        row_names = df.index.tolist()
        col_names = df.columns.tolist()

Foo = my_class(df)

What poses a difficulty is whether operations .to_numpy(), .shape, etc. can be performed in the Julia structure or whether beforehand. Or perhaps this necessitates using a mutable structure?

At my current level of knowledge I would do something like this:

struct MyClass
    df::DataFrame
    X::Matrix{Float64}
    n::Int64
    m::Int64
    row_names::Vector{String}
    col_names::Vector{String}
end

X = Matrix{Float64}(df[:,begin+1:end])
n, m = size(X)
row_names = df[:,begin]
col_names = names(SAT[:,begin+1:end])

Foo = MyClass(df, X, n, m, row_names, col_names)

Remark: thinking of it, I could probably wrap the code above in a function, which returns the initialized structure Foo. But it seems like a convoluted way of doing it.

The function you are talking about is called a constructor for the type MyClass and this is indeed the way to approach it. Note that you have the function __init__ in python that serves the same purpose?

4 Likes

Thank you! Based on the explanations in the link that you provided, the answer coudl be like this:

struct MyClass
    df::DataFrame
    X::Matrix{Float64}
    n::Int64
    m::Int64
    row_names::Vector{String}
    col_names::Vector{String}
    function MyClass(df::DataFrame)
        X = Matrix{Float64}(df[:,begin+1:end])
        n, m = size(X)
        row_names = df[:,begin]
        col_names = names(SAT[:,begin+1:end])
        new(df, X, n, m, row_names, col_names)
    end
end

Foo = MyClass(df)
1 Like