text:
struct CityIndex
    value::String
end

function CityIndex(int::Int64)
    return CityIndex(string(int))
end

mutable struct City

    x::Float64
    y::Float64

    index::CityIndex
end

function City(x::Float64, y::Float64, index::Int64)
    return City(x, y, CityIndex(index))
end

function distance(city_1::City, city_2::City)
    return sqrt( (city_1.x - city_2.x)^2 + (city_1.y - city_2.y)^2 )
end

ext: jl


text:
### A Pluto.jl notebook ###
# v0.17.3

using Markdown
using InteractiveUtils

# ╔═╡ 3b168ad0-a058-4b58-83f2-88d43f5f759e
# hideall

using Genie, Genie.Router, Genie.Requests, Genie.Renderer.Json

# ╔═╡ a9da1cae-319b-11ec-0640-9f3d4f563ec0
md"""
# Using JSON payloads

A very common design pattern, especially when developing REST APIs, is to accept JSON payloads sent as `application/json` data over `POST` requests. Genie efficiently handles this use case through the utility function `Requests.jsonpayload`. Under the cover, Genie will process the `POST` request and will attempt to parse the JSON text payload. If this fails, you can still access the raw data (the text payload not converted to JSON) by using the `Requests.rawpayload` method.

### Example
"""

# ╔═╡ da62003e-14b1-484d-9970-6220be9e9901
md"""

```julia
using Genie, Genie.Router, Genie.Requests, Genie.Renderer.Json

route("/jsonpayload", method = POST) do
  @show jsonpayload()
  @show rawpayload()

  json("Hello $(jsonpayload()["name"])")
end

up()
```
"""

# ╔═╡ b18f7c01-2198-4d5d-877e-ce07f18df258
# hideall

route("/jsonpayload", method = POST) do
  @show jsonpayload()
  @show rawpayload()

  json("Hello $(jsonpayload()["name"])")
end;

# ╔═╡ 1bcb3a09-b0b8-41a9-ae2b-385b408a9e28
# hideall

up(async=true);

# ╔═╡ c82c424a-4c09-45e0-bb3e-6a2fe959c716
md"""
Next we make a `POST` request using the `HTTP` package:
"""

# ╔═╡ 69b9d891-deb1-4793-bf14-4b219ca076bb
md"""

```julia
using HTTP

HTTP.request("POST", "http://localhost:8000/jsonpayload", [("Content-Type", "application/json")], \"""{"name":"Adrian"}\""")
```

We will get the following output:

```julia
jsonpayload() = Dict{String,Any}("name"=>"Adrian")
rawpayload() = "{\"name\":\"Adrian\"}"

INFO:Main: /jsonpayload 200

HTTP.Messages.Response:
\"""
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked

"Hello Adrian"
\"""
```
"""

# ╔═╡ 8801795d-2e75-404c-b51f-aa6c949cda60
# hideall

#using HTTP

# ╔═╡ a2e870b8-ac88-460e-8c88-70f2f2419c44
# hideall

#HTTP.request("POST", "http://localhost:8000/jsonpayload", [("Content-Type", "application/json")], """{"name":"Adrian"}""");

# ╔═╡ f293a37d-3076-4762-965c-3604cf046057
md"""

```julia
down() # shutdown server
```

"""

# ╔═╡ ef740069-4020-449b-a916-83245c845f56
# hideall

down();

# ╔═╡ f8f5a72d-33f6-403a-b66c-377be78029bb
md"""

First, for the two `@show` calls, notice how `jsonpayload` had successfully converted the `POST` data to a `Dict`. While the `rawpayload` returns the `POST` data as a `String`, exactly as received. Finally, our route handler returns a JSON response, greeting the user by extracting the name from within the `jsonpayload` `Dict`.
"""

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
Genie = "c43c736e-a2d1-11e8-161f-af95117fbd1e"

[compat]
Genie = "~4.9.1"
"""

# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised

[[ArgParse]]
deps = ["Logging", "TextWrap"]
git-tree-sha1 = "3102bce13da501c9104df33549f511cd25264d7d"
uuid = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
version = "1.1.4"

[[ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"

[[Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[CSTParser]]
deps = ["Tokenize"]
git-tree-sha1 = "f9a6389348207faf5e5c62cbc7e89d19688d338a"
uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
version = "3.3.0"

[[CodeTracking]]
deps = ["InteractiveUtils", "UUIDs"]
git-tree-sha1 = "9aa8a5ebb6b5bf469a7e0e2b5202cf6f8c291104"
uuid = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
version = "1.0.6"

[[CommonMark]]
deps = ["Crayons", "JSON", "URIs"]
git-tree-sha1 = "4aff51293dbdbd268df314827b7f409ea57f5b70"
uuid = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
version = "0.8.5"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "44c37b4636bc54afac5c574d2d02b625349d6582"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.41.0"

[[Crayons]]
git-tree-sha1 = "249fe38abf76d48563e2f4556bebd215aa317e15"
uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
version = "4.1.1"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "3daef5523dd2e769dad2365274f760ff5f282c7d"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.11"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

[[EzXML]]
deps = ["Printf", "XML2_jll"]
git-tree-sha1 = "0fa3b52a04a4e210aeb1626def9c90df3ae65268"
uuid = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
version = "1.1.0"

[[FilePathsBase]]
deps = ["Compat", "Dates", "Mmap", "Printf", "Test", "UUIDs"]
git-tree-sha1 = "04d13bfa8ef11720c24e4d840c0033d145537df7"
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
version = "0.9.17"

[[FileWatching]]
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"

[[GMP_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "781609d7-10c4-51f6-84f2-b8444358ff6d"

[[Genie]]
deps = ["ArgParse", "Dates", "Distributed", "EzXML", "FilePathsBase", "HTTP", "HttpCommon", "Inflector", "JSON3", "JuliaFormatter", "Logging", "Markdown", "MbedTLS", "Millboard", "Nettle", "OrderedCollections", "Pkg", "REPL", "Random", "Reexport", "Revise", "SHA", "Serialization", "Sockets", "UUIDs", "Unicode", "VersionCheck", "YAML"]
git-tree-sha1 = "d0362686961375e910e437c76ff4ed6f31b54fef"
uuid = "c43c736e-a2d1-11e8-161f-af95117fbd1e"
version = "4.9.1"

[[HTTP]]
deps = ["Base64", "Dates", "IniFile", "Logging", "MbedTLS", "NetworkOptions", "Sockets", "URIs"]
git-tree-sha1 = "0fa77022fe4b511826b39c894c90daf5fce3334a"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
version = "0.9.17"

[[HttpCommon]]
deps = ["Dates", "Nullables", "Test", "URIParser"]
git-tree-sha1 = "46313284237aa6ca67a6bce6d6fbd323d19cff59"
uuid = "77172c1b-203f-54ac-aa54-3f1198fe9f90"
version = "0.5.0"

[[Inflector]]
deps = ["Unicode"]
git-tree-sha1 = "8555b54ddf27806b070ce1d1cf623e1feb13750c"
uuid = "6d011eab-0732-4556-8808-e463c76bf3b6"
version = "1.0.1"

[[IniFile]]
deps = ["Test"]
git-tree-sha1 = "098e4d2c533924c921f9f9847274f2ad89e018b8"
uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f"
version = "0.5.0"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[JLLWrappers]]
deps = ["Preferences"]
git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.4.1"

[[JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "8076680b162ada2a031f707ac7b4953e30667a37"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.2"

[[JSON3]]
deps = ["Dates", "Mmap", "Parsers", "StructTypes", "UUIDs"]
git-tree-sha1 = "7d58534ffb62cd947950b3aa9b993e63307a6125"
uuid = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
version = "1.9.2"

[[JuliaFormatter]]
deps = ["CSTParser", "CommonMark", "DataStructures", "Pkg", "Tokenize"]
git-tree-sha1 = "da0c8830cebe2337093bb46fc117498517a9df80"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
version = "0.21.2"

[[JuliaInterpreter]]
deps = ["CodeTracking", "InteractiveUtils", "Random", "UUIDs"]
git-tree-sha1 = "6ca01d8e5bc75d178e8ac2d1f741d02946dc1853"
uuid = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"
version = "0.9.2"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"

[[LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"

[[LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[Libiconv_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778"
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.16.1+1"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[LoweredCodeUtils]]
deps = ["JuliaInterpreter"]
git-tree-sha1 = "f46e8f4e38882b32dcc11c8d31c131d556063f39"
uuid = "6f1432cf-f94c-5a45-995e-cdbf5db27b0b"
version = "2.2.0"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[MbedTLS]]
deps = ["Dates", "MbedTLS_jll", "Random", "Sockets"]
git-tree-sha1 = "1c38e51c3d08ef2278062ebceade0e46cefc96fe"
uuid = "739be429-bea8-5141-9913-cc70e7f3736d"
version = "1.0.3"

[[MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[Millboard]]
git-tree-sha1 = "ea6a5b7e56e76d8051023faaa11d91d1d881dac3"
uuid = "39ec1447-df44-5f4c-beaa-866f30b4d3b2"
version = "0.2.5"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

[[Nettle]]
deps = ["Libdl", "Nettle_jll"]
git-tree-sha1 = "a68340b9edfd98d0ed96aee8137cb716ea3b6dea"
uuid = "49dea1ee-f6fa-5aa6-9a11-8816cee7d4b9"
version = "0.5.1"

[[Nettle_jll]]
deps = ["Artifacts", "GMP_jll", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "eca63e3847dad608cfa6a3329b95ef674c7160b4"
uuid = "4c82536e-c426-54e4-b420-14f461c4ed8b"
version = "3.7.2+0"

[[NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[Nullables]]
git-tree-sha1 = "8f87854cc8f3685a60689d8edecaa29d2251979b"
uuid = "4d1e1d77-625e-5b40-9113-a560ec7a8ecd"
version = "1.0.0"

[[OrderedCollections]]
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.4.1"

[[Parsers]]
deps = ["Dates"]
git-tree-sha1 = "0b5cfbb704034b5b4c1869e36634438a047df065"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.2.1"

[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "2cf929d64681236a2e074ffafb8d568733d2e6af"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.3"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[ProgressMeter]]
deps = ["Distributed", "Printf"]
git-tree-sha1 = "afadeba63d90ff223a6a48d2009434ecee2ec9e8"
uuid = "92933f4c-e287-5a05-a399-4b506db050ca"
version = "1.7.1"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Reexport]]
git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
version = "1.2.2"

[[Requires]]
deps = ["UUIDs"]
git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7"
uuid = "ae029012-a4dd-5104-9daa-d747884805df"
version = "1.3.0"

[[Revise]]
deps = ["CodeTracking", "Distributed", "FileWatching", "JuliaInterpreter", "LibGit2", "LoweredCodeUtils", "OrderedCollections", "Pkg", "REPL", "Requires", "UUIDs", "Unicode"]
git-tree-sha1 = "2f9d4d6679b5f0394c52731db3794166f49d5131"
uuid = "295af30f-e4ad-537b-8983-00126c2a3abe"
version = "3.3.1"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[Scratch]]
deps = ["Dates"]
git-tree-sha1 = "0b4b7f1393cff97c33891da2a0bf69c6ed241fda"
uuid = "6c6a2e73-6563-6170-7368-637461726353"
version = "1.1.0"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[StringEncodings]]
deps = ["Libiconv_jll"]
git-tree-sha1 = "50ccd5ddb00d19392577902f0079267a72c5ab04"
uuid = "69024149-9ee7-55f6-a4c4-859efe599b68"
version = "0.3.5"

[[StructTypes]]
deps = ["Dates", "UUIDs"]
git-tree-sha1 = "d24a825a95a6d98c385001212dc9020d609f2d4f"
uuid = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
version = "1.8.1"

[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[[Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[TextWrap]]
git-tree-sha1 = "9250ef9b01b66667380cf3275b3f7488d0e25faf"
uuid = "b718987f-49a8-5099-9789-dcd902bef87d"
version = "1.0.1"

[[Tokenize]]
git-tree-sha1 = "0952c9cee34988092d73a5708780b3917166a0dd"
uuid = "0796e94c-ce3b-5d07-9a54-7f471281c624"
version = "0.5.21"

[[URIParser]]
deps = ["Unicode"]
git-tree-sha1 = "53a9f49546b8d2dd2e688d216421d050c9a31d0d"
uuid = "30578b45-9adc-5946-b283-645ec420af67"
version = "0.4.1"

[[URIs]]
git-tree-sha1 = "97bbe755a53fe859669cd907f2d96aee8d2c1355"
uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
version = "1.3.0"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[UrlDownload]]
deps = ["HTTP", "ProgressMeter"]
git-tree-sha1 = "05f86730c7a53c9da603bd506a4fc9ad0851171c"
uuid = "856ac37a-3032-4c1c-9122-f86d88358c8b"
version = "1.0.0"

[[VersionCheck]]
deps = ["Dates", "JSON3", "Logging", "Pkg", "Random", "Scratch", "UrlDownload"]
git-tree-sha1 = "89ef2431dd59344ebaf052d0737205854ded0c62"
uuid = "a637dc6b-bca1-447e-a4fa-35264c9d0580"
version = "0.2.0"

[[XML2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "Zlib_jll"]
git-tree-sha1 = "1acf5bdf07aa0907e0a37d3718bb88d4b687b74a"
uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"
version = "2.9.12+0"

[[YAML]]
deps = ["Base64", "Dates", "Printf", "StringEncodings"]
git-tree-sha1 = "3c6e8b9f5cdaaa21340f841653942e1a6b6561e5"
uuid = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
version = "0.4.7"

[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
"""

# ╔═╡ Cell order:
# ╟─a9da1cae-319b-11ec-0640-9f3d4f563ec0
# ╟─da62003e-14b1-484d-9970-6220be9e9901
# ╠═3b168ad0-a058-4b58-83f2-88d43f5f759e
# ╠═b18f7c01-2198-4d5d-877e-ce07f18df258
# ╠═1bcb3a09-b0b8-41a9-ae2b-385b408a9e28
# ╟─c82c424a-4c09-45e0-bb3e-6a2fe959c716
# ╟─69b9d891-deb1-4793-bf14-4b219ca076bb
# ╠═8801795d-2e75-404c-b51f-aa6c949cda60
# ╠═a2e870b8-ac88-460e-8c88-70f2f2419c44
# ╟─f293a37d-3076-4762-965c-3604cf046057
# ╠═ef740069-4020-449b-a916-83245c845f56
# ╟─f8f5a72d-33f6-403a-b66c-377be78029bb
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002


text:
#module preprocessing

#export readCSV, cellSelect

using DataFrames, Queryverse, DelimitedFiles, CSV, Plots

#add library function here

#function dictionary(cellno, )

function readCSV()
    #function should eventually take file as argument while calling

    f=open("/Users/nirmitdeshpande/.julia/dev/impedance/src/EIS1.txt")

    #splitting the text file into an array with cyc, f, Re(Z) and Im(Z)
    df=CSV.read(f, DataFrame; delim = '\t', type=Float64, header =1, select=[2,3,5,6])

    #df is [cycle number, frequency, Re(Z), Im(Z)]

    close(f)
    return df

end

function cellSelect(cyc1, cyc2)
    #read the entire CSV and split into select range, then return df subset
    df=readCSV()

   #df=df[(df[!,1] .> (cyc1)) .& (df[!,1] .< (cyc2)), :]
   df=df[(df[!,1] .== (cyc1)), :]

    #println(df)
    return df
end

#end

max_stars_repo_name: nirmitdeshpande/EISModeling

max_issues_repo_name: nirmitdeshpande/EISModeling

max_forks_repo_name: nirmitdeshpande/EISModeling


text:
_map(s, x) = x
_map(s, x::PyComplex) = PyComplex(s(x.real), s(x.imag))
_map(s, x::PyRange) = PyRange(s(x.start), s(x.stop), s(x.step))
_map(s, x::PySlice) = PySlice(s(x.start), s(x.stop), s(x.step))
_map(s, x::PyList) = PyList(s(v) for v in x.values)
_map(s, x::PyTuple) = PyTuple(s(v) for v in x.values)
_map(s, x::PyDict) = PyDict(s(k) => s(v) for (k, v) in x.items)
_map(s, x::PySet) = PySet(s(v) for v in x.values)
_map(s, x::PyFrozenSet) = PyFrozenSet(s(v) for v in x.values)
_map(s, x::PyFuncCall) = PyFuncCall(s(x.func), [s(x) for x in x.args], s(x.state))
_map(s, x::PyNewObj) = PyNewObj(s(x.cls), [s(x) for x in x.args], [s(k) => s(v) for (k, v) in x.kwargs], s(x.state))

walk(f, x; cache::IdDict=IdDict()) = get!(cache, x) do
    z = _map(x) do y
        walk(f, y; cache)
    end
    f(z)
end

simplify_walker(x) = x
simplify_walker(x::PyGlobal) = get(GLOBALS, (x.mod, x.attr), x)
simplify_walker(x::PyFuncCall) =
    try
        _funccall(x.func, x.args, x.state)
    catch err
        @debug "_funccall" x err
        x
    end

simplify(x; cache=IdDict()) = walk(simplify_walker, x; cache)

native_walker(x) = x
native_walker(x::PyComplex) = Complex(x.real, x.imag)
native_walker(x::PyRange) = StepRange(x.start, x.step, x.stop)
native_walker(x::PyList) = [x for x in x.values]
native_walker(x::PyTuple) = Tuple(x.values)
native_walker(x::PyDict) = Dict(k=>v for (k,v) in x.items)
native_walker(x::PySet) = Set(x for x in x.values)
native_walker(x::PyFrozenSet) = Set(x for x in x.values)
native_walker(x::PyBytes) = codeunits(String(x.values))
native_walker(x::PyByteArray) = copy(x.values)
native_walker(x::PyArray) = copy(x.values)

walk(f::Bool, x; cache::IdDict=IdDict()) = f ? walk(native_walker, x; cache) : x

map_walk(f, x; cache=IdDict()) = map(x->walk(f, x; cache), x)
map_simplify(x; cache=IdDict()) = map(x->simplify(x; cache), x)


text:
using CircModDesign
using Test

@testset "CircModDesign.jl" begin
    # Write your tests here.
end


text:
const allowed_errors = [
  ErrorException,
  UndefVarError,
  ArgumentError,
  MethodError,
  TypeError
]

function attempt_shard_load!(cur_package::Module, cur_info::FileInfo, cur_shard::Expr, is_test::Bool=false)

  is_include_call = (
    !is_test &&
    cur_shard.head == :call &&
    first(cur_shard.args) == :include
  )

  is_include_call && return true

  if cur_shard.head == :export
    append!(cur_info.parent.export_list, cur_shard.args)
    return true
  end

  if cur_shard.head == :module
    check_module_node!(cur_package, cur_info, cur_shard) || return false
  end

  cur_error = nothing

  cur_func_name = _get_function_name(cur_shard)

  methods_list = ( cur_func_name != nothing && isdefined(cur_package, cur_func_name) ) ?
    methods(getfield(cur_package, cur_func_name)) : []

  cur_symbol_count = get_method_count(cur_package)
  cur_docs_count = length(Docs.meta(cur_package))

  cur_time = @elapsed(
    try
      cur_package.eval(cur_shard)
    catch tmp_error
      cur_error = tmp_error
    end
  )

  if cur_error == nothing
    if cur_shard.head == :module
      cur_package.eval(parse("using $(cur_package).$(cur_shard.args[2])"))
    end

    cur_symbol_count -= get_method_count(cur_package)
    cur_docs_count -= length(Docs.meta(cur_package))

    cur_symbol_count *= -1
    cur_docs_count *= -1

    no_adds = iszero(cur_symbol_count)
    no_adds &= iszero(cur_docs_count)

    no_adds && return true

    setfield!(cur_shard, :head, :tuple)
    empty!(cur_shard.args)

    new_exports = Symbol[]

    for cur_export in cur_info.parent.export_list
      isdefined(cur_package, cur_export) || continue
      cur_package.eval(parse("export $(cur_export)"))
      push!(new_exports, cur_export)
    end

    filter!(cur_export -> !in(cur_export, new_exports), cur_info.parent.export_list)

    return true
  end

  while isa(cur_error, LoadError)
    cur_error = cur_error.error
  end

  is_valid_file = any(
    a_error -> isa(cur_error, a_error),
    allowed_errors
  )

  is_valid_file || load_invalid_file(cur_package, cur_info)

  methods_list = ( cur_func_name != nothing && isdefined(cur_package, cur_func_name) ) ?
    setdiff( methods(getfield(cur_package, cur_func_name)), methods_list) : []

  isempty(methods_list) || purge_corrupted_methods!(cur_package, methods_list)

  cur_info.time = cur_time

  cur_info.undef = nothing

  if isa(cur_error, UndefVarError)
    cur_info.undef = cur_error.var
  else
    cur_info.time *= 10
  end

  return false

end

function _get_function_name(cur_shard::Expr)
  cur_func_name = nothing

  if cur_shard.head == :macrocall && string(cur_shard.args[1]) == "Core.@doc"
    cur_shard = cur_shard.args[3]
    isa(cur_shard, Symbol) && return
  end

  if cur_shard.head == :function

    cur_func_name = cur_shard.args[1]

    isa(cur_func_name, Symbol) && return cur_func_name

    cur_func_name = cur_func_name.args[1]

    if isa(cur_func_name, Expr)

      if cur_func_name.head == :(.)
        cur_func_name = nothing
      else
        cur_func_name = cur_func_name.args[1]
      end

    end

    isa(cur_func_name, Expr) && ( cur_func_name = nothing )

  end

  cur_func_name
end


text:
__precompile__()
module VerTeX

#   This file is part of VerTeX.jl. It is licensed under the MIT license
#   Copyright (C) 2018 Michael Reed

export dict2toml, tex2dict, tex2toml, dict2tex, toml2tex, toml2dict
export zathura, latexmk, pdf, texedit

using Pkg, UUIDs, Dates, REPL, Requires
using Pkg.TOML, Pkg.Pkg2, REPL.TerminalMenus

function drawgraph end

global AUTHOR = "anonymous"

checkhome(path::String) = occursin(r"^~/",path) ? joinpath(homedir(),path[3:end]) : path

function relhome(path::String,home::String=homedir())
    reg = Regex("(?<=^$(replace(home,'/'=>"\\/")))\\/?\\X+")
    return occursin(reg,path) ? '~'*match(reg,path).match : path
end

function preamble(path::String="default.tex",repo::String="julia")
    depos = getdepot()
    !haskey(depos,repo) && throw(error("did not load preamble, $repo depot not found"))
    load = ""
    try
        open(joinpath(checkhome(depos[repo]),path), "r") do f
            load = read(f,String)
        end
    catch
        open(joinpath("..","vtx",path), "r") do f
            load = read(f,String)
        end
    end
    dep = repo ≠ "julia" ? "$repo:~:" : ""
    return load*"%vtx:$dep$(relhome(path))"
end

function article(str::String,pre::String=preamble()*"\n")
    return pre*"\n\\begin{document}\n"*str*"\n\\end{document}"
end

function dict2toml(data::Dict)
    io = IOBuffer()
    TOML.print(io,data)
    return String(take!(io))
end

toml2dict(toml::String) = TOML.parse(toml)

const regtextag = "[[:alnum:] !\"#\$%&'()*+,\\-.\\/:;<=>?@\\[\\]^_`|~]+"

function textagdel(tag::Symbol,tex::String)
    join(split(tex,Regex("\n?(\\\\$tag{)"*regtextag*"(})")))
end

function texlocate(tag::Symbol,tex::String,neg::String="none")
    out = collect((m.match for m = eachmatch(Regex("(?<=\\\\$tag{)"*regtextag*"(?=})"), tex)))
    return isempty(out) ? [neg] : join.(out)
end

function lbllocate(tex::String,neg::String="none")
    out = collect((m.match for m = eachmatch(Regex("(?<=\\\\lbl{)"*regtextag*"(}{)"*regtextag*"(?=})"), tex)))
    return isempty(out) ? [neg] : replace.(join.(out), Ref("}{"=>":"))
end

tomlocate(tag::String,data::Dict,neg::String="none") = haskey(data,tag) ? data[tag] : neg

setval!(d::Dict,key,val) = haskey(d,key) ? (d[key] = val) : push!(d,key=>val)
addval!(d::Dict,key,val) = haskey(d,key) ? (val ∉ d[key] && push!(d[key],val)) : push!(d,key=>Any[val])
addkey!(d::Dict,key,val,pair) = !haskey(d[key],val) && push!(d[key],val=>pair)

preview_vertex(title,author,date,doc) = @info "$title by $author ($date)\n$doc"
preview_vertex(data::Dict) = preview_vertex(data["title"],data["author"],data["date"],data["tex"])

function checkmerge(a::DateTime,data,title,author,date,doc,msg="Merge conflict detected, proceed?")
    val = 2
    errors = ["",""]
    if a ≠ DateTime(data["revised"])
        errors[1] = "VerTeX: unable to merge into \"$(data["dir"])\" ($(data["revised"]))"
        errors[2] = "BUFFER: invalid date stamp $a"
    end
    if errors ≠ ["",""]
        @warn "$(errors[1])"
        preview_vertex(data)
        @warn "$(errors[2])"
        preview_vertex(title,author,date,doc)
        val = request(msg,RadioMenu(["skip / discard","merge / replace"]))
    end
    return val
end

checkmerge(a::String,data,title,author,date,doc,msg="Merge conflict detected, proceed?") = checkmerge(DateTime(a),data,title,author,date,doc,msg)

function tex2dict(tex::String,data=nothing,disp=false,sav::Array=[])
    tim = Dates.unix2datetime(time())
    uid = UUIDs.uuid1()
    pd = String.(split(split(tex,"\n\\end{document}")[1],"\n\\begin{document}\n"))
    (pre,doc) = length(pd) == 1 ? ["default",pd] : pd
    ## locate identifying info
    unk = "unknown"
    author = texlocate(:author,pre,data ≠ nothing ? data["author"] : unk)[1]
    date = texlocate(:date,pre,data ≠ nothing ? data["date"] : unk)[1]
    title = texlocate(:title,pre,data ≠ nothing ? data["title"] : unk)[1]
    for item ∈ [:author,:date,:title]
        pre = textagdel(item,pre)
    end
    prereg = "%vtx:"*regtextag*"\n?"
    occursin(Regex(prereg),pre) && (pre = match(Regex("(?:"*prereg*")\\X+"),pre).match)
    pre = replace(pre,r"\n+$"=>"")
    ## date check
    docre = rsplit(doc,"%rev:";limit=2)
    if (length(docre) == 1)
        docdc = tim
    elseif occursin(r"%vtx:",docre[2])
        docdc = tim
    else
        doc = docre[1]
        docdc = DateTime(match(Regex(regtextag*"(?<=\n)?"),docre[2]).match)
    end
    out = deepcopy(data)
    if (data ≠ nothing)
        cmv = checkmerge(docdc,data,title,author,date,doc)
        if cmv == 0
            throw(error("VerTeX unable to proceed due to merge failure"))
        elseif cmv < 2
            doc = preload(data,true,false)
            author = data["author"]
            date = data["date"]
            title = data["title"]
            pre = data["pre"]
        end
    end
    ## deconstruct VerTeX
    cp = split(doc,r"%extend:((true)|(false))\n?";limit=2)
    choice = match(r"(?<=%extend:)((true)|(false))(?=\n)?",doc)
    extend = choice ≠ nothing ? Meta.parse(choice.match) : true
    compact = !occursin(r"%vtx:",cp[1]) && !((length(cp) > 1) ? extend : true)
    remdoc = ""
    if compact
        doc = cp[1]
    elseif choice ≠ nothing
        remdoc = "$(cp[2])\n"
        if occursin(r"%vtx:",cp[1])
            doc = split(cp[1],r"%vtx:")[1]
        else
            doc = cp[1]
        end
    else
        remdoc = "$doc\n"
        doc = split(doc,Regex(prereg);limit=2)[1]
    end
    occursin(r"%vtx:",cp[1]) && (remdoc = match(Regex(prereg*"\\X+"),docre[1]).match)
    doc = join(chomp(doc))
    if |(([author,date,title] .== unk)...)
        @info "Missing VerTeX metadata for $uid"
        println(disp ? (data ≠ nothing ? data["tex"] : doc) : doc)
        println("%rev:",disp ? (data ≠ nothing ? data["revised"] : tim) : tim)
        print("title: ")
        title == unk ? (title = readline()) : println(title)
        print("author: ")
        author == unk ? (author = readline()) : println(author)
        print("date: ")
        date == unk ? (date = readline()) : println(date)
    end
    if out == nothing
        out = Dict(
            "editor" => AUTHOR,
            "author" => author,
            "pre" => pre,
            "tex" => doc,
            "date" => date,
            "title" => title,
            "created" => "$tim",
            "revised" => "$tim",
            "uuid" => "$uid",
            "version" => ["VerTeX", "v\"0.1.0\""],
            "compact" => "$compact") #twins
    else
        setval!(out,"editor",AUTHOR)
        setval!(out,"author",author)
        setval!(out,"pre",pre)
        setval!(out,"tex",doc)
        setval!(out,"date",date)
        setval!(out,"title",title)
        setval!(out,"edit","$tim")
        setval!(out,"compact","$compact")
    end
    ## parse additional vertices
    extra = []
    comments = []
    (data ≠ nothing) && haskey(data,"save") && (sav = data["save"]) #push!(sav,data["save"])
    haskey(out,"show") && pop!(out,"show")
    haskey(out,"comments") && pop!(out,"comments")
    if !compact
        while occursin(Regex(prereg),remdoc)
            ms = join.(collect((m.match for m = eachmatch(Regex(prereg),remdoc))))[1]
            sp = split(remdoc,ms;limit=3)
            push!(comments,compact ? "" : join(chomp(sp[1])))
            re = rsplit(sp[2],"%rev:";limit=2)
            dc = length(re) == 1 ? tim : DateTime(match(Regex(regtextag*"(?<=\n)?"),re[2]).match)
            # try to open it, to see if update
            df = split(join(match(Regex("(?<=%vtx:)"*regtextag*"(?<=\n)?"),ms).match),":~:";limit=2)
            file = join(df[end])
            depo = length(df) > 1 ? join(df[1]) : "julia"
            ods = nothing
            add2q = true
            try
                for s ∈ sav
                    if haskey(s,"depot") && haskey(s,"dir") &&
                            s["depot"] == depo && s["dir"] == file
                        ods = s
                        break
                    end
                end
                ods == nothing && (ods = load(file,depo))
                add2q = DateTime(ods["revised"]) == dc && ods["tex"] ≠ join(chomp(re[1]))
                # check date and compare if opened
                # terminal menu if date is not a mach
            catch
            end
            ds = tex2dict(pre*"\n\\begin{document}\n"*sp[2]*"\n\\end{document}",ods,!add2q,sav)
            push!(extra,repr(!Meta.parse(ds["compact"])))
            addval!(out,"show",[ds["uuid"],depo,file])
            #addkey!(out,"ids",ds["uuid"],[ds["uuid"], depo, file])
            # add to save queue, for when actual save happens
            !haskey(ds,"dir") && setval!(ds,"dir",file)
            !haskey(ds,"depot") && setval!(ds,"depot",depo)
            ins = 0
            haskey(out,"save") && for k ∈ 1:length(out["save"])
                out["save"][k]["uuid"] == ds["uuid"] && (ins = k; break)
            end
            add2q && (ins > 0 ? (out["save"][ins] = ds) : addval!(out,"save",ds))
            remdoc = join(sp[3])
        end
        push!(comments,join(chomp(remdoc)))
        choice == nothing && popfirst!(comments)
        setval!(out,"comments",comments)
        setval!(out,"extend",extra)
    else
        for item ∈ ["show","comments","extend"]
            haskey(out,item) && pop!(out,item)
        end
    end
    ## double check reference nodes
    bins = ["ref","used","deps"]
    items = [bins...,"cite","label"] # "refby", "citeby", "depsby"
    # update referral lists
    for item ∈ items
        temp = [(item == "label" ? lbllocate(doc) : []); texlocate(Symbol(item),doc)]
        if temp ≠ ["none"]
            setval!(out,item,temp)
        else
            haskey(out,item) && pop!(out,item)
        end
    end
    return out::Dict
end

function preload(data::Dict,extend::Bool,rev::Bool=true)
    doc = tomlocate("tex",data)*"\n"
    if extend && haskey(data,"comments") && length(data["comments"]) > 0
        shift = 0
        ls = haskey(data,"show") ? length(data["show"]) : 0
        if length(data["comments"]) ≠ ls
            if data["comments"][1] ≠ ""
                doc *= "%extend:true\n$(data["comments"][1])\n"
            else
            end
            shift += 1
        end
        if ls > 0
            for k ∈ 1:ls
                key = data["show"][k]
                ta = ""
                try
                    da = nothing
                    haskey(data,"save") && for s ∈ data["save"]
                        s["uuid"] == key[1] && (da = s; break)
                    end
                    da == nothing && (da = load(key[3],key[2]))
                    ta = preload(da,Meta.parse(data["extend"][k]))
                catch
                    @warn "could not load $(key[3]) from $(key[2])"
                end
                dep = data["depot"] ≠ "julia" ? "$(data["depot"]):~:" : ""
                vtx = "%vtx:$dep$(key[3])\n"
                doc *= join([vtx,ta,"\n",vtx,data["comments"][k+shift]])
            end
        end
    end
    return rev ? "$doc%rev:$(data["revised"])" : join(chomp(doc))
end

function dict2tex(data::Dict)
    unk = "unknown"
    pre = tomlocate("pre",data)
    author = tomlocate("author",data,unk)
    date = tomlocate("date",data,unk)
    title = tomlocate("title",data,unk)
    dep = (haskey(data,"depot") && (data["depot"] ≠ "julia")) ? "$(data["depot"]):~:" : ""
    reg = "^%vtx:"*regtextag
    if occursin(Regex(reg*"\n?"),pre)
        prereg = "(?<=%vtx:)"*regtextag*"(?<=\n)?"
        df = split(join(match(Regex(prereg),pre).match),":~:";limit=2)
        file = join(df[end])
        depo = length(df) > 1 ? join(df[1]) : "julia"
        pre = preamble(file,depo)*replace(pre,Regex(reg)=>"")
    end
    tex = pre*"\n"
    author ≠ unk && (tex *= "\n\\author{$author}")
    date ≠ unk && (tex *= "\n\\date{$date}")
    title ≠ unk && (tex *= "\n\\title{$title}")
    return article(join(chomp(preload(data,true))),tex)
end

toml2tex(toml::String) = dict2tex(TOML.parse(toml))
tex2toml(tex::String) = dict2toml(tex2dict(tex))

include("depot.jl")
include("search.jl")
include("repl.jl")

function __init__()
    haskey(ENV,"AUTHOR") && (global AUTHOR = ENV["AUTHOR"])
    # load manifest and dictionary at ini
    readmanifest()
    readdictionary()
    # save manifest and dictionary at end
    atexit(() -> (writemanifest(); writedictionary()))
    if isdefined(Base, :active_repl)
        REPLMode.repl_init(Base.active_repl)
    else
        atreplinit() do repl
            if isinteractive() && repl isa REPL.LineEditREPL
                isdefined(repl, :interface) || (repl.interface = REPL.setup_interface(repl))
                REPLMode.repl_init(repl)
            end
        end
    end
    @require LightGraphs="093fc24a-ae57-5d10-9952-331d41423f4d" begin
        using LightGraphs
        function makegraph(manifest=manifest,dictionary=dictionary,index=collect(keys(dictionary)))
            readmanifest()
            readdictionary()
            g = LightGraphs.SimpleDiGraph(length(index))
            for i ∈ 1:length(index)
                key = collect(values(dictionary[index[i]]))[1]
                more = manifest[key[2]][key[1]]
                if haskey(more,"deps")
                    for dep ∈ more["deps"]
                        f = findall(index .== split(dep,':')[1])
                        !isempty(f) && LightGraphs.add_edge!(g,f[1],i)
                    end
                end
            end
            return g
        end
    end
    @require GraphPlot="a2cc645c-3eea-5389-862e-a155d0052231" begin
        using LightGraphs, GraphPlot, Compose, Cairo
        function drawgraph(name="/tmp/vtx-data.pdf",manifest=manifest,dictionary=dictionary,index=collect(keys(dictionary)))
            p = gplot(makegraph(manifest,dictionary,index),nodelabel=index,layout=circular_layout)
            eval(:(draw(PDF($name, $(Expr(:call,:*,32,:cm)), $(Expr(:call,:*,32,:cm))), $p)))
        end
    end
end

end # module


text:
# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder

name = "libwebp"
version = v"1.1.0"

# Collection of sources required to build libwebp
sources = [
    ArchiveSource("https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-$(version).tar.gz",
                  "98a052268cc4d5ece27f76572a7f50293f439c17a98e67c4ea0c7ed6f50ef043"),
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/libwebp-*/
export CFLAGS="-std=c99"
export CPPFLAGS="-I${prefix}/include"
if [[ "${target}" == *-freebsd* ]]; then
    export LDFLAGS="-L${libdir}"
fi
./configure --prefix=${prefix} --host=${target} \
    --enable-swap-16bit-csp \
    --enable-experimental \
    --enable-libwebp{mux,demux,decoder,extras} \
    --disable-static
make -j${nproc}
make install
"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = supported_platforms()

# The products that we will ensure are always built
products = [
    LibraryProduct("libwebpdecoder", :libwebpdecoder),
    LibraryProduct("libwebpdemux", :libwebpdemux),
    LibraryProduct("libwebpmux", :libwebpmux),
    LibraryProduct("libwebp", :libwebp),
    ExecutableProduct("cwebp", :cwebp),
    ExecutableProduct("dwebp", :dwebp),
    ExecutableProduct("gif2webp", :gif2webp),
    ExecutableProduct("img2webp", :img2webp),
    ExecutableProduct("webpinfo", :webpinfo),
    ExecutableProduct("webpmux", :webpmux),
]

# Dependencies that must be installed before this package can be built
dependencies = [
    Dependency("Giflib_jll"),
    Dependency("JpegTurbo_jll"),
    Dependency("libpng_jll"),
    Dependency("Libtiff_jll"),
    Dependency("Libglvnd_jll"),
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

max_stars_repo_name: blenessy/Yggdrasil

max_issues_repo_name: blenessy/Yggdrasil

max_forks_repo_name: blenessy/Yggdrasil


text:
export LabStream

abstract type LabStream end

function close(stream::LabStream)
    for dev in stream.devices
        close(dev)
    end
    close(stream.stream)
    return
end


text:
using Test

@testset "ITensorChainRules" begin
  for filename in ["chainrules.jl", "optimization.jl"]
    include(filename)
  end
end


text:
"""
	gmtselect(cmd0::String="", arg1=nothing, kwargs...)

Select data table subsets based on multiple spatial criteria.

Full option list at [`gmtselect`]($(GMTdoc)gmtselect.html)

Parameters
----------

- $(GMT.opt_R)
- **A** | **area** :: [Type => Str | Number]

    Features with an area smaller than min_area in km^2 or of hierarchical level that is
    lower than min_level or higher than max_level will not be plotted.
    ($(GMTdoc)gmtselect.html#a)
- **C** | **dist2pt** :: [Type => Str]   `Arg = pointfile+ddist[unit]`

    Pass all records whose location is within dist of any of the points in the ASCII file pointfile.
    If dist is zero then the 3rd column of pointfile must have each point’s individual radius of influence.
    ($(GMTdoc)gmtselect.html#c)
- **D** | **res** | **resolution** :: [Type => Str]      `Arg = c|l|i|h|f`

    Ignored unless N is set. Selects the resolution of the coastline data set to use
    ((f)ull, (h)igh, (i)ntermediate, (l)ow, or (c)rude).
    ($(GMTdoc)gmtselect.html#d)
- **E** | **boundary** :: [Type => Str | []]            `Arg = [fn]`

    Specify how points exactly on a polygon boundary should be considered.
    ($(GMTdoc)gmtselect.html#e)
- **F** | **polygon** :: [Type => Str | GMTdaset | Mx2 array]     `Arg = polygonfile`

    Pass all records whose location is within one of the closed polygons in the multiple-segment
    file ``polygonfile`` or a GMTdataset type or a Mx2 array defining the polygon.
    ($(GMTdoc)gmtselect.html#f)
- **G** | **gridmask** :: [Type => Str | GRDgrid]        `Arg = gridmask`

    Pass all locations that are inside the valid data area of the grid gridmask.
    Nodes that are outside are either NaN or zero.
    ($(GMTdoc)gmtselect.html#g)
- **I** | **reverse** :: [Type => Str | []]    `Arg = [cflrsz]`

    Reverses the sense of the test for each of the criteria specified.
    ($(GMTdoc)gmtselect.html#i)
- $(GMT.opt_J)
- **L** | **dist2line** :: [Type => Str]    `Arg = linefile+ddist[unit][+p]`

    Pass all records whose location is within dist of any of the line segments in the ASCII
    multiple-segment file linefile.
    ($(GMTdoc)gmtselect.html#l)
- **N** | **mask** :: [Type => Str | List]     `Arg = ocean/land/lake/island/pond or wet/dry`

    Pass all records whose location is inside specified geographical features.
    ($(GMTdoc)gmtselect.html#n)
- **Z** | **in_range** :: [Type => Str | List]     `Arg = min[/max][+a][+ccol][+i]`

    Pass all records whose 3rd column (z; col = 2) lies within the given range or is NaN.
    ($(GMTdoc)gmtselect.html#z)
- $(GMT.opt_V)
- $(GMT.opt_write)
- $(GMT.opt_append)
- $(GMT.opt_b)
- $(GMT.opt_d)
- $(GMT.opt_e)
- $(GMT.opt_f)
- $(GMT.opt_g)
- $(GMT.opt_h)
- $(GMT.opt_i)
- $(GMT.opt_o)
- $(GMT.opt_swap_xy)
"""
function gmtselect(cmd0::String="", arg1=nothing, arg2=nothing, arg3=nothing; kwargs...)

	length(kwargs) == 0 && return monolitic("gmtselect", cmd0, arg1, arg2, arg3)

	d = KW(kwargs)
	cmd = parse_common_opts(d, "", [:R :V_params :b :d :e :f :g :h :i :o :yx])
	cmd = parse_these_opts(cmd, d, [[:A :area], [:D :res :resolution], [:E :boundary], [:F :polygon],
	                [:G :gridmask], [:I :reverse], [:N :mask], [:Z :in_range]])
	#cmd = add_opt(cmd, 'N', d, [:N :mask], (ocean=("", arg2str, 1), land=("", arg2str, 2)) )

	cmd, arg2 = dist2PtLine(d, cmd, 'C')
	cmd, arg3 = dist2PtLine(d, cmd, 'L')
	if (arg1 === nothing)  arg1 = arg2;  arg2 = arg3;  arg3 = nothing  end
	if (arg2 === nothing && arg3 !== nothing)  arg2 = arg3  end

	common_grd(d, cmd0, cmd, "gmtselect ", arg1, arg2, arg3)		# Finish build cmd and run it
end

function dist2PtLine(d, cmd, opt)
	# Accept (GMTdataset[,dist [,whatever]]); or (fname[,dist [,whatever]]); or 'fname' or full GMT syntax (string)
	# Accept dist2line|dist2pt(line=fname|dataset, pts=fname|datset [,dist=value [,ortho=whatever]])
	arg = nothing
	opt == 'C' ? symbs = [:C :dist2pt :dist] : symbs = [:L :dist2line]
	if ((val = find_in_dict(d, symbs)[1]) !== nothing)
		cmd *= " -" * opt
		if (isa(val, NamedTuple))
			d = nt2dict(val)
			if ((target = find_in_dict(d, [:line :pts])[1]) === nothing)  error(":line or :pts member cannot be missing")  end
			dist = "+d0"		# The default when dist is not provided
			if ((dist = find_in_dict(d, [:dist])[1]) !== nothing)  dist = "+d" * arg2str(dist)  end
			(isa(target, String) || isa(target, String)) ? cmd *= target : arg = target
			cmd *= dist
			if (haskey(d, :ortho))  cmd *= "+p"  end
		elseif (isa(val, Tuple))
			if (isa(val[1], String) || isa(val[1], Symbol))
				cmd = string(cmd, val[1], "+d", val[2])
			else
				cmd = string(cmd, "+d", val[2]);	arg = val[1]
			end
			if (length(val) == 3)  cmd *= "+p"  end
		elseif (isa(val, String) || isa(val, Symbol))
			cmd *= string(val)
		end
	end
	return cmd, arg
end

# ---------------------------------------------------------------------------------------------------
gmtselect(arg1, cmd0::String=""; kw...) = gmtselect(cmd0, arg1; kw...)


text:
using Documenter, Games

include("auto_doc_gen.jl")

makedocs(
    modules = [Games],
    format = Documenter.HTML(prettyurls = false),
    sitename = "Games.jl",
    pages = PAGES,
)

deploydocs(
    repo = "github.com/QuantEcon/Games.jl.git",
    branch = "gh-pages",
    target = "build",
    deps = nothing,
    make = nothing,
)


text:
function htonofarray(array::AbstractArray{T,1}) where {T<:Union{Real,Char}}
    return [hton(x) for x in array]
end

function ntohofarray(array::AbstractArray{T,1}) where {T<:Union{Real,Char}}
    return [ntoh(x) for x in array]
end

"""
Parse the header and return it along with the input array minus the header.
Returns the codec, the length of the decoded array, the parameter and the remainder
of the array
"""
function parseheader(input_array::Array{UInt8,1})
    codec, length, param = htonofarray(reinterpret(Int32,input_array[1:12]))
    return codec,length,param,input_array[13:end]
end

"""
Add the header to the appropriate array
"""
function addheader(input_array::Array{UInt8,1}, codec::Integer, length::Integer, param::Integer)
    out_array=UInt8[]
    append!(out_array, reinterpret(UInt8,[ntoh(Int32(codec))]))
    append!(out_array, reinterpret(UInt8,[ntoh(Int32(length))]))
    append!(out_array, reinterpret(UInt8,[ntoh(Int32(param))]))
    append!(out_array, input_array)
end


text:
function precompile_bb()
    #Start server
    server = run_server(3001, debug=true)

    #Pretend to be Computer
    clientside = connect(3001)

    #Precompile serialize
    initdev("debug", Int32(1))

    serialize(clientside, (true, Int32(2), ("debug", Int32(1), true), ("debug", Int32(1), (1,2.0,"asd"))))
    serialize(clientside, (true, Int32(2), ("debug", Int32(1), Int32(1)), ("debug", Int32(1), 1.0)))
    serialize(clientside, (false, Int32(2),    ("debug", Int32(1)), ("debug", Int32(1))))

    serialize(clientside, (true, Int32(1), ("debug", Int32(1), true)))
    serialize(clientside, (false, Int32(1), ("debug", Int32(1))))
    serialize(clientside, (true, Int32(4), ("debug", Int32(1), true), ("debug", Int32(2), false), ("debug", Int32(3), true), ("debug", Int32(4), false)))

    # Close the client side
    close(clientside)

    #Close server
    close(server)

    debug = true

    #Precompile SysLED
    led = initdev("sysled",Int32(1))
    write!(led, "1", debug)
    read(led, debug)

    ind = 1
    println("False: $(ind ∉ [1,2,3,4])")

    # Precompile GPIO
    gpio = initdev("gpio",Int32(1))

    write!(gpio, (Int32(1), "1"), debug)
    #read(gpio, ind, args, debug)

    # Precompile PWM
    pwm = initdev("pwm", Int32(1))
    write!(pwm, (Int32(1),"1"), debug)

    #Do read/write to file
    val = true
    testfile = joinpath(@__DIR__,"startup/testfile.txt")
    file = open(testfile, "r+")
    write(file, val ? "1" : "0")
    close(file)

    file = open(testfile, "r")
    l = readline(file)
    close(file)

    try getdev("nonexistent")       catch end
    try bbparse("Invalid input")    catch end
    try bbparse(("Invalid input"))  catch end

    println("Internal Precompile Finished")
end


text:
#!/usr/bin/env julia

#==============================================================================
 = Example of utilization of the library 'ros_interface'
 =
 = Maintainer: Sidney Carvalho - sydney.rdc@gmail.com
 = Last Change: 2020 Abr 17 22:37:55
 = Info: Send and receive information from a node in the ROS environment.
 =============================================================================#

# check if the Cxx package is installed
if typeof(Pkg.installed("Cxx")) == Void
    Pkg.add("Cxx")
end

# package to wrapper C++ code (demands Julia v0.6 or later)
using Cxx

# default C++ lib and include paths
const path_to_lib = "../lib/"
const path_to_include = "../include/"

# define the path_to_include as default C++ headers folder
addHeaderDir(path_to_include, kind=C_System)

# load ROS interface shared library
Libdl.dlopen(path_to_lib * "libros_interface.so", Libdl.RTLD_GLOBAL)

cxxinclude("ros_interface.hpp")                 # ROS interface header
cxxinclude("vector")                            # C++ vector header

# instantiate a 'ros_interface' object
ros_com = @cxxnew ros_interface(pointer("julia_ros"))

# initial positions
pose1 = @cxxnew space_t()
pose2 = @cxxnew space_t()

@cxx pose1->zero()
@cxx pose2->set_x(4)
@cxx pose2->set_y(2)
@cxx pose2->set_yaw(0)

# initial velocities
vel1 = @cxxnew space_t()
vel2 = @cxxnew space_t()

# insert the robots (if do you want to use another type of robot, change
# the second parameter of 'add_node' with the following values - for now, julia
# does not support macros from C, so use its equivalence in integer):
# T_REAL for rosaria robots (use 1)
# T_STAGE for stageros robots (use 2)
# T_TURTLE for turtlesim robots (use 3)
@cxx ros_com->add_node(1, 2, pointer("robot1"), pose1)
@cxx ros_com->add_node(2, 2, pointer("robot2"), pose2)

# main loop
while @cxx ros_com->ros_ok()
    # set the data capture frequency (s)
    @cxx ros_com->clock(0.1)

    # set random velocities
    @cxx vel1->set_x(Cdouble(1 - randn(1)[1]))
    @cxx vel1->set_yaw(Cdouble(randn(1)[1]))

    @cxx vel2->set_x(Cdouble(1 - randn(1)[1]))
    @cxx vel2->set_yaw(Cdouble(randn(1)[1]))

    # send velocities to ROS
    @cxx ros_com->node_vel(1, vel1)
    @cxx ros_com->node_vel(2, vel2)

    # read positions from ROS
    pose1 = @cxx ros_com->node_pose(1)
    pose2 = @cxx ros_com->node_pose(2)

    # laser readings from ROS
    lc1 = @cxx ros_com->node_laser(1)
    lc2 = @cxx ros_com->node_laser(2)

    # translate raw C++ readings to Julia array
    laser1 = unsafe_wrap(Array, pointer(lc1), length(lc1))
    laser2 = unsafe_wrap(Array, pointer(lc2), length(lc2))

    println("laser 1 -> n:$(length(laser1)) data:$(laser1)")

    # print robot's positions
    println("Robot 1 -> x:$(@cxx pose1->x) y:$(@cxx pose1->y) yaw:$((@cxx pose1->yaw)*180/pi)")
    println("Robot 2 -> x:$(@cxx pose2->x) y:$(@cxx pose2->y) yaw:$((@cxx pose2->yaw)*180/pi)")
end


text:
abstract type Tree{N} end

struct Leaf{N} <: Tree{N}
    label::Int

    Leaf{N}(n) where N = (1 <= n <= N) ? new{N}(n) : error("Illegal construction")
end

struct Branch{N} <: Tree{N}
    left::Tree{N}
    right::Tree{N}
end

import Base: show

function show(io::IO, l::Leaf, indent = 0)
    print(io, " " ^ indent, "Leaf($(l.label))")
end

function show(io::IO, b::Branch, indent = 0)
    print(io, " " ^ indent)
    print(io, "Branch(\n")
    show(io, b.left, indent + 2)
    print(io, ",\n")
    show(io, b.right, indent + 2)
    print(io, ")")
end

import Base: rand
using Random: AbstractRNG, Sampler, SamplerType

rand(rng::AbstractRNG, ::SamplerType{Leaf{N}}) where {N} = Leaf{N}(rand(rng, 1:N))

"""
    BoltzmannSampler{N}(m, n)
Configuration for Boltzmann sampling `DecisionTree{N}`s with minimum size `m` and maximum size `n`.
"""
struct BoltzmannSampler{N} <: Sampler{Tree{N}}
    minsize::Int
    maxsize::Int
end

function boltzmann_ub(rng::AbstractRNG, sampler::BoltzmannSampler{N}, cursize) where {N}
    minsize = sampler.minsize
    maxsize = sampler.maxsize

    if cursize ≥ maxsize        # maximum bound exceeded
        return nothing, cursize
    elseif rand(rng) ≤ 0.5      # generate random leaf
        return rand(rng, Leaf{N}), cursize
    else                        # try generating a branch -- if its size is small enough
        # try generating children, or immediately fail
        left_children, cursize = boltzmann_ub(rng, sampler, cursize + 1)
        left_children === nothing && return nothing, cursize

        right_children, cursize = boltzmann_ub(rng, sampler, cursize + 1)
        right_children === nothing && return nothing, cursize

        # `cursize` is now the size of the whole thing, and less than maxsize
        return Branch{N}(left_children, right_children), cursize
    end
end

function rand(rng::AbstractRNG, sampler::BoltzmannSampler{N}) where {N}
    candidate, size = boltzmann_ub(rng, sampler, 0)
    while candidate === nothing || size ≤ sampler.minsize
        candidate, size = boltzmann_ub(rng, sampler, 0)
    end

    return candidate
end

function randomsplitnaive_impl(l::Leaf, n, context, continuation)
    n += 1
    if rand() ≤ 1/n
        continuation = k -> (context(k(l)), l)
    end

    return continuation, n
end

function randomsplitnaive_impl(b::Branch, n, context, continuation)
    continuation, n = randomsplitnaive_impl(b.left,
                                            n,
                                            t -> context(Branch(t, b.right)),
                                            continuation)
    continuation, n = randomsplitnaive_impl(b.right,
                                            n,
                                            t -> context(Branch(b.left, t)),
                                            continuation)

    n += 1
    if rand() ≤ 1/n
        continuation = k -> (context(k(b)), b)
    end

    return continuation, n
end

"""
    randomsplitnaive(action, t::Tree)
Select uniformly at random a subtree `s` of `t`, then replace `s` by `action(s)`.
Returns both the new tree and the replaced subtree.
"""
function randomsplitnaive(action, t::Tree)
    # The continuation argument will be assigned a default with probability 1 on the first leaf,
    # so passing `nothing` is safe here.
    cont, _ = randomsplitnaive_impl(t, 0, identity, nothing)
    cont(action)
end

randomchildnaive(t::Tree) = randomsplitnaive(identity, t)[2]

sumtree(t::Leaf) = t.label
sumtree(t::Branch) = sumtree(t.left) + sumtree(t.right)

function testvalues_native(t, N)
    zd = [sumtree(randomchildnaive(t)) for _ in 1:N]
    [(i, sum(zd .== i)) for i in unique(zd)]
end

# Context types:
abstract type Context{N} end

struct LeftContext{N} <: Context{N}
    right::Tree{N}
    parent::Context{N}
end

struct RightContext{N} <: Context{N}
    left::Tree{N}
    parent::Context{N}
end

# This will be used instead of `nothing` in the top-level call
struct NoContext{N} <: Context{N} end

# Continuation type:
struct Cont{N}
    context::Context{N}
    chunk::Tree{N}
end

function (context::RightContext{N})(t::Tree{N}) where N
    context.parent(Branch(context.left, t))
end

function (context::LeftContext{N})(t::Tree{N}) where N
    context.parent(Branch(t, context.right))
end

function (context::NoContext{N})(t::Tree{N}) where N
    t
end

function (continuation::Cont{N})(k::Function) where N
    newchunk = k(continuation.chunk)::Tree{N}
    tree = continuation.context(newchunk)::Tree{N}
    return tree, continuation.chunk
end

function randomsplit_impl(t::Leaf{N}, n::Int, context::Context{N}, continuation::Cont{N}) where {N}
    n += 1
    if rand() ≤ 1/n
        return Cont{N}(context, t), n
    else
        return continuation, n
    end
end

function randomsplit_impl(t::Branch{N}, n::Int, context::Context{N}, continuation::Cont{N}) where {N}
    c1, n = randomsplit_impl(t.left, n, LeftContext{N}(t.right, context), continuation)
    c2, n = randomsplit_impl(t.right, n, RightContext{N}(t.left, context), c1)

    n += 1
    if rand() ≤ 1/n
        return Cont{N}(context, t), n
    else
        return c2, n
    end
end

function randomsplit(action, t::Tree{N}) where {N}
    default_context = NoContext{N}()
    cont, _ = randomsplit_impl(t, 0, default_context, Cont{N}(default_context, t))
    cont(action)
end

randomchild(t::Tree{N}) where {N} = randomsplit(identity, t)[2]

function testvalues(t, N)
    zd = [sumtree(randomchild(t)) for _ in 1:N]
    [(i, sum(zd .== i)) for i in unique(zd)]
end


text:
using MittagLefflerFunctions
using SpecialFunctions: erfcx
using PyPlot

α = 1/2
β = 1.0
r = 5

x = range(0, 10, length=201)
y = erfcx.(x)

figure(1)
m_lo = 4
style = ("-", "--", ":", "-.", "-")
for m = m_lo:2r+2-m_lo
    n = 2r + 1 - m
    E = MLPade(α, β, m, n, :svd)
    err = E.(x) - y
    plot(x, err, style[m-m_lo+1], label="m = $m, n = $n")
end
xlabel(L"$x$")
legend()
grid(true)

savefig("figure08.pdf")


text:
import Adapt: adapt, adapt_storage
using  LinearAlgebra: Cholesky
using Zygote: IdSet
import Functors: Functors, @functor, functor, fmap, isleaf

trainable(m) = functor(m)[1]

"""
    testmode!(m, mode = true)

Set a layer or model's test mode (see below).
Using `:auto` mode will treat any gradient computation as training.

_Note_: if you manually set a model into test mode, you need to manually place
it back into train mode during training phase.

Possible values include:
- `false` for training
- `true` for testing
- `:auto` or `nothing` for Flux to detect the mode automatically
"""
testmode!(m, mode = true) = (foreach(x -> testmode!(x, mode), trainable(m)); m)

"""
    trainmode!(m, mode = true)

Set a layer of model's train mode (see below).
Symmetric to [`testmode!`](@ref) (i.e. `trainmode!(m, mode) == testmode!(m, !mode)`).

_Note_: if you manually set a model into train mode, you need to manually place
it into test mode during testing phase.

Possible values include:
- `true` for training
- `false` for testing
- `:auto` or `nothing` for Flux to detect the mode automatically
"""
trainmode!(m, mode = true) = mode isa Bool ? testmode!(m, !mode) : testmode!(m, mode)

params!(p::Params, x::AbstractArray{<:Number}, seen = IdSet()) = push!(p, x)

function params!(p::Params, x, seen = IdSet())
  x in seen && return
  push!(seen, x)
  for child in trainable(x)
    params!(p, child, seen)
  end
end

function params(m...)
  ps = Params()
  params!(ps, m)
  return ps
end

function loadparams!(m, xs)
  for (p, x) in zip(params(m), xs)
    size(p) == size(x) ||
      error("Expected param size $(size(p)), got $(size(x))")
    copyto!(p, x)
  end
end

# CPU/GPU movement conveniences
"""
    cpu(m)

Moves `m` onto the CPU, the opposite of [`gpu`](@ref).
Recurses into structs marked [`@functor`](@ref).

```julia-repl
julia> m = Dense(1,2)
Dense(1, 2)

julia> m_gpu = gpu(m)
Dense(1, 2)

julia> typeof(m_gpu.W)
CuArray{Float32, 2}

julia> m_cpu = cpu(m_gpu)
Dense(1, 2)

julia> typeof(m_cpu.W)
Matrix{Float32}
```
"""
cpu(x) = fmap(_cpu_array, x; exclude = _isbitsarray)

_cpu_array(x::AbstractArray) = adapt(Array, x)
# adapt(Array, x) materialises some lazy arrays, on which cpu() should do nothing:
_cpu_array(x::AbstractRange) = x
_cpu_array(x::Zygote.FillArrays.AbstractFill) = x
_cpu_array(x::Zygote.OneElement) = x

function Zygote.ChainRules.rrule(::typeof(_cpu_array), x::AbstractArray)
  y = _cpu_array(x)
  if x === y
    # Trivial use: cpu(x::Array) shouldn't push its gradient to GPU
    return y, dy -> (Zygote.ChainRules.NoTangent(), dy)
  else
    # Allows both cpu(x::CuArray) and cpu(x::Adjoint{T,CuArray}):
    return y, dy -> (Zygote.ChainRules.NoTangent(), _gpu_array(dy))
  end
end

_isbitsarray(::AbstractArray{<:Number}) = true
_isbitsarray(::AbstractArray{T}) where T = isbitstype(T)
_isbitsarray(x) = false

"""
    gpu(x)

Moves `m` to the current GPU device, if available. It is a no-op otherwise.
See the [CUDA.jl docs](https://juliagpu.github.io/CUDA.jl/stable/usage/multigpu/)
to help identify the current device.

This works for functions, and any struct marked with [`@functor`](@ref).

```julia-repl
julia> m = Dense(1,2)
Dense(1, 2)

julia> typeof(m.W)
Matrix{Float32}

julia> m_gpu = gpu(m)
Dense(1, 2)

julia> typeof(m_gpu.W) # notice the type of the array changed to a CuArray
CuArray{Float32, 2}
```
"""
gpu(x) = use_cuda[] ? fmap(_gpu_array, x; exclude = _isbitsarray) : x

_gpu_array(x::AbstractArray) = CUDA.cu(x)

# While `cu` moves Arrays to the GPU, we also want to move some structured arrays
# https://github.com/FluxML/Zygote.jl/issues/1005
_gpu_array(x::Zygote.FillArrays.AbstractFill) = CUDA.fill(first(x), size(x))  # gradient of sum
function _gpu_array(x::Zygote.OneElement)  # gradient of getindex
  y = CUDA.zeros(eltype(x), size(x))
  CUDA.@allowscalar y[x.ind...] = x.val
  y
end

function Zygote.ChainRules.rrule(::typeof(_gpu_array), x::AbstractArray)
  y = _gpu_array(x)
  if x === y  # trivial case, e.g. gpu(x::Adjoint{T,CuArray})
    return y, dy -> (Zygote.ChainRules.NoTangent(), dy)
  else
    return y, dy -> (Zygote.ChainRules.NoTangent(), _cpu_array(dy))
  end
end

# Precision

adapt_storage(T::Type{<:Real}, xs::AbstractArray{<:Real}) = convert.(T, xs) # piracy

paramtype(T::Type{<:Real}, m) = fmap(x -> adapt(T, x), m)

"""
    f32(m)

Convert the `eltype` of model's parameters to `Float32`.
"""
f32(m) = paramtype(Float32, m)

"""
    f64(m)

Convert the `eltype` of model's parameters to `Float64`.
"""
f64(m) = paramtype(Float64, m)

# Functors for certain Julia data structures
@functor Cholesky
trainable(c::Cholesky) = ()


text:
#==========================================================================================#

# TYPE

mutable struct Abadie <: TwoStageModel

    first_stage::OneStageModel
    second_stage::ParModel
    pscore::Vector{Float64}
    eweights::ProbabilityWeights{Float64, Float64, Vector{Float64}}

    Abadie() = new()
end

#==========================================================================================#

# FIRST STAGE

function first_stage(::Type{Abadie}, M₁::Type{<:OneStageModel}, MD::Microdata; kwargs...)

    FSD                    = Microdata(MD)
    FSD.mapping[:response] = MD.mapping[:instrument]

    pop!(FSD.mapping, :treatment)
    pop!(FSD.mapping, :instrument)

    return fit(M₁, FSD; kwargs...)
end

#==========================================================================================#

# ESTIMATION

function fit(
        ::Type{Abadie},
        M₂::Type{<:OneStageModel},
        M₁::Type{<:ParModel},
        MD::Microdata;
        novar::Bool = false,
        kwargs...
    )

    m₁ = first_stage(Abadie, M₁, MD, novar = novar)
    return fit(Abadie, M₂, m₁, MD; novar = novar, kwargs...)
end

function fit(
        ::Type{Abadie},
        M₂::Type{<:OneStageModel},
        M₁::OneStageModel,
        MD::Microdata;
        novar::Bool = false,
        trim::AbstractFloat = 0.0,
        kwargs...
    )

    w = getweights(MD)
    d = getvector(MD, :treatment)
    z = getvector(MD, :instrument)
    π = fitted(M₁)
    v = [1.0 - (1.0 - di) * zi / πi - di * (1.0 - zi) / (1.0 - πi)
         for (di, zi, πi) in zip(d, z, π)]

    v[((trim .> π) .| (1.0 - trim .< π))] .= 0.0

    SSD                   = Microdata(MD)
    SSD.mapping[:control] = vcat(MD.mapping[:treatment], MD.mapping[:control])

    obj              = Abadie()
    obj.first_stage  = M₁
    obj.second_stage = M₂(SSD; kwargs...)
    obj.pscore       = π
    obj.eweights     = pweights(v)

    _fit!(second_stage(obj), reweight(w, obj.eweights))
    novar || _vcov!(obj, getcorr(obj), w)

    return obj
end

#==========================================================================================#

# SCORE (MOMENT CONDITIONS)

score(obj::Abadie) = lmul!(Diagonal(obj.eweights), score(second_stage(obj)))

# EXPECTED JACOBIAN OF SCORE × NUMBER OF OBSERVATIONS

jacobian(obj::Abadie, ::UnitWeights) = jacobian(second_stage(obj), obj.eweights)

function jacobian(obj::Abadie, w::AbstractWeights)
    return jacobian(second_stage(obj), reweight(w, obj.eweights))
end

# EXPECTED JACOBIAN OF SCORE W.R.T. FIRST-STAGE PARAMETERS × NUMBER OF OBSERVATIONS

function crossjacobian(obj::Abadie, ::UnitWeights)

    d = getvector(obj, :treatment)
    z = getvector(obj, :instrument)
    π = obj.pscore
    D = [(1.0 - di) * zi / abs2(πi) - di * (1.0 - zi) / abs2(1.0 - πi)
         for (di, zi, πi) in zip(d, z, π)]

    D[iszero.(obj.eweights)] .= 0.0

    g₁ = jacobexp(obj.first_stage)
    g₂ = score(obj.second_stage)

    return g₂' * lmul!(Diagonal(D), g₁)
end

function crossjacobian(obj::Abadie, w::AbstractWeights)

    d = getvector(obj, :treatment)
    z = getvector(obj, :instrument)
    π = obj.pscore
    D = [wi * ((1.0 - di) * zi / abs2(πi) - di * (1.0 - zi) / abs2(1.0 - πi))
         for (di, zi, πi, wi) in zip(d, z, π, w)]

    D[iszero.(obj.eweights)] .= 0.0

    g₁ = jacobexp(obj.first_stage)
    g₂ = score(obj.second_stage)

    return g₂' * lmul!(Diagonal(D), g₁)
end

#==========================================================================================#

# LINEAR PREDICTOR

predict(obj::Abadie) = predict(second_stage(obj))

# FITTED VALUES

fitted(obj::Abadie) = fitted(second_stage(obj))

# DERIVATIVE OF FITTED VALUES

jacobexp(obj::Abadie) = jacobexp(second_stage(obj))

#==========================================================================================#

# UTILITIES

coefnames(obj::Abadie) = coefnames(second_stage(obj))
mtitle(obj::Abadie)    = "Abadie (2003)"


text:
using Test
using ClickHouse
using ClickHouse: read_client_packet, read_server_packet
using DataFrames
using Dates

@test begin
    sock = IOBuffer([0xC2, 0x0A]) |> ClickHouseSock
    ClickHouse.chread(sock, ClickHouse.VarUInt) == ClickHouse.VarUInt(0x542)
end

@test begin
    sock = IOBuffer(UInt8[], read=true, write=true, maxsize=10) |>
        ClickHouseSock
    ClickHouse.chwrite(sock, ClickHouse.VarUInt(100_500))
    seek(sock.io, 0)
    read(sock.io, 3) == [0x94, 0x91, 0x06]
end

@testset "Decode & re-encode client packets (SELECT 1)" begin
    # This .bin file was extracted from a tcpdump captured from a session
    # with the official ClickHouse command line client.
    data = read(open("select1/client-query.bin"), 100_000, all = true)
    sock = data |> IOBuffer |> ClickHouseSock
    packets = []

    # Read packets.

    packet = read_client_packet(sock)
    push!(packets, packet)
    @test typeof(packet) == ClickHouse.ClientHello

    packet = read_client_packet(sock)
    push!(packets, packet)
    @test typeof(packet) == ClickHouse.ClientPing

    packet = read_client_packet(sock)
    push!(packets, packet)
    @test typeof(packet) == ClickHouse.ClientQuery

    packet = read_client_packet(sock)
    push!(packets, packet)
    @test typeof(packet) == ClickHouse.Block

    @test eof(sock.io)

    # Re-encode them.

    sock = IOBuffer(UInt8[], write = true, read = true, maxsize = 100_000) |>
        ClickHouseSock

    for packet ∈ packets
        ClickHouse.write_packet(sock, packet)
    end

    seek(sock.io, 0)
    reencoded_data = read(sock.io, 100_000)

    @test reencoded_data == data
end

@testset "Decode server packets (SELECT 1)" begin
    sock = open("select1/server-query-resp.bin") |> ClickHouseSock

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.ServerInfo

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.ServerPong

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.Block

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.Block

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.ServerProfileInfo

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.ServerProgress

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.Block

    packet = read_server_packet(sock)
    @test typeof(packet) == ClickHouse.ServerEndOfStream

    @test eof(sock.io)
end

@testset "Decode client packets (INSERT INTO woof VALUES (1))" begin
    sock = open("insert1/client.bin") |> ClickHouseSock

    while !eof(sock.io)
        packet = read_client_packet(sock)
    end

    @test true
end

@testset "Decode server packets (OHLC data)" begin
    sock = open("insert-ohlc/server.bin") |> ClickHouseSock

    while !eof(sock.io)
        packet = read_server_packet(sock)
    end
end

@testset "Decode server packets (enums)" begin
    sock = open("enum/server.bin") |> ClickHouseSock

    while !eof(sock.io)
        packet = read_server_packet(sock)
    end
end

@testset "Decode & re-encode client packets (enums)" begin
    data = read(open("enum/client.bin"), 10_000, all = true)
    sock = IOBuffer(data) |> ClickHouseSock

    # Read packets.

    packets = []
    while !eof(sock.io)
        packet = read_client_packet(sock)
        push!(packets, packet)
    end

    @test eof(sock.io)

    # Re-encode them.

    sock = IOBuffer(UInt8[], write = true, read = true, maxsize = 10_000) |>
        ClickHouseSock
    for packet ∈ packets
        ClickHouse.write_packet(sock, packet)
    end

    seek(sock.io, 0)
    reencoded_data = read(sock.io, 10_000)

    @test reencoded_data == data
end

@testset "Decode server packets (exception)" begin
    sock = open("error/server.bin") |> ClickHouseSock

    while !eof(sock.io)
        try
            packet = read_server_packet(sock)
        catch exc
            if !isa(exc, ClickHouseServerException)
                rethrow()
            end
        end
    end
end

@testset "Decode & re-encode client packets (OHLC data)" begin
    data = read(open("insert-ohlc/client.bin"), 1_000_000, all = true)
    sock = IOBuffer(data) |> ClickHouseSock

    # Read packets.

    packets = []
    while !eof(sock.io)
        packet = read_client_packet(sock)
        push!(packets, packet)
    end

    @test eof(sock.io)

    # Re-encode them.

    sock = IOBuffer(UInt8[], write = true, read = true, maxsize = 1_000_000) |>
        ClickHouseSock
    for packet ∈ packets
        ClickHouse.write_packet(sock, packet)
    end

    seek(sock.io, 0)
    reencoded_data = read(sock.io, 1_000_000)

    @test reencoded_data == data
end

@testset "Queries on localhost DB" begin
    table = "ClickHouseJL_Test"
    sock = connect()

    try
        execute(sock, """
            CREATE TABLE $(table) (
                lul UInt64,
                oof Float32,
                foo String,
                ddd Date,
                enu Enum8('a' = 1, 'c' = 3, 'foobar' = 44, 'd' = 9)
            )
            ENGINE = Memory
        """)
    catch exc
        exc::ClickHouseServerException
        occursin(r"Table .* already exists", exc.exc.message) || rethrow()
        sock = connect()
    end

    td = today()
    data = Dict(
        :lul => UInt64[42, 1337, 123],
        :oof => Float32[0., ℯ, π],
        :foo => String["aa", "bb", "cc"],
        :ddd => DateTime[td, td, td],
        :enu => ["a", "c", "foobar"],
    )

    # Single block inserts.
    for _ ∈ 1:3
        insert(sock, table, [data])
    end

    # Multi block insert.
    insert(sock, table, repeat([data], 100))

    # SELECT -> Dict
    proj = ClickHouse.select(sock, "SELECT * FROM $(table) LIMIT 4")
    @test proj[:lul] == UInt64[42, 1337, 123, 42]
    @test proj[:oof] == Float32[0., ℯ, π, 0.]
    @test proj[:foo] == String["aa", "bb", "cc", "aa"]
    @test proj[:ddd] == DateTime[td, td, td, td]

    # SELECT -> DF
    proj_df = select_df(sock, "SELECT * FROM $(table) LIMIT 3, 3")
    exp_df = DataFrame(data)

    # Normalize column order.
    order = [:lul, :oof, :foo, :ddd]
    proj_df = proj_df[:, order]
    exp_df = exp_df[:, order]

    @test proj_df == exp_df

    # Clean up.
    execute(sock, "DROP TABLE $(table)")
end


text:
using ComputationalHeatTransfer

using Plots
#using LaTeXStrings

ρ = 1;
c = 1;
k = 0.1;
d = 0.1;
params = HeatConductionParameters(ρ,c,k,thickness=d)

xlim = (-2.1,2.1)
ylim = (-2.1,2.1)
Δx, Δt = setstepsizes(params.α,gridPe=0.4,fourier=2.0)

#=
Here we set up a rectangular boundary
=#
#bdry = Circle(2.0,1.5Δx)
bdry = Rectangle(2.0,2.0,1.5Δx,shifted=true)

#eb = Circle(1.0,1.5*Δx)
eb = Rectangle(0.5,1.0,1.5*Δx)
Te = RigidTransform((0.0,0.0),0.0)
Te(eb)
cb1 = Rectangle(0.5,1.0,1.5*Δx)
cb2 = Rectangle(0.5,1.0,1.5*Δx)
Tc1 = RigidTransform((1.0,0.0),0.0)
Tc2 = RigidTransform((-1.0,0.0),0.0)

Tc1(cb1)
Tc2(cb2)

qe = 5.0
hc = 10.0
Tc = -10.0

eparams = PrescribedHeatFluxRegion(qe,eb);
cparams1 = PrescribedHeatModelRegion(hc,Tc,cb1);
cparams2 = PrescribedHeatModelRegion(hc,Tc,cb2);

#=
Note the use of the flag `bctype=ComputationalHeatTransfer.AdiabaticBC` here.
We also specify the boundary of the domain in `bodies`.
=#
sys = HeatConduction(params,Δx,xlim,ylim,Δt,bodies=BodyList([bdry]),qflux=eparams,qmodel=[cparams1,cparams2],bctype=ComputationalHeatTransfer.AdiabaticBC)

u0 = newstate(sys)
tspan = (0.0,10.0)
integrator = init(u0,tspan,sys)

step!(integrator,2.0)

plot(temperature(integrator),sys.grid,legend=true,color=cgrad(:RdBu,rev=true))
plot!(bdry,fillalpha=0.0)

xc, yc = coordinates(temperature(integrator),sys.grid);

plot(xc,temperature(integrator)[:,floor(Int,size(sys.grid,2)/2)])

max_stars_repo_path: test/literate/Neumann conditions.jl

max_issues_repo_path: test/literate/Neumann conditions.jl

max_forks_repo_path: test/literate/Neumann conditions.jl


text:
@testset "objective types" begin
    x_seed = [0.0, 0.0]
    g_seed = [0.0, 0.0]
    h_seed = [0.0 0.0; 0.0 0.0]
    f_x_seed = 8157.682077608529

    nd = NonDifferentiable(exponential, x_seed)
    @test nd.f == exponential
    @test value(nd) == 0.0
    @test nd.f_calls == [0]

    od = OnceDifferentiable(exponential, exponential_gradient!, nothing, x_seed, 0.0, g_seed)
    @test od.f == exponential
    @test od.df == exponential_gradient!
    @test value(od) == 0.0
    @test od.f_calls == [0]
    @test od.df_calls == [0]
    od.x_df .= x_seed
    gold = copy(od.DF)
    xnew = rand(eltype(x_seed), size(x_seed))
    gnew = gradient(od, xnew)
    @test od.x_df == x_seed
    @test od.DF == gold
    @test gnew == gradient(od, xnew)

    td = TwiceDifferentiable(exponential, exponential_gradient!, nothing, exponential_hessian!, x_seed, 0.0, g_seed, h_seed)
    @test td.f == exponential
    @test td.df == exponential_gradient!
    @test value(td) == 0.0
    @test td.f_calls == [0]
    @test td.df_calls == [0]
    @test td.h_calls == [0]

    @testset "no fg!" begin
        Random.seed!(324)
        od = OnceDifferentiable(exponential, exponential_gradient!, x_seed, 0.0, g_seed)
        xrand = rand(2)
        value_gradient!(od, xrand)
        ndod = NonDifferentiable(od, xrand)
        @test value(ndod, xrand) === value(od, xrand)
        fcache = value(od)
        gcache = copy(gradient(od))
        value_gradient!(od, zeros(2))
        gradient!(od, xrand)
        @test value(od, zeros(2)) == od.F
        @test value(od, zeros(2)) == value(od)
        @test gradient(od) == gcache

        od = OnceDifferentiable(exponential, exponential_gradient!, x_seed)
        xrand = rand(2)
        value_gradient!(od, xrand)
        fcache = value(od)
        gcache = copy(gradient(od))
        value_gradient!(od, zeros(2))
        gradient!(od, xrand)
        @test value(od, zeros(2)) == od.F
        @test value(od, zeros(2)) == value(od)
        @test gradient(od) == gcache

        td = TwiceDifferentiable(exponential, exponential_gradient!, exponential_hessian!, x_seed, 0.0, g_seed)
        xrand = rand(2)
        value_gradient!(td, xrand)
        ndtd = NonDifferentiable(td, xrand)
        @test value(ndtd, xrand) === value(td, xrand)

        fcache = value(td)
        gcache = copy(gradient(td))
        value_gradient!(td, zeros(2))
        gradient!(td, xrand)
        @test value(td, zeros(2)) == td.F
        @test value(td, zeros(2)) == value(td)
        @test gradient(td) == gcache
    end
end


text:
# ---------------------------------------------------------------------------
# Dot Plot (strip plot, beeswarm)

@recipe function f(::Type{Val{:dotplot}}, x, y, z; mode = :density, side=:both)
    # if only y is provided, then x will be UnitRange 1:size(y, 2)
    if typeof(x) <: AbstractRange
        if step(x) == first(x) == 1
            x = plotattributes[:series_plotindex]
        else
            x = [getindex(x, plotattributes[:series_plotindex])]
        end
    end

    grouplabels = sort(collect(unique(x)))
    barwidth = plotattributes[:bar_width]
    barwidth == nothing && (barwidth = 0.8)

    getoffsets(halfwidth, y) =
        mode == :uniform ?
            (rand(length(y)) .* 2 .- 1) .* halfwidth :
        mode == :density ?
            violinoffsets(halfwidth, y) :
        zeros(length(y))

    points_x, points_y = zeros(0), zeros(0)

    for (i,grouplabel) in enumerate(grouplabels)
        # filter y
        groupy = y[filter(i -> _cycle(x,i) == grouplabel, 1:length(y))]

        center = Plots.discrete_value!(plotattributes[:subplot][:xaxis], grouplabel)[1]
        halfwidth = 0.5_cycle(barwidth, i)

        offsets = getoffsets(halfwidth, groupy)

        if side == :left
            offsets = -abs.(offsets)
        elseif side == :right
            offsets = abs.(offsets)
        end

        append!(points_y, groupy)
        append!(points_x, center .+ offsets)
    end

    seriestype := :scatter
    x := points_x
    y := points_y
    ()
end

Plots.@deps dotplot scatter
Plots.@shorthands dotplot

function violinoffsets(maxwidth, y)
    normalizewidths(maxwidth, widths) = maxwidth * widths / Plots.ignorenan_maximum(widths)

    function getlocalwidths(widths, centers, y)
        upperbounds = [violincenters[violincenters .> yval] for yval ∈ y] .|> findmin .|> first
        lowercenters = findmax.([violincenters[violincenters .≤ yval] for yval ∈ y])
        lowerbounds, lowerindexes = first.(lowercenters), last.(lowercenters)
        δs = (y .- lowerbounds) ./ (upperbounds .- lowerbounds)

        itp = interpolate(widths, BSpline(Quadratic(Reflect(OnCell()))))
        localwidths = itp.(lowerindexes .+ δs)
    end

    violinwidths, violincenters = violin_coords(y)
    violinwidths = normalizewidths(maxwidth, violinwidths)
    localwidths = getlocalwidths(violinwidths, violincenters, y)
    offsets = (rand(length(y)) .* 2 .- 1) .* localwidths
end

# ------------------------------------------------------------------------------
# Grouped dotplot

@userplot GroupedDotplot

recipetype(::Val{:groupeddotplot}, args...) = GroupedDotplot(args)

@recipe function f(g::GroupedDotplot; spacing = 0.1)
    x, y = grouped_xy(g.args...)

    # extract xnums and set default bar width.
    # might need to set xticks as well
    ux = unique(x)
    x = if eltype(x) <: Number
        bar_width --> (0.8 * mean(diff(sort(ux))))
        float.(x)
    else
        bar_width --> 0.8
        xnums = [findfirst(isequal(xi), ux) for xi in x] .- 0.5
        xticks --> (eachindex(ux) .- 0.5, ux)
        xnums
    end

    # shift x values for each group
    group = get(plotattributes, :group, nothing)
    if group != nothing
        gb = Plots.extractGroupArgs(group)
        labels, idxs = getfield(gb, 1), getfield(gb, 2)
        n = length(labels)
        bws = plotattributes[:bar_width] / n
        bar_width := bws * clamp(1 - spacing, 0, 1)
        for i in 1:n
            groupinds = idxs[i]
            Δx = _cycle(bws, i) * (i - (n + 1) / 2)
            x[groupinds] .+= Δx
        end
    end

    seriestype := :dotplot
    x, y
end

Plots.@deps groupeddotplot dotplot


text:
@params mutable struct ElementK{T} <: AbstractFunction{T}
    solver::AbstractDisplacementSolver
    Kes::AbstractVector{<:AbstractMatrix{T}}
    Kes_0::AbstractVector{<:AbstractMatrix{T}} # un-interpolated
    penalty::AbstractPenalty{T}
    xmin::T
end

function Base.show(::IO, ::MIME{Symbol("text/plain")}, ::ElementK)
    return println("TopOpt element stiffness matrix construction function")
end

function ElementK(solver::AbstractDisplacementSolver)
    @unpack elementinfo = solver
    dh = solver.problem.ch.dh
    penalty = getpenalty(solver)
    xmin = solver.xmin
    solver.vars = ones(getncells(dh.grid))
    # trigger Ke construction
    solver()
    Kes_solver = solver.elementinfo.Kes

    _Ke1 = rawmatrix(Kes_solver[1])
    mat_type = _Ke1 isa Symmetric ? typeof(_Ke1.data) : typeof(_Ke1)
    Kes = mat_type[]
    Kes_0 = mat_type[]
    for (cellidx, _) in enumerate(CellIterator(dh))
        _Ke = rawmatrix(Kes_solver[cellidx])
        Ke0 = _Ke isa Symmetric ? _Ke.data : _Ke
        Ke = similar(Ke0)
        push!(Kes_0, Ke0)
        push!(Kes, Ke)
    end

    return ElementK(solver, Kes, Kes_0, penalty, xmin)
end

function (ek::ElementK)(xe::Number, ci::Integer)
    @unpack xmin, Kes_0, penalty = ek
    if PENALTY_BEFORE_INTERPOLATION
        px = density(penalty(xe), xmin)
    else
        px = penalty(density(xe), xmin)
    end
    return px * Kes_0[ci]
end

function (ek::ElementK{T})(x::AbstractVector{T}) where {T}
    @unpack solver, Kes = ek
    @assert getncells(solver.problem.ch.dh.grid) == length(x)
    for ci in 1:length(x)
        Kes[ci] = ek(x[ci], ci)
    end
    return copy(Kes)
end

function ChainRulesCore.rrule(ek::ElementK, x)
    @unpack solver, Kes = ek
    @assert getncells(solver.problem.ch.dh.grid) == length(x)
    Kes = ek(x)

    """
    g(F(x)), where F = ElementK

    Want: dg/dK_e_ij -> dg/dx_e

    dg/dx_e = sum_i'j' dg/dK_e_i'j' * dK_e_i'j'/dx_e
            = Delta[e][i,j] * dK_e_i'j'/dx_e
    """
    function pullback_fn(Δ)
        Δx = similar(x)
        for ci in 1:length(x)
            ek_cell_fn = xe -> vec(ek(xe, ci))
            jac_cell = ForwardDiff.derivative(ek_cell_fn, x[ci])
            Δx[ci] = jac_cell' * vec(Δ[ci])
        end
        return Tangent{typeof(ek)}(;
            solver=NoTangent(),
            Kes=Δ,
            Kes_0=NoTangent(),
            penalty=NoTangent(),
            xmin=NoTangent(),
        ),
        Δx
    end
    return Kes, pullback_fn
end


text:
using Compat
using CSTParser: FunctionDef, OPERATOR, PUNCTUATION, Curly, BinaryOpCall, BinarySyntaxOpCall, ComparisonOp, DeclarationOp, MacroCall, MacroName

function is_where_expr(expr)
    isexpr(expr, BinarySyntaxOpCall) || isexpr(expr, CSTParser.WhereOpCall) || return false
    isexpr(children(expr)[2], OPERATOR, Tokens.WHERE) || return false
    return true
end

function is_macroname(x::OverlayNode{MacroCall}, name)
    c = children(x)[1]
    isexpr(c, MacroName) || return false
    return is_identifier(children(c)[2], name)
end

is_identifier(x, id) = false
function is_identifier(x::CSTParser.IDENTIFIER, id)
    x.val == id
end
is_identifier(x::OverlayNode, id) = is_identifier(x.expr, id)

# Rewrites related to the new parametric type syntax on 0.6, including
# rewriting inner constructors
include("database/parametric.jl")
# Delete comparisons against VERSION for versions that are no longer supported
include("database/versioncompare.jl")
# Delete compat macro invocations that are no longer needed
include("database/oldcompat.jl")
# Grabbag of relatively simple rewrites (mostly handed using the AST matcher code)
include("database/simple.jl")
# Rewrites uses of `&x` in ccall to use the equivalent `Ref` instead
include("database/ccallampersand.jl")
# Removes calls to isdefined(Base, :symbol) when these are always true or false
include("database/isdefined_base.jl")
# Adds `catch` blocks to `try; end` blocks
include("database/trycatch.jl")


text:
using StatsBase, Distributions, Plots

function rouletteSpins(r,p)
    x = 0
    wins = 0
    while true
        x += 1
        if rand() < p
            wins += 1
            if wins == r
                return x
            end
        end
    end
end

r, p, N = 5, 18/37,10^6
xGrid = r:r+15

mcEstimate = counts([rouletteSpins(r,p) for _ in 1:N],xGrid)/N

nbDist = NegativeBinomial(r,p)
nbPmf = [pdf(nbDist,x-r) for x in xGrid]

plot( xGrid, mcEstimate,
	line=:stem, marker=:circle, c=:blue,
	ms=10, msw=0, lw=4, label="MC estimate")
plot!( xGrid, nbPmf, line=:stem,
	 marker=:xcross, c=:red, ms=6, msw=0, lw=2, label="PMF",
	 xlims=(0,maximum(xGrid)), ylims=(0,0.2),
	 xlabel="x", ylabel="Probability")

max_stars_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook

max_issues_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook

max_forks_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook


text:
using Base.Test
using Meganet

@testset "Meganet" begin
@testset "integrators" begin
    include("integrators/runtests.jl")
end

@testset "kernels" begin
    include("kernel/runtests.jl")
end
@testset "layer" begin
    include("layer/runtests.jl")
end
@testset "optim" begin
	include("optimization/runtests.jl")
end

@testset "loss" begin
	include("loss/runtests.jl")
end

@testset "regularizer" begin
	include("regularization/runtests.jl")
end
end


text:
# Autogenerated wrapper script for tblis_jll for x86_64-unknown-freebsd
export tblis, tci

using Hwloc_jll
using CompilerSupportLibraries_jll
JLLWrappers.@generate_wrapper_header("tblis")
JLLWrappers.@declare_library_product(tblis, "libtblis.so.0")
JLLWrappers.@declare_library_product(tci, "libtci.so.0")
function __init__()
    JLLWrappers.@generate_init_header(Hwloc_jll, CompilerSupportLibraries_jll)
    JLLWrappers.@init_library_product(
        tblis,
        "lib/libtblis.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        tci,
        "lib/libtci.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()


text:
# crank_nicolson_jacandrhs.jl
#
# Contains Jacobian and RHS calculation functions,
#   for both forward sweep CN equation and reverse sweep adjoint CN equation
#----------------------------------------------------------------------------------
export cnJac_uadj, cnRhs_uadj
export cnAdjJac, cnAdjRhs

function cnAdjJac(newton_data, mesh, sbp, adj_nextstep, opts, jac, ctx, t)
  # adj_nextstep contains psi_i
  # ctx will pass in adj, which contains psi_(i+1)
  # eqn contains q at time i (check not i+1)

  myrank = MPI.Comm_rank(MPI.COMM_WORLD)

  physics_func = ctx[1]
  adj = ctx[2]
  h = ctx[3]
  newton_data = ctx[4]
  i_fwd = ctx[5]

  # Forming the CN Adjoint Jacobian:
  #   call physicsJac with eqn_nextstep & t_nextstep
  #   then form cnAdjJac = I - dt/2 * physics_Jac

  #println(" === typeof eqn: ", typeof(eqn))
  # TODO: why is eqn Array{Float64,2}
  @debug1 println(" === typeof adj: ", typeof(adj))
  # Note: setupNewton shouldn't be here, bc Newton can't properly iterate on it if the jac is cleared.
  #       jac is passed in as arg to cnAdjJac

  # TODO: need to double check that t_nextstep is used here, not t. I believe it should be t_nextstep
  t_nextstep = t - h
  t_nextstep = negativeZeroCheck(t_nextstep)   # ensure negative zero is changed to zero

  # Fixed 20170330: physicsJac needs to be calculated at time i, which corresponds to adj_nextstep in the reverse sweep
  NonlinearSolvers.physicsJac(newton_data, mesh, sbp, adj_nextstep, opts, jac, ctx, t_nextstep)

  # need to flush assembly cache before performing the scale operation.
  #   These are defined for Julia matrices; they're just noops
  MatAssemblyBegin(jac, PETSc.MAT_FINAL_ASSEMBLY)
  MatAssemblyEnd(jac, PETSc.MAT_FINAL_ASSEMBLY)

  #--------------------------
  # applying dt/2 to jac
  # Jacobian is always 2D
  scale_factor = h*-0.5
  # make this into a petsc_scale_factor
  petsc_scale_factor = PetscScalar(scale_factor)

  # PetscMatScale is defined for all jac types, PETSc and Julia.
  #   When jac is julia array, this effectively does: scale!(jac, scale_factor)
  PetscMatScale(jac, petsc_scale_factor)

  # PetscMatAssembly___ not necessary here; PetscMatScale is provably local so it doesn't cache its stuff

  #--------------------------
  # adding identity
  ix_petsc_row = zeros(PetscInt, 1)
  ix_petsc_col = zeros(PetscInt, 1)
  value_to_add = zeros(PetscScalar, 1, 1)
  value_to_add[1,1] = 1.0
  flag = PETSc.ADD_VALUES

  for i = 1:mesh.numDof
    ix_petsc_row[1,1] = i + mesh.dof_offset       # jac diag index + local to global offset
    ix_petsc_col[1,1] = i + mesh.dof_offset       # jac diag index + local to global offset

    # PETSc function: set_values1!(Jac, [2], [2], Jac[2,2] + 1)
    # args: array, row (as an array of len 1), col (as an array of len 1), new values (as a 2D array of len 1)
    #   set_values1! has different methods for both PETSc matrices and Julia matrices
    #   for Julia dense & sparse arrays, set_values1! effectively does this: jac[i,i] += 1
    #   in serial, mesh.dof_offset is set to 0 automatically
    set_values1!(jac, ix_petsc_row, ix_petsc_col, value_to_add, flag)
  end

  # set_values1! only caches the results; need to be assembled. This happens in petscSolve in petsc_funcs.jl
  #   (The assemble funcs are defined for Julia matrices; they're just noops)

  # jac is now I - dt/2 * physics_jac

end     # end of function cnAdjJac

"""
NonlinearSolvers.cnJac_uadj

  Jac of the CN calculation.
  Effectively a wrapper for physicsJac, because the CN Jac is:
    CN_Jac = I - dt/2 * physicsJac

  ctx:
    physics func must be the first element, i.e. evalEuler
    eqn_nextstep must be the second element
    h must be the third element
    newton_data must be the fourth element
"""
function cnJac_uadj(newton_data, mesh, sbp, eqn_nextstep, opts, jac, ctx, t)

  myrank = MPI.Comm_rank(MPI.COMM_WORLD)

  physics_func = ctx[1]
  eqn = ctx[2]
  h = ctx[3]
  newton_data = ctx[4]

  jac_type = opts["jac_type"]

  t_nextstep = t + h

  # Forming the CN Jacobian:
  #   call physicsJac with eqn_nextstep & t_nextstep
  #   then form CN_Jac = I - dt/2 * physics_Jac

  NonlinearSolvers.physicsJac(newton_data, mesh, sbp, eqn_nextstep, opts, jac, ctx, t_nextstep)

  # need to flush assembly cache before performing the scale operation.
  #   These are defined for Julia matrices; they're just noops
  PetscMatAssemblyBegin(jac, PETSc.MAT_FINAL_ASSEMBLY)
  PetscMatAssemblyEnd(jac, PETSc.MAT_FINAL_ASSEMBLY)

  #--------------------------
  # applying dt/2 to jac
  # Jacobian is always 2D
  scale_factor = h*-0.5
  # make this into a petsc_scale_factor
  petsc_scale_factor = PetscScalar(scale_factor)

  # PetscMatScale is defined for all jac types, PETSc and Julia
  # when jac is julia array, this effectively does: scale!(jac, scale_factor)
  PetscMatScale(jac, petsc_scale_factor)

  # PetscMatAssembly___ not necessary here; PetscMatScale is provably local so it doesn't cache its stuff

  #--------------------------
  # adding identity
  ix_petsc_row = zeros(PetscInt, 1)
  ix_petsc_col = zeros(PetscInt, 1)
  value_to_add = zeros(PetscScalar, 1, 1)
  value_to_add[1,1] = 1.0
  flag = PETSc.ADD_VALUES

  for i = 1:mesh.numDof
    ix_petsc_row[1,1] = i + mesh.dof_offset       # jac diag index + local to global offset
    ix_petsc_col[1,1] = i + mesh.dof_offset       # jac diag index + local to global offset

    # PETSc function: set_values1!(Jac, [2], [2], Jac[2,2] + 1)
    # args: array, row (as an array of len 1), col (as an array of len 1), new values (as a 2D array of len 1)
    #   set_values1! has different methods for both PETSc matrices and Julia matrices
    #   for Julia dense & sparse arrays, set_values1! effectively does this: jac[i,i] += 1
    #   in serial, mesh.dof_offset is set to 0 automatically
    set_values1!(jac, ix_petsc_row, ix_petsc_col, value_to_add, flag)
  end

  # set_values1! only caches the results; need to be assembled. This happens in petscSolve in petsc_funcs.jl
  #   (The assemble funcs are defined for Julia matrices; they're just noops)

  # jac is now I - dt/2 * physics_jac

  return nothing

end     # end of function cnJac_uadj

"""
NonlinearSolvers.cnAdjRhs

  RHS of the CN unsteady adjoint calculation

  cnAdjRhs counts on the fact that crank_nicolson above will call it stepping through time in the negative direction

"""
function cnAdjRhs(mesh::AbstractMesh, sbp::AbstractSBP, adj_nextstep::AbstractSolutionData, opts, rhs_vec, ctx, t)

  physics_func = ctx[1]
  adj = ctx[2]
  h = ctx[3]
  newton_data = ctx[4]
  i_fwd = ctx[5]
  dJdu = ctx[6]

  # TODO: need to double check that t_nextstep is used here, not t. I believe it should be t_nextstep
  t_nextstep = t - h    # adjoint going backwards in time
  t_nextstep = negativeZeroCheck(t_nextstep)   # ensure negative zero is changed to zero

  eqn_dummy = cnAdjLoadChkpt(mesh, sbp, opts, adj, physics_func, i_fwd, t_nextstep)
  jac = cnAdjCalcdRdu(mesh, sbp, opts, eqn_dummy, physics_func, t_nextstep)
  dRdu_i = transpose(jac)    # dRdu_i: we don't need dRdu_(i-1), see derivation

  # Fix 20170330: the dRdu_i used below in forming rhs_vec needs to be transposed
  dRdu_i = transpose(jac)    # dRdu_i: we don't need dRdu_(i-1), see derivation

  # println(" mesh.numDof: ", mesh.numDof)

  # println(" size dJdu: ", size(dJdu))
  # println(" size rhs_vec: ", size(rhs_vec))
  # println(" size adj.q_vec: ", size(adj.q_vec))
  # println(" size adj_nextstep.q_vec: ", size(adj_nextstep.q_vec))
  # println(" size dRdu_i: ", size(dRdu_i))

  # println(" typeof dJdu: ", typeof(dJdu))
  # println(" typeof rhs_vec: ", typeof(rhs_vec))
  # println(" typeof adj.q_vec: ", typeof(adj.q_vec))
  # println(" typeof adj_nextstep.q_vec: ", typeof(adj_nextstep.q_vec))
  # println(" typeof dRdu_i: ", typeof(dRdu_i))

  for i = 1:mesh.numDof

    # the Jacobian vector product needs to be jac * a vector, so use q_vec, not q in:
    #   dRdu_i*adj_nextstep.q_vec

    # adj_nextstep corresponds to psi_i
    # adj corresponds to psi_i+1

    rhs_vec[i] = dJdu[i]
                  + adj_nextstep.q_vec[i]
                  - 0.5*h*(dRdu_i*adj_nextstep.q_vec)[i]
                  - adj.q_vec[i]
                  - 0.5*h*(dRdu_i*adj.q_vec)[i]

    # note: something about how I'm multiplying dRdu_i & q_vec

    # Note: as written above: the q_vec field is being used for psi in the unsteady adj eqn

  end     # end of loop: i = 1:mesh.numDof

end     # end of function cnAdjRhs

"""
NonlinearSolvers.cnRhs_uadj

  RHS of the CN calculation

  ctx:
    physics func must be the first element, i.e. evalEuler
    eqn_nextstep must be the second element
    h must be the third element

"""
# function cnRhs_uadj(mesh::AbstractMesh, sbp::AbstractSBP, eqn_nextstep::AbstractSolutionData, opts, rhs_vec, ctx, t)
function cnRhs_uadj(mesh, sbp, eqn_nextstep, opts, rhs_vec, ctx, t)

  # eqn comes in through ctx_residual, which is set up in CN before the newtonInner call

  physics_func = ctx[1]
  eqn = ctx[2]
  h = ctx[3]

  t_nextstep = t + h

  # array3DTo1D needs to be called after physics_func TODO: explain why
  physics_func(mesh, sbp, eqn_nextstep, opts, t_nextstep)
  array3DTo1D(mesh, sbp, eqn_nextstep, opts, eqn_nextstep.res, eqn_nextstep.res_vec)

  if opts["parallel_type"] == 2 && mesh.npeers > 0
    startDataExchange(mesh, opts, eqn.q, eqn.q_face_send, eqn.q_face_recv, eqn.params.f)
  end
  physics_func(mesh, sbp, eqn, opts, t)
  array3DTo1D(mesh, sbp, eqn, opts, eqn.res, eqn.res_vec)

  #   what this is doing:
  #   u_(n+1) - 0.5*dt* (del dot G_(n+1)) - u_n - 0.5*dt* (del dot G_n)
  for i = 1:mesh.numDof

    temp1 = eqn_nextstep.q_vec[i] - 0.5*h*eqn_nextstep.res_vec[i]
    temp2 = eqn.q_vec[i] + 0.5*h*eqn.res_vec[i]

    rhs_vec[i] = temp1 - temp2

    # NOTE: question: is there a sign problem here? should rhs_vec = -rhs_vec ?
    #     NO. this negative gets applied in newton.jl, where res_0[i] = -res_0[i]

  end

  return nothing

end     # end of function cnRhs_uadj


text:
module Mustache

using Tables

include("utils.jl")
include("tokens.jl")
include("context.jl")
include("writer.jl")
include("parse.jl")

export @mt_str, @mt_mstr, render, render_from_file

"""
    mt"string"

Macro to parse tokens from a string. Useful when template is to be reused.

"""
macro mt_str(s)
    parse(s)
end

macro mt_mstr(s)
    parse(s)
end

"""
    render([io], tokens, view)

Render a set of tokens with a view, using optional `io` object to print or store.

Arguments
---------

* `io::IO`: Optional `IO` object.

* `tokens`: Either Mustache tokens, or a string to parse into tokens

* `view`: A view provides a context to look up unresolved symbols
  demarcated by mustache braces. A view may be specified by a
  dictionary, a module, a composite type, a vector, a named tuple, a
  data frame, a `Tables` object, or keyword arguments.

"""
function render(io::IO, tokens::MustacheTokens, view)
    _writer = Writer()
    render(io, _writer, tokens, view)
end
function render(io::IO, tokens::MustacheTokens; kwargs...)
    render(io, tokens, kwargs)
end

render(tokens::MustacheTokens, view) = sprint(io -> render(io, tokens, view))
render(tokens::MustacheTokens; kwargs...) = sprint(io -> render(io, tokens; kwargs...))

## Exported call without first parsing tokens via mt"literal"
##
## @param template a string containing the template for expansion
## @param view a Dict, Module, CompositeType, DataFrame holding variables for expansion
function render(io::IO, template::AbstractString, view)
    _writer = Writer()
    render(io, _writer, parse(template), view)
end
function render(io::IO, template::AbstractString; kwargs...)
    _writer = Writer()
    render(io, _writer, parse(template), kwargs)
end
render(template::AbstractString, view) = sprint(io -> render(io, template, view))
render(template::AbstractString; kwargs...) = sprint(io -> render(io, template; kwargs...))

## Dict for storing parsed templates
TEMPLATES = Dict{AbstractString, MustacheTokens}()

## Load template from file
function template_from_file(filepath)
    f = open(x -> read(x, String), filepath)
    tpl = parse(f)
    return tpl
end

"""

Renders a template from `filepath` and `view`. If it has seen the file
before then it finds the compiled `MustacheTokens` in `TEMPLATES` rather
than calling `parse` a second time.

"""
function render_from_file(filepath, view)
    if haskey(TEMPLATES, filepath)
        render(TEMPLATES[filepath], view)
    else
        try
            tpl = template_from_file(filepath)
            TEMPLATES[filepath] = tpl
            render(tpl, view)
        catch
            nothing
        end
    end
end
function render_from_file(filepath::AbstractString; kwargs...)
    render_from_file(filepath, kwargs)
end

end


text:
using JuMP

struct MockSolver <: MathProgBase.AbstractMathProgSolver end
struct MockModel <: AbstractPolyhedraModel end
Polyhedra.PolyhedraModel(::MockSolver) = MockModel()
MathProgBase.LinearQuadraticModel(s::MockSolver) = PolyhedraToLPQPBridge(Polyhedra.PolyhedraModel(s))
function MathProgBase.loadproblem!(::MockModel, lp, obj, sense)
    @test obj == [1.0, 1.0, 1.0, -2.0]
    @test sense == :Max
end
function MathProgBase.optimize!(::MockModel) end
MathProgBase.status(::MockModel) = :Optimal
MathProgBase.getsolution(::MockModel) = [0.0, 1.0, 0.0, 1.0]
MathProgBase.getobjval(::MockModel) = -1.0
MathProgBase.getconstrsolution(::MockModel) = [1.0, 1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0, -1.0, -1.0]
MathProgBase.getconstrduals(::MockModel) = [-2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.0, 0.0, -0.0]

function polyhedramodeltest(s::MathProgBase.AbstractMathProgSolver)
    m = Model(solver=s)
    n = 3
    @variable(m, 0 <= x[1:n] <= 1)
    @variable(m, y == 1)
    @constraint(m, sum(x[i] for i=1:n) == 1)
    @constraint(m, x[1] - x[2] <= 1)
    @constraint(m, x[2] - x[3] >= 1)
    @objective(m, Max, sum(x) - 2y)

    lp = hrep(m)
    coloffset, rowoffset = Polyhedra.computeoffsets(lp)
    @test coloffset == [[3, -4], [5, -6], [7, -8], [1]]
    @test rowoffset == [[2], [9], [-10]]

    status = solve(m)
    @test status == :Optimal
    @test MathProgBase.getsolution(m.internalModel) ≈ [0.0, 1.0, 0.0, 1.0]
    @test MathProgBase.getobjval(m.internalModel) ≈ -1.0
    @test MathProgBase.getconstrsolution(m.internalModel) ≈ [1.0, -1.0, 1.0]
    @test MathProgBase.getconstrduals(m.internalModel) ≈ [1.0, 0.0, 0.0]
end

@testset "PolyhedraToLPQPBridge" begin
    polyhedramodeltest(MockSolver())
end