-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
700 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"dub": { | ||
"cachedUpgrades": { | ||
"pyd": "0.9.7" | ||
}, | ||
"lastUpgrade": "2015-04-23T15:06:38.966706" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "atmosphere_gm_charts", | ||
"name": "plotting_example", | ||
"dependencies": { | ||
"pyd": "~>0.9.4", | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,64 @@ | ||
import std.stdio; | ||
import pyd.pyd; | ||
import pyd.embedded; | ||
import pyd.extra; | ||
|
||
void main() { | ||
/++ | ||
`d_to_python_numpy_ndarray` converts a D array to numpy.ndarray. | ||
`toNumpyArray` is only an alias. | ||
+/ | ||
alias toNumpyArray = d_to_python_numpy_ndarray; | ||
|
||
/++ | ||
A static constructor is a function that performs initializations of | ||
thread local data before the `main()` function gets control for the | ||
main thread. | ||
Shared static constructors are executed before any static | ||
constructors, and are intended for initializing any shared global | ||
data. | ||
+/ | ||
shared static this() { | ||
//initializes PyD package. | ||
py_init(); | ||
} | ||
|
||
void main() | ||
{ | ||
auto pythonContext = new InterpContext(); | ||
/+ | ||
Uniform Function Call Syntax (UFCS) | ||
is used in the following line if code. | ||
} | ||
Equivalent code would be just: | ||
-------- | ||
pythonContext.sample = toNumpyArray(readData("view/data.txt")); | ||
-------- | ||
+/ | ||
pythonContext.sample = "view/data.txt".readData.toNumpyArray; | ||
pythonContext.py_stmts(script); | ||
} | ||
|
||
double[] readData(string file) | ||
{ | ||
import std.algorithm.iteration : map, splitter; | ||
import std.array : array; | ||
import std.conv : to; | ||
import std.file : readText; | ||
|
||
return file | ||
.readText //Reads the contents of a text file into a string. | ||
.splitter //Lazily splits words. | ||
.map!(to!double) //Lazily converts words to doubles. | ||
.array; //Creates an array. | ||
} | ||
|
||
immutable script = ` | ||
import numpy as np | ||
import matplotlib | ||
import matplotlib.pyplot as plt | ||
num_bins = 50 | ||
ax = plt.subplot() | ||
n, bins, patches = ax.hist(sample, num_bins, normed=1, label='sample') | ||
plt.show() | ||
`; |
Oops, something went wrong.