Code Monkey home page Code Monkey logo

swix's Introduction

Swift Matrix and Machine Learning Library

Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-like ndarrays and autodiff. Fast AI has a good overview: https://www.fast.ai/2019/01/10/swift-numerics/

An alternate and much mature library is https://github.com/AlexanderTar/LASwift

Apple's Swift is a high level language that's asking for some numerical library to perform computation fast or at the very least easily. This is a bare-bones wrapper for that library.

A way to have iOS run high-level code similar to Python or Matlab is something I've been waiting for, and am incredibly excited to see the results. This will make porting complex signal processing algorithms to C much easier. Porting from Python/MATLAB to C was (and is) a pain in the butt, and this library aims to make the conversion between a Python/Matlab algorithm and a mobile app simple.

In most cases, this library calls Accelerate or OpenCV. If you want to speed up some function or add add another feature in those libraries, feel free to file an issue or submit a pull request (preferred!).

Currently, this library gives you

  • operators and various functions (sin, etc) that operate on entire arrays
  • helper function (reshape, reverse, delete, repeat, etc)
  • easy initializers for 1D and 2D arrays
  • complex math (dot product, matrix inversion, eigenvalues, etc)
  • machine learning algorithms (SVM, kNN, SVD/PCA, more to come)
  • one dimensional Fourier transforms
  • speed optimization using Accelerate and OpenCV

When I was crafting this library, I primarily followed the footsteps and example set by NumPy. For the more complex mathematical functions (e.g., SVD) I tested it against NumPy. Matlab, at least for the SVD, returns slightly different output.

Additionally, I followed NumPy's syntax whenever possible. For example, NumPy and Matlab differ in their initializer called ones by ones((M,N)) and ones(M, N) respectively. If in doubt or getting weird compiler bugs, look at NumPy for Matlab users or the section on possible swix bugs that may pop up during the Install or other Bugs you may find.

Details on how to install can be found in Install. The swix documentation includes details on each individual function and possible bugs.

Third Party Frameworks/Libraries

To be integrated

Uses

FAQ

Why does this library exist?

Not only should you be able to do simple math in arrays like in Surge, Swift makes it possible to call high level mathematical functions just like in Python/Matlab.

How does this library compare to Python/Matlab?

Complete speed results can be found in Speed

swix's People

Contributors

arondes avatar harenbrs avatar jcarlson23 avatar sergey-salnikov avatar stsievert avatar thomaskilian avatar torb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swix's Issues

Migration to newer constructors

With Swift still changing, factor methods are being replaced by constructors.
Not sure what timeline is best for this, but at some point code such as:

var x = String.stringWithContentsOfFile(prefix+"../"+filename, encoding: NSUTF8StringEncoding, error: nil)

needs to be changed to:

var x = String(contentsOfFile: prefix+"../"+filename, encoding: NSUTF8StringEncoding, error: nil)

to continue building (thats necessary as of Xcode 6.1 beta 2)

Ambiguous use of operator '/'

Hi!

I am trying to install swix in this project https://github.com/hollance/Forge/tree/master/Examples/YOLO. After following the swix manual install instructions of adding the swix folder to the project and adding swix-master/swix/swix/swix to the objective c bridging header and running, I get this error Ambiguous use of operator '/', pointing to these line of code as canidates.
func / (lhs: Int, rhs: Int) -> Double{
return lhs.double / rhs.double}
func / (lhs: Double, rhs: Int) -> Double{
return lhs / rhs.double}

I also get an error Use of unresolved identifier 'Process', pointing to this line of code

Process.launchedProcess(launchPath: "cd", arguments: [S2_PREFIX+";", PYTHON_PATH, "imshow.py", filename, "(save)", "(show)"])
print("savefig: Removing CSV FILE " + S2_PREFIX + "temp.csv")
Process.launchedProcess(launchPath: "rm", arguments: [S2_PREFIX+"temp.csv"])

This is on xcode version 8.3.3. What is the cause of these issues? Any advice on fixing?

Thank you so much!
Jordan

Plotting

Opinions on including some plotting functionality?
It would probably not be a random window popping up like in MATLAB, but maybe it could output to a PNG, or a UIView / NSView, which could then be embedded directly in the given app's view hierarchy?

Performance no acceptable

Running this little Python snippet on my machine takes 3.5 seconds:

import numpy as np
import time

t = time.time()
for i in range(1000):
    a = np.arange(100000)
    a += 1
    a = a * a
    a = a / a
    b = np.ones(100000)
    if not np.allclose(a, b): print "?"
print time.time() - t

The corresponding Swix-variant takes 33.7 seconds (factor 10 slower than NumPy).

intersection gives incorrect output

var a = array(1, 2, 3, 4)
var b = array(1, 2, 3, 4)
var c = intersection(a, b)
print(c) // array([])

This is obviously wrong and was pointed out by @srambhatla while working on a different project that incorporates swix.

The find function issue

the find function pretty much returns the original matrix. Modify it to take a logical statement like find (X > 0) and return

  1. argwhere (X > 0 )
  2. X [ argwhere( X > 0 ) ]
    this will also make it more MATLAB like.

creating eye(1)

eye(1) creates an empty matrix:

print(eye(1))

output:
matrix([])

Swift bridging header modification issue

Hi
I'm trying to install swix in my IOS app. To do that I follow the different instructions of the swix 0.30 documentation in the "How to creat a new iOS app that includes swix" paragraph.

For me all the different steps are ok until the Swift bridging header modification. As you can see in the attach image, i don't have the objective-c bridging header menu in my project setting.

Have you an idea of my problem?? (for information i used Xcode 8.2.1)

Best regards
bridging header swift

Detailed steps for installing swix on a new project

Please give a detailed and working steps for installing swix on a new project.
I tried several times to follow the given steps.
First I found that OpenCV must be added as Framework in order to compile the project.
In this way the project compiles but the functions cannot be called in any swift file, how to make swix to work?

Many thanks

Tests reference files outside local swix folder

Motivated by this email:

Thanks for Swix. The program is stuck because it cannot find the file below:
/Users/askoh/askoh/apple/swift/swix-master 2/python_testing/csvs/image.csv

What must I change in the program to make it run to completion.
Inside func write_csv
prefix String "/Users/askoh/Developer/swix/swix/swix/swix/"
filename String "../../python_testing/csvs/image.csv"

str.writeToFile(prefix+"../"+filename, atomically: false, encoding: NSUTF8StringEncoding, error: &error)

Thanks,
Aik-Siong Koh

In the function runTests, read/write CSV references ../../python_testing/csvs/. This means that tests do not run unless that folder is included (i.e., unless the repo is cloned). The tests should run independently of the file structure.

Ideas
  • Delete the file after testing. This might cause issues with iOS and OS X -- I don't think there's a system call on iOS. I'm not inclined to go with this option -- it excludes the Python testing.
  • Include another folder called testing in the local swix folder. I'm inclined to go with this option. The Python testing is reading files related to swix; those files should belong to swix.

Doesn't work with OpenCV 3

I followed the directions in your installation instructions and the library seems to be working well with the included OpenCV 2.4.9. I would like to make it work with OpenCV 3.X.X but when I try to replace the included OpenCV with OpenCV 3 I would get multiple errors:

error core.hpp header must be compiled as C++
error base.hpp header must be compiled as C++

It seems like they added #ifndef __cplusplus in core.hpp that forces C++ in OpenCV 3

Anyway I can get OpenCV 3 to work with this library?

Wrong FFT implementation

the fft and ifft implementations in complex-math.swift are wrong:

Seeding random variables

Random numbers currently have a fixed seed. Would be nice to have a command to control it. For e.g. MATLAB has rng for this.

image classification support

Hi Team,

does this library support the image classification or we can say object detection in image? for example, identifying cat, dog, etc in a image.

I will be working on my research topic where I need this support?

Regards,
Sandeep

Update to Swift 3

Many errors like:

Argument labels '(_:)' do not match any available overloads

swixMacOSX produces 199 errors

Just downloaded the latest version siwx-master and opened "swixMacOSX.xcodeproj it with Xcode6 beta 5 and received 199 errors. All were in 2 files "oneD-operators.swift" and "twoD-operators.swift"
I would like to use this file for 1D and 2D signal processing. I would especially like a Complex 2D FFT. I have been able to produce a working 1d version using the Accelerate framework (without the aid of swix) that works without any c- files, but a 2D version eludes, at present.
I have a Imac OSX 10.9.4

`fft` and `ifft` call `vDSP_create_fftsetupD` without calling the corresponding destroy function, resulting in memory leaks

In ndarray/complex-math.swift, fft(x: ndarray) and ifft(yr: ndarray, yi: ndarray) each call vDSP_create_fftsetupD to set up the following call of vDSP_fft_zropD. The system then allocates memory that is never released.

Simply appending a call to vDSP_destroy_fftsetupD at the end of each function would technically fix the leak, but these functions are not meant to be called in a performance-sensitive context anyway. In my own project, I have split the method into two, the first of which is only called once per value of N:

func fftInit(N: Int) -> FFTSetupD {
    var radix: FFTRadix = FFTRadix(FFT_RADIX2)
    var pass: vDSP_Length = vDSP_Length((log2(N.double)+1.0).int)
    var setup: FFTSetupD = vDSP_create_fftsetupD(pass, radix)
    return setup
}
func fft(x: ndarray, withSetup setup: FFTSetupD) -> (ndarray, ndarray) {
    //... (where the contents of fftInit above have been removed)
    vDSP_fft_zropD(setup, &x2, stride, &y, stride, log2n.length, dir)
    //...
}

The non-memory-leaking alternative closest to the original would be:

func fft(x: ndarray) -> (ndarray, ndarray) {
    //...
    var setup:FFTSetupD = vDSP_create_fftsetupD(pass, radix)
    //...
    vDSP_fft_zropD(setup, &x2, stride, &y, stride, log2n.length, dir)
    vDSP_destroy_fftsetupD(setup)
    //...
}

The reason I have not submitted a pull request is that clearly there is a decision to be made here: performance vs flexibility. My project requires performance, so I create the FFTSetupD structure once and reuse it over and over. Swix, as I understand it, is meant to emulate the NumPy model, where the flexibility of being able to use one single function call to compute the FFT is preferred over potential speed gains.

In summary, I would like to draw the maintainer's attention to the memory leak caused by repeated calls of fft or ifft, and introduced by vDSP_create_fftsetupD. I would also like to start a discussion on the limits of emulating the elegance of NumPy in Swift, and making number crunching in Swift fast and flexible at the same time.

Suggestion

This is a very cool project. I'm wondering if there is any interest in adding a shape attribute of ndarrays? I see there is some interest (for good reason) in being similar to numpy/matlab, and the program I'm writing makes its own shape variables for ndarrays. I'd make a pull request if project maintainers believe it would be a desirable and straightforward change.

issue about: linker command failed with exit code 1

Everything works fine on the Simulation, as soon as I plug any device I cannot install the App.
The following error appars.

ld: '/mypath/myproject/swix/objc/opencv2.framework/opencv2(knearest.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems a clang problem any idea how to solve it?

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.