Skip to content

Instantly share code, notes, and snippets.

@44100hertz
Last active November 16, 2024 21:19
Show Gist options
  • Save 44100hertz/cb0a88df0ff668b36fd7106c1ea9f8c7 to your computer and use it in GitHub Desktop.
Save 44100hertz/cb0a88df0ff668b36fd7106c1ea9f8c7 to your computer and use it in GitHub Desktop.
Find the most tactile switch within the database at https://github.com/ThereminGoat/force-curves
local basedir = "/tmp/analyze-forces/"
os.execute("mkdir -p " .. basedir)
local function list_dir(path)
local date = os.date()
local listing_path = basedir .. date .. ".txt"
os.execute(string.format('ls "%s" > "%s"', path, listing_path))
local out = {}
for line in io.lines(listing_path) do
out[#out + 1] = line
end
return out
end
local function get_downstroke(file)
local last_distance = -100
local out = {}
for line in io.lines(file) do
local matcher = string.gmatch(line, "([^,]*),")
matcher() -- skip data point number
local force = tonumber(matcher() or "")
matcher() -- skip force unit
local distance = tonumber(matcher() or "")
if distance and force then
if last_distance > distance then
return out
end
out[#out + 1] = { distance, force }
last_distance = distance
end
end
return out
end
local function calculate_tactility(data, halfway, last)
local first_half_work = 0
local last_half_work = 0
local last_distance = 0
for _, point in ipairs(data) do
local distance, force = unpack(point)
local d_distance = last_distance - distance
if distance <= halfway then
first_half_work = first_half_work + d_distance * force
elseif distance <= last then
last_half_work = last_half_work + d_distance * force
end
last_distance = distance
end
return first_half_work, last_half_work
end
local function find_best_tactility(data)
local work1_s, work2_s = calculate_tactility(data, 1.0, 2.0)
local work1_d, work2_d = calculate_tactility(data, 1.5, 3.0)
local shallow_tactility = work1_s / work2_s
local deep_tactility = work1_d / work2_d
if shallow_tactility > deep_tactility then
return "shallow", work1_s, work2_s
else
return "deep", work1_d, work2_d
end
end
local tactilities = {}
for _, dir in ipairs(list_dir(".")) do
for _, file in ipairs(list_dir(dir)) do
if string.find(file, "Raw Data CSV") then
local data = get_downstroke(dir .. "/" .. file)
local depth, work1, work2 = find_best_tactility(data)
local name = string.gsub(file, "Raw Data CSV.csv", ""):gsub(",", "")
tactilities[#tactilities + 1] = { name, depth, work1, work2, work1 / work2 }
end
end
end
table.sort(tactilities, function(a, b)
local _, _, _, _, tactility_a = unpack(a)
local _, _, _, _, tactility_b = unpack(b)
return tactility_a > tactility_b
end)
print("Name, Work Type, First half work (gfmm), Second half work (gfmm), Ratio")
for _, entry in ipairs(tactilities) do
print(table.concat(entry, ","))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment